-
Notifications
You must be signed in to change notification settings - Fork 2
/
inference.py
278 lines (236 loc) · 11 KB
/
inference.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
# inference
# just obtain the results, no evaluation
# import yaml
import json
import os
import argparse
from tqdm import tqdm
import numpy as np
import torch
import logging
from util.config_utils import get_configs
from util.transforms import get_transform
from util.box_utils import BBoxReScaler
from util.monitor import TrainingMonitor
from torch.utils.data import DataLoader
from arkit_dataset import AppleDataHandler
from arkit_dataset import SimpleDataset, simple_collate_fn
from mapper import TopoMapperHandler as TopoMapper
# from mapper import TopoMapperv2 as TopoMapper
# from evaluator import Evaluator
from models.msg import MSGer
from util.checkpointing import load_checkpoint
import cv2
import supervision as sv
from typing import List
def annotate(image_source: np.ndarray, boxes: torch.Tensor, logits: torch.Tensor, class_ids: List[int], uniqs: List[str]) -> np.ndarray:
padding = 20
# Create a new image with padding
annotated_frame = cv2.copyMakeBorder(image_source, padding, padding, padding, padding, cv2.BORDER_CONSTANT, value=[1, 1, 1])
xyxy = boxes.numpy()
# padding the bounding box as well
xyxy += padding
class_ids = np.array(class_ids)
cat_labels = []
instance_ids = []
pred_uids = []
for uniq in uniqs:
cat, uid, pred_uid = uniq.strip().split(":")
instance_ids.append(int(uid))
cat_labels.append(cat)
pred_uids.append(pred_uid)
instance_ids = np.array(instance_ids)
cat_labels = np.array(cat_labels)
detections = sv.Detections(xyxy=xyxy, class_id=class_ids, tracker_id=instance_ids, confidence=logits.numpy())
labels = [
"-".join(uniq.split(":")[::-1])
for uniq in uniqs
]
box_annotator = sv.BoxAnnotator()
annotated_frame = box_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
return annotated_frame
def visualize_det(out_path, det_type, pred, video_id, video_data_dir):
frame_dir = os.path.join(video_data_dir, video_id+'_frames', 'lowres_wide')
output_dir = os.path.join(out_path, "detection_frames_"+det_type)
os.makedirs(output_dir, exist_ok=True)
for frame_id, frame_dict in pred['detections'].items():
image_path = os.path.join(frame_dir, f'{video_id}_{frame_id}.png')
image_source = cv2.imread(image_path)
# restore the classical detection data format
bboxes = []
labels = []
logits = []
uniqs = []
for obj_id in frame_dict:
bboxes.append(torch.tensor(frame_dict[obj_id]['bbox']))
label = int(frame_dict[obj_id]['label'])
# labels.append(self.inv_class_map[label] if label in self.inv_class_map else label)
labels.append(label)
logits.append(frame_dict[obj_id]['score'])
uniqs.append(frame_dict[obj_id]['uniq']+f":p{obj_id}")
if len(bboxes) == 0:
cv2.imwrite(os.path.join(output_dir, f'detected_{frame_id}.jpg'), image_source)
else:
bboxes = torch.stack(bboxes)
logits = torch.tensor(logits)
# mark with the unique id to show the instance results
annotated_frame = annotate(image_source=image_source, boxes=bboxes, logits=logits, class_ids=labels, uniqs=uniqs)
cv2.imwrite(os.path.join(output_dir, f'detected_{frame_id}.jpg'), annotated_frame)
def set_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
# torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def create_logger(output_dir, output_file):
"""
Create logger for traning records
"""
logfile = output_file.split('.')[0]+"_inference.log"
logpath = os.path.join(output_dir, logfile)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
logging.basicConfig(
level = logging.INFO,
format = '%(asctime)s - %(levelname)s - %(message)s',
datefmt = '%Y-%m-%d %H:%M:%S',
handlers=[logging.StreamHandler(), logging.FileHandler(logpath, mode='w')]
)
logger = logging.getLogger(__name__)
return logger
# read frames sequentially, pass to the model, get the embeddings, and do the mapping
def inference_per_video(dataset, dataloader, config, mapper, model, device, backproc, logger):
"""
Handles inference for each video
Parameters:
- dataset: the video dataset per video
- dataloader: the torch dataloader
- config: the config file
- mapper: the mapping handler
- model: the model used for evalaution, can be loaded from checkpoint or initialized from scratch
- device: cuda:x or cpu
- backproc: post processing handle for detection bounding boxes, used to back project them to the original image size.
"""
model.eval()
local_monitor = TrainingMonitor()
local_monitor.add('running_loss_total')
local_monitor.add('steps')
with torch.no_grad():
for batch in tqdm(dataloader):
images = batch['image'].to(device)
additional_info = {
# 'gt_bbox': batch['bbox'].type(torch.FloatTensor).to(device),
# 'obj_label': batch['obj_label'].to(device),
# 'obj_idx': batch['obj_idx'].to(device),
# 'mask': batch['mask'].to(device),
# # 'image_idx': batch['image_idx']
# # 'place_label': batch['place_label'].to(device),
}
if 'pred_bbox' in batch:
additional_info['pred_bbox'] = batch['pred_bbox'].to(device)
additional_info['pred_bbox_mask'] = batch['pred_bbox_mask'].to(device)
additional_info['pred_label'] = batch['pred_label'].to(device)
results = model(images, additional_info)
# move the results to cpu
results['place_embeddings'] = results['place_embeddings'].detach().cpu()
# padded
results['embeddings'] = results['embeddings'].detach().cpu()
# rescale predicted bounding box to the original image size
results['detections'] = backproc.post_rescale_bbox(results['detections'])
# pass the results to the mapper
mapper.map_update(batch, results)
# save the results
# print(map_results)
output_path = os.path.join(config['eval_output_dir'], config['eval_split'], dataset.video_id, "results.json")
# obtain results
# map_results = mapper.output_mapping()
# # if save embedding, use topomapperv2 instead of topomapperhandler at line 19
# map_results = mapper.output_mapping(save_emb_dir = os.path.dirname(output_path))
# if save place-place similarity
map_results = mapper.output_mapping(save_pp_sim=True)
detector_type = config['detector']['model']
if config['vis_det']:
visualize_det(
det_type=detector_type,
pred=map_results,
out_path = os.path.dirname(output_path),
video_id = dataset.video_id,
video_data_dir=dataset.video_data_dir,
)
# save the results
if config["save_every"]:
# check directory, make directory
os.makedirs(os.path.dirname(output_path), exist_ok=True)
with open(output_path, 'w') as f:
json.dump(map_results, f)
return
def inference_map(config, logger):
arkit_data = AppleDataHandler(config['dataset_path'], split=config['eval_split'])
# print("Number of videos in the validation set: {}".format(len(arkit_data)))
logger.info(f"Number of videos in the set: {len(arkit_data)}")
# build or load model
backproc = BBoxReScaler(orig_size=config['image_size'], new_size=config['model_image_size'], device='cpu')
# get model
device_no = config['device']
device = torch.device("cuda:{}".format(device_no) if torch.cuda.is_available() else "cpu")
model = MSGer(config, device)
# if load from checkpoint
if config["eval_chkpt"] is not None:
chkpt_path = os.path.join(config["eval_output_dir"], "checkpoints", config["eval_chkpt"])
load_checkpoint(
model = model,
checkpoint_path=chkpt_path,
logger = logger,
)
logger.info(f"Loading model from checkpoint: {chkpt_path}")
else:
assert config['associator']['model'] == 'SepMSG-direct', "No specified checkpoints for evaluation, so you can only do direct eval!"
model = model.to(device)
for i, next_video_id in enumerate(arkit_data.videos):
# print("Processing video {}, progress {}/{}".format(next_video_id, i, len(arkit_data)))
logger.info(f"Processing video {next_video_id}, progress {i+1}/{len(arkit_data)}")
next_video_path = os.path.join(arkit_data.data_split_dir, next_video_id)
dataset = SimpleDataset(arkit_data.data_split_dir, next_video_id, config, get_transform(config['model_image_size']), split=config['eval_split'])
mapper = TopoMapper(config, next_video_path, next_video_id)
dataloader = DataLoader(dataset, batch_size=config["eval_bs"], shuffle=False, num_workers=config["num_workers"], collate_fn=simple_collate_fn)
inference_per_video(
dataset,
dataloader,
config,
mapper,
model,
device,
backproc,
logger,
)
return
if __name__ == '__main__':
# get the config
parser = argparse.ArgumentParser(description="Experiment configurations")
parser.add_argument("--dataset_path", type=str, help="Path to the dataset")
parser.add_argument('--experiment', type=str, help='Name of the experiment config to use')
parser.add_argument('--output_dir', type=str, help='Output directory to save the results')
parser.add_argument('--output_file', type=str, help='Output file name')
parser.add_argument('--device', type=int, help="specify device")
parser.add_argument('--eval_split', type=str, help= "specify evaluation split")
parser.add_argument('--eval_output_dir', type=str, help="specify the eval model directory")
parser.add_argument('--eval_chkpt', type=str, help="specify the checkpoint file")
parser.add_argument('--vis_det', type=bool, help="if output frame visualization results")
parser.add_argument('--object_threshold', type=float, help="specify object threshold if want")
parser.add_argument('--pp_threshold', type=float, help="specify place threshold if want")
args = parser.parse_args()
base_config_dir = './configs/defaults'
config = get_configs(base_config_dir, args, creat_subdir=False)
eval_file = config["eval_split"]
if config["eval_chkpt"] is not None:
eval_file = config["eval_split"] + config["eval_chkpt"].split(".")[0]
if config["detector"]["model"] == "grounding-dino":
eval_file += "-gdino"
logger = create_logger(config["eval_output_dir"], eval_file)
# print(config)
logger.info("Inference config: %s\n", json.dumps(config, indent=4))
set_seed(config['seed'])
# evaluate
inference_map(config, logger)
logger.info(f"Inference done!")