-
Notifications
You must be signed in to change notification settings - Fork 9
/
evaluate.py
501 lines (361 loc) · 15.4 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import json
import language_evaluation
import numpy as np
from tqdm import tqdm
import argparse
def load_data(gt_data, pred_data):
gt = gt_data
pred = pred_data
if isinstance(gt_data, str):
with open(gt_data, 'r') as f:
gt = json.load(f)
else:
assert isinstance(gt, dict), "GT data should be a str path or a dict"
if isinstance(pred_data, str):
with open(pred_data, 'r') as f:
pred = json.load(f)
else:
assert isinstance(pred, dict), "Prediction data should be a str path or a dict"
return gt, pred
def compute_iou(interval_1, interval_2):
start_i, end_i = interval_1[0], interval_1[1]
start, end = interval_2[0], interval_2[1]
intersection = max(0, min(end, end_i) - max(start, start_i))
union = min(max(end, end_i) - min(start, start_i), end-start + end_i-start_i)
iou = float(intersection) / (union + 1e-8)
return iou
def evaluate_video_retrieval(gt_data, pred_data):
gt, pred = load_data(gt_data, pred_data)
ks = [ 1, 5, 10, 50 ]
count = { }
total = { }
for cat in PROMPT_CATEGORIES:
count[cat] = {}
total[cat] = 0
for k in ks:
count[cat][f"{k}"] = 0
for prompt in tqdm(gt):
prompt_cat = PROMPT_TO_CAT[prompt]
gt_videos = list(gt[prompt].keys())
total["all"] += 1
total[prompt_cat] += 1
videos = pred[prompt]["videos"]
scores = pred[prompt]["scores"]
scores, videos = zip(*sorted(zip(scores, videos)))
scores = scores[::-1]
videos = videos[::-1]
for k in ks:
recall_k_videos = videos[:k]
for v in recall_k_videos:
if v in gt_videos:
count["all"][f"{k}"] += 1
count[prompt_cat][f"{k}"] += 1
break
results = {}
for cat in PROMPT_CATEGORIES:
if total[cat] > 0:
results[cat] = {}
results[cat]["total_prompt_count"] = total[cat]
for k in ks:
results[cat][f"R@{k}"] = (count[cat][f"{k}"] / total[cat]) * 100
return results
def evaluate_moment_retrieval(gt_data, pred_data):
gt, pred = load_data(gt_data, pred_data)
score_dict = { }
for cat in PROMPT_CATEGORIES:
score_dict[cat] = { }
tIoUs = [ 0.5, 0.7 ]
for tIoU in tIoUs:
scores = {}
for cat in PROMPT_CATEGORIES:
scores[cat] = []
for prompt in tqdm(gt):
prompt_cat = PROMPT_TO_CAT[prompt]
for video in gt[prompt]:
if gt[prompt][video]["clip"]:
gt_bounds = gt[prompt][video]["bounds"]
pred_bounds = pred[prompt][video]["bounds"]
iou = compute_iou(gt_bounds, pred_bounds)
if iou < tIoU:
score = 0
else:
score = 1
scores["all"].append(score)
scores[prompt_cat].append(score)
for cat in PROMPT_CATEGORIES:
if len(scores[cat]) > 0:
score_dict[cat]["total_videos"] = len(scores[cat])
score_dict[cat][f"R@{tIoU}"] = np.mean(scores[cat]) * 100
return score_dict
def compute_step_bound_scores(gt_data, pred_data):
gt, pred = load_data(gt_data, pred_data)
results = {}
for cat in PROMPT_CATEGORIES:
results[cat] = {}
results[cat]["recall"] = {}
results[cat]["precision"] = {}
for tiou in [ 0.5, 0.7 ]:
recall = {}
precision = {}
ious = {}
for cat in PROMPT_CATEGORIES:
recall[cat] = []
precision[cat] = []
ious[cat] = []
for i, video in tqdm(enumerate(gt), total=len(gt)):
video_cat = VIDEOS_TO_CAT[video]
best_recall = 0
best_precision = 0
ref_set_covered = set([])
pred_set_covered = set([])
refs = gt[video]["bounds"]
preds = pred[video]["bounds"]
for pred_i, pred_x in enumerate(preds):
local_ious = []
for ref_i, gt_x in enumerate(refs):
iu = compute_iou(pred_x, gt_x)
local_ious.append(iu)
if iu > tiou:
ref_set_covered.add(ref_i)
pred_set_covered.add(pred_i)
ious[video_cat].append(max(local_ious))
ious["all"].append(max(local_ious))
new_precision = float(len(pred_set_covered)) / (pred_i + 1)
best_precision = max(best_precision, new_precision)
new_recall = float(len(ref_set_covered)) / len(refs)
best_recall = max(best_recall, new_recall)
recall[video_cat].append(best_recall)
precision[video_cat].append(best_precision)
recall["all"].append(best_recall)
precision["all"].append(best_precision)
for cat in PROMPT_CATEGORIES:
if len(recall[cat]) > 0:
results[cat]["recall"][f"{tiou}"] = sum(recall[cat]) / len(recall[cat]) * 100
results[cat]["precision"][f"{tiou}"] = sum(precision[cat]) / len(precision[cat]) * 100
results[cat]["total"] = len(recall[cat])
return results
def evaluate_moment_summarization(gt_data, pred_data, gpu_device: int):
gt, pred = load_data(gt_data, pred_data)
all_results = {}
bert_scores = []
from allennlp_models import pretrained
predictor = pretrained.load_predictor(
"pair-classification-decomposable-attention-elmo",
cuda_device=gpu_device
)
print("Loaded Entailment evaluation model")
if gpu_device != -1 and args.frame_dir != "None":
import clip
model, preprocess = clip.load("ViT-B/32", device=f"cuda:{gpu_device}")
from glob import glob
import torch
from PIL import Image
print("Loaded CLIP model")
for cat in PROMPT_CATEGORIES:
refs = []
cands = []
total_videos = 0
entailment_scores = [ 0, 0, 0 ]
total_entailment_count = 0
k = 0
clip_scores = []
entailment_list = []
for video in tqdm(gt):
video_cat = VIDEOS_TO_CAT[video]
vid_clip_scores = []
if cat == video_cat or cat == 'all':
total_videos += 1
for i, d in enumerate(gt[video]["captions"]):
gt_sent = d["sentence"].lower()
cand = pred[video]["captions"][i]["sentence"].lower()
if gpu_device != -1 and args.frame_dir != "None":
frames = glob(f"{args.frame_dir}/{video}/*.jpg")
frames.sort(key=lambda a : int(a.split("_")[-1].replace(".jpg", "")))
skip = False
if d["start"] >= len(frames) or d["end"] >= len(frames):
skip = True
if not skip:
text = clip.tokenize([cand]).to(f"cuda:{gpu_device}")
idxes = np.linspace(d["start"], min(d["end"], len(frames))-1, 4).astype(int)
frames = np.array(frames)
img_features = [ ]
for frame in frames[idxes]:
image = preprocess(Image.open(frame)).cpu()
img_features.append(image)
img_features = torch.stack(img_features)
with torch.no_grad():
image_features = model.encode_image(img_features.to(f"cuda:{gpu_device}"))
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
dot_score = image_features @ text_features.T
score = torch.mean(dot_score)
vid_clip_scores.append(float(score.cpu()))
k += 1
refs.append(gt_sent)
cands.append(cand)
x = predictor.predict(
premise=gt_sent,
hypothesis=cand
)
entail_idx = np.argmax(x["label_probs"])
if entail_idx == 0:
entailment_list.append(1)
else:
entailment_list.append(0)
entailment_scores[entail_idx] += 1
total_entailment_count += 1
clip_scores.extend(vid_clip_scores)
if len(refs) == 0 or len(cands) == 0:
continue
print("Computing BERTScore...")
from bert_score import score
p, r, f = score(cands, refs, lang='en', verbose=True,
device=f"cuda:{gpu_device}"
)
print("Computing COCO Eval metrics...")
evaluator = language_evaluation.CocoEvaluator()
coco_results = evaluator.run_evaluation(cands, refs)
if len(clip_scores) == 0:
clip_scores = [ 0 ]
results = {
"CLIPScore": np.average(clip_scores),
"BERTScore_F1": f.mean().item(),
"Total": total_videos,
"Entailment": (entailment_scores[0] / total_entailment_count) * 100,
"Contradiction": (entailment_scores[1] / total_entailment_count) * 100,
"Netural": (entailment_scores[2] / total_entailment_count) * 100
}
for metric in coco_results:
results[metric] = coco_results[metric] * 100
all_results[cat] = results
return all_results
def NMS(boxes, overlapThresh=0):
if len(boxes) == 0:
return []
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
pick = []
x1 = boxes[:,0]
y1 = boxes[:,1]
x2 = boxes[:,2]
y2 = boxes[:,3]
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)
while len(idxs) > 0:
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
overlap = (w * h) / area[idxs[:last]]
idxs = np.delete(idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0])))
return boxes[pick]
def preprocess_moment_bounds(gt_data, pred_data):
gt, pred = load_data(gt_data, pred_data)
for i, video in tqdm(enumerate(pred), total=len(pred)):
bounds = pred[video]["bounds"]
gt_bounds = gt[video]["bounds"]
min_x = gt_bounds[0][0]
max_x = gt_bounds[-1][1]
bounds = [bound for bound in bounds if (bound[0] > min_x and bound[1] < max_x) ]
x1 = [ ]
x2 = [ ]
y1 = [ 0 ] * len(bounds)
y2 = [ 1 ] * len(bounds)
for bound in bounds:
x1.append(bound[0])
x2.append(bound[1])
boxes = np.zeros((len(bounds), 4))
boxes[:, 0] = x1
boxes[:, 1] = y1
boxes[:, 2] = x2
boxes[:, 3] = y2
boxes = NMS(boxes)
if len(boxes) > 0:
x1 = boxes[:, 0]
x2 = boxes[:, 2]
bounds = []
for i in range(len(x1)):
bounds.append([ x1[i], x2[i] ])
bounds.sort(key=lambda x: x[0])
new_bounds = []
if bounds[0][0] > min_x:
new_bounds.append([min_x, bounds[0][0]])
for i in range(0, len(bounds)):
new_bounds.append(bounds[i])
if (i+1) < len(bounds):
new_bounds.append([bounds[i][1], bounds[i+1][0]])
if new_bounds[-1][1] < max_x:
new_bounds.append([new_bounds[-1][1], max_x])
else:
new_bounds = [ [min_x, max_x] ]
pred[video]["bounds"] = new_bounds
return pred
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Run experiment', add_help=False)
parser.add_argument('--task', type=str, required=True)
parser.add_argument('--gt_data', type=str, required=False)
parser.add_argument('--pred_data', type=str, required=True)
parser.add_argument('--device', type=int, default=-1)
parser.add_argument('--print_per_category', action='store_true')
parser.add_argument('--help', action='store_true')
parser.add_argument('--preprocess_moment_bounds', action='store_true')
parser.add_argument('--replace_pred_moment_bounds', action='store_true')
parser.add_argument('--frame_dir', type=str, default="None")
args = parser.parse_args()
print(args)
if args.preprocess_moment_bounds:
if args.gt_data is None:
args.gt_data = './data/evaluation/formatted_moment_evaluation_gt.json'
new_pred = preprocess_moment_bounds(args.gt_data, args.pred_data)
if args.replace_pred_moment_bounds:
assert isinstance(args.pred_data, str), "You must provide a path to the source file"
with open(args.pred_data, 'w') as f:
json.dump(new_pred, f)
args.pred_data = new_pred
PROMPT_CATEGORIES = set()
PROMPT_TO_CAT = { }
VIDEOS_TO_CAT = { }
category_path = './data/evaluation/categories.json'
with open(category_path, 'r') as f:
data = json.load(f)
PROMPT_TO_CAT = data["prompt_to_cat"]
VIDEOS_TO_CAT = data["video_to_cat"]
for p in PROMPT_TO_CAT:
PROMPT_CATEGORIES.add(PROMPT_TO_CAT[p])
for v in VIDEOS_TO_CAT:
PROMPT_CATEGORIES.add(VIDEOS_TO_CAT[v])
PROMPT_CATEGORIES = list(PROMPT_CATEGORIES)
PROMPT_CATEGORIES.append("all")
if (args.help):
print("Please see the 'examples_for_evaluation_folder' for input examples")
else:
if args.task == "video_retrieval":
if args.gt_data is None:
args.gt_data = './data/splits/all_data_test.json'
result = evaluate_video_retrieval(args.gt_data, args.pred_data)
elif args.task == "moment_retrieval":
if args.gt_data is None:
args.gt_data = './data/splits/all_data_test.json'
result = evaluate_moment_retrieval(args.gt_data, args.pred_data)
elif args.task == "moment_segmentation":
if args.gt_data is None:
args.gt_data = './data/evaluation/formatted_moment_evaluation_gt.json'
result = compute_step_bound_scores(args.gt_data, args.pred_data)
elif args.task == "step_captioning":
if args.gt_data is None:
args.gt_data = './data/evaluation/formatted_moment_evaluation_gt.json'
if not (args.print_per_category):
PROMPT_CATEGORIES = [ "all" ]
result = evaluate_moment_summarization(args.gt_data, args.pred_data, args.device)
else:
result = { "all": {} }
if not (args.print_per_category):
print(result["all"])
else:
print(result)