-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
359 lines (298 loc) · 10.7 KB
/
main.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import argparse
import os
from pathlib import Path
import numpy as np
from scipy.spatial.distance import cdist
from scipy.special import softmax
from shapely.geometry import Polygon
from tqdm import trange
from calibrators import DoublyBoundedScaling
from calibrators import PlattScaling
from calibrators import TemperatureScaling
def convert_format(boxes_array):
polygons = [
Polygon([(box[i, 0], box[i, 1]) for i in range(4)])
for box in boxes_array
]
return np.array(polygons)
def compute_iou(box, boxes):
iou = [box.intersection(b).area / box.union(b).area for b in boxes]
return np.array(iou, dtype=np.float32)
def nms_rotated(boxes, scores, threshold):
if boxes.shape[0] == 0:
return np.array([], dtype=np.int32)
polygons = convert_format(boxes)
top = 1000
ixs = scores.argsort()[::-1][:top]
pick = []
while len(ixs) > 0:
i = ixs[0]
pick.append(i)
iou = compute_iou(polygons[i], polygons[ixs[1:]])
remove_ixs = np.where(iou > threshold)[0] + 1
ixs = np.delete(ixs, remove_ixs)
ixs = np.delete(ixs, 0)
return np.array(pick, dtype=np.int32)
def caluclate_tp_fp(det_boxes, det_score, gt_boxes, result_stat, iou_thresh):
fp = []
tp = []
gt = gt_boxes.shape[0]
if det_boxes is not None:
# sort the prediction bounding box by score
score_order_descend = np.argsort(-det_score)
det_polygon_list = list(convert_format(det_boxes))
gt_polygon_list = list(convert_format(gt_boxes))
# match prediction and gt bounding box
for i in range(score_order_descend.shape[0]):
det_polygon = det_polygon_list[score_order_descend[i]]
ious = compute_iou(det_polygon, gt_polygon_list)
if len(gt_polygon_list) == 0 or np.max(ious) < iou_thresh:
fp.append(1)
tp.append(0)
continue
fp.append(0)
tp.append(1)
gt_index = np.argmax(ious)
gt_polygon_list.pop(gt_index)
result_stat[iou_thresh]['fp'] += fp
result_stat[iou_thresh]['tp'] += tp
result_stat[iou_thresh]['gt'] += gt
def init_stats():
stats = {}
for iou in [0.3, 0.5, 0.7]:
stats[iou] = {'tp': [], 'fp': [], 'gt': 0}
return stats
def evaluation(preds, probs, trues, stats):
for iou in [0.3, 0.5, 0.7]:
caluclate_tp_fp(preds, probs, trues, stats, iou)
def voc_ap(rec, prec):
"""
VOC 2010 Average Precision.
"""
rec.insert(0, 0.0)
rec.append(1.0)
mrec = rec[:]
prec.insert(0, 0.0)
prec.append(0.0)
mpre = prec[:]
for i in range(len(mpre) - 2, -1, -1):
mpre[i] = max(mpre[i], mpre[i + 1])
i_list = []
for i in range(1, len(mrec)):
if mrec[i] != mrec[i - 1]:
i_list.append(i)
ap = 0.0
for i in i_list:
ap += ((mrec[i] - mrec[i - 1]) * mpre[i])
return ap, mrec, mpre
def calculate_ap(result_stat, iou):
"""
Calculate the average precision and recall, and save them into a txt.
Parameters
----------
result_stat : dict
A dictionary contains fp, tp and gt number.
iou : float
"""
iou_5 = result_stat[iou]
fp = iou_5['fp']
tp = iou_5['tp']
assert len(fp) == len(tp)
gt_total = iou_5['gt']
cumsum = 0
for idx, val in enumerate(fp):
fp[idx] += cumsum
cumsum += val
cumsum = 0
for idx, val in enumerate(tp):
tp[idx] += cumsum
cumsum += val
rec = tp[:]
for idx, val in enumerate(tp):
rec[idx] = float(tp[idx]) / gt_total
prec = tp[:]
for idx, val in enumerate(tp):
prec[idx] = float(tp[idx]) / (fp[idx] + tp[idx])
ap, _, _ = voc_ap(rec[:], prec[:])
return ap
def eval_final_results(result_stat):
ap_30 = calculate_ap(result_stat, 0.30)
ap_50 = calculate_ap(result_stat, 0.50)
ap_70 = calculate_ap(result_stat, 0.70)
print('The Average Precision at IOU 0.3 is %.3f, '
'The Average Precision at IOU 0.5 is %.3f, '
'The Average Precision at IOU 0.7 is %.3f' % (ap_30, ap_50, ap_70))
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
'--model-dir',
type=str,
required=True,
help='Model directory.',
)
parser.add_argument(
'--cav-model-dir',
type=str,
default="none",
help='CAV Model directory.',
)
parser.add_argument('--calibrator',
type=str,
default="none",
help='Choose calibrator from {none, ps, ts, dbs}.')
parser.add_argument('--aggregator',
type=str,
default="nms",
help='Choose aggregator from {nms, psa}.')
parser.add_argument('--threshold',
type=float,
default=0.0,
help='Confidence threshold for filtering.')
args = parser.parse_args()
print(args)
return args
def non_maximum_suppression(preds, probs):
keep_index = nms_rotated(preds, probs, threshold=0.15)
preds = preds[keep_index]
probs = probs[keep_index]
return preds, probs
def load_data(frame_dir):
if frame_dir is None:
return None, None, None
data = np.load(frame_dir)
preds = data["preds"]
probs = data["probs"]
trues = data["trues"]
return preds, probs, trues
def calibration(calibrator, probs):
if calibrator is None or len(probs) == 0:
return probs
else:
return calibrator.transform(probs)
def to_polygon(box):
return Polygon([(box[i, 0], box[i, 1]) for i in range(4)])
def compute_self_iou_mat(boxes, dist_threshold=10.0):
centers = boxes.mean(axis=1)
dist_mat = cdist(centers, centers)
iou_mat = np.zeros_like(dist_mat)
np.fill_diagonal(iou_mat, 1.0)
for i in range(len(boxes)):
for j in range(i + 1, len(boxes)):
if dist_mat[i, j] < dist_threshold:
polygon1 = to_polygon(boxes[i])
polygon2 = to_polygon(boxes[j])
iou = polygon1.intersection(polygon2).area / polygon1.union(
polygon2).area
if iou > iou_mat[i, j]:
iou_mat[i, j] = iou
iou_mat[j, i] = iou
return iou_mat
def aggregation(args, preds, probs):
if args.aggregator == "nms":
preds, probs = non_maximum_suppression(preds, probs)
elif args.aggregator == "psa":
iou_mat = compute_self_iou_mat(preds)
clusters, visited, selected = [], [], []
for idx, ious in enumerate(iou_mat):
if idx in visited:
continue
neighbor_idxs = np.nonzero(ious)[0]
clusters.append(neighbor_idxs)
visited.extend(neighbor_idxs)
for cluster in clusters:
sub_iou_mat = iou_mat[np.ix_(cluster, cluster)]
sub_probs = probs[cluster]
values = sub_iou_mat.dot(sub_probs)
soft_bools = softmax(values / 1e-6)
bools = soft_bools > 0.5
selected.extend(cluster[bools])
preds = preds[selected]
probs = probs[selected]
else:
raise ValueError("Choose aggregator from {nms, psa}.")
return preds, probs
def load_calibrator(calibrator_name, calibrator_path):
if calibrator_name == "none":
return None
elif calibrator_name == "ps":
calibrator = PlattScaling()
elif calibrator_name == "dbs":
calibrator = DoublyBoundedScaling()
elif calibrator_name == "ts":
calibrator = TemperatureScaling()
else:
raise ValueError("Choose calibrator from {none, ps, ts, dbs}.")
print(f"Loading calibrator from {calibrator_path}")
calibrator.load_model(calibrator_path)
return calibrator
def get_calibrators(args):
calibrator_path = os.path.join(args.model_dir, f"{args.calibrator}.pt")
calibrator = load_calibrator(args.calibrator, calibrator_path)
if args.cav_model_dir == "none":
cav_calibrator = None
else:
cav_calibrator_path = os.path.join(args.cav_model_dir,
f"{args.calibrator}.pt")
cav_calibrator = load_calibrator(args.calibrator, cav_calibrator_path)
return calibrator, cav_calibrator
def filtration(args, preds, probs):
if preds is None:
return None, None
selected = probs > args.threshold
preds = preds[selected]
probs = probs[selected]
return preds, probs
def get_data_dirs(args):
# Hetero setting
if args.cav_model_dir != "none":
data_dir = os.path.join(args.model_dir, f"hetero")
cav_data_dir = os.path.join(args.cav_model_dir, f"hetero")
else: # Homo setting
data_dir = os.path.join(args.model_dir, f"test")
cav_data_dir = None
# Make sure that the directories exist
if not Path(data_dir).exists():
raise ValueError(f"{data_dir} does not exist!")
if cav_data_dir and not Path(cav_data_dir).exists():
raise ValueError(f"{cav_data_dir} does not exist!")
return data_dir, cav_data_dir
def get_frame_paths(data_dir, cav_data_dir, frame_id):
frame_path = os.path.join(data_dir, f"{frame_id:04d}.npz")
if not Path(frame_path).exists():
# print(f"{frame_path} is missing!")
return None, None
if cav_data_dir is None:
cav_frame_path = None
else:
cav_frame_path = os.path.join(cav_data_dir, f"{frame_id:04d}.npz")
if not Path(cav_frame_path).exists():
# print(f"{cav_frame_path} is missing!")
return None, None
return frame_path, cav_frame_path
def main():
args = parse_arguments()
data_dir, cav_data_dir = get_data_dirs(args)
calibrator, cav_calibrator = get_calibrators(args)
stats = init_stats()
for frame_id in trange(2170):
frame_path, cav_frame_path = get_frame_paths(data_dir, cav_data_dir,
frame_id)
if frame_path is None and cav_frame_path is None:
continue
preds, probs, trues = load_data(frame_path)
cav_preds, cav_probs, cav_trues = load_data(cav_frame_path)
if cav_trues is not None:
np.testing.assert_allclose(trues, cav_trues)
probs = calibration(calibrator, probs)
cav_probs = calibration(cav_calibrator, cav_probs)
preds, probs = filtration(args, preds, probs)
cav_preds, cav_probs = filtration(args, cav_preds, cav_probs)
if cav_preds is not None:
preds = np.vstack((preds, cav_preds))
if cav_probs is not None:
probs = np.hstack((probs, cav_probs))
preds, probs = aggregation(args, preds, probs)
evaluation(preds, probs, trues, stats)
eval_final_results(stats)
if __name__ == "__main__":
main()