-
Notifications
You must be signed in to change notification settings - Fork 1
/
Azure_Custom_Vision_Predictions.py
270 lines (224 loc) · 11.5 KB
/
Azure_Custom_Vision_Predictions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<<<<<<< HEAD
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry
from msrest.authentication import ApiKeyCredentials
import os, time
from sklearn.preprocessing import LabelEncoder
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, accuracy_score
# Load the endpoint and keys of your resource
training_endpoint = ('https://southeastasia.api.cognitive.microsoft.com/')
training_key = ('0ac267da81354216b38b707aa9dcc333')
prediction_endpoint = ('https://southeastasia.api.cognitive.microsoft.com/')
prediction_key = ('0ac267da81354216b38b707aa9dcc333')
prediction_resource_id = ('/subscriptions/1eefc508-750d-47e1-aae2-10fab405f629/resourceGroups/MicroalgaeV1/providers/Microsoft.CognitiveServices/accounts/Microalgae1')
# Authenticate the client
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(training_endpoint, credentials)
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(prediction_endpoint, prediction_credentials)
# Get path to images folder
dirname = os.path.dirname(__file__)
images_folder = os.path.join(dirname, 'images/Test')
# Create variables for your project
publish_iteration_name = 'Microalgae_Batch_1_Python_Azure'
project_id = "f0843f52-d404-4898-8cbd-b916627d3c86"
# Create variables for your prediction resource
prediction_key = "0ac267da81354216b38b707aa9dcc333"
endpoint = "https://southeastasia.api.cognitive.microsoft.com/"
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(endpoint, prediction_credentials)
# Define the path to test images folder
test_images_folder_path = os.path.join(os.path.dirname(__file__), "Images_Batch_1", "Test")
test_images_folder_path = "E:\CodingProjects\machine_learning\Azure_Custom_Vision_V3\Batch 1\preprocessing_batch_1_Azure\Images_Batch_1\Test/"
# Encode the tags
encoder = LabelEncoder()
encoder.fit(["Spirulina_Platensis", "Chlorella_FSP", "Chlamydomonas_Reinhardtii"])
encoded_labels = encoder.transform(["Spirulina_Platensis", "Chlorella_FSP", "Chlamydomonas_Reinhardtii"])
decoded_labels = encoder.inverse_transform(encoded_labels)
# Set threshold value for prediction probabilities
threshold = 0.5
# Test - Make a prediction
print("Testing the prediction endpoint with threshold value...")
true_labels = []
pred_labels = []
for subfolder in os.listdir(test_images_folder_path):
subfolder_path = os.path.join(test_images_folder_path, subfolder)
if os.path.isdir(subfolder_path):
for test_image_filename in os.listdir(subfolder_path):
if test_image_filename.endswith('.jpg'):
image_path = os.path.join(subfolder_path, test_image_filename)
with open(image_path, "rb") as image_contents:
results = predictor.classify_image_with_no_store(project_id, publish_iteration_name, image_contents.read(), threshold=threshold)
# Get the true label and the predicted label
true_label = subfolder
if len(results.predictions) > 0:
pred_label = results.predictions[0].tag_name
else:
pred_label = "Unknown"
true_labels.append(true_label)
pred_labels.append(pred_label)
# Display the results
print(f"Testing image {test_image_filename}...")
for prediction in results.predictions:
print(f"\t{prediction.tag_name}: {prediction.probability*100 :.2f}%")
# Encode the true and predicted labels
true_labels = encoder.transform(true_labels)
pred_labels = encoder.transform(pred_labels)
# Compute the confusion matrix
cm = confusion_matrix(true_labels, pred_labels)
# Plot the heatmap
fig, ax = plt.subplots()
im = ax.imshow(cm, cmap='Blues')
# Add labels to the x and y axes
classes = ["Chlamydomonas_Reinhardtii", "Chlorella_FSP", "Spirulina_Platensis"]
ax.set_xticks(np.arange(len(classes)))
ax.set_yticks(np.arange(len(classes)))
ax.set_xticklabels(classes)
ax.set_yticklabels(classes)
# Rotate the x-axis labels
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# Add numbers to each cell in the confusion matrix
thresh = cm.max() / 2.
for i in range(len(classes)):
for j in range(len(classes)):
ax.text(j, i, cm[i, j], ha="center", va="center", color="white" if cm[i, j] > thresh else "black")
plt.colorbar(im)
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.title('Confusion Matrix Heatmap')
plt.show()
# Compute the overall accuracy
overall_accuracy = accuracy_score(true_labels, pred_labels)
print(f"Overall accuracy: {overall_accuracy*100:.2f}%")
# Compute precision, recall, and F1-score for each class
class_names = ["Chlamydomonas_Reinhardtii", "Chlorella_FSP", "Spirulina_Platensis"]
metrics = {}
for i, class_name in enumerate(class_names):
tp = cm[i,i]
fp = np.sum(cm[:,i]) - tp
fn = np.sum(cm[i,:]) - tp
tn = np.sum(cm) - tp - fp - fn
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1_score = 2 * (precision * recall) / (precision + recall)
metrics[class_name] = {"Precision": precision, "Recall": recall, "F1-score": f1_score}
# Print the metrics
for class_name, metric in metrics.items():
print(f"Metrics for class {class_name}:")
for metric_name, metric_value in metric.items():
=======
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry
from msrest.authentication import ApiKeyCredentials
import os, time
from sklearn.preprocessing import LabelEncoder
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, accuracy_score
# Load the endpoint and keys of your resource
training_endpoint = ('https://southeastasia.api.cognitive.microsoft.com/')
training_key = ('0ac267da81354216b38b707aa9dcc333')
prediction_endpoint = ('https://southeastasia.api.cognitive.microsoft.com/')
prediction_key = ('0ac267da81354216b38b707aa9dcc333')
prediction_resource_id = ('/subscriptions/1eefc508-750d-47e1-aae2-10fab405f629/resourceGroups/MicroalgaeV1/providers/Microsoft.CognitiveServices/accounts/Microalgae1')
# Authenticate the client
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(training_endpoint, credentials)
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(prediction_endpoint, prediction_credentials)
# Get path to images folder
dirname = os.path.dirname(__file__)
images_folder = os.path.join(dirname, 'images/Test')
# Create variables for your project
publish_iteration_name = 'Microalgae_Batch_1_Python_Azure'
project_id = "f0843f52-d404-4898-8cbd-b916627d3c86"
# Create variables for your prediction resource
prediction_key = "0ac267da81354216b38b707aa9dcc333"
endpoint = "https://southeastasia.api.cognitive.microsoft.com/"
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(endpoint, prediction_credentials)
# Define the path to test images folder
test_images_folder_path = os.path.join(os.path.dirname(__file__), "Images_Batch_1", "Test")
test_images_folder_path = "E:\CodingProjects\machine_learning\Azure_Custom_Vision_V3\Batch 1\preprocessing_batch_1_Azure\Images_Batch_1\Test/"
# Encode the tags
encoder = LabelEncoder()
encoder.fit(["Spirulina_Platensis", "Chlorella_FSP", "Chlamydomonas_Reinhardtii"])
encoded_labels = encoder.transform(["Spirulina_Platensis", "Chlorella_FSP", "Chlamydomonas_Reinhardtii"])
decoded_labels = encoder.inverse_transform(encoded_labels)
# Set threshold value for prediction probabilities
threshold = 0.5
# Test - Make a prediction
print("Testing the prediction endpoint with threshold value...")
true_labels = []
pred_labels = []
for subfolder in os.listdir(test_images_folder_path):
subfolder_path = os.path.join(test_images_folder_path, subfolder)
if os.path.isdir(subfolder_path):
for test_image_filename in os.listdir(subfolder_path):
if test_image_filename.endswith('.jpg'):
image_path = os.path.join(subfolder_path, test_image_filename)
with open(image_path, "rb") as image_contents:
results = predictor.classify_image_with_no_store(project_id, publish_iteration_name, image_contents.read(), threshold=threshold)
# Get the true label and the predicted label
true_label = subfolder
if len(results.predictions) > 0:
pred_label = results.predictions[0].tag_name
else:
pred_label = "Unknown"
true_labels.append(true_label)
pred_labels.append(pred_label)
# Display the results
print(f"Testing image {test_image_filename}...")
for prediction in results.predictions:
print(f"\t{prediction.tag_name}: {prediction.probability*100 :.2f}%")
# Encode the true and predicted labels
true_labels = encoder.transform(true_labels)
pred_labels = encoder.transform(pred_labels)
# Compute the confusion matrix
cm = confusion_matrix(true_labels, pred_labels)
# Plot the heatmap
fig, ax = plt.subplots()
im = ax.imshow(cm, cmap='Blues')
# Add labels to the x and y axes
classes = ["Chlamydomonas_Reinhardtii", "Chlorella_FSP", "Spirulina_Platensis"]
ax.set_xticks(np.arange(len(classes)))
ax.set_yticks(np.arange(len(classes)))
ax.set_xticklabels(classes)
ax.set_yticklabels(classes)
# Rotate the x-axis labels
plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
# Add numbers to each cell in the confusion matrix
thresh = cm.max() / 2.
for i in range(len(classes)):
for j in range(len(classes)):
ax.text(j, i, cm[i, j], ha="center", va="center", color="white" if cm[i, j] > thresh else "black")
plt.colorbar(im)
plt.xlabel('Predicted Labels')
plt.ylabel('True Labels')
plt.title('Confusion Matrix Heatmap')
plt.show()
# Compute the overall accuracy
overall_accuracy = accuracy_score(true_labels, pred_labels)
print(f"Overall accuracy: {overall_accuracy*100:.2f}%")
# Compute precision, recall, and F1-score for each class
class_names = ["Chlamydomonas_Reinhardtii", "Chlorella_FSP", "Spirulina_Platensis"]
metrics = {}
for i, class_name in enumerate(class_names):
tp = cm[i,i]
fp = np.sum(cm[:,i]) - tp
fn = np.sum(cm[i,:]) - tp
tn = np.sum(cm) - tp - fp - fn
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1_score = 2 * (precision * recall) / (precision + recall)
metrics[class_name] = {"Precision": precision, "Recall": recall, "F1-score": f1_score}
# Print the metrics
for class_name, metric in metrics.items():
print(f"Metrics for class {class_name}:")
for metric_name, metric_value in metric.items():
>>>>>>> dba0e08d7687f8f7342f77c8c8209b2271af25b9
print(f"\t{metric_name}: {metric_value:.2f}")