-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheval.py
210 lines (180 loc) · 7.33 KB
/
eval.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
import numpy as np
from scipy import interpolate
import json
import glob
import os
import pdb
def IOU(box1, gts):
# compute overlaps
# intersection
ixmin = np.maximum(gts[:, 0], box1[0])
iymin = np.maximum(gts[:, 1], box1[1])
ixmax = np.minimum(gts[:, 2], box1[2])
iymax = np.minimum(gts[:, 3], box1[3])
iw = np.maximum(ixmax - ixmin + 1., 0.)
ih = np.maximum(iymax - iymin + 1., 0.)
inters = iw * ih
# union
uni = ((box1[2] - box1[0] + 1.) * (box1[3] - box1[1] + 1.) +
(gts[:, 2] - gts[:, 0] + 1.) *
(gts[:, 3] - gts[:, 1] + 1.) - inters)
overlaps = inters / uni
return overlaps
def FROC(boxes_all, gts_all, iou_th):
# Compute the FROC curve, for single class only
nImg = len(boxes_all)
# img_idxs_ori : array([ 0., 0., 0., ..., 4830., 4830., 4830.])
img_idxs_ori = np.hstack([[i]*len(boxes_all[i]) for i in range(nImg)]).astype(int)
boxes_cat = np.vstack(boxes_all)
scores = boxes_cat[:, -1]
ord = np.argsort(scores)[::-1]
boxes_cat = boxes_cat[ord, :4]
img_idxs = img_idxs_ori[ord]
hits = [np.zeros((len(gts),), dtype=bool) for gts in gts_all]
nHits = 0
nMiss = 0
tps = []
fps = []
no_lesion = 0
for i in range(len(boxes_cat)):
overlaps = IOU(boxes_cat[i, :], gts_all[img_idxs[i]])
if overlaps.shape[0] == 0:
no_lesion += 1
nMiss += 1
elif overlaps.max() < iou_th:
nMiss += 1
else:
max_index = np.argmax(overlaps)
if overlaps[max_index] >= iou_th and not hits[img_idxs[i]][max_index]:
hits[img_idxs[i]][max_index] = True
nHits += 1
elif overlaps[max_index] >= iou_th and hits[img_idxs[i]][max_index]:
nMiss += 1
tps.append(nHits)
fps.append(nMiss)
nGt = len(np.vstack(gts_all))
sens = np.array(tps, dtype=float) / nGt
fp_per_img = np.array(fps, dtype=float) / nImg
if len(tps) != 0:
tp_gt = [tps[-1], nGt]
else:
tp_gt = None
return tp_gt, sens, fp_per_img
def sens_at_FP(boxes_all, gts_all, avgFP, iou_th):
# compute the sensitivity at avgFP (average FP per image)
tp_gt, sens, fp_per_img = FROC(boxes_all, gts_all, iou_th)
# gt = tp and no fp
if len(fp_per_img)!= 0 and sens[-1] == 1 and fp_per_img[-1] == 0:
res = np.ones(1)
valid_avgFP = np.zeros(1)
return res, valid_avgFP
if len(fp_per_img) != 0:
max_fp = fp_per_img[-1]
# tp = 0 and fp = 1
if len(fp_per_img) == 1:
fp_per_img = np.insert(fp_per_img, 0, 0.0)
sens = np.insert(sens, 0, 0.0)
if max_fp != 0:
f = interpolate.interp1d(fp_per_img, sens, fill_value='extrapolate')
if(avgFP[-1] < max_fp):
valid_avgFP_end_idx = len(avgFP)
else:
valid_avgFP_end_idx = np.argwhere(np.array(avgFP) >= max_fp)[0][0]
valid_avgFP = np.hstack((avgFP[:valid_avgFP_end_idx], max_fp))
if max_fp != 0:
res = f(valid_avgFP)
# no fp and tp < gt
elif max_fp == 0 and tp_gt:
res = np.zeros(len(valid_avgFP))
res[:] = 1.0*tp_gt[0] / tp_gt[1]
# no tp
else:
res = np.zeros(1)
valid_avgFP = np.zeros(1)
return res, valid_avgFP
def eval_FROC(gt_boxes, all_boxes, CLASSES, avgFP=[0.05, 0.1, 0.2], iou_th=0.5):
# all_boxes[cls][image] = N x 5 array with columns (x1, y1, x2, y2, score)
# all_boxes as same as gt_boxes
# all_boxes and gt_boxes [ [arr, arr, ...], [arr, arr, ...], ...]
# img0_bbox img1_bbox img0_bbox img1_bbox
# class0 class1
# classes(outside) and img_num(inside)
# max_fp: using all fp to calculate fp_per_img
# note that: max_fp = all_fp / nImg Recall@max_fp = all_tp / nGt
mean_recall = [0.0 for i in range(len(avgFP) + 1)]
for cls in range(len(all_boxes)):
result, valid_avgFP = sens_at_FP(all_boxes[cls], gt_boxes[cls], avgFP, iou_th)
# pdb.set_trace()
# max_fp >= 0
assert len(valid_avgFP) >= 1
# avgFP_ = avgFP.copy()
avgFP_ = avgFP[:]
avgFP_.append(valid_avgFP[-1])
result_ = [0.0 for i in range(len(avgFP_))]
valid_avgFP_ = [0.0 for i in range(len(avgFP_))]
for i in range(len(avgFP_)):
if i < len(valid_avgFP):
result_[i] = result[i]
valid_avgFP_[i] = avgFP_[i]
else:
result_[i] = result[-1]
valid_avgFP_[i] = avgFP_[i]
for idx, (recall, fp) in enumerate(zip(result_, valid_avgFP_)):
mean_recall[idx] += float(recall)
print('='*20, 'recall', '='*20)
avg_recall = 0.0
for i in range(len(avgFP)):
print('Recall@%.2f=%.2f%%' % (avgFP[i], mean_recall[i]/len(CLASSES)*100.0))
avg_recall += mean_recall[i]/len(CLASSES)
print('avg recall = %.2f%%' % (avg_recall/len(avgFP)*100.0))
def get_pred_info(pred_dir, file_id_list, CLASSES):
class_num = len(CLASSES)
all_boxes = [[] for _ in range(class_num)]
for cls_idx, class_name in enumerate(CLASSES):
for file_id in file_id_list[cls_idx]:
file_id = file_id.replace('.png', '.json')
json_file = os.path.join(pred_dir, file_id)
all_box = []
if os.path.exists(json_file) and os.stat(json_file).st_size > 0:
annos = json.load(open(json_file))
for i in range(len(annos)):
if class_name in annos[i].keys():
all_box.append(annos[i][class_name])
elif class_name.lower() in annos[i].keys():
all_box.append(annos[i][class_name.lower()])
if all_box:
all_box = np.array(all_box, dtype=np.float32)
else:
all_box = np.zeros((0, 5), dtype=np.float32)
all_boxes[cls_idx].append(all_box)
return all_boxes
def get_gt_info(test_json, CLASSES):
class_num = len(CLASSES)
gt_boxes = [[] for _ in range(class_num)]
file_id_list = [[] for _ in range(class_num)]
annos = json.load(open(test_json))
for cls_idx, class_name in enumerate(CLASSES):
for entry in annos:
syms, boxes = entry['syms'], entry['boxes']
file_name = entry['file_name']
gt_box = []
for idx, (sym, box) in enumerate(zip(syms, boxes)):
if sym == class_name:
gt_box.append(box)
if gt_box:
gt_box = np.array(gt_box, dtype=np.float32)
else:
gt_box = np.zeros((0, 4), dtype=np.float32)
gt_boxes[cls_idx].append(gt_box)
file_id_list[cls_idx].append(file_name)
return gt_boxes, file_id_list
def main():
CLASSES = ['Consolidation', 'Fibrosis', 'Effusion', 'Nodule', 'Mass',
'Emphysema', 'Calcification', 'Atelectasis', 'Fracture', 'Pneumothorax']
test_json = '/home/lianjie/deepwise_x-ray/jsons/test.json'
pred_dir = '/home/lianjie/deepwise_x-ray/results/'
gt_boxes, file_id_list = get_gt_info(test_json, CLASSES)
all_boxes = get_pred_info(pred_dir, file_id_list, CLASSES)
eval_FROC(gt_boxes, all_boxes, CLASSES)
if __name__ == '__main__':
main()