-
Notifications
You must be signed in to change notification settings - Fork 7
/
predictTestDataset.py
191 lines (152 loc) · 5.76 KB
/
predictTestDataset.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
import json
from PIL import Image, ImageDraw
import numpy as np
from skimage.draw import polygon2mask
from utils import pairwise, plot_roc_curve, plot_confusion_matrix
import matplotlib.pyplot as plt
from keras.models import load_model
from visualizer import visualize
from metrics import mean_iou, my_iou_metric, get_iou_vector, plot_best_iou_threshold, mean_iou_simple
from sklearn.metrics import roc_curve, auc, confusion_matrix
from losses import dice_coef, np_dice_coef
TOTAL_IMAGE = 0
TOTAL_FP = 0
TOTAL_TP = 0
TOTAL_AUC = 0
FINAL_TRUE = np.array([])
FINAL_PRED = np.array([])
SCORES = {
'GROUP_TYPE': ['PM', 'CP', 'IC', 'I', 'E', 'O', 'TOTAL'],
'COUNT': {
'TOTAL': 0,
'PM': 0,
'CP': 0,
'IC': 0,
'I': 0,
'E': 0,
'O': 0
},
'Y_TRUE': {
'TOTAL': np.array([]),
'PM': np.array([]),
'CP': np.array([]),
'IC': np.array([]),
'I': np.array([]),
'E': np.array([]),
'O': np.array([])
},
'Y_PRED': {
'TOTAL': np.array([]),
'PM': np.array([]),
'CP': np.array([]),
'IC': np.array([]),
'I': np.array([]),
'E': np.array([]),
'O': np.array([])
}
}
json_raw_data_filepath = './dataTest/annotations.json'
image_test_dir = './dataTest/images/'
modelPath = 'C:\\model.h5'
model = load_model(modelPath, compile=False)
with open(json_raw_data_filepath) as raw_json_data:
annotations_def = json.load(raw_json_data)
for image_name, images_values in annotations_def.items():
image_path = image_test_dir + image_name
img_loaded = Image.open(image_path).convert('RGB')
# img_loaded = img_loaded_raw.resize(size=(256, 256))
width, height = img_loaded.size
mask = np.zeros(shape=(height, width), dtype=np.uint8)
tooth_type = ''
for carry in images_values:
if carry['type'] == "tag":
tooth_type = carry['name']
if (carry['type'] == "polygon"):
# print(carry['points'])
res = []
for a, b in pairwise(carry['points']):
res.append((b, a))
# METHODE CREER MASK V1
tmpMask = polygon2mask((height, width), res).astype(int)
tmpMask[tmpMask == 1] = int(carry['classId'])
mask[:, :] = np.maximum(mask[:, :], tmpMask[:, :])
# fin traitement img
# faire predict
# SHOW
# plt.imshow(mask)
# plt.show()
img_loaded = img_loaded.resize(size=(256, 256))
img_to_predict = np.asarray(img_loaded, dtype=np.float32) / 255.
dimension = img_to_predict.shape
img_to_predict = img_to_predict.reshape(1, dimension[0], dimension[1], dimension[2])
prediction = model.predict(img_to_predict)
res = np.asarray(prediction[0] * 100)
res[res >= 0.95] = 1
res[res < 0.95] = 0
toSave = Image.fromarray(mask, 'L')
toSave_resized = toSave.resize(size=(256, 256))
mask_resize = np.asarray(toSave_resized) # need convert en plus 'L' ?
preed = res[np.newaxis, :, :]
truue = mask_resize[np.newaxis, :, :]
print("res : " + str(res.shape))
print("mask_resize : " + str(mask_resize.shape))
print("preed : " + str(preed.shape))
print("truue : " + str(truue.shape))
'''
res: (256, 256, 1)
mask_resize: (256, 256)
preed: (1, 256, 256, 1)
truue: (1, 256, 256)
'''
final_pred = res[:, :, 0] # (256, 256)
final_true = mask_resize # (256, 256)
print("DICE COEF")
print(np_dice_coef(final_true.flatten(), final_pred.flatten()))
# print(preed.shape)
# (truue.shape)
# print(another_iou_metric(res, tt))
'''
res_custom = res.reshape(1, res.shape[0], res.shape[1], 1)
predict_custom = tt.reshape(1, tt.shape[0], tt.shape[1], 1)
print(res_custom.shape)
print(predict_custom.shape)
print(mean_iou(predict_custom,res_custom))
'''
'''
res_custom = res.reshape(1, 1, res.shape[0], res.shape[1])
predict_custom = tt.reshape(1, 1, tt.shape[0], tt.shape[1])
print(res_custom.shape)
print(predict_custom.shape)
print(get_iou_vector(predict_custom, res_custom))
'''
# VIZUALIIIIIZE
# print(plot_best_iou_threshold(mask_resize, res[:,:,0]))
visualize(res, toSave_resized)
'''
###### CALCULATE SCORES #############
'''
SCORES['COUNT']['TOTAL'] = SCORES['COUNT']['TOTAL'] + 1
SCORES['COUNT'][tooth_type] = SCORES['COUNT'][tooth_type] + 1
SCORES['Y_TRUE']['TOTAL'] = np.concatenate([SCORES['Y_TRUE']['TOTAL'], final_true.flatten()])
SCORES['Y_PRED']['TOTAL'] = np.concatenate([SCORES['Y_PRED']['TOTAL'], final_pred.flatten()])
SCORES['Y_TRUE'][tooth_type] = np.concatenate([SCORES['Y_TRUE'][tooth_type], final_true.flatten()])
SCORES['Y_PRED'][tooth_type] = np.concatenate([SCORES['Y_PRED'][tooth_type], final_pred.flatten()])
print('========')
'''
###### PLOT ROC/AUC CURVE #############
'''
for group_type in SCORES['GROUP_TYPE']:
print("============= GROUPE {} =====================".format(group_type))
plot_roc_curve(SCORES['Y_TRUE'][group_type], SCORES['Y_PRED'][group_type], group_type)
print('DICE : {}'.format(np_dice_coef(SCORES['Y_TRUE'][group_type], SCORES['Y_PRED'][group_type])))
matrix_data = confusion_matrix(SCORES['Y_TRUE'][group_type], SCORES['Y_PRED'][group_type])
plot_confusion_matrix(cm=matrix_data, normalize=True, target_names=['Background', 'Carry'], title='Confusion Matrix for {}'.format(group_type), cmap=plt.cm.Blues)
try:
tn, fp, fn, tp = confusion_matrix(SCORES['Y_TRUE'][group_type], SCORES['Y_PRED'][group_type]).ravel()
except:
pass
sensivity = tp / (tp+fn)
specificity = tn / (tn+fp)
print("SENSIBILITE : {}".format(sensivity))
print("SPECIFICITE : {}".format(specificity))
print("TN: {}, FP: {}, FN: {}, TP: {}".format(tn, fp, fn, tp))