-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
71 lines (59 loc) · 2.68 KB
/
evaluate.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
"""
This script is used to evaluate trained model based on the appropriate test set.
The script needs model files (e.g. trained_model.h5) to be found in "tmp" folder.
The script will produce predicted labels for test images and "evaluation.csv" file with the appropriate accuracies.
"""
from model import *
from math import ceil
# LOADING MODEL
print('Loading model')
model_path = os.path.join(tmp_folder, 'trained_model.h5')
model = tf.keras.models.load_model(model_path, compile=False)
acc_report = [['image_name', 'accuracy', 'matches', 'size']]
x_test = np.zeros((batch_size, image_height, image_width, 3), dtype=np.float32)
batch_num = ceil(len(test_images) / batch_size)
for batch_idx in range(batch_num):
print('Batch {} / {}'.format(batch_idx + 1, batch_num))
batch_start = batch_idx * batch_size
batch_end = min(len(test_images), (batch_idx + 1) * batch_size)
batch_images = test_images[batch_start:batch_end]
# LOADING IMAGES
print('Loading batch images')
for i in range(len(batch_images)):
filename_image = batch_images[i] + '.png'
image_path = os.path.join(test_folder, filename_image)
img = cv.imread(image_path)
img = cv.resize(img, (image_width, image_height), interpolation=cv.INTER_LINEAR)
x_test[i] = img
# PREDICTING CLASSES
print('Predicting batch classes')
y_out = model.predict(preprocessing(x_test))
y_out = np.argmax(y_out, axis=3).astype(np.uint8)
# SAVING RESULTS
print('Saving batch outputs')
for i in range(len(batch_images)):
filename_label = batch_images[i] + '_L.png'
filename_prediction = batch_images[i] + '_P.png'
label_path = os.path.join(test_labels_folder, filename_label)
prediction_path = os.path.join(tmp_folder, filename_prediction)
label = cv.imread(label_path)
label_cat = bgr2cat(label)
prediction = cv.resize(y_out[i], (label.shape[1], label.shape[0]), interpolation=cv.INTER_NEAREST)
# Calculate metrics
matches = np.equal(label_cat, prediction)
acc = np.mean(matches)
num_matches = np.sum(matches)
num_elements = np.prod(matches.shape)
acc_report.append([batch_images[i], acc, num_matches, num_elements])
# Convert prediction to BGR image and save it
prediction_bgr = cat2bgr(prediction)
cv.imwrite(prediction_path, prediction_bgr)
# Calculate overall accuracy
acc_sum = 0
for row in acc_report[1:]:
acc_sum += row[1]
acc_report.append(['OVERALL ACCURACY', acc_sum / (len(acc_report) - 1), '', ''])
# Save evaluation report
with open(os.path.join(tmp_folder, 'evaluation.csv'), 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(acc_report)