-
Notifications
You must be signed in to change notification settings - Fork 12
/
tadtr.py
519 lines (435 loc) · 22.2 KB
/
tadtr.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# ------------------------------------------------------------------------
# TadTR: End-to-end Temporal Action Detection with Transformer
# Copyright (c) 2021. Xiaolong Liu.
# ------------------------------------------------------------------------
# Modified from Deformable DETR (https://github.com/fundamentalvision/Deformable-DETR)
# Copyright (c) 2020 SenseTime. All Rights Reserved.
# Licensed under the Apache License, Version 2.0
# ------------------------------------------------------------------------
# and DETR (https://github.com/facebookresearch/detr)
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
# ------------------------------------------------------------------------
"""
TadTR model and criterion classes.
"""
import math
import copy
import torch
import torch.nn.functional as F
from torch import nn
from util import segment_ops
from util.misc import (NestedTensor, nested_tensor_from_tensor_list,
accuracy, get_world_size,
is_dist_avail_and_initialized, inverse_sigmoid)
from models.matcher import build_matcher
from models.position_encoding import build_position_encoding
from .custom_loss import sigmoid_focal_loss
from .transformer import build_deformable_transformer
from opts import cfg
if not cfg.disable_cuda:
from models.ops.roi_align import ROIAlign
def _get_clones(module, N):
return nn.ModuleList([copy.deepcopy(module) for i in range(N)])
def get_norm(norm_type, dim, num_groups=None):
if norm_type == 'gn':
assert num_groups is not None, 'num_groups must be specified'
return nn.GroupNorm(num_groups, dim)
elif norm_type == 'bn':
return nn.BatchNorm1d(dim)
else:
raise NotImplementedError
class TadTR(nn.Module):
""" This is the TadTR module that performs temporal action detection """
def __init__(self, position_embedding, transformer, num_classes, num_queries, aux_loss=True, with_segment_refine=True, with_act_reg=True):
""" Initializes the model.
Parameters:
backbone: torch module of the backbone to be used. See backbone.py
transformer: torch module of the transformer architecture. See deformable_transformer.py
num_classes: number of action classes
num_queries: number of action queries, ie detection slot. This is the maximal number of actions
TadTR can detect in a single video.
aux_loss: True if auxiliary decoding losses (loss at each decoder layer) are to be used.
with_segment_refine: iterative segment refinement
"""
super().__init__()
self.num_queries = num_queries
self.transformer = transformer
hidden_dim = transformer.d_model
self.class_embed = nn.Linear(hidden_dim, num_classes)
self.segment_embed = MLP(hidden_dim, hidden_dim, 2, 3)
self.query_embed = nn.Embedding(num_queries, hidden_dim*2)
self.input_proj = nn.ModuleList([
nn.Sequential(
nn.Conv1d(2048, hidden_dim, kernel_size=1),
nn.GroupNorm(32, hidden_dim),
)])
# self.backbone = backbone
self.position_embedding = position_embedding
self.aux_loss = aux_loss
self.with_segment_refine = with_segment_refine
self.with_act_reg = with_act_reg
prior_prob = 0.01
bias_value = -math.log((1 - prior_prob) / prior_prob)
self.class_embed.bias.data = torch.ones(num_classes) * bias_value
nn.init.constant_(self.segment_embed.layers[-1].weight.data, 0)
nn.init.constant_(self.segment_embed.layers[-1].bias.data, 0)
for proj in self.input_proj:
nn.init.xavier_uniform_(proj[0].weight, gain=1)
nn.init.constant_(proj[0].bias, 0)
num_pred = transformer.decoder.num_layers
if with_segment_refine:
self.class_embed = _get_clones(self.class_embed, num_pred)
self.segment_embed = _get_clones(self.segment_embed, num_pred)
nn.init.constant_(
self.segment_embed[0].layers[-1].bias.data[1:], -2.0)
# hack implementation for segment refinement
self.transformer.decoder.segment_embed = self.segment_embed
else:
nn.init.constant_(
self.segment_embed.layers[-1].bias.data[1:], -2.0)
self.class_embed = nn.ModuleList(
[self.class_embed for _ in range(num_pred)])
self.segment_embed = nn.ModuleList(
[self.segment_embed for _ in range(num_pred)])
self.transformer.decoder.segment_embed = None
if with_act_reg:
# RoIAlign params
self.roi_size = 16
self.roi_scale = 0
self.roi_extractor = ROIAlign(self.roi_size, self.roi_scale)
self.actionness_pred = nn.Sequential(
nn.Linear(self.roi_size * hidden_dim, hidden_dim),
nn.ReLU(inplace=True),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(inplace=True),
nn.Linear(hidden_dim, 1),
nn.Sigmoid()
)
def _to_roi_align_format(self, rois, T, scale_factor=1):
'''Convert RoIs to RoIAlign format.
Params:
RoIs: normalized segments coordinates, shape (batch_size, num_segments, 4)
T: length of the video feature sequence
'''
# transform to absolute axis
B, N = rois.shape[:2]
rois_center = rois[:, :, 0:1]
rois_size = rois[:, :, 1:2] * scale_factor
rois_abs = torch.cat(
(rois_center - rois_size/2, rois_center + rois_size/2), dim=2) * T
# expand the RoIs
rois_abs = torch.clamp(rois_abs, min=0, max=T) # (N, T, 2)
# add batch index
batch_ind = torch.arange(0, B).view((B, 1, 1)).to(rois_abs.device)
batch_ind = batch_ind.repeat(1, N, 1)
rois_abs = torch.cat((batch_ind, rois_abs), dim=2)
# NOTE: stop gradient here to stablize training
return rois_abs.view((B*N, 3)).detach()
def forward(self, samples):
""" The forward expects a NestedTensor, which consists of:
- samples.tensors: batched images, of shape [batch_size x 3 x H x W]
- samples.mask: a binary mask of shape [batch_size x H x W], containing 1 on padded pixels
or a tuple of tensors and mask
It returns a dict with the following elements:
- "pred_logits": the classification logits (including no-action) for all queries.
Shape= [batch_size x num_queries x (num_classes + 1)]
- "pred_segments": The normalized segments coordinates for all queries, represented as
(center, width). These values are normalized in [0, 1],
relative to the size of each individual image (disregarding possible padding).
See PostProcess for information on how to retrieve the unnormalized segment.
- "aux_outputs": Optional, only returned when auxilary losses are activated. It is a list of
dictionnaries containing the two above keys for each decoder layer.
"""
if not isinstance(samples, NestedTensor):
if isinstance(samples, (list, tuple)):
samples = NestedTensor(*samples)
else:
samples = nested_tensor_from_tensor_list(samples) # (n, c, t)
pos = [self.position_embedding(samples)]
src, mask = samples.tensors, samples.mask
srcs = [self.input_proj[0](src)]
masks = [mask]
query_embeds = self.query_embed.weight
hs, init_reference, inter_references, memory = self.transformer(
srcs, masks, pos, query_embeds)
outputs_classes = []
outputs_coords = []
# gather outputs from each decoder layer
for lvl in range(hs.shape[0]):
if lvl == 0:
reference = init_reference
else:
reference = inter_references[lvl - 1]
reference = inverse_sigmoid(reference)
outputs_class = self.class_embed[lvl](hs[lvl])
tmp = self.segment_embed[lvl](hs[lvl])
# the l-th layer (l >= 2)
if reference.shape[-1] == 2:
tmp += reference
# the first layer
else:
assert reference.shape[-1] == 1
tmp[..., 0] += reference[..., 0]
outputs_coord = tmp.sigmoid()
outputs_classes.append(outputs_class)
outputs_coords.append(outputs_coord)
outputs_class = torch.stack(outputs_classes)
outputs_coord = torch.stack(outputs_coords)
if not self.with_act_reg:
out = {'pred_logits': outputs_class[-1],
'pred_segments': outputs_coord[-1]}
else:
# perform RoIAlign
B, N = outputs_coord[-1].shape[:2]
origin_feat = memory
rois = self._to_roi_align_format(
outputs_coord[-1], origin_feat.shape[2], scale_factor=1.5)
roi_features = self.roi_extractor(origin_feat, rois)
roi_features = roi_features.view((B, N, -1))
pred_actionness = self.actionness_pred(roi_features)
last_layer_cls = outputs_class[-1]
last_layer_reg = outputs_coord[-1]
out = {'pred_logits': last_layer_cls,
'pred_segments': last_layer_reg, 'pred_actionness': pred_actionness}
if self.aux_loss:
out['aux_outputs'] = self._set_aux_loss(
outputs_class, outputs_coord)
return out
@torch.jit.unused
def _set_aux_loss(self, outputs_class, outputs_coord):
# this is a workaround to make torchscript happy, as torchscript
# doesn't support dictionary with non-homogeneous values, such
# as a dict having both a Tensor and a list.
return [{'pred_logits': a, 'pred_segments': b}
for a, b in zip(outputs_class[:-1], outputs_coord[:-1])]
class SetCriterion(nn.Module):
""" This class computes the loss for TadTR.
The process happens in two steps:
1) we compute hungarian assignment between ground truth segments and the outputs of the model
2) we supervise each pair of matched ground-truth / prediction (supervise class and segment)
"""
def __init__(self, num_classes, matcher, weight_dict, losses, focal_alpha=0.25):
""" Create the criterion.
Parameters:
num_classes: number of action categories, omitting the special no-action category
matcher: module able to compute a matching between targets and proposals
weight_dict: dict containing as key the names of the losses and as values their relative weight.
losses: list of all the losses to be applied. See get_loss for list of available losses.
focal_alpha: alpha in Focal Loss
"""
super().__init__()
self.num_classes = num_classes
self.matcher = matcher
self.weight_dict = weight_dict
self.losses = losses
self.focal_alpha = focal_alpha
def loss_labels(self, outputs, targets, indices, num_segments, log=True):
"""Classification loss (NLL)
targets dicts must contain the key "labels" containing a tensor of dim [nb_target_segments]
"""
assert 'pred_logits' in outputs
src_logits = outputs['pred_logits']
idx = self._get_src_permutation_idx(indices)
target_classes_o = torch.cat([t["labels"][J] for t, (_, J) in zip(targets, indices)])
target_classes = torch.full(src_logits.shape[:2], self.num_classes,
dtype=torch.int64, device=src_logits.device)
target_classes[idx] = target_classes_o
target_classes_onehot = torch.zeros([src_logits.shape[0], src_logits.shape[1], src_logits.shape[2] + 1],
dtype=src_logits.dtype, layout=src_logits.layout, device=src_logits.device)
target_classes_onehot.scatter_(2, target_classes.unsqueeze(-1), 1)
target_classes_onehot = target_classes_onehot[:,:,:-1]
loss_ce = sigmoid_focal_loss(src_logits, target_classes_onehot, num_segments, alpha=self.focal_alpha, gamma=2) * src_logits.shape[1] # nq
losses = {'loss_ce': loss_ce}
if log:
# TODO this should probably be a separate loss, not hacked in this one here
losses['class_error'] = 100 - accuracy(src_logits[idx], target_classes_o)[0]
return losses
def loss_segments(self, outputs, targets, indices, num_segments):
"""Compute the losses related to the segmentes, the L1 regression loss and the IoU loss
targets dicts must contain the key "segments" containing a tensor of dim [nb_target_segments, 2]
The target segments are expected in format (center, width), normalized by the video length.
"""
assert 'pred_segments' in outputs
idx = self._get_src_permutation_idx(indices)
src_segments = outputs['pred_segments'][idx]
target_segments = torch.cat([t['segments'][i] for t, (_, i) in zip(targets, indices)], dim=0)
loss_segment = F.l1_loss(src_segments, target_segments, reduction='none')
losses = {}
losses['loss_segments'] = loss_segment.sum() / num_segments
loss_iou = 1 - torch.diag(segment_ops.segment_iou(
segment_ops.segment_cw_to_t1t2(src_segments),
segment_ops.segment_cw_to_t1t2(target_segments)))
losses['loss_iou'] = loss_iou.sum() / num_segments
return losses
def loss_actionness(self, outputs, targets, indices, num_segments):
"""Compute the actionness regression loss
targets dicts must contain the key "segments" containing a tensor of dim [nb_target_segments, 2]
The target segments are expected in format (center, width), normalized by the video length.
"""
assert 'pred_segments' in outputs
assert 'pred_actionness' in outputs
src_segments = outputs['pred_segments'].view((-1, 2))
target_segments = torch.cat([t['segments'] for t in targets], dim=0)
losses = {}
iou_mat = segment_ops.segment_iou(
segment_ops.segment_cw_to_t1t2(src_segments),
segment_ops.segment_cw_to_t1t2(target_segments))
gt_iou = iou_mat.max(dim=1)[0]
pred_actionness = outputs['pred_actionness']
loss_actionness = F.l1_loss(pred_actionness.view(-1), gt_iou.view(-1).detach())
losses['loss_iou'] = loss_actionness
return losses
def _get_src_permutation_idx(self, indices):
# permute predictions following indices
batch_idx = torch.cat([torch.full_like(src, i) for i, (src, _) in enumerate(indices)])
src_idx = torch.cat([src for (src, _) in indices])
return batch_idx, src_idx
def _get_tgt_permutation_idx(self, indices):
# permute targets following indices
batch_idx = torch.cat([torch.full_like(tgt, i) for i, (_, tgt) in enumerate(indices)])
tgt_idx = torch.cat([tgt for (_, tgt) in indices])
return batch_idx, tgt_idx
def get_loss(self, loss, outputs, targets, indices, num_segments, **kwargs):
loss_map = {
'labels': self.loss_labels,
'segments': self.loss_segments,
'actionness': self.loss_actionness,
}
assert loss in loss_map, f'do you really want to compute {loss} loss?'
return loss_map[loss](outputs, targets, indices, num_segments, **kwargs)
def forward(self, outputs, targets):
""" This performs the loss computation.
Parameters:
outputs: dict of tensors, see the output specification of the model for the format
targets: list of dicts, such that len(targets) == batch_size.
The expected keys in each dict depends on the losses applied, see each loss' doc
"""
outputs_without_aux = {k: v for k, v in outputs.items() if k != 'aux_outputs'}
# Retrieve the matching between the outputs of the last layer and the targets
indices = self.matcher(outputs_without_aux, targets)
# Compute the average number of target segments accross all nodes, for normalization purposes
num_segments = sum(len(t["labels"]) for t in targets)
num_segments = torch.as_tensor([num_segments], dtype=torch.float, device=next(iter(outputs.values())).device)
if is_dist_avail_and_initialized():
torch.distributed.all_reduce(num_segments)
num_segments = torch.clamp(num_segments / get_world_size(), min=1).item()
# Compute all the requested losses
losses = {}
for loss in self.losses:
kwargs = {}
losses.update(self.get_loss(loss, outputs, targets, indices, num_segments, **kwargs))
# In case of auxiliary losses, we repeat this process with the output of each intermediate layer.
if 'aux_outputs' in outputs:
for i, aux_outputs in enumerate(outputs['aux_outputs']):
indices = self.matcher(aux_outputs, targets)
for loss in self.losses:
# we do not compute actionness loss for aux outputs
if 'actionness' in loss:
continue
kwargs = {}
if loss == 'labels':
# Logging is enabled only for the last layer
kwargs['log'] = False
l_dict = self.get_loss(loss, aux_outputs, targets, indices, num_segments, **kwargs)
l_dict = {k + f'_{i}': v for k, v in l_dict.items()}
losses.update(l_dict)
self.indices = indices
return losses
class PostProcess(nn.Module):
""" This module converts the model's output into the format expected by the TADEvaluator"""
@torch.no_grad()
def forward(self, outputs, target_sizes, fuse_score=True):
""" Perform the computation
Parameters:
outputs: raw outputs of the model
target_sizes: tensor of dimension [batch_size] containing the duration of each video of the batch
"""
out_logits, out_segments = outputs['pred_logits'], outputs['pred_segments']
assert len(out_logits) == len(target_sizes)
# assert target_sizes.shape[1] == 1
prob = out_logits.sigmoid() # [bs, nq, C]
if fuse_score:
prob *= outputs['pred_actionness']
segments = segment_ops.segment_cw_to_t1t2(out_segments) # bs, nq, 2
if cfg.postproc_rank == 1: # default
# sort across different instances, pick top 100 at most
topk_values, topk_indexes = torch.topk(prob.view(
out_logits.shape[0], -1), min(cfg.postproc_ins_topk, prob.shape[1]*prob.shape[2]), dim=1)
scores = topk_values
topk_segments = topk_indexes // out_logits.shape[2]
labels = topk_indexes % out_logits.shape[2]
# bs, nq, 2; bs, num, 2
segments = torch.gather(
segments, 1, topk_segments.unsqueeze(-1).repeat(1, 1, 2))
query_ids = topk_segments
else:
# pick topk classes for each query
# pdb.set_trace()
scores, labels = torch.topk(prob, cfg.postproc_cls_topk, dim=-1)
scores, labels = scores.flatten(1), labels.flatten(1)
# (bs, nq, 1, 2)
segments = segments[:, [
i//cfg.postproc_cls_topk for i in range(cfg.postproc_cls_topk*segments.shape[1])], :]
query_ids = (torch.arange(0, cfg.postproc_cls_topk*segments.shape[1], 1, dtype=labels.dtype,
device=labels.device) // cfg.postproc_cls_topk)[None, :].repeat(labels.shape[0], 1)
# from normalized [0, 1] to absolute [0, length] coordinates
vid_length = target_sizes
scale_fct = torch.stack([vid_length, vid_length], dim=1)
segments = segments * scale_fct[:, None, :]
results = [{'scores': s, 'labels': l, 'segments': b, 'query_ids': q}
for s, l, b, q in zip(scores, labels, segments, query_ids)]
return results
class MLP(nn.Module):
""" Very simple multi-layer perceptron (also called FFN)"""
def __init__(self, input_dim, hidden_dim, output_dim, num_layers):
super().__init__()
self.num_layers = num_layers
h = [hidden_dim] * (num_layers - 1)
self.layers = nn.ModuleList(nn.Linear(n, k)
for n, k in zip([input_dim] + h, h + [output_dim]))
def forward(self, x):
for i, layer in enumerate(self.layers):
x = F.relu(layer(x)) if i < self.num_layers - 1 else layer(x)
return x
def build(args):
if args.binary:
num_classes = 1
else:
if args.dataset_name == 'thumos14':
num_classes = 20
elif args.dataset_name == 'muses':
num_classes = 25
elif args.dataset_name in ['activitynet', 'hacs']:
num_classes = 200
else:
raise ValueError('unknown dataset {}'.format(args.dataset_name))
pos_embed = build_position_encoding(args)
transformer = build_deformable_transformer(args)
model = TadTR(
pos_embed,
transformer,
num_classes=num_classes,
num_queries=args.num_queries,
aux_loss=args.aux_loss,
with_segment_refine=args.seg_refine,
with_act_reg=args.act_reg
)
matcher = build_matcher(args)
losses = ['labels', 'segments']
weight_dict = {
'loss_ce': args.cls_loss_coef,
'loss_seg': args.seg_loss_coef,
'loss_iou': args.iou_loss_coef}
if args.act_reg:
weight_dict['loss_actionness'] = args.act_loss_coef
losses.append('actionness')
if args.aux_loss:
aux_weight_dict = {}
for i in range(args.dec_layers - 1):
aux_weight_dict.update({k + f'_{i}': v for k, v in weight_dict.items()})
aux_weight_dict.update({k + f'_enc': v for k, v in weight_dict.items()})
weight_dict.update(aux_weight_dict)
criterion = SetCriterion(num_classes, matcher,
weight_dict, losses, focal_alpha=args.focal_alpha)
postprocessor = PostProcess()
return model, criterion, postprocessor