-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_ml.py
250 lines (195 loc) · 9.3 KB
/
util_ml.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
import os
from scipy import ndimage
import csv
import numpy as np
from sklearn.metrics import precision_recall_fscore_support, roc_curve, auc, precision_recall_curve
import matplotlib.pyplot as plt
import cv2
def normalize_sample(img, pixel_depth=255):
ori_data = np.float32(img)
ori_data = (ori_data - pixel_depth / 2) / pixel_depth
return ori_data
def get_metrics(pred_folder, gt_folder, weights_folder, pixel_depth=255):
print('Computing performance metrics...')
precision_ar = []
recall_ar = []
fscore_ar = []
files = os.listdir(pred_folder)
csv_path = os.path.join(pred_folder, 'metrics.csv')
with open(csv_path, mode='w') as metrics_file:
metrics_writer = csv.writer(metrics_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for f in files:
try:
print(f)
filename, file_ext = os.path.splitext(f)
gt_path = os.path.join(os.path.join(gt_folder), f)
weight_path = os.path.join(os.path.join(weights_folder), filename + '.png') # valid
pred_path = os.path.join(os.path.join(pred_folder), filename + '.png')
# gt = (ndimage.imread(gt_path).astype(float))
# pred = (ndimage.imread(pred_path).astype(float))
# weight_img = (ndimage.imread(weight_path).astype(float))
gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE)
pred = cv2.imread(pred_path, cv2.IMREAD_GRAYSCALE)
weight_img = cv2.imread(weight_path, cv2.IMREAD_GRAYSCALE)
pred_rs = (pred.flatten() / pixel_depth).astype(int)
gt_rs = (gt > (pixel_depth // 2)).astype(int).flatten()
weight_img_rs = (weight_img > (pixel_depth // 2)).astype(int).flatten()
precision, recall, fscore, _ = precision_recall_fscore_support(
gt_rs,
pred_rs,
sample_weight=weight_img_rs,
pos_label=1,
average='binary')
precision_ar.append(precision)
recall_ar.append(recall)
fscore_ar.append(fscore)
metrics_writer.writerow([f, precision, recall, fscore])
except IOError as e:
print('Could not process data.\nError', e, '- Skipping file.')
metrics_writer.writerow(['avg', np.mean(precision_ar), np.mean(recall_ar), np.mean(fscore_ar)])
metrics_writer.writerow(['std', np.std(precision_ar), np.std(recall_ar), np.std(fscore_ar)])
print('Performance metrics saved...')
def get_fold_scores(pred_folder, gt_folder, weights_folder, cv_folder=None, pixel_depth=255):
print('Computing ROC curve...')
gt_scores = np.array([])
pred_scores = np.array([])
if cv_folder is None:
files = os.listdir(pred_folder)
else:
files = os.listdir(cv_folder)
csv_path = os.path.join(pred_folder, 'metrics_roc.csv')
with open(csv_path, mode='w') as metrics_file:
csv.writer(metrics_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for f in files:
try:
print(f)
filename, file_ext = os.path.splitext(f)
gt_path = os.path.join(os.path.join(gt_folder), f)
weight_path = os.path.join(os.path.join(weights_folder), filename + '.png') # valid
pred_path = os.path.join(os.path.join(pred_folder), filename + '.npy')
pred = np.load(pred_path)
gt = cv2.imread(gt_path, cv2.IMREAD_GRAYSCALE)
weight_img = cv2.imread(weight_path, cv2.IMREAD_GRAYSCALE)
pred_rs = pred.flatten()
gt_rs = (gt > (pixel_depth // 2)).astype(int).flatten()
weight_img_rs = (weight_img > (pixel_depth // 2)).astype(int).flatten()
valid_pred = pred_rs[np.where(weight_img_rs==1)]
valid_gt = gt_rs[np.where(weight_img_rs==1)]
gt_scores = np.append(gt_scores, valid_gt)
pred_scores = np.append(pred_scores, valid_pred)
except IOError as e:
print('Could not process data.\nError', e, '- Skipping file.')
return gt_scores, pred_scores
def get_metrics_roc_fold(pred_folder, gt_folder, weights_folder, cv_folder=None, pixel_depth=255):
if cv_folder is not None:
auc_ar = np.zeros((5))
mean_fpr = np.linspace(0, 1, 100)
tprs = []
aucs = []
# plt.figure()
fig, ax = plt.subplots()
for fold in range(0,5):
# kk = '/fold' + str(fold) + '/Done'
cv_folder_fold = cv_folder + '/fold' + str(fold) + '/Done'
pred_folder_fold = pred_folder + '/pych_cnptool_f' + str(fold) + '/out/out' + str(fold) + '/auto_rev/ch1'
gt_scores, pred_scores = get_fold_scores(pred_folder_fold, gt_folder, weights_folder, cv_folder_fold)
fpr_rf, tpr_rf, _ = roc_curve(gt_scores, pred_scores)
auc_ar[fold] = auc(fpr_rf, tpr_rf)
plt.plot(fpr_rf, tpr_rf,
alpha=0.4,
label='Fold ' + str(fold) + ' (AUC=' + str(round(auc_ar[fold], 2)) + ')')
interp_tpr = np.interp(mean_fpr, fpr_rf, tpr_rf)
interp_tpr[0] = 0.0
tprs.append(interp_tpr)
aucs.append(auc_ar[fold])
mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
ax.plot(mean_fpr, mean_tpr, color='b',
label=r'Mean ROC (AUC = %0.2f (%0.2f))' % (mean_auc, std_auc),
lw=2, alpha=.8)
std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
ax.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.5,
label=r'$\pm$ 1 std. dev.')
plt.plot([0, 1], [0, 1], 'k--', label='Chance')
plt.xlim([0.0, 1.0])#1.02])
plt.ylim([0.0, 1.0])#1.02])
plt.gca().set_aspect('equal')#, adjustable='box')
# ax.spines['right'].set_visible(False)
# ax.spines['top'].set_visible(False)
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
# plt.xlabel('1 - Specificity')
# plt.ylabel('Sensitivity')
plt.title('ROC curves')
plt.legend(loc='best')
plt.show()
print('auc:', auc_ar)
print('mean:', np.mean(auc_ar))
print('std:', np.std(auc_ar))
###############################
auprc_ar = np.zeros((5))
mean_rec = np.linspace(0, 1, 100)
tprs = []
aucs = []
# plt.figure()
fig, ax = plt.subplots()
# plt.plot([1, 0], [0, 1], 'k--')
for fold in range(0, 5):
kk = '/fold' + str(fold) + '/Done'
cv_folder_fold = cv_folder + '/fold' + str(fold) + '/Done'
pred_folder_fold = pred_folder + '/pych_cnptool_f' + str(fold) + '/out/out' + str(fold) + '/auto_rev/ch1'
gt_scores, pred_scores = get_fold_scores(pred_folder_fold, gt_folder, weights_folder, cv_folder_fold)
precision_ar, recall_ar, _ = precision_recall_curve(gt_scores, pred_scores)
auprc_ar[fold] = auc(recall_ar, precision_ar)
#
precision_ar2 = np.append(0, precision_ar)
recall_ar2 = np.append(1, recall_ar)
#
plt.plot(precision_ar2, recall_ar2,
alpha=0.4,
label='Fold ' + str(fold) + ' (AUPRC=' + str(round(auprc_ar[fold], 2)) + ')')
interp_prec = np.interp(mean_rec, precision_ar2, recall_ar2)
interp_prec[0] = 1.0
tprs.append(interp_prec)
aucs.append(auprc_ar[fold])
mean_prec = np.mean(tprs, axis=0)
mean_prec[-1] = 0.0
mean_auc = auc(mean_rec, mean_prec)
std_auc = np.std(aucs)
ax.plot(mean_fpr, mean_prec, color='b',
label=r'Mean PRC (AUPRC = %0.2f (%0.2f))' % (mean_auc, std_auc),
lw=2, alpha=.8)
std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_prec + std_tpr, 1)
tprs_lower = np.maximum(mean_prec - std_tpr, 0)
ax.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.5,
label=r'$\pm$ 1 std. dev.')
plt.plot([1, 0], [0, 1], 'k--', label='Chance')
plt.xlim([0.0, 1.0])#1.02])
plt.ylim([0.0, 1.0])#1.02])
plt.gca().set_aspect('equal')#, adjustable='box')
# ax.spines['right'].set_visible(False)
# ax.spines['top'].set_visible(False)
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('PRC curves')
plt.legend(loc='best')
plt.show()
print('auprc:', auprc_ar)
print('mean:', np.mean(auprc_ar))
print('std:', np.std(auprc_ar))
else:
gt_scores, pred_scores = get_fold_scores(pred_folder, gt_folder, weights_folder)
fpr_rf, tpr_rf, _ = roc_curve(gt_scores, pred_scores)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.gca().set_aspect('equal', adjustable='box')
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curves')
plt.legend(loc='best')
plt.show()