-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinfer_epic_kitchens.py
180 lines (159 loc) · 5.62 KB
/
infer_epic_kitchens.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
"""
Perform inference on object frames of the Epic Kitchens Dataset
"""
import logging
import os
import torch
import json
from tqdm import tqdm
from collections import OrderedDict
from torch.nn.parallel import DistributedDataParallel
import detectron2.utils.comm as comm
from detectron2.config import get_cfg
from detectron2.engine import (
default_argument_parser,
default_setup,
launch,
DefaultPredictor,
)
from detectron2.checkpoint import DetectionCheckpointer
from dataset.dataloader import build_detection_test_loader
from detectron2.modeling import build_model
from utils.register import register_dataset
logger = logging.getLogger("detectron2")
def setup(args):
"""
Create configs and perform basic setups.
"""
cfg = get_cfg()
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
default_setup(cfg, args)
return cfg
def format_detections(outputs, inputs):
detections = []
for output, input in zip(outputs, inputs):
vid_id = input["file_name"].split("/")[-2]
frame = int(input["file_name"].split("/")[-1].split(".")[0])
height, width = input["height"], input["width"]
shift_y = round(1.0 / height, 5)
shift_x = round(1.0 / width, 5)
bboxes = output["instances"].pred_boxes
bboxes.scale(1 / width, 1 / height)
scores = output["instances"].scores
pred_classes = output["instances"].pred_classes
assert (
len(bboxes) == len(scores) == len(pred_classes)
), f"Number of detected instances do not match"
for idx in range(len(bboxes)):
# if scores[idx] < 1e-4:
# continue
det_dict = OrderedDict()
det_dict["video_id"] = vid_id
det_dict["frame"] = frame
det_dict["category_id"] = int(pred_classes[idx])
bbox = bboxes[idx].tensor[0].tolist()
bbox = [
round(bbox[1], 5), # ymin
round(bbox[0], 5), # xmin
round(bbox[3], 5), # ymax
round(bbox[2], 5), # xmax
]
if bbox[0] == bbox[2] or bbox[1] == bbox[3]:
print(f"Correcting bbox data for video {vid_id} and frame {frame}")
if bbox[0] == bbox[2]:
if bbox[0] - shift_y >= 0:
bbox[0] -= shift_y
else:
bbox[2] += shift_y
if bbox[1] == bbox[3]:
if bbox[1] - shift_x >= 0:
bbox[1] -= shift_x
else:
bbox[3] += shift_x
assert (
bbox[0] < bbox[2] and bbox[1] < bbox[3]
), f"Box data {bbox} for video {vid_id} and frame {frame} is invalid"
det_dict["bbox"] = bbox
det_dict["score"] = float(scores[idx])
detections.append(det_dict)
return detections
def do_infer(cfg, args, model):
if args.read_meta_cache:
read_cache = True
else:
read_cache = False
model.eval()
for dataset_name in cfg.DATASETS.TEST:
register_dataset(
args.root_dir, args.ann_dir, dataset_name, read_cache=read_cache
)
results = OrderedDict()
results["version"] = "0.1"
results["challenge"] = "object_detection"
data_loader = build_detection_test_loader(cfg, dataset_name)
logger.info("Number of frames: {}".format(len(data_loader.dataset)))
detections = []
with torch.no_grad():
for inputs in tqdm(data_loader):
outputs = model(inputs)
detections.extend(format_detections(outputs, inputs))
results["results"] = detections
if dataset_name == "epic_kitchens_test_s1":
json_file = os.path.join(cfg.OUTPUT_DIR, "seen.json")
with open(json_file, "w") as f:
json.dump(results, f)
logger.info(f"Results for {dataset_name} saved to {json_file}")
elif dataset_name == "epic_kitchens_test_s2":
json_file = os.path.join(cfg.OUTPUT_DIR, "unseen.json")
with open(json_file, "w") as f:
json.dump(results, f)
logger.info(f"Results for {dataset_name} saved to {json_file}")
def main(args):
cfg = setup(args)
model = build_model(cfg)
logger.info("Model:\n{}".format(model))
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=args.resume
)
distributed = comm.get_world_size() > 1
if distributed:
model = DistributedDataParallel(
model, device_ids=[comm.get_local_rank()], broadcast_buffers=False
)
do_infer(cfg, args, model)
if __name__ == "__main__":
parser = default_argument_parser()
parser.add_argument(
"--root-dir",
dest="root_dir",
required=True,
default="",
help="path to image files",
)
parser.add_argument(
"--ann-dir",
dest="ann_dir",
required=True,
default="",
help="path to image files",
)
parser.add_argument(
"--read-meta-cache", action="store_true", help="Read metadata from cache file"
)
args = parser.parse_args()
num_gpus = torch.cuda.device_count()
if num_gpus == 0:
raise Exception(
"No GPU found. The model is not implemented without GPU support."
)
print("Command Line Args:", args)
launch(
main,
num_gpus,
num_machines=args.num_machines,
machine_rank=args.machine_rank,
dist_url=args.dist_url,
args=(args,),
)