-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistic.py
236 lines (185 loc) · 6.77 KB
/
statistic.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
from retinanet.dataset import Ring_Cell_all_dataset
from tqdm import tqdm
import numpy as np
import torch
# from lib.nms.pth_nms import pth_nms
from lib_new.nms.gpu_nms import gpu_nms
from metric import compute_overlap
import matplotlib.pyplot as plt
import matplotlib
import os
import glob
from lib_new.nms.nums_py import py_cpu_nms
def nms(dets, thresh):
"Dispatch to either CPU or GPU NMS implementations.\
Accept dets as tensor"""
dets = dets.cpu().detach().numpy()
# return gpu_nms(dets, thresh)
return py_cpu_nms(dets, thresh)
csv_dir = './test_result_da'
csv_path_list = glob.glob(os.path.join(csv_dir, 'retinanet_resnet18_round4_fold_*_weight_loss_1_on_test_data_best_valid_recall_original_0.4(round0)_0.4(round1)_0.4(round2)_0.4_multi3(round3).csv'))
for pred_csv in csv_path_list:
# if pred_csv.split('/')[-1] != 'retinanet_resnet18_round0_fold_0_weight_loss_1_on_train_data_best_valid_recall_new1.csv':
# continue
# pred_csv = './test_result/retinanet_resnet101_round{}_test_fold_{}.csv'.format(round, test_fold)
# if not os.path.exists(pred_csv):
# # continue
# # if os.path.exists(pred_csv.replace('.csv', '.jpg')):
# # continue
# #
# # if os.path.exists(ap_image):
# # continue
test_fold = pred_csv.split('_fold_')[1][0]
# test_fold = 0
# dataset = pred_csv.split('_data_')[0][-1]
# if dataset == 't':
# dataset = 'test'
# # else:
# dataset = 'train'
dataset = 'test'
pred_dict_box = {}
pred_dict_score = {}
with open(pred_csv, 'r') as f:
lines = f.readlines()
for line in lines:
line = line[:-1]
line = line.split(',')
image_name = line[0]
pred_dict_box[image_name] = []
pred_dict_score[image_name] = []
if len(line[1]) != 0:
preds = line[1].split(';')[:-1]
for pred in preds:
pred = pred.split(' ')
box = []
for elemet in pred[:-1]:
box.append(float(elemet))
pred_dict_box[image_name].append(box)
pred_dict_score[image_name].append(float(pred[-1]))
test_dataset = Ring_Cell_all_dataset('/data/sqy/code/miccai2019/train_test_4/{}_{}.txt'.format(dataset, test_fold))
result_dict = {}
nms_threshold = 0.3
scores = np.zeros((0,))
iou_threshold = 0.3
scores_all_prediction = []
if not os.path.exists(pred_csv.replace('.csv', '.jpg')):
for i, (image, bbox, image_, image_name) in enumerate(tqdm(test_dataset)):
detected_annotations = []
scores_all_prediction.extend(pred_dict_score[image_name])
if len(bbox) != 0:
# pos image
pred_scores = pred_dict_score[image_name]
detections = pred_dict_box[image_name]
annotations = np.array(bbox)
for j, d in enumerate(detections):
score = pred_scores[j]
overlaps = compute_overlap(np.expand_dims(d, axis=0), annotations)
assigned_annotation = np.argmax(overlaps, axis=1)
max_overlap = overlaps[0, assigned_annotation]
if max_overlap >= iou_threshold and assigned_annotation not in detected_annotations:
detected_annotations.append(assigned_annotation)
scores = np.append(scores, score)
annotations_not_detected = len(bbox) - len(detected_annotations)
for j in range(annotations_not_detected):
scores = np.append(scores, 0)
x_axis = [ 1. * x / 10 for x in range(0, 10)]
x_axis = [str(x) for x in x_axis]
num_record_between = [0 for x in range(10)]
num_record_over = [0 for x in range(10)]
for score in scores:
for i in range(0, 10):
j = 1. * i / 10
j_ = 1. * (i + 1) / 10
if score >= j and score < j_:
num_record_between[i] += 1
if score > j:
num_record_over[i] += 1
num_record_between_num = num_record_between
num_record_between = [x / len(scores) for x in num_record_between]
num_record_over = [x / len(scores) for x in num_record_over]
x = np.arange(len(x_axis))
plt.figure(figsize=(15, 15))
plt.subplot(2,2,1)
rects = plt.bar(x + 0.5, height=num_record_between, width=0.5)
plt.title('between (gt)')
for rect in rects:
height = rect.get_height()
value = height * 100
value = str(value)[:2]
if value[-1] == '.':
value = value[0]
value = value + '%'
plt.text(rect.get_x() + rect.get_width() / 2, height, value, ha="center", va="bottom")
plt.ylim(0, 1)
x_axis_ = x_axis.copy()
x_axis_.append(str(1.0))
x_ = np.arange(len(x_axis))
plt.xticks([index for index in x_], x_axis_)
plt.subplot(2,2,2)
rects = plt.bar(x + 0.5, height=num_record_over, width=0.5, color='#FF8006')
plt.title('over')
for rect in rects:
height = rect.get_height()
value = height * 100
if value == 100:
value = str(value)
else:
value = str(value)[:2]
if value[-1] == '.':
value = value[0]
value = value + '%'
plt.text(rect.get_x() + rect.get_width() / 2, height, value, ha="center", va="bottom")
plt.ylim(0, 1)
x_axis_ = x_axis.copy()
x_axis_.append(str(1.0))
x_ = np.arange(len(x_axis))
plt.xticks([index for index in x_], x_axis_)
plt.subplot(2, 2, 3)
num_record_between_all = [0 for x in range(10)]
for score in scores_all_prediction:
for i in range(0, 10):
j = 1. * i / 10
j_ = 1. * (i + 1) / 10
if score >= j and score < j_:
num_record_between_all[i] += 1
num_record_between_all_num = num_record_between_all
num_record_between_all = [x / len(scores_all_prediction) for x in num_record_between_all]
rects = plt.bar(x + 0.5, height=num_record_between_all, width=0.5, color='#F54545')
plt.title('between (all prediction)')
for rect in rects:
height = rect.get_height()
value = height * 100
if value == 100:
value = str(value)
else:
value = str(value)[:2]
if value[-1] == '.':
value = value[0]
value = value + '%'
plt.text(rect.get_x() + rect.get_width() / 2, height, value, ha="center", va="bottom")
plt.ylim(0, 1)
x_axis_ = x_axis.copy()
x_axis_.append(str(1.0))
x_ = np.arange(len(x_axis))
plt.xticks([index for index in x_], x_axis_)
plt.subplot(2, 2, 4)
num_record_between_gt_over_all = [num_record_between_num[x] / num_record_between_all_num[x] for x in range(10)]
rects = plt.bar(x + 0.5, height=num_record_between_gt_over_all, width=0.5, color='#45F545')
plt.title('between (gt / all)')
for rect in rects:
height = rect.get_height()
value = height * 100
if value == 100:
value = str(value)
else:
value = str(value)[:2]
if value[-1] == '.':
value = value[0]
value = value + '%'
plt.text(rect.get_x() + rect.get_width() / 2, height, value, ha="center", va="bottom")
plt.ylim(0, 1)
x_axis_ = x_axis.copy()
x_axis_.append(str(1.0))
x_ = np.arange(len(x_axis))
plt.xticks([index for index in x_], x_axis_)
plt.savefig(pred_csv.replace('.csv', '.jpg'))