-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvisualize_att_maps.py
348 lines (289 loc) · 15.7 KB
/
visualize_att_maps.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
"""
Training script of DeVIS
Modified from DETR (https://github.com/facebookresearch/detr)
"""
import argparse
import random
from contextlib import redirect_stdout
from pathlib import Path
import os
import tqdm
import numpy as np
import torch
from torch.utils.data import DataLoader, DistributedSampler
import torch.nn.functional as F
import src.util.misc as utils
from src.datasets import build_dataset
from src.models import build_model
from src.config import get_cfg_defaults
from main import sanity_check
from src.models.tracker import Tracker, Track, encode_mask
from src.util.att_maps_viz import visualize_clips_with_att_maps_per_reslvl, \
visualize_clips_with_att_maps_merged_res_v2, create_masks
def get_args_parser():
parser = argparse.ArgumentParser('DeVIS argument parser', add_help=False)
parser.add_argument('--merge-resolution',
help="Allows converting all sampling location from "
"each resolution level to the same one.",
choices=[0, 1, 2, 3],
type=int,
default=None)
parser.add_argument('--layer',
help="Allows selecting the layer in which visualizing the attention maps",
choices=[0, 1, 2, 3, 4, 5],
type=int,
default=5)
parser.add_argument('--used-resolution',
help="If merge_resolution=None, allows selecting the resolution to save attention maps",
choices=[0, 1, 2, 3], default=1)
parser.add_argument('--config-file', help="Run test only")
parser.add_argument('--eval-only', action='store_true', help="Run test only")
# distributed training parameters
parser.add_argument('--world-size', default=1, type=int, help='number of distributed processes')
parser.add_argument('--dist-url', default='env://',
help='url used to set up distributed training')
parser.add_argument('--device', default='cuda', help='device to use for training / testing')
parser.add_argument(
"opts",
help="""
Modify config options at the end of the command. For Yacs configs, use
space-separated "PATH.KEY VALUE" pairs".
""".strip(),
default=None,
nargs=argparse.REMAINDER,
)
return parser
# TODO: Implement Attention maps visualization when
# MODEL.DEVIS.DEFORMABLE_ATTENTION.ENC_CONNECT_ALL_FRAME = False
class TrackerAttMaps(Tracker):
def __init__(self, model: torch.nn.Module, hungarian_matcher, tracker_cfg: dict,
att_maps_cfg: dict, visualization_cfg: dict, focal_loss: bool, num_frames: int,
overlap_window: int,
use_top_k: bool, num_workers: int):
super().__init__(model, hungarian_matcher, tracker_cfg, visualization_cfg, focal_loss,
num_frames, overlap_window, use_top_k, num_workers)
self.att_maps_cfg = utils.nested_dict_to_namespace(att_maps_cfg)
def process_masks(self, start_idx, idx, tgt_size, masks):
processed_masks = []
num_masks = masks.shape[0]
for t in range(num_masks):
mask = masks[t]
mask = F.interpolate(mask[None, None], tgt_size, mode="bilinear",
align_corners=False).detach().sigmoid()[0, 0]
processed_masks.append(encode_mask(mask))
return processed_masks
def parse_att_maps_to_tracks(self, tracks, topk_idxs, merge_resolution, spatial_shapes,
init_ref_point, inter_ref_points, sampling_locations,
temporal_sampling_locations, attn_weights, temporal_attn_weights):
if isinstance(self.model, torch.nn.parallel.DistributedDataParallel):
model_without_ddp = self.model.module
else:
model_without_ddp = self.model
t_window = model_without_ddp.def_detr.transformer.decoder.layers[-1].cross_attn.t_window
n_levels = model_without_ddp.def_detr.transformer.decoder.layers[-1].cross_attn.n_levels
embd_per_frame = sampling_locations[0].shape[1]
sampling_offsets = torch.cat(sampling_locations, dim=0)
coordinates_lvl_res_factor = torch.flip(spatial_shapes, dims=(1,))
if merge_resolution is not None:
coordinates_lvl_res_factor = coordinates_lvl_res_factor[merge_resolution].repeat(
spatial_shapes.shape[0], 1)
sampling_locations = sampling_offsets[:, topk_idxs] * coordinates_lvl_res_factor[None, None,
None, :, None]
temporal_sampling_offsets = torch.cat(temporal_sampling_locations, dim=0).unflatten(3, [
t_window, n_levels])
temporal_sampling_locations = temporal_sampling_offsets[:,
topk_idxs] * coordinates_lvl_res_factor[None, None, None,
None, :, None]
attn_weights = attn_weights[:, topk_idxs]
temporal_attn_weights = temporal_attn_weights.unflatten(3, [t_window, n_levels])[:,
topk_idxs]
if self.att_maps_cfg.layer == 0:
ref_points = init_ref_point[0].sigmoid()
else:
ref_points = inter_ref_points[self.att_maps_cfg.layer - 1, 0]
ref_points = ref_points.reshape(
[model_without_ddp.num_frames, embd_per_frame, ref_points.shape[-1]])[:, topk_idxs]
for i, track in enumerate(tracks):
# TODO: This round should be replaced by proper interpolation of corresponding pixels
track.curr_position = torch.round(sampling_locations[:, i]).type(torch.long)
track.curr_att_weights = attn_weights[:, i]
track.temporal_positions = torch.round(temporal_sampling_locations[:, i]).type(
torch.long)
track.temporal_att_weights = temporal_attn_weights[:, i]
track.spatial_shapes = spatial_shapes
track.ref_point = ref_points[:, i]
def __call__(self, video, device, all_times):
sampler_val = torch.utils.data.SequentialSampler(video)
video_loader = DataLoader(video, 1, sampler=sampler_val, num_workers=self.num_workers)
real_video_length = video.real_video_length
clip_length = self.num_frames if real_video_length is None or real_video_length >= self.num_frames else real_video_length
cat_names = video.cat_names
video_info = {
"tgt_size": video.original_size,
"clip_length": clip_length
}
# use lists to store the outputs via up-values
init_ref_point, inter_ref_points, raw_deformable_attention = [], [], []
if isinstance(self.model, torch.nn.parallel.DistributedDataParallel):
model_without_ddp = self.model.module
else:
model_without_ddp = self.model
hooks = [
model_without_ddp.def_detr.transformer.reference_points.register_forward_hook(
lambda self, input, output: init_ref_point.append(output)
),
model_without_ddp.def_detr.transformer.decoder.layers[
self.att_maps_cfg.layer].cross_attn.register_forward_hook(
lambda self, input, output: raw_deformable_attention.append(output[1:])
),
model_without_ddp.def_detr.transformer.decoder.register_forward_hook(
lambda self, input, output: inter_ref_points.append(output[1])
),
]
for idx, video_clip in enumerate(video_loader):
init_ref_point, inter_ref_points, raw_deformable_attention = [], [], []
clip_tracks_category_dict = {}
video_clip = video_clip.to(device)
results = self.model(video_clip.squeeze(0), video_info)
init_ref_point = init_ref_point[0]
inter_ref_points = inter_ref_points[0]
sampling_offsets, temporal_sampling_offsets, attn_weights, temporal_attn_weights = \
raw_deformable_attention[0]
pred_scores, pred_classes, pred_boxes, pred_masks, pred_center_points = results["scores"], \
results["labels"], results["boxes"], \
results["masks"], results["center_points"]
detected_instances = pred_scores.shape[1]
start_idx = 0 if idx != len(video_loader) - 1 else video.last_real_idx
clip_tracks = [Track(track_id, clip_length, start_idx) for track_id in
range(detected_instances)]
processed_masks_dict = {}
for i, track in enumerate(clip_tracks):
mask_id = results['inverse_idxs'][i].item()
if mask_id not in processed_masks_dict.keys():
processed_masks_dict[mask_id] = self.process_masks(start_idx, idx,
video.original_size,
pred_masks[:, mask_id])
cat_track = pred_classes[0, i].item()
if cat_track not in clip_tracks_category_dict:
clip_tracks_category_dict[cat_track] = []
clip_tracks_category_dict[cat_track].append(i)
track.update(pred_scores[:, i], pred_classes[:, i], pred_boxes[:, i],
processed_masks_dict[mask_id], pred_center_points[:, i], mask_id)
self.parse_att_maps_to_tracks(clip_tracks, results["top_k_idxs"],
self.att_maps_cfg.merge_resolution,
results['spatial_shapes'], init_ref_point,
inter_ref_points, sampling_offsets,
temporal_sampling_offsets, attn_weights,
temporal_attn_weights)
if self.tracker_cfg.track_min_detection_score != 0:
for track in clip_tracks:
track.filter_frame_detections(self.tracker_cfg.track_min_detection_score)
keep = np.array([track.valid(min_detections=1) for track in clip_tracks])
clips_to_show = [track for i, track in enumerate(clip_tracks) if keep[i]]
if self.tracker_cfg.track_min_score != 0:
keep = [track.mean_score() > self.tracker_cfg.track_min_score for track in
clips_to_show]
clips_to_show = [track for i, track in enumerate(clips_to_show) if keep[i]]
for track in clips_to_show:
track.encode_all_masks()
track.process_centroid(video.original_size)
if self.att_maps_cfg.merge_resolution is None:
visualize_clips_with_att_maps_per_reslvl(idx, video.images_folder,
video.video_clips[idx][:clip_length],
clips_to_show,
self.att_maps_cfg.layer,
self.att_maps_cfg.used_resolution,
out_path=self.visualization_cfg.out_viz_path,
class_name=cat_names)
else:
visualize_clips_with_att_maps_merged_res_v2(idx, video.images_folder,
video.video_clips[idx][:clip_length],
clips_to_show,
self.att_maps_cfg.layer,
self.att_maps_cfg.merge_resolution,
out_path=self.visualization_cfg.out_viz_path,
class_name=cat_names)
for hook in hooks:
hook.remove()
@torch.no_grad()
def run_demo(args, cfg):
sanity_check(cfg)
utils.init_distributed_mode(args)
# print("git:\n {}\n".format(utils.get_sha()))
device = torch.device(args.device)
# fix the seed for reproducibility
seed = cfg.SEED + utils.get_rank()
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.cuda.manual_seed(seed)
dataset_val, num_classes = build_dataset(image_set="VAL", cfg=cfg)
model, criterion, postprocessors = build_model(num_classes, device, cfg)
model.to(device)
model.eval()
model_without_ddp = model
if args.distributed:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
model_without_ddp = model.module
tracker = TrackerAttMaps(
model=model,
hungarian_matcher=None,
tracker_cfg={
"per_class_matching": cfg.TEST.CLIP_TRACKING.PER_CLASS_MATCHING,
"track_min_detection_score": cfg.TEST.CLIP_TRACKING.MIN_FRAME_SCORE,
"track_min_score": cfg.TEST.CLIP_TRACKING.MIN_TRACK_SCORE,
"track_min_detections": cfg.TEST.CLIP_TRACKING.MIN_DETECTIONS,
"final_class_policy": cfg.TEST.CLIP_TRACKING.FINAL_CLASS_POLICY,
"final_score_policy": cfg.TEST.CLIP_TRACKING.FINAL_SCORE_POLICY,
},
num_workers=cfg.NUM_WORKERS,
use_top_k=cfg.TEST.USE_TOP_K,
overlap_window=cfg.MODEL.DEVIS.NUM_FRAMES - cfg.TEST.CLIP_TRACKING.STRIDE,
num_frames=cfg.MODEL.DEVIS.NUM_FRAMES,
focal_loss=cfg.MODEL.LOSS.FOCAL_LOSS,
visualization_cfg={
"out_viz_path": cfg.TEST.VIZ.OUT_VIZ_PATH,
"save_clip_viz": cfg.TEST.VIZ.SAVE_CLIP_VIZ,
"merge_tracks": cfg.TEST.VIZ.SAVE_MERGED_TRACKS,
},
att_maps_cfg={
"merge_resolution": args.merge_resolution,
"layer": args.layer,
"used_resolution": args.used_resolution,
}
)
n_total_params = sum(p.numel() for p in model.parameters())
print(f'Total num params: {n_total_params}')
if args.distributed:
sampler_val = DistributedSampler(dataset_val, shuffle=False)
else:
sampler_val = torch.utils.data.SequentialSampler(dataset_val)
data_loader_val = DataLoader(dataset_val, cfg.SOLVER.BATCH_SIZE, sampler=sampler_val,
collate_fn=utils.val_collate if cfg.DATASETS.TYPE == 'vis' else utils.collate_fn,
num_workers=cfg.NUM_WORKERS)
resume_state_dict = torch.load(cfg.MODEL.WEIGHTS, map_location=device)['model']
model_without_ddp.load_state_dict(resume_state_dict, strict=True)
selected_videos = False
if cfg.TEST.VIZ.VIDEO_NAMES:
selected_videos = cfg.TEST.VIZ.VIDEO_NAMES.split(",")
for idx, video in tqdm.tqdm(enumerate(data_loader_val)):
if selected_videos and video.video_clips[0][0].split("/")[0] not in selected_videos:
continue
tracker(video, device, [])
return
if __name__ == "__main__":
parser = argparse.ArgumentParser('DeVIS attention maps demo script',
parents=[get_args_parser()])
args_ = parser.parse_args()
cfg_ = get_cfg_defaults()
cfg_.merge_from_file(args_.config_file)
cfg_.merge_from_list(args_.opts)
cfg_.freeze()
if cfg_.OUTPUT_DIR:
Path(cfg_.OUTPUT_DIR).mkdir(parents=True, exist_ok=True)
with open(os.path.join(cfg_.OUTPUT_DIR, 'config.yaml'), 'w') as yaml_file:
with redirect_stdout(yaml_file):
print(cfg_.dump())
run_demo(args_, cfg_)