-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
318 lines (246 loc) · 14.2 KB
/
evaluate.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
import torch
import pickle
from .utils_func import load_json, traj_cutoff,dura_intersection_ts
class EvalFmtCvtor(object):
def __init__(self,
dataset_type,
enti_split_info_path,
pred_split_info_path,
score_merge="mul",
segment_cvt=False,
is_debug=False
):
self.segment_cvt = segment_cvt
self.dataset_type = dataset_type.lower()
if score_merge == "mul":
self.score_merge = torch.prod
elif score_merge == "mean":
self.score_merge = torch.mean
elif score_merge == "sum":
self.score_merge = torch.sum
else:
assert False
self.enti_id2cls = load_json(enti_split_info_path)["id2cls"] # the key in json is str, e.g, "0":__background__
self.pred_id2cls = load_json(pred_split_info_path)["id2cls"]
self.enti_id2cls = {int(k):v for k,v in self.enti_id2cls.items()}
self.pred_id2cls = {int(k):v for k,v in self.pred_id2cls.items()}
if is_debug:
pass
# TODO add code for debug
def _reset_vsig(self,vsig):
if self.dataset_type == "vidor":
temp = vsig.split('_') # e.g., "0001_3598080384" or "0001_3598080384-0015-0045"
assert len(temp) == 2
vsig = temp[1]
else: # for vidvrd, e.g., vsig == "ILSVRC2015_train_00005015" or "ILSVRC2015_train_00005015-0015-0045"
pass
return vsig
def to_eval_json_format(self,vsig,pr_5tuple,pso_scores,traj_bboxes,traj_starts,triplets_topk=-1,debug_infos=dict()):
'''
refer to `VidVRDhelperEvalAPIs/README.md` for submission json format
vsig: video signature, which represents video_name for segment_cvt=False and segment_tag for segment_cvt=True
pr_5tuple, # shape == (n_pair,5) # format: [pred_catid,subj_catid,obj_catid,subj_tid,obj_tid]
pr_scores, # shape == (n_pair,3)
traj_bboxes, # list[tensor] , len == n_det
traj_starts, # (n_det,) w.r.t segment f_start
FIXME add segment fstart, seg_fs
'''
vsig = self._reset_vsig(vsig)
if self.segment_cvt:
video_name, fstart, fend = vsig.split('-') # e.g., "ILSVRC2015_train_00010001-0015-0045"
fstart,fend = int(fstart),int(fend)
traj_starts = traj_starts + fstart
n_pair,_ = pr_5tuple.shape
if n_pair == 0:
print("for {}, n_pair==0".format(vsig))
return {vsig:[]}
device = traj_starts.device
lens = torch.as_tensor([tb.shape[0] for tb in traj_bboxes],device=device) # (n_det,)
duras = torch.stack([traj_starts,traj_starts+lens-1],dim=-1) # (n_det,2), closed interval, w.r.t segment
duras_inter,mask = dura_intersection_ts(duras,duras,broadcast=True) # (n_det,n_det,2), (n_det,n_det)
debug_names = list(debug_infos.keys())
if len(debug_names) > 0:
for name,info in debug_infos.items():
assert isinstance(info,torch.Tensor)
assert info.shape[0] == n_pair # (n_pair,*,...)
# pr_scores = self.score_merge(pso_scores,dim=-1) # (n_pair,)
pr_scores = self.score_merge(pso_scores,dim=-1)
if triplets_topk > 0:
# keep topk triplets (for saving time when doing the mAP evaluation API)
topkids = pr_scores.argsort(descending=True)[:triplets_topk] # (k,), e.g., k=200
pr_scores = pr_scores[topkids]
pr_5tuple = pr_5tuple[topkids,:]
pso_scores = pso_scores[topkids,:]
n_pair,_ = pr_5tuple.shape
for name in debug_names:
debug_infos[name] = debug_infos[name][topkids,...]
else:
# else, send all predictions into the mAP evaluation API
pass
if isinstance(pr_5tuple,torch.Tensor):
pr_5tuple = pr_5tuple.tolist()
if isinstance(pr_scores,torch.Tensor):
pr_scores = pr_scores.tolist()
results_per_video = []
for p_id in range(n_pair):
pred_catid,subj_catid,obj_catid,subj_tid,obj_tid = pr_5tuple[p_id]
if not mask[subj_tid,obj_tid]:
continue
ori_sub_traj = traj_bboxes[subj_tid]
ori_obj_traj = traj_bboxes[obj_tid]
so_dura = duras_inter[subj_tid,obj_tid,:].tolist()
subj_dura = duras[subj_tid,:].tolist()
obj_dura = duras[obj_tid,:].tolist()
so_dura = (so_dura[0],so_dura[1]+1) # convert to end_fid exclusive format, the same bellow
subj_dura = (subj_dura[0],subj_dura[1]+1)
obj_dura = (obj_dura[0],obj_dura[1]+1)
subject_traj = traj_cutoff(ori_sub_traj,subj_dura,so_dura,vsig)
object_traj = traj_cutoff(ori_obj_traj,obj_dura,so_dura,vsig)
assert len(subject_traj) == len(object_traj)
assert len(subject_traj) == so_dura[1] - so_dura[0]
result_per_triplet = dict()
result_per_triplet["triplet"] = [self.enti_id2cls[subj_catid],self.pred_id2cls[pred_catid],self.enti_id2cls[obj_catid]]
result_per_triplet["duration"] = so_dura # [strat_fid, end_fid) starting (inclusive) and ending (exclusive) frame ids
result_per_triplet["score"] = float(pr_scores[p_id])
# print("pr_scores[p_id]",pr_scores[p_id])
result_per_triplet["sub_traj"] = subject_traj.cpu().tolist() # len == duration_spo[1] - duration_spo[0]
result_per_triplet["obj_traj"] = object_traj.cpu().tolist()
################## for debug #################
result_per_triplet["triplet_tid"] = (int(subj_tid),int(pred_catid),int(obj_tid)) # 如果用 [s_id,p_id,p_catid,o_id]的话,那肯定是唯一的
result_per_triplet["pso_scores"] = pso_scores[p_id,:].tolist()
for name in debug_names:
result_per_triplet[name] = debug_infos[name][p_id,...].tolist()
################## for debug #################
results_per_video.append(result_per_triplet)
# results_per_video = sorted(results_per_video,key=lambda x: x["score"],reverse=True) # large --> small
# results_per_video = results_per_video[:100]
return {vsig : results_per_video}
def to_eval_json_format_posOnly(self,vsig,pr_5tuple,pr_scores,traj_bboxes,traj_starts,triplets_topk=-1,debug_infos=dict()):
'''
refer to `VidVRDhelperEvalAPIs/README.md` for submission json format
vsig: video signature, which represents video_name for segment_cvt=False and segment_tag for segment_cvt=True
pr_5tuple, # shape == (n_pair,5) # format: [pred_catid,subj_catid,obj_catid,subj_tid,obj_tid]
pr_scores, # shape == (n_pair,3)
traj_bboxes, # list[tensor] , len == n_det
traj_starts, # (n_det,) w.r.t segment f_start
FIXME add segment fstart, seg_fs
'''
vsig = self._reset_vsig(vsig)
if self.segment_cvt:
video_name, fstart, fend = vsig.split('-') # e.g., "ILSVRC2015_train_00010001-0015-0045"
fstart,fend = int(fstart),int(fend)
traj_starts = traj_starts + fstart
n_pair,_ = pr_5tuple.shape
if n_pair == 0:
print("for {}, n_pair==0".format(vsig))
return {vsig:[]}
device = traj_starts.device
lens = torch.as_tensor([tb.shape[0] for tb in traj_bboxes],device=device) # (n_det,)
duras = torch.stack([traj_starts,traj_starts+lens-1],dim=-1) # (n_det,2), closed interval, w.r.t segment
duras_inter,mask = dura_intersection_ts(duras,duras,broadcast=True) # (n_det,n_det,2), (n_det,n_det)
debug_names = list(debug_infos.keys())
if len(debug_names) > 0:
for name,info in debug_infos.items():
assert isinstance(info,torch.Tensor)
assert info.shape[0] == n_pair # (n_pair,*,...)
pr_scores = self.score_merge(pr_scores,dim=-1) # (n_pair,)
if triplets_topk > 0:
# keep topk triplets (for saving time when doing the mAP evaluation API)
topkids = pr_scores.argsort(descending=True)[:triplets_topk] # (k,), e.g., k=200
pr_scores = pr_scores[topkids]
pr_5tuple = pr_5tuple[topkids,:]
n_pair,_ = pr_5tuple.shape
for name in debug_names:
debug_infos[name] = debug_infos[name][topkids,...]
else:
# else, send all predictions into the mAP evaluation API
pass
if isinstance(pr_5tuple,torch.Tensor):
pr_5tuple = pr_5tuple.tolist()
if isinstance(pr_scores,torch.Tensor):
pr_scores = pr_scores.tolist()
results_per_video = []
for p_id in range(n_pair):
pred_catid,subj_catid,obj_catid,subj_tid,obj_tid = pr_5tuple[p_id]
if not mask[subj_tid,obj_tid]:
continue
ori_sub_traj = traj_bboxes[subj_tid]
ori_obj_traj = traj_bboxes[obj_tid]
so_dura = duras_inter[subj_tid,obj_tid,:].tolist()
subj_dura = duras[subj_tid,:].tolist()
obj_dura = duras[obj_tid,:].tolist()
so_dura = (so_dura[0],so_dura[1]+1) # convert to end_fid exclusive format, the same bellow
subj_dura = (subj_dura[0],subj_dura[1]+1)
obj_dura = (obj_dura[0],obj_dura[1]+1)
subject_traj = traj_cutoff(ori_sub_traj,subj_dura,so_dura,vsig)
object_traj = traj_cutoff(ori_obj_traj,obj_dura,so_dura,vsig)
assert len(subject_traj) == len(object_traj)
assert len(subject_traj) == so_dura[1] - so_dura[0]
result_per_triplet = dict()
# result_per_triplet["triplet"] = [self.enti_id2cls[subj_catid],self.pred_id2cls[pred_catid],self.enti_id2cls[obj_catid]]
result_per_triplet["triplet"] = [self.enti_id2cls[subj_catid],"fg",self.enti_id2cls[obj_catid]]
result_per_triplet["duration"] = so_dura # [strat_fid, end_fid) starting (inclusive) and ending (exclusive) frame ids
result_per_triplet["score"] = float(pr_scores[p_id])
# print("pr_scores[p_id]",pr_scores[p_id])
result_per_triplet["sub_traj"] = subject_traj.cpu().tolist() # len == duration_spo[1] - duration_spo[0]
result_per_triplet["obj_traj"] = object_traj.cpu().tolist()
################## for debug #################
result_per_triplet["triplet_tid"] = (int(subj_tid),int(pred_catid),int(obj_tid)) # 如果用 [s_id,p_id,p_catid,o_id]的话,那肯定是唯一的
for name in debug_names:
result_per_triplet[name] = debug_infos[name][p_id,...].tolist()
################## for debug #################
results_per_video.append(result_per_triplet)
# results_per_video = sorted(results_per_video,key=lambda x: x["score"],reverse=True) # large --> small
# results_per_video = results_per_video[:100]
return {vsig : results_per_video}
def to_eval_json_format_v2(self,vsig,pr_5tuple,pr_scores,traj_bboxes,traj_starts,preserve_debug_info=True,triplets_topk=-1):
'''
this func without traj_cutoff
'''
vsig = self._reset_vsig(vsig)
n_pair,_ = pr_5tuple.shape
if n_pair == 0:
print("for {}, n_pair==0".format(vsig))
return {vsig:[]}
lens = torch.as_tensor([tb.shape[0] for tb in traj_bboxes]) # (n_det,)
duras = torch.stack([traj_starts,traj_starts+lens-1],dim=-1) # (n_det,2), closed interval
duras_inter,mask = dura_intersection_ts(duras,duras,broadcast=True) # (n_det,n_det,2), (n_det,n_det)
pr_scores = self.score_merge(pr_scores,dim=-1) # (n_pair,)
if triplets_topk > 0:
# keep topk triplets (for saving time when doing the mAP evaluation API)
topkids = pr_scores.argsort(descending=True)[:triplets_topk] # (k,), e.g., k=200
pr_scores = pr_scores[topkids]
pr_5tuple = pr_5tuple[topkids,:]
n_pair,_ = pr_5tuple.shape
else:
# else, send all predictions into the mAP evaluation API
pass
# pred_duras_float = debug_info
if isinstance(pr_5tuple,torch.Tensor):
pr_5tuple = pr_5tuple.tolist()
if isinstance(pr_scores,torch.Tensor):
pr_scores = pr_scores.tolist()
results_per_video = []
for p_id in range(n_pair):
pred_catid,subj_catid,obj_catid,subj_tid,obj_tid = pr_5tuple[p_id]
if not mask[subj_tid,obj_tid]:
continue
ori_sub_traj = traj_bboxes[subj_tid]
ori_obj_traj = traj_bboxes[obj_tid]
so_dura = duras_inter[subj_tid,obj_tid,:].tolist()
so_dura = (so_dura[0],so_dura[1]+1) # convert to end_fid exclusive format, the same bellow
result_per_triplet = dict()
result_per_triplet["triplet"] = [self.enti_id2cls[subj_catid],self.pred_id2cls[pred_catid],self.enti_id2cls[obj_catid]]
result_per_triplet["duration"] = so_dura # [strat_fid, end_fid) starting (inclusive) and ending (exclusive) frame ids
result_per_triplet["score"] = float(pr_scores[p_id])
result_per_triplet["sub_traj"] = ori_sub_traj.cpu().tolist() # len == duration_spo[1] - duration_spo[0]
result_per_triplet["obj_traj"] = ori_obj_traj.cpu().tolist()
################## for debug #################
if preserve_debug_info:
result_per_triplet["triplet_tid"] = (int(subj_tid),int(pred_catid),int(obj_tid)) # 如果用 [s_id,p_id,p_catid,o_id]的话,那肯定是唯一的
# result_per_triplet["debug_dura"] = {"s":subject_dura,"o":object_dura,"inter":dura_,"pr_float_dura":pr_float_dura}
################## for debug #################
results_per_video.append(result_per_triplet)
# results_per_video = sorted(results_per_video,key=lambda x: x["score"],reverse=True) # large --> small
# results_per_video = results_per_video[:100]
return {vsig : results_per_video}