-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevaluate_egtr.py
268 lines (236 loc) · 9.01 KB
/
evaluate_egtr.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
# EGTR
# Copyright (c) 2024-present NAVER Cloud Corp.
# Apache-2.0
import argparse
import json
from glob import glob
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
from data.open_image import OIDataset
from data.visual_genome import VGDataset
from lib.evaluation.coco_eval import CocoEvaluator
from lib.evaluation.oi_eval import OIEvaluator
from lib.evaluation.sg_eval import (
BasicSceneGraphEvaluator,
calculate_mR_from_evaluator_list,
)
from model.deformable_detr import DeformableDetrConfig, DeformableDetrFeatureExtractor
from model.egtr import DetrForSceneGraphGeneration
from train_egtr import collate_fn, evaluate_batch
@torch.no_grad()
def calculate_fps(model, dataloader):
model.eval()
for batch in tqdm(dataloader):
outputs = model(
pixel_values=batch["pixel_values"].cuda(),
pixel_mask=batch["pixel_mask"].cuda(),
output_attentions=False,
output_attention_states=True,
output_hidden_states=True,
)
# Reference: https://github.com/facebookresearch/detr/blob/main/engine.py
@torch.no_grad()
def evaluate(
model,
dataloader,
num_labels,
multiple_sgg_evaluator=None,
single_sgg_evaluator=None,
oi_evaluator=None,
coco_evaluator=None,
feature_extractor=None,
):
metric_dict = {}
model.eval()
multiple_sgg_evaluator_list = [] # mR@k (for each rel category)
single_sgg_evaluator_list = []
if multiple_sgg_evaluator is not None:
for index, name in enumerate(dataloader.dataset.rel_categories):
multiple_sgg_evaluator_list.append(
(index, name, BasicSceneGraphEvaluator.all_modes(multiple_preds=True))
)
if single_sgg_evaluator is not None:
for index, name in enumerate(dataloader.dataset.rel_categories):
single_sgg_evaluator_list.append(
(index, name, BasicSceneGraphEvaluator.all_modes(multiple_preds=False))
)
for batch in tqdm(dataloader):
outputs = model(
pixel_values=batch["pixel_values"].cuda(),
pixel_mask=batch["pixel_mask"].cuda(),
output_attentions=False,
output_attention_states=True,
output_hidden_states=True,
)
targets = batch["labels"]
evaluate_batch(
outputs,
targets,
multiple_sgg_evaluator,
multiple_sgg_evaluator_list,
single_sgg_evaluator,
single_sgg_evaluator_list,
oi_evaluator,
num_labels,
)
if coco_evaluator is not None:
orig_target_sizes = torch.stack(
[target["orig_size"] for target in targets], dim=0
)
results = feature_extractor.post_process(
outputs, orig_target_sizes.to(model.device)
) # convert outputs of model to COCO api
res = {
target["image_id"].item(): output
for target, output in zip(targets, results)
}
coco_evaluator.update(res)
if coco_evaluator is not None:
coco_evaluator.synchronize_between_processes()
coco_evaluator.accumulate()
coco_evaluator.summarize()
metric_dict.update({"AP50": coco_evaluator.coco_eval["bbox"].stats[1]})
if multiple_sgg_evaluator is not None:
recall = multiple_sgg_evaluator["sgdet"].print_stats()
mean_recall = calculate_mR_from_evaluator_list(
multiple_sgg_evaluator_list, "sgdet", multiple_preds=True
)
metric_dict.update(recall)
metric_dict.update(mean_recall)
if single_sgg_evaluator is not None:
recall = single_sgg_evaluator["sgdet"].print_stats()
mean_recall = calculate_mR_from_evaluator_list(
single_sgg_evaluator_list, "sgdet", multiple_preds=False
)
recall = {f"(single){key}": value for key, value in recall.items()}
mean_recall = {f"(single){key}": value for key, value in mean_recall.items()}
metric_dict.update(recall)
metric_dict.update(mean_recall)
if oi_evaluator is not None:
metrics = oi_evaluator.aggregate_metrics()
metric_dict.update(metrics)
return metric_dict
if __name__ == "__main__":
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")
parser = argparse.ArgumentParser()
# Path
parser.add_argument("--data_path", type=str, default="dataset/visual_genome")
parser.add_argument(
"--artifact_path",
type=str,
required=True,
)
# Architecture
parser.add_argument("--architecture", type=str, default="SenseTime/deformable-detr")
parser.add_argument("--num_queries", type=int, default=200)
# Evaluation
parser.add_argument("--split", type=str, default="test", choices=["val", "test"])
parser.add_argument("--eval_batch_size", type=int, default=1)
parser.add_argument("--eval_single_preds", type=str2bool, default=True)
parser.add_argument("--eval_multiple_preds", type=str2bool, default=False)
parser.add_argument("--logit_adjustment", type=str2bool, default=False)
parser.add_argument("--logit_adj_tau", type=float, default=0.3)
# FPS
parser.add_argument("--min_size", type=int, default=800)
parser.add_argument("--max_size", type=int, default=1333)
parser.add_argument("--infer_only", type=str2bool, default=False)
# Speed up
parser.add_argument("--num_workers", type=int, default=4)
args, unknown = parser.parse_known_args() # to ignore args when training
# Feature extractor
feature_extractor = DeformableDetrFeatureExtractor.from_pretrained(
args.architecture, size=args.min_size, max_size=args.max_size
)
# Dataset
if "visual_genome" in args.data_path:
test_dataset = VGDataset(
data_folder=args.data_path,
feature_extractor=feature_extractor,
split=args.split,
num_object_queries=args.num_queries,
)
id2label = {
k - 1: v["name"] for k, v in test_dataset.coco.cats.items()
} # 0 ~ 149
coco_evaluator = CocoEvaluator(
test_dataset.coco, ["bbox"]
) # initialize evaluator with ground truths
oi_evaluator = None
elif "open-image" in args.data_path:
test_dataset = OIDataset(
data_folder=args.data_path,
feature_extractor=feature_extractor,
split=args.split,
num_object_queries=args.num_queries,
)
id2label = test_dataset.classes_to_ind # 0 ~ 600
oi_evaluator = OIEvaluator(
test_dataset.rel_categories, test_dataset.ind_to_classes
)
coco_evaluator = None
# Dataloader
test_dataloader = DataLoader(
test_dataset,
collate_fn=lambda x: collate_fn(x, feature_extractor),
batch_size=args.eval_batch_size,
pin_memory=True,
num_workers=args.num_workers,
persistent_workers=True,
)
# Evaluator
multiple_sgg_evaluator = None
single_sgg_evaluator = None
if args.eval_multiple_preds:
multiple_sgg_evaluator = BasicSceneGraphEvaluator.all_modes(multiple_preds=True)
if args.eval_single_preds:
single_sgg_evaluator = BasicSceneGraphEvaluator.all_modes(multiple_preds=False)
# Model
config = DeformableDetrConfig.from_pretrained(args.artifact_path)
config.logit_adjustment = args.logit_adjustment
config.logit_adj_tau = args.logit_adj_tau
model = DetrForSceneGraphGeneration.from_pretrained(
args.architecture, config=config, ignore_mismatched_sizes=True
)
ckpt_path = sorted(
glob(f"{args.artifact_path}/checkpoints/epoch=*.ckpt"),
key=lambda x: int(x.split("epoch=")[1].split("-")[0]),
)[-1]
state_dict = torch.load(ckpt_path, map_location="cpu")["state_dict"]
for k in list(state_dict.keys()):
state_dict[k[6:]] = state_dict.pop(k) # "model."
model.load_state_dict(state_dict)
model.cuda()
model.eval()
# FPS
if args.infer_only:
calculate_fps(model, test_dataloader)
# Eval
else:
metric = evaluate(
model,
test_dataloader,
max(id2label.keys()) + 1,
multiple_sgg_evaluator,
single_sgg_evaluator,
oi_evaluator,
coco_evaluator,
feature_extractor,
)
# Save eval metric
device = "".join(torch.cuda.get_device_name(0).split()[1:2])
filename = f'{ckpt_path.replace(".ckpt", "")}__{args.split}__{len(test_dataloader)}__{device}'
if args.logit_adjustment:
filename += f"__la_{args.logit_adj_tau}"
metric["eval_arg"] = args.__dict__
with open(f"{filename}.json", "w") as f:
json.dump(metric, f)
print("metric is saved in", f"{filename}.json")