-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate_ego4d_nlq.py
167 lines (144 loc) · 5.47 KB
/
evaluate_ego4d_nlq.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
#! /usr/bin/env python
"""
Modified from https://github.com/EGO4D/episodic-memory/blob/main/NLQ/VSLNet/utils/evaluate_ego4d_nlq.py
Script to evaluate performance of any model for Ego4D Episodic Memory Natural Language Queries (NLQ) task
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import argparse
import json
import numpy as np
import terminaltables
def load_json(file):
data = json.load(open(file, "r"))
return data
def display_results(results, mIoU, thresholds, topK, title=None):
display_data = [
[f"Rank@{ii}\nmIoU@{jj}" for ii in topK for jj in thresholds] + ["mIoU"]
]
results *= 100
mIoU *= 100
display_data.append(
[
f"{results[jj][ii]:.02f}"
for ii in range(len(topK))
for jj in range(len(thresholds))
]
+ [f"{mIoU:.02f}"]
)
table = terminaltables.AsciiTable(display_data, title)
for ii in range(len(thresholds) * len(topK)):
table.justify_columns[ii] = "center"
return table.table
def compute_IoU(pred, gt):
"""Compute the IoU given predicted and ground truth windows."""
assert isinstance(pred, list) and isinstance(gt, list)
pred_is_list = isinstance(pred[0], list)
gt_is_list = isinstance(gt[0], list)
if not pred_is_list:
pred = [pred]
if not gt_is_list:
gt = [gt]
pred, gt = np.array(pred), np.array(gt)
inter_left = np.maximum(pred[:, 0, None], gt[None, :, 0])
inter_right = np.minimum(pred[:, 1, None], gt[None, :, 1])
inter = np.maximum(0.0, inter_right - inter_left)
union_left = np.minimum(pred[:, 0, None], gt[None, :, 0])
union_right = np.maximum(pred[:, 1, None], gt[None, :, 1])
union = np.maximum(0.0, union_right - union_left)
overlap = 1.0 * inter / union
if not gt_is_list:
overlap = overlap[:, 0]
if not pred_is_list:
overlap = overlap[0]
return overlap
def evaluate_nlq_performance(
predictions, ground_truth, thresholds, topK, per_instance=False
):
"""Evalutes the performances."""
gt_dict = {}
num_gt_queries = 0
for video_datum in ground_truth["videos"]:
for clip_datum in video_datum["clips"]:
clip_uid = clip_datum["clip_uid"]
for ann_datum in clip_datum["annotations"]:
key = (clip_uid, ann_datum["annotation_uid"])
gt_dict[key] = ann_datum
num_gt_queries += len(ann_datum["language_queries"])
results = [[[] for _ in topK] for _ in thresholds]
average_IoU = []
num_instances = 0
for idx, pred_datum in enumerate(predictions):
if not isinstance(pred_datum, dict):
pred_datum = predictions[pred_datum]
key = (pred_datum["clip_uid"], pred_datum["annotation_uid"])
assert key in gt_dict, "Instance not present!"
query_id = pred_datum["query_idx"]
gt_datum = gt_dict[key]
gt_query_datum = gt_datum["language_queries"][query_id]
# Compute overlap and recalls.
overlap = compute_IoU(
pred_datum["predicted_times"],
[[gt_query_datum["clip_start_sec"], gt_query_datum["clip_end_sec"]]],
)
average_IoU.append(overlap[0])
for tt, threshold in enumerate(thresholds):
for rr, KK in enumerate(topK):
results[tt][rr].append((overlap > threshold)[:KK].any())
mean_results = np.array(results).mean(axis=-1)
mIoU = np.mean(average_IoU)
print(f"Evaluated: {num_instances} / {num_gt_queries} instances")
if per_instance:
per_instance_results = {
"overlap": overlap,
"average_IoU": average_IoU,
"results": results,
}
return mean_results, mIoU, per_instance_results
else:
return mean_results, mIoU
def main(args):
print(f"""Reading predictions: {args["model_prediction_json"]}""")
with open(args["model_prediction_json"], "r") as file_id:
predictions = json.load(file_id)
print(f"""Reading gt: {args["ground_truth_json"]}""")
with open(args["ground_truth_json"], "r") as file_id:
ground_truth = json.load(file_id)
assert predictions.get("version", None) == "1.0", "Ego4D version does not match!"
assert predictions.get("challenge", None) == "ego4d_nlq_challenge", (
"Ego4D challenge does not match!"
)
results, mIoU = evaluate_nlq_performance(
predictions["results"], ground_truth, args["thresholds"], args["topK"]
)
print(display_results(results, mIoU, args["thresholds"], args["topK"]))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--ground_truth_json",
required=True,
help="Ground truth temporal windows",
)
parser.add_argument(
"--model_prediction_json",
required=True,
help="Model predicted temporal windows",
)
parser.add_argument(
"--thresholds",
required=True,
nargs="+",
type=float,
help="Thresholds for IoU computation",
)
parser.add_argument(
"--topK",
required=True,
nargs="+",
type=int,
help="Top K for computing recall@k",
)
try:
parsed_args = vars(parser.parse_args())
except (IOError) as msg:
parser.error(str(msg))
main(parsed_args)