-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmodel.py
1237 lines (1075 loc) · 65.1 KB
/
model.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""
LLM EPET model and criterion classes.
"""
import torch
import torch.nn.functional as F
from torch import nn
import sys
cpath = "D:\\fletcher\\LLMEPET"
sys.path.append(cpath)
from llm_epet.span_utils import generalized_temporal_iou, generalized_temporal_iou_, span_cxw_to_xx
from llm_epet.matcher import build_matcher, build_event_matcher
from llm_epet.transformer import build_transformer, TransformerEncoderLayer, TransformerEncoder
from llm_epet.position_encoding import build_position_encoding
from llm_epet.misc import accuracy
import numpy as np
import copy
def inverse_sigmoid(x, eps=1e-3):
x = x.clamp(min=0, max=1)
x1 = x.clamp(min=eps)
x2 = (1 - x).clamp(min=eps)
return torch.log(x1 / x2)
def init_weights(module):
if isinstance(module, (nn.Linear, nn.Embedding)):
module.weight.data.normal_(mean=0.0, std=0.02)
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
def find_nth(vid, underline, n):
max_len = len(vid)
start = vid.find(underline)
while start >= 0 and n > 1:
start = vid.find(underline, start + len(underline))
n -= 1
if start == -1:
start = max_len
return start
def element_wise_list_equal(listA, listB):
res = []
for a, b in zip(listA, listB):
if a == b:
res.append(True)
else:
res.append(False)
return res
class LLM_EPET(nn.Module):
""" LLM EPET. """
def __init__(self, transformer, position_embed, txt_position_embed, txt_dim, vid_dim,
num_queries, input_dropout, aux_loss=False,
contrastive_align_loss=False, contrastive_hdim=64,
max_v_l=75, span_loss_type="l1", use_txt_pos=False, n_input_proj=2, aud_dim=0, args=None):
super().__init__()
self.args = args
self.num_queries = num_queries
self.transformer = transformer
self.position_embed = position_embed
self.txt_position_embed = txt_position_embed
hidden_dim = transformer.d_model
self.span_loss_type = span_loss_type
self.max_v_l = max_v_l
span_pred_dim = 2 if span_loss_type == "l1" else max_v_l * 2
self.span_embed = MLP(hidden_dim, hidden_dim, span_pred_dim, 3)
self.event_span_embed = MLP(hidden_dim, hidden_dim, span_pred_dim, 3)
nn.init.constant_(self.event_span_embed.layers[-1].weight.data, 0)
nn.init.constant_(self.event_span_embed.layers[-1].bias.data, 0)
self.class_embed = nn.Linear(hidden_dim, 2) # 0: background, 1: foreground
self.token_type_embeddings = nn.Embedding(2, hidden_dim)
self.token_type_embeddings.apply(init_weights)
self.use_txt_pos = use_txt_pos
self.n_input_proj = n_input_proj
self.query_embed = nn.Embedding(num_queries, 2)
relu_args = [True] * 3
relu_args[n_input_proj - 1] = False
self.input_txt_proj = nn.Sequential(*[
LinearLayer(txt_dim, hidden_dim, layer_norm=True,
dropout=input_dropout, relu=relu_args[0]),
LinearLayer(hidden_dim, hidden_dim, layer_norm=True,
dropout=input_dropout, relu=relu_args[1]),
LinearLayer(hidden_dim, hidden_dim, layer_norm=True,
dropout=input_dropout, relu=relu_args[2])
][:n_input_proj])
self.input_vid_proj = nn.Sequential(*[
LinearLayer(vid_dim + aud_dim, hidden_dim, layer_norm=True,
dropout=input_dropout, relu=relu_args[0]),
LinearLayer(hidden_dim, hidden_dim, layer_norm=True,
dropout=input_dropout, relu=relu_args[1]),
LinearLayer(hidden_dim, hidden_dim, layer_norm=True,
dropout=input_dropout, relu=relu_args[2])
][:n_input_proj])
self.contrastive_align_loss = contrastive_align_loss
if contrastive_align_loss:
self.contrastive_align_projection_query = nn.Linear(hidden_dim, contrastive_hdim)
self.contrastive_align_projection_txt = nn.Linear(hidden_dim, contrastive_hdim)
self.contrastive_align_projection_vid = nn.Linear(hidden_dim, contrastive_hdim)
self.saliency_proj1 = nn.Linear(hidden_dim, hidden_dim)
self.saliency_proj2 = nn.Linear(hidden_dim, hidden_dim)
self.aux_loss = aux_loss
self.hidden_dim = hidden_dim
self.global_rep_token = torch.nn.Parameter(torch.randn(args.total_prompts, hidden_dim))
self.global_rep_pos = torch.nn.Parameter(torch.randn(1, hidden_dim))
self.moment_rep_token = torch.nn.Parameter(torch.randn(hidden_dim))
self.moment_rep_pos = torch.nn.Parameter(torch.randn(hidden_dim))
self.dummy_rep_token = torch.nn.Parameter(torch.randn(args.num_dummies, hidden_dim))
self.dummy_rep_pos = torch.nn.Parameter(torch.randn(args.num_dummies, hidden_dim))
normalize_before = False
self.sent_rep_token = torch.nn.Parameter(torch.randn(hidden_dim))
self.sent_rep_pos = torch.nn.Parameter(torch.randn(hidden_dim))
self.txt_proj_linear = LinearLayer(txt_dim, hidden_dim, layer_norm=True)
input_txt_sa_proj = TransformerEncoderLayer(hidden_dim, 8, self.args.dim_feedforward, 0.1, "prelu",
normalize_before)
txtproj_encoder_norm = nn.LayerNorm(hidden_dim) if normalize_before else None
self.txtproj_encoder = TransformerEncoder(input_txt_sa_proj, args.dummy_layers, txtproj_encoder_norm)
scls_encoder_layer = TransformerEncoderLayer(hidden_dim, 8, self.args.dim_feedforward, 0.1, "prelu",
normalize_before)
scls_encoder_norm = nn.LayerNorm(hidden_dim) if normalize_before else None
self.scls_encoder = TransformerEncoder(scls_encoder_layer, args.sent_layers, scls_encoder_norm)
dim = 4096
self.llama_dim_mapper1 = nn.Linear(hidden_dim, dim, bias=False)
self.llama_dim_mapper2 = nn.Linear(dim, hidden_dim, bias=False)
self.token_fc = nn.Linear(max_v_l, self.args.num_dummies)
def generate_pseudo_event(self, src_vid, src_vid_mask, targets):
bsz, L_src, _ = src_vid.size()
norm_vid = src_vid / (src_vid.norm(dim=2, keepdim=True) + 1e-8)
tsm = torch.bmm(norm_vid, norm_vid.transpose(1, 2))
mask = torch.tensor([[1., 1., 0., -1., -1.],
[1., 1., 0., -1., -1.],
[0., 0., 0., 0., 0.],
[-1., -1., 0., 1., 1.],
[-1., -1., 0., 1., 1.]], device=src_vid.device)
mask_size = mask.size(0)
mask = mask.view(1, mask_size, mask_size)
pad_tsm = nn.ZeroPad2d(mask_size // 2)(tsm)
score = torch.diagonal(F.conv2d(pad_tsm.unsqueeze(1), mask.unsqueeze(1)).squeeze(1), dim1=1,
dim2=2) # [bsz,L_src]
# average score as threshold
tau = score.mean(1).unsqueeze(1).repeat(1, L_src)
# fill the start, end indices with the max score
L_vid = torch.count_nonzero(src_vid_mask, 1)
st_ed = torch.cat([torch.zeros_like(L_vid).unsqueeze(1), L_vid.unsqueeze(1) - 1], dim=-1)
score[torch.arange(score.size(0)).unsqueeze(1), st_ed] = 100
# adjacent point removal and thresholding
score_r = torch.roll(score, 1, -1)
score_l = torch.roll(score, -1, -1)
bnds = torch.where((score_r <= score) & (score_l <= score) & (tau <= score), 1., 0.)
bnd_indices = bnds.nonzero()
temp = torch.roll(bnd_indices, 1, 0)
center = (bnd_indices + temp) / 2
width = bnd_indices - temp
bnd_spans = torch.cat([center, width[:, 1:]], dim=-1)
pseudo_event_spans = [bnd_spans[bnd_spans[:, 0] == i, :][:, 1:] / L_vid[i] for i in range(bsz)]
pseudo_event_spans_used = [bnd_spans[bnd_spans[:, 0] == i, :][:, 1:] for i in range(bsz)]
# output_pseudo_event_spans = []
# # 伪标签和真实标签结合
# for pseudo_span, real_span in zip(pseudo_event_spans, targets["span_labels"]):
# tmp = pseudo_span + real_span["spans"]
# output_pseudo_event_spans.append(tmp)
# pseudo_event_spans = pseudo_event_spans + targets["span_labels"]
return pseudo_event_spans, pseudo_event_spans_used
def forward(self, src_txt, src_txt_mask, src_vid, src_vid_mask, vid, qid, src_aud=None, src_aud_mask=None,
targets=None):
"""The forward expects two tensors:
- src_txt: [batch_size, L_txt, D_txt]
- src_txt_mask: [batch_size, L_txt], containing 0 on padded pixels,
will convert to 1 as padding later for transformer
- src_vid: [batch_size, L_vid, D_vid]
- src_vid_mask: [batch_size, L_vid], containing 0 on padded pixels,
will convert to 1 as padding later for transformer
It returns a dict with the following elements:
- "pred_spans": The normalized boxes coordinates for all queries, represented as
(center_x, 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 bounding box.
- "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.
"""
## For discovering real negative samples
if vid is not None: ## for demo (run_on_video/run.py)
_count = [v.count('_') for v in vid]
if self.args.dset_name == 'hl':
_position_to_cut = [find_nth(v, '_', _count[i] - 1) for i, v in enumerate(vid)]
ori_vid = [v[:_position_to_cut[i]] for i, v in enumerate(vid)]
else:
ori_vid = [v for v in vid]
if src_aud is not None:
src_vid = torch.cat([src_vid, src_aud], dim=2)
src_vid = self.input_vid_proj(src_vid)
src_txt = self.input_txt_proj(src_txt)
src_vid = src_vid + self.token_type_embeddings(torch.full_like(src_vid_mask.long(), 1))
src_txt = src_txt + self.token_type_embeddings(torch.zeros_like(src_txt_mask.long()))
pos_vid = self.position_embed(src_vid, src_vid_mask) # (bsz, L_vid, d)
pos_txt = self.txt_position_embed(src_txt) if self.use_txt_pos else torch.zeros_like(src_txt) # (bsz, L_txt, d)
# flattened_features = src_vid.view(-1, src_vid.size(-1))
#
# # 然后,我们可以使用任何可用的嵌入层(如 nn.Embedding)来将一维张量转换为特征令牌
# embedding_layer = torch.nn.Embedding(num_embeddings=flattened_features.size(0),
# embedding_dim=flattened_features.size(1))
# feature_tokens = embedding_layer(flattened_features)
### insert dummy token in front of txt
txt_dummy = self.dummy_rep_token.reshape([1, self.args.num_dummies, self.hidden_dim]).repeat(src_txt.shape[0],
1, 1)
# txt_dummy_vid = self.token_fc(src_vid.permute(0, 2, 1)).permute(0, 2, 1)
src_txt_dummy = torch.cat([txt_dummy, src_txt], dim=1)
mask_txt = torch.tensor([[True] * self.args.num_dummies]).to(src_txt_mask.device).repeat(src_txt_mask.shape[0],
1)
# mask_txt_vid = self.token_fc(src_vid_mask)
src_txt_mask_dummy = torch.cat([mask_txt, src_txt_mask], dim=1)
pos_dummy = self.dummy_rep_pos.reshape([1, self.args.num_dummies, self.hidden_dim]).repeat(pos_txt.shape[0], 1,
1)
# pos_dummy_vide = self.token_fc(pos_vid.permute(0, 2, 1)).permute(0, 2, 1)
pos_txt_dummy = torch.cat([pos_dummy, pos_txt], dim=1)
src_txt_dummy = src_txt_dummy.permute(1, 0, 2) # (L, batch_size, d)
pos_txt_dummy = pos_txt_dummy.permute(1, 0, 2) # (L, batch_size, d)
memory = self.txtproj_encoder(src_txt_dummy, src_key_padding_mask=~(src_txt_mask_dummy.bool()),
pos=pos_txt_dummy) # (L, batch_size, d)
dummy_token = memory[:self.args.num_dummies].permute(1, 0, 2)
pos_txt_dummy = pos_txt_dummy.permute(1, 0, 2) # (L, batch_size, d)
src_txt_dummy = torch.cat([dummy_token, src_txt], dim=1)
mask_txt_dummy = torch.tensor([[True] * self.args.num_dummies]).to(src_txt_mask.device).repeat(
src_txt_mask.shape[0], 1)
src_txt_mask_dummy = torch.cat([mask_txt_dummy, src_txt_mask], dim=1)
# Input : Concat video, dummy, txt
src = torch.cat([src_vid, src_txt_dummy], dim=1) # (bsz, L_vid+L_txt, d)
mask = torch.cat([src_vid_mask, src_txt_mask_dummy], dim=1).bool() # (bsz, L_vid+L_txt)
pos = torch.cat([pos_vid, pos_txt_dummy], dim=1)
### sentence token
smask_ = torch.tensor([[True]]).to(mask.device).repeat(src_txt_mask.shape[0], 1)
smask = torch.cat([smask_, src_txt_mask.bool()], dim=1)
ssrc_ = self.sent_rep_token.reshape([1, 1, self.hidden_dim]).repeat(src_txt.shape[0], 1, 1)
ssrc = torch.cat([ssrc_, src_txt], dim=1)
spos_ = self.sent_rep_pos.reshape([1, 1, self.hidden_dim]).repeat(pos_txt.shape[0], 1, 1)
spos = torch.cat([spos_, pos_txt], dim=1)
### dummy sentence token
smaskd = torch.cat([smask_, mask_txt_dummy.bool()], dim=1)
ssrcd = torch.cat([ssrc_, dummy_token], dim=1)
sposd = torch.cat([spos_, pos_dummy], dim=1)
if targets is not None: # train
mmask_ = torch.tensor([[True]]).to(mask.device).repeat(src_vid_mask.shape[0], 1)
mmask = torch.cat([mmask_, src_vid_mask.bool()], dim=1)
moment_mask_ = torch.clamp(targets["relevant_clips"], 0, 1).bool()
moment_mask = torch.cat([mmask_, moment_mask_], dim=1)
mmask = mmask * moment_mask
msrc_ = self.moment_rep_token.reshape([1, 1, self.hidden_dim]).repeat(src_vid.shape[0], 1, 1)
msrc = torch.cat([msrc_, src_vid], dim=1)
mpos_ = self.moment_rep_pos.reshape([1, 1, self.hidden_dim]).repeat(pos_vid.shape[0], 1, 1)
mpos = torch.cat([mpos_, pos_vid], dim=1)
### for Not moment token ####
nmmask_ = torch.tensor([[True]]).to(mask.device).repeat(src_vid_mask.shape[0], 1)
nmmask = torch.cat([nmmask_, src_vid_mask.bool()], dim=1)
nmoment_mask_ = ~(torch.clamp(targets["relevant_clips"], 0, 1).bool())
nmoment_mask = torch.cat([nmmask_, nmoment_mask_], dim=1)
nmmask = nmmask * nmoment_mask
nmsrc_ = self.moment_rep_token.reshape([1, 1, self.hidden_dim]).repeat(src_vid.shape[0], 1, 1)
nmsrc = torch.cat([nmsrc_, src_vid], dim=1)
nmpos_ = self.moment_rep_pos.reshape([1, 1, self.hidden_dim]).repeat(pos_vid.shape[0], 1, 1)
nmpos = torch.cat([nmpos_, pos_vid], dim=1)
###########
else:
moment_mask_ = None
# for t2vidavg sal token
vidsrc_ = torch.zeros((len(src_vid), 1, self.hidden_dim)).cuda()
for i in range(len(src_vid)):
vidsrc_[i] = src_vid[i][:src_vid_mask.sum(1)[i].long()].mean(0).clone().detach()
video_length = src_vid.shape[1]
if targets is not None: ## train
ssrc = ssrc.permute(1, 0, 2) # (L, batch_size, d)
spos = spos.permute(1, 0, 2) # (L, batch_size, d)
smemory = self.scls_encoder(ssrc, src_key_padding_mask=~smask, pos=spos) # (L, batch_size, d)
sentence_txt, smemory_words = smemory[0], smemory[1:] # sentence_txt : (batch_size, d)
ssrcd = ssrcd.permute(1, 0, 2) # (L, batch_size, d)
sposd = sposd.permute(1, 0, 2) # (L, batch_size, d)
smemoryd = self.scls_encoder(ssrcd, src_key_padding_mask=~smaskd, pos=sposd) # (L, batch_size, d)
sentence_dummy, smemory_words_dummy = smemoryd[0], smemoryd[1:]
txt_dummy_proj = torch.cat([smemory_words_dummy, smemory_words], dim=0)
hs, reference, memory, memory_global, attn_weights, memory_moment, nmmemory_moment, mmemory_frames, nmmemory_frames = self.transformer(
src, ~mask, self.query_embed.weight, pos, video_length=video_length,
moment_idx=targets["relevant_clips"], msrc=msrc, mpos=mpos, mmask=~mmask, nmsrc=nmsrc, nmpos=nmpos,
nmmask=~nmmask,
ctxtoken=vidsrc_, gtoken=self.global_rep_token, gpos=self.global_rep_pos,
vlen=src_vid_mask.sum(1).long())
moment2txt_similarity = torch.matmul(mmemory_frames.permute(1, 0, 2), txt_dummy_proj.permute(1, 2, 0))
nmoment2txt_similarity = torch.matmul(nmmemory_frames.permute(1, 0, 2), txt_dummy_proj.permute(1, 2, 0))
else: ## inference
sentence_dummy, sentence_txt, moment2txt_similarity, nmoment2txt_similarity = None, None, None, None
hs, reference, memory, memory_global, attn_weights, memory_moment, nmmemory_moment, mmemory_frames, nmmemory_frames = self.transformer(
src, ~mask, self.query_embed.weight, pos, video_length=video_length,
ctxtoken=vidsrc_, gtoken=self.global_rep_token, gpos=self.global_rep_pos,
vlen=src_vid_mask.sum(1).long())
pseudo_event_spans, pseudo_event_spans_used = self.generate_pseudo_event(src_vid,
src_vid_mask,
targets) # comment the line for computational cost check
event_tmp = self.event_span_embed(hs[0])
event_outputs_coord = event_tmp.sigmoid()
outputs_class = self.class_embed(hs) # (#layers, batch_size, #queries, #classes)
reference_before_sigmoid = inverse_sigmoid(reference)
tmp = self.span_embed(hs)
outputs_coord = tmp + reference_before_sigmoid
if self.span_loss_type == "l1":
outputs_coord = outputs_coord.sigmoid()
out = {'pred_logits': outputs_class[-1], 'pred_spans': outputs_coord[-1]}
out['pseudo_event_spans'] = pseudo_event_spans # comment the line for computational cost check
out['pred_event_spans'] = event_outputs_coord
out['pseudo_event_spans_used'] = pseudo_event_spans_used # (100)
out['pos'] = pos # pos (100, 148, 256)
txt_mem = memory[:, src_vid.shape[1]:] # (bsz, L_txt, d)
vid_mem = memory[:, :src_vid.shape[1]] # (bsz, L_vid, d)
if self.contrastive_align_loss:
proj_queries = F.normalize(self.contrastive_align_projection_query(hs), p=2, dim=-1)
proj_txt_mem = F.normalize(self.contrastive_align_projection_txt(txt_mem), p=2, dim=-1)
proj_vid_mem = F.normalize(self.contrastive_align_projection_vid(vid_mem), p=2, dim=-1)
out.update(dict(
proj_queries=proj_queries[-1],
proj_txt_mem=proj_txt_mem,
proj_vid_mem=proj_vid_mem
))
if vid is not None: ## for demo (run_on_video/run.py)
### Neg Pairs ###
neg_vid = ori_vid[1:] + ori_vid[:1]
real_neg_mask = torch.Tensor(element_wise_list_equal(ori_vid, neg_vid)).to(src_txt_dummy.device)
real_neg_mask = real_neg_mask == False
if real_neg_mask.sum() != 0:
src_txt_dummy_neg = torch.cat([src_txt_dummy[1:], src_txt_dummy[0:1]], dim=0)
src_txt_mask_dummy_neg = torch.cat([src_txt_mask_dummy[1:], src_txt_mask_dummy[0:1]], dim=0)
src_dummy_neg = torch.cat([src_vid, src_txt_dummy_neg], dim=1)
mask_dummy_neg = torch.cat([src_vid_mask, src_txt_mask_dummy_neg], dim=1).bool()
pos_neg = pos.clone() # since it does not use actual content
mask_dummy_neg = mask_dummy_neg[real_neg_mask]
src_dummy_neg = src_dummy_neg[real_neg_mask]
pos_neg = pos_neg[real_neg_mask]
src_txt_mask_dummy_neg = src_txt_mask_dummy_neg[real_neg_mask]
_, _, memory_neg, memory_global_neg, attn_weights_neg, _, _, _, _ = self.transformer(src_dummy_neg,
~mask_dummy_neg,
self.query_embed.weight,
pos_neg,
video_length=video_length,
ctxtoken=vidsrc_[
real_neg_mask],
gtoken=self.global_rep_token,
gpos=self.global_rep_pos,
vlen=src_vid_mask[
real_neg_mask].sum(
1).long())
vid_mem_neg = memory_neg[:, :src_vid.shape[1]]
out["saliency_scores_neg"] = (torch.sum(
self.saliency_proj1(vid_mem_neg) * self.saliency_proj2(memory_global_neg).unsqueeze(1),
dim=-1) / np.sqrt(self.hidden_dim))
out["src_txt_mask_neg"] = src_txt_mask_dummy_neg
out["t2vattnvalues_neg"] = (attn_weights_neg[:, :, self.args.num_dummies:] * (
src_txt_mask_dummy_neg[:, self.args.num_dummies:].unsqueeze(1).repeat(1, video_length, 1))).sum(2)
out["t2vattnvalues_neg"] = torch.clamp(out["t2vattnvalues_neg"], 0, 1)
else:
out["saliency_scores_neg"] = None
out["t2vattnvalues_neg"] = None
out["real_neg_mask"] = real_neg_mask
else:
out["saliency_scores_neg"] = None
out["t2vattnvalues_neg"] = None
out["real_neg_mask"] = None
out["saliency_scores"] = (
torch.sum(self.saliency_proj1(vid_mem) * self.saliency_proj2(memory_global).unsqueeze(1),
dim=-1) / np.sqrt(self.hidden_dim))
out["memory_moment"] = memory_moment
out["nmmemory_moment"] = nmmemory_moment
## sentence token embeeded with text / dummy
out["sentence_txt"] = sentence_txt
out["sentence_dummy"] = sentence_dummy
out["moment2txt_similarity"] = moment2txt_similarity
out["nmoment2txt_similarity"] = nmoment2txt_similarity
out["cate_attn_weights"] = attn_weights
out["moment_mask"] = moment_mask_
out["txt_mask"] = src_txt_mask_dummy
out["t2vattnvalues"] = (attn_weights[:, :, self.args.num_dummies:] * (
src_txt_mask.unsqueeze(1).repeat(1, video_length, 1))).sum(
2) # (batch_size, L_vid, L_txt) / (batch_size, L_txt)
out["t2vattnvalues"] = torch.clamp(out["t2vattnvalues"], 0, 1)
out["dummy_tokens"] = dummy_token
out["global_rep_tokens"] = self.global_rep_token
if targets is not None:
out["src_vid"] = mmemory_frames.permute(1, 0, 2) * moment_mask_.unsqueeze(2) + nmmemory_frames.permute(1, 0,
2) * (
~(moment_mask_.unsqueeze(2).bool())).float()
else:
out["src_vid"] = None
out["video_mask"] = src_vid_mask
if self.aux_loss:
# assert proj_queries and proj_txt_mem
out['aux_outputs'] = [
{'pred_logits': a, 'pred_spans': b} for a, b in zip(outputs_class[:-1], outputs_coord[:-1])]
if self.contrastive_align_loss:
assert proj_queries is not None
for idx, d in enumerate(proj_queries[:-1]):
out['aux_outputs'][idx].update(dict(proj_queries=d, proj_txt_mem=proj_txt_mem))
return out
class SetCriterion(nn.Module):
""" This class computes the loss for DETR.
The process happens in two steps:
1) we compute hungarian assignment between ground truth boxes and the outputs of the model
2) we supervise each pair of matched ground-truth / prediction (supervise class and box)
"""
def __init__(self, matcher, weight_dict, eos_coef, losses, temperature, span_loss_type, max_v_l,
saliency_margin=1, event_matcher=None, use_matcher=True, args=None):
""" Create the criterion.
Parameters:
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.
eos_coef: relative classification weight applied to the no-object category
losses: list of all the losses to be applied. See get_loss for list of available losses.
temperature: float, temperature for NCE loss
span_loss_type: str, [l1, ce]
max_v_l: int,
saliency_margin: float
"""
super().__init__()
self.args = args
self.matcher = matcher
self.event_matcher = event_matcher
self.weight_dict = weight_dict
self.losses = losses
self.temperature = temperature
self.span_loss_type = span_loss_type
self.max_v_l = max_v_l
self.saliency_margin = saliency_margin
# foreground and background classification
self.foreground_label = 0
self.background_label = 1
self.eos_coef = eos_coef
empty_weight = torch.ones(2)
empty_weight[-1] = self.eos_coef # lower weight for background (index 1, foreground index 0)
self.register_buffer('empty_weight', empty_weight)
# for tvsum,
self.use_matcher = use_matcher
# moment sentence contrastive
self.criterion = torch.nn.CrossEntropyLoss().to(self.args.device)
self.l2_criterion = torch.nn.MSELoss().to(self.args.device)
self.kld_criterion = torch.nn.KLDivLoss(reduction='none').to(self.args.device)
self.bce_criterion = nn.BCELoss(reduction='none')
def loss_spans(self, outputs, targets, indices):
"""Compute the losses related to the bounding boxes, the L1 regression loss and the GIoU loss
targets dicts must contain the key "spans" containing a tensor of dim [nb_tgt_spans, 2]
The target spans are expected in format (center_x, w), normalized by the image size.
"""
assert 'pred_spans' in outputs
targets = targets["span_labels"]
idx = self._get_src_permutation_idx(indices)
src_spans = outputs['pred_spans'][idx] # (#spans, max_v_l * 2)
tgt_spans = torch.cat([t['spans'][i] for t, (_, i) in zip(targets, indices)], dim=0) # (#spans, 2)
if self.span_loss_type == "l1":
loss_span = F.l1_loss(src_spans, tgt_spans, reduction='none')
loss_giou = 1 - torch.diag(generalized_temporal_iou(span_cxw_to_xx(src_spans), span_cxw_to_xx(tgt_spans)))
else: # ce
n_spans = src_spans.shape[0]
src_spans = src_spans.view(n_spans, 2, self.max_v_l).transpose(1, 2)
loss_span = F.cross_entropy(src_spans, tgt_spans, reduction='none')
loss_giou = loss_span.new_zeros([1])
losses = {}
losses['loss_span'] = loss_span.mean()
losses['loss_giou'] = loss_giou.mean()
return losses
def loss_labels(self, outputs, targets, indices, log=True):
"""Classification loss (NLL)
targets dicts must contain the key "labels" containing a tensor of dim [nb_target_boxes]
"""
# TODO add foreground and background classifier. use all non-matched as background.
assert 'pred_logits' in outputs
src_logits = outputs['pred_logits'] # (batch_size, #queries, #classes=2)
# idx is a tuple of two 1D tensors (batch_idx, src_idx), of the same length == #objects in batch
idx = self._get_src_permutation_idx(indices)
target_classes = torch.full(src_logits.shape[:2], self.background_label,
dtype=torch.int64, device=src_logits.device) # (batch_size, #queries)
target_classes[idx] = self.foreground_label
loss_ce = F.cross_entropy(src_logits.transpose(1, 2), target_classes, self.empty_weight, reduction="none")
losses = {'loss_label': loss_ce.mean()}
if log:
# TODO this should probably be a separate loss, not hacked in this one here
losses['class_error'] = 100 - accuracy(src_logits[idx], self.foreground_label)[0]
return losses
def loss_saliency(self, outputs, targets, indices, log=True):
"""higher scores for positive clips"""
if "saliency_pos_labels" not in targets:
return {"loss_saliency": 0}
# Neg pair loss
if outputs["saliency_scores_neg"] is not None: ## When batch size is not 1 (negative pair exists)
vid_token_mask = outputs["video_mask"]
real_neg_mask = outputs["real_neg_mask"]
saliency_scores_neg = outputs["saliency_scores_neg"].clone() # (N, L)
loss_neg_pair = (
- torch.log(1. - torch.sigmoid(saliency_scores_neg)) * (vid_token_mask[real_neg_mask])).sum(
dim=1).mean()
saliency_scores = outputs["saliency_scores"].clone() # (N, L)
saliency_contrast_label = targets["saliency_all_labels"]
# real neg
realneg_saliency_scores = torch.cat([saliency_scores[real_neg_mask], saliency_scores_neg], dim=1)
realneg_saliency_contrast_label = torch.cat(
[saliency_contrast_label[real_neg_mask], torch.zeros_like(saliency_contrast_label)[real_neg_mask]],
dim=1)
realneg_vid_token_mask = vid_token_mask[real_neg_mask].repeat([1, 2])
realneg_saliency_scores = realneg_vid_token_mask * realneg_saliency_scores + (
1. - realneg_vid_token_mask) * -1e+3
tau = 0.5
loss_rank_contrastive = 0.
for rand_idx in range(1, 12):
drop_mask = ~(realneg_saliency_contrast_label > 100) # no drop
pos_mask = (realneg_saliency_contrast_label >= rand_idx) # positive when equal or higher than rand_idx
if torch.sum(pos_mask) == 0: # no positive sample
continue
else:
batch_drop_mask = torch.sum(pos_mask, dim=1) > 0 # negative sample indicator
# drop higher ranks
cur_saliency_scores = realneg_saliency_scores * drop_mask / tau + ~drop_mask * -1e+3
# numerical stability
logits = cur_saliency_scores - torch.max(cur_saliency_scores, dim=1, keepdim=True)[0]
# softmax
exp_logits = torch.exp(logits)
log_prob = logits - torch.log(exp_logits.sum(1, keepdim=True) + 1e-6)
mean_log_prob_pos = (pos_mask * log_prob * realneg_vid_token_mask).sum(1) / (pos_mask.sum(1) + 1e-6)
loss = - mean_log_prob_pos * batch_drop_mask
loss_rank_contrastive = loss_rank_contrastive + loss.mean()
loss_rank_contrastive = loss_rank_contrastive / 12
false_neg_mask = ~(real_neg_mask)
if false_neg_mask.sum() != 0:
if false_neg_mask.sum() == 1:
falseneg_saliency_scores = saliency_scores[false_neg_mask].unsqueeze(0)
falseneg_saliency_contrast_label = saliency_contrast_label[false_neg_mask].unsqueeze(0)
falseneg_vid_token_mask = vid_token_mask[false_neg_mask].unsqueeze(0)
falseneg_saliency_scores = falseneg_vid_token_mask * falseneg_saliency_scores + (
1. - falseneg_vid_token_mask) * -1e+3
else:
falseneg_saliency_scores = saliency_scores[false_neg_mask]
falseneg_saliency_contrast_label = saliency_contrast_label[false_neg_mask]
falseneg_vid_token_mask = vid_token_mask[false_neg_mask]
falseneg_saliency_scores = falseneg_vid_token_mask * falseneg_saliency_scores + (
1. - falseneg_vid_token_mask) * -1e+3
tau = 0.5
falseneg_loss_rank_contrastive = 0.
for rand_idx in range(1, 12):
drop_mask = ~(falseneg_saliency_contrast_label > 100) # no drop
pos_mask = (
falseneg_saliency_contrast_label >= rand_idx) # positive when equal or higher than rand_idx
if torch.sum(pos_mask) == 0: # no positive sample
continue
else:
batch_drop_mask = torch.sum(pos_mask, dim=1) > 0 # negative sample indicator
# drop higher ranks
cur_saliency_scores = falseneg_saliency_scores * drop_mask / tau + ~drop_mask * -1e+3
# numerical stability
logits = cur_saliency_scores - torch.max(cur_saliency_scores, dim=1, keepdim=True)[0]
# softmax
exp_logits = torch.exp(logits)
log_prob = logits - torch.log(exp_logits.sum(1, keepdim=True) + 1e-6)
mean_log_prob_pos = (pos_mask * log_prob * falseneg_vid_token_mask).sum(1) / (
pos_mask.sum(1) + 1e-6)
loss = - mean_log_prob_pos * batch_drop_mask
falseneg_loss_rank_contrastive = falseneg_loss_rank_contrastive + loss.mean()
falseneg_loss_rank_contrastive = falseneg_loss_rank_contrastive / 12
loss_rank_contrastive += falseneg_loss_rank_contrastive
saliency_scores = outputs["saliency_scores"] # (N, L)
pos_indices = targets["saliency_pos_labels"] # (N, #pairs)
neg_indices = targets["saliency_neg_labels"] # (N, #pairs)
num_pairs = pos_indices.shape[1] # typically 2 or 4
batch_indices = torch.arange(len(saliency_scores)).to(saliency_scores.device)
pos_scores = torch.stack(
[saliency_scores[batch_indices, pos_indices[:, col_idx]] for col_idx in range(num_pairs)], dim=1)
neg_scores = torch.stack(
[saliency_scores[batch_indices, neg_indices[:, col_idx]] for col_idx in range(num_pairs)], dim=1)
loss_saliency = torch.clamp(self.saliency_margin + neg_scores - pos_scores, min=0).sum() \
/ (len(pos_scores) * num_pairs) * 2 # * 2 to keep the loss the same scale
if self.args.dset_name in ['youtube_uni']:
loss_saliency = loss_saliency + loss_rank_contrastive + loss_neg_pair * 0.
else:
loss_saliency = loss_saliency + loss_rank_contrastive + loss_neg_pair
########### Saliency loss to t2v attn weights ##############
"""higher scores for positive clips"""
vid_token_mask = outputs["video_mask"]
# Neg pair loss
if outputs["t2vattnvalues_neg"] is not None:
saliency_scores_neg = outputs["t2vattnvalues_neg"].clone() # (N, L)
loss_neg_pair_attn = (- torch.log(1. - saliency_scores_neg) * (vid_token_mask[real_neg_mask])).sum(
dim=1).mean()
saliency_scores = outputs["t2vattnvalues"].clone() # (N, L)
saliency_contrast_label = targets["saliency_all_labels"]
# real neg
realneg_saliency_scores = torch.cat([saliency_scores[real_neg_mask], saliency_scores_neg], dim=1)
realneg_saliency_contrast_label = torch.cat(
[saliency_contrast_label[real_neg_mask], torch.zeros_like(saliency_contrast_label)[real_neg_mask]],
dim=1)
realneg_vid_token_mask = vid_token_mask[real_neg_mask].repeat([1, 2])
realneg_saliency_scores = realneg_vid_token_mask * realneg_saliency_scores + (
1. - realneg_vid_token_mask) * -1e+3
tau = 0.5
loss_rank_contrastive_attn = 0.
for rand_idx in range(1, 12):
drop_mask = ~(realneg_saliency_contrast_label > 100) # no drop
pos_mask = (realneg_saliency_contrast_label >= rand_idx) # positive when equal or higher than rand_idx
if torch.sum(pos_mask) == 0: # no positive sample
continue
else:
batch_drop_mask = torch.sum(pos_mask, dim=1) > 0 # negative sample indicator
# drop higher ranks
cur_saliency_scores = realneg_saliency_scores * drop_mask / tau + ~drop_mask * -1e+3
# numerical stability
logits = cur_saliency_scores - torch.max(cur_saliency_scores, dim=1, keepdim=True)[0]
# softmax
exp_logits = torch.exp(logits)
log_prob = logits - torch.log(exp_logits.sum(1, keepdim=True) + 1e-6)
mean_log_prob_pos = (pos_mask * log_prob * realneg_vid_token_mask).sum(1) / (pos_mask.sum(1) + 1e-6)
loss = - mean_log_prob_pos * batch_drop_mask
loss_rank_contrastive_attn = loss_rank_contrastive_attn + loss.mean()
loss_rank_contrastive_attn = loss_rank_contrastive_attn / 12
false_neg_mask = ~(real_neg_mask)
if false_neg_mask.sum() != 0:
if false_neg_mask.sum() == 1:
falseneg_saliency_scores = saliency_scores[false_neg_mask].unsqueeze(0)
falseneg_saliency_contrast_label = saliency_contrast_label[false_neg_mask].unsqueeze(0)
falseneg_vid_token_mask = vid_token_mask[false_neg_mask].unsqueeze(0)
falseneg_saliency_scores = falseneg_vid_token_mask * falseneg_saliency_scores + (
1. - falseneg_vid_token_mask) * -1e+3
else:
falseneg_saliency_scores = saliency_scores[false_neg_mask]
falseneg_saliency_contrast_label = saliency_contrast_label[false_neg_mask]
falseneg_vid_token_mask = vid_token_mask[false_neg_mask]
falseneg_saliency_scores = falseneg_vid_token_mask * falseneg_saliency_scores + (
1. - falseneg_vid_token_mask) * -1e+3
tau = 0.5
falseneg_loss_rank_contrastive = 0.
for rand_idx in range(1, 12):
drop_mask = ~(falseneg_saliency_contrast_label > 100) # no drop
pos_mask = (
falseneg_saliency_contrast_label >= rand_idx) # positive when equal or higher than rand_idx
if torch.sum(pos_mask) == 0: # no positive sample
continue
else:
batch_drop_mask = torch.sum(pos_mask, dim=1) > 0 # negative sample indicator
# drop higher ranks
cur_saliency_scores = falseneg_saliency_scores * drop_mask / tau + ~drop_mask * -1e+3
# numerical stability
logits = cur_saliency_scores - torch.max(cur_saliency_scores, dim=1, keepdim=True)[0]
# softmax
exp_logits = torch.exp(logits)
log_prob = logits - torch.log(exp_logits.sum(1, keepdim=True) + 1e-6)
mean_log_prob_pos = (pos_mask * log_prob * falseneg_vid_token_mask).sum(1) / (
pos_mask.sum(1) + 1e-6)
loss = - mean_log_prob_pos * batch_drop_mask
falseneg_loss_rank_contrastive = falseneg_loss_rank_contrastive + loss.mean()
falseneg_loss_rank_contrastive = falseneg_loss_rank_contrastive / 12
loss_rank_contrastive += falseneg_loss_rank_contrastive
saliency_scores = outputs["t2vattnvalues"] # (N, L)
pos_indices = targets["saliency_pos_labels"] # (N, #pairs)
neg_indices = targets["saliency_neg_labels"] # (N, #pairs)
num_pairs = pos_indices.shape[1] # typically 2 or 4
batch_indices = torch.arange(len(saliency_scores)).to(saliency_scores.device)
pos_scores = torch.stack(
[saliency_scores[batch_indices, pos_indices[:, col_idx]] for col_idx in range(num_pairs)], dim=1)
neg_scores = torch.stack(
[saliency_scores[batch_indices, neg_indices[:, col_idx]] for col_idx in range(num_pairs)], dim=1)
loss_saliency_attn = torch.clamp(self.saliency_margin + neg_scores - pos_scores, min=0).sum() \
/ (len(pos_scores) * num_pairs) * 2 # * 2 to keep the loss the same scale
saliency_binary_label = torch.clamp(targets["saliency_all_labels"], 0, 1)
logits = saliency_scores.reshape(-1)
labels_x = saliency_binary_label.reshape(-1)
BCEcriterion = nn.BCELoss()
bceloss = BCEcriterion(logits, labels_x)
if self.args.dset_name in ['youtube_uni']:
loss_saliency_attn = loss_rank_contrastive_attn + bceloss + loss_neg_pair_attn * 0 + loss_saliency_attn
else:
loss_saliency_attn = loss_rank_contrastive_attn + bceloss + loss_neg_pair_attn + loss_saliency_attn
loss_saliency += (loss_saliency_attn * self.args.lw_wattn)
else: ## when batch size == 1
vid_token_mask = outputs["video_mask"]
saliency_scores = outputs["saliency_scores"].clone() # (N, L)
saliency_contrast_label = targets["saliency_all_labels"]
saliency_scores = vid_token_mask * saliency_scores + (1. - vid_token_mask) * -1e+3
tau = 0.5
loss_rank_contrastive = 0.
for rand_idx in range(1, 12):
drop_mask = ~(saliency_contrast_label > 100) # no drop
pos_mask = (saliency_contrast_label >= rand_idx) # positive when equal or higher than rand_idx
if torch.sum(pos_mask) == 0: # no positive sample
continue
else:
batch_drop_mask = torch.sum(pos_mask, dim=1) > 0 # negative sample indicator
# drop higher ranks
cur_saliency_scores = saliency_scores * drop_mask / tau + ~drop_mask * -1e+3
# numerical stability
logits = cur_saliency_scores - torch.max(cur_saliency_scores, dim=1, keepdim=True)[0]
# softmax
exp_logits = torch.exp(logits)
log_prob = logits - torch.log(exp_logits.sum(1, keepdim=True) + 1e-6)
mean_log_prob_pos = (pos_mask * log_prob * vid_token_mask).sum(1) / (pos_mask.sum(1) + 1e-6)
loss = - mean_log_prob_pos * batch_drop_mask
loss_rank_contrastive = loss_rank_contrastive + loss.mean()
loss_rank_contrastive = loss_rank_contrastive / 12
saliency_scores = outputs["saliency_scores"] # (N, L)
pos_indices = targets["saliency_pos_labels"] # (N, #pairs)
neg_indices = targets["saliency_neg_labels"] # (N, #pairs)
num_pairs = pos_indices.shape[1] # typically 2 or 4
batch_indices = torch.arange(len(saliency_scores)).to(saliency_scores.device)
pos_scores = torch.stack(
[saliency_scores[batch_indices, pos_indices[:, col_idx]] for col_idx in range(num_pairs)], dim=1)
neg_scores = torch.stack(
[saliency_scores[batch_indices, neg_indices[:, col_idx]] for col_idx in range(num_pairs)], dim=1)
loss_saliency = torch.clamp(self.saliency_margin + neg_scores - pos_scores, min=0).sum() \
/ (len(pos_scores) * num_pairs) * 2 # * 2 to keep the loss the same scale
loss_saliency = loss_saliency + loss_rank_contrastive
########### Saliency loss to t2v attn weights ##############
"""higher scores for positive clips"""
vid_token_mask = outputs["video_mask"]
saliency_scores = outputs["t2vattnvalues"].clone() # (N, L)
saliency_contrast_label = targets["saliency_all_labels"]
saliency_scores = vid_token_mask * saliency_scores + (1. - vid_token_mask) * -1e+3
tau = 0.5
loss_rank_contrastive = 0.
for rand_idx in range(1, 12):
drop_mask = ~(saliency_contrast_label > 100) # no drop
pos_mask = (saliency_contrast_label >= rand_idx) # positive when equal or higher than rand_idx
if torch.sum(pos_mask) == 0: # no positive sample
continue
else:
batch_drop_mask = torch.sum(pos_mask, dim=1) > 0 # negative sample indicator
# drop higher ranks
cur_saliency_scores = saliency_scores * drop_mask / tau + ~drop_mask * -1e+3
# numerical stability
logits = cur_saliency_scores - torch.max(cur_saliency_scores, dim=1, keepdim=True)[0]
# softmax
exp_logits = torch.exp(logits)
log_prob = logits - torch.log(exp_logits.sum(1, keepdim=True) + 1e-6)
mean_log_prob_pos = (pos_mask * log_prob * vid_token_mask).sum(1) / (pos_mask.sum(1) + 1e-6)
loss = - mean_log_prob_pos * batch_drop_mask
loss_rank_contrastive = loss_rank_contrastive + loss.mean()
loss_rank_contrastive_attn = loss_rank_contrastive / 12
saliency_scores = outputs["t2vattnvalues"] # (N, L)
pos_indices = targets["saliency_pos_labels"] # (N, #pairs)
neg_indices = targets["saliency_neg_labels"] # (N, #pairs)
num_pairs = pos_indices.shape[1] # typically 2 or 4
batch_indices = torch.arange(len(saliency_scores)).to(saliency_scores.device)
pos_scores = torch.stack(
[saliency_scores[batch_indices, pos_indices[:, col_idx]] for col_idx in range(num_pairs)], dim=1)
neg_scores = torch.stack(
[saliency_scores[batch_indices, neg_indices[:, col_idx]] for col_idx in range(num_pairs)], dim=1)
loss_saliency_attn = torch.clamp(self.saliency_margin + neg_scores - pos_scores, min=0).sum() \
/ (len(pos_scores) * num_pairs) * 2 # * 2 to keep the loss the same scale
saliency_binary_label = torch.clamp(targets["saliency_all_labels"], 0, 1)
logits = saliency_scores.reshape(-1)
labels_x = saliency_binary_label.reshape(-1)
BCEcriterion = nn.BCELoss()
bceloss = BCEcriterion(logits, labels_x)
loss_saliency_attn = loss_rank_contrastive_attn + bceloss + loss_saliency_attn
loss_saliency += (loss_saliency_attn * self.args.lw_wattn)
return {"loss_saliency": loss_saliency}
def loss_contrastive_moment_sentence(self, outputs, targets, indices, log=True):
if outputs["memory_moment"] is not None:
moment_token = outputs["memory_moment"]
nmmemory_moment = outputs["nmmemory_moment"]
sentence_token = outputs["sentence_txt"].squeeze(1)
sentence_dummy = outputs["sentence_dummy"].squeeze(1) # b, 1, d
moment_logits = F.normalize(moment_token, dim=1)
nmoment_logits = F.normalize(nmmemory_moment, dim=1)
sentence_logits = F.normalize(sentence_token, dim=1)
dummy_logits = F.normalize(sentence_dummy, dim=1)
similarity_matrix = torch.matmul(moment_logits, sentence_logits.T) # B B
nsimilarity_matrix = torch.matmul(nmoment_logits, sentence_logits.T) # B B
similarity_matrix = torch.cat([similarity_matrix, nsimilarity_matrix], dim=1)
labels = torch.eye(similarity_matrix.shape[0]).to(self.args.device)
nlabels = torch.zeros_like(nsimilarity_matrix).to(self.args.device)
labels = torch.cat([labels, nlabels], dim=1).max(dim=1)[1]
loss_ms_align = self.criterion(similarity_matrix, labels)
dummy_similarity_matrix = torch.matmul(moment_logits, dummy_logits.T)
dummy_nsimilarity_matrix = torch.matmul(nmoment_logits, dummy_logits.T)
dummy_similarity_matrix = torch.cat([dummy_similarity_matrix, dummy_nsimilarity_matrix], dim=1)
dummy_labels = (~(torch.eye(similarity_matrix.shape[0]).to(self.args.device).bool())).float()
dummy_nlabels = torch.ones_like(nsimilarity_matrix).to(self.args.device)
dummy_labels = torch.cat([dummy_labels, dummy_nlabels], dim=1).max(dim=1)[1]
dummy_loss_ms_align = self.criterion(dummy_similarity_matrix, dummy_labels)
loss_ms_align += dummy_loss_ms_align
video_mask = outputs['video_mask']
src_vid = outputs['src_vid'] # [bsz, L_vid, D_vid]
moment_mask_ = torch.clamp(targets["relevant_clips"], 0, 1)
momtokcls_pred = torch.matmul(moment_token.unsqueeze(1), src_vid.permute(0, 2, 1)) # bsz 1 L_vid
momtokcls_label = moment_mask_
momtokcls_logit = torch.sigmoid(momtokcls_pred)
loss_ms_align += (self.bce_criterion(momtokcls_logit.reshape(-1),
momtokcls_label.reshape(-1)) * video_mask.reshape(-1)).mean()
else:
loss_ms_align = 0.
return {"loss_ms_align": loss_ms_align}
#
def loss_moment2txt_sim_distill(self, outputs, targets, indices, log=True):
if outputs["moment2txt_similarity"] is not None:
moment2txt_similarity = outputs["moment2txt_similarity"] # bsz L_clip 22
moment_mask = outputs["moment_mask"].int() # bsz L_clip 1
txt_mask = outputs["txt_mask"].unsqueeze(1).repeat(1, outputs["cate_attn_weights"].size(1), 1) # bsz l_t
attn_weights = outputs["cate_attn_weights"] # bsz L_clip 22
b, L_vid, L_txt = attn_weights.size()
loss_distill = self.kld_criterion(
torch.log(attn_weights + 1e-6).reshape(b * L_vid, -1),
torch.softmax(moment2txt_similarity, dim=-1).clone().detach().reshape(b * L_vid, -1)).mean(
1) * moment_mask.reshape(-1)
loss_distill = loss_distill.sum() / moment_mask.sum()
else:
loss_distill = 0.
return {"loss_distill": loss_distill}
def loss_orthogonal_dummy(self, outputs, targets, indices, log=True):
dummy_tokens = outputs["dummy_tokens"] # (n_dum, dim)
if dummy_tokens.size(1) != 1:
dummy_tokens_norm = dummy_tokens / dummy_tokens.norm(dim=2)[:, :, None]
dummy_tokens_sim = torch.matmul(dummy_tokens_norm, dummy_tokens_norm.permute(0, 2, 1).detach())
for i in range(len(dummy_tokens_sim)):
dummy_tokens_sim[i].fill_diagonal_(0)
loss_dummy_ortho = dummy_tokens_sim.abs().mean()
else:
loss_dummy_ortho = 0.
global_tokens = outputs["global_rep_tokens"]
global_tokens_norm = global_tokens / global_tokens.norm(dim=1)[:, None]
global_tokens_sim = torch.matmul(global_tokens_norm, global_tokens_norm.permute(1, 0).detach())
for i in range(len(global_tokens_sim)):
global_tokens_sim.fill_diagonal_(0)
loss_dummy_ortho += global_tokens_sim.abs().mean()
return {"loss_orthogonal_dummy": loss_dummy_ortho}
def loss_contrastive_align(self, outputs, targets, indices, log=True):
"""encourage higher scores between matched query span and input text"""
normalized_text_embed = outputs["proj_txt_mem"] # (bsz, #tokens, d) text tokens
normalized_img_embed = outputs["proj_queries"] # (bsz, #queries, d)
logits = torch.einsum(
"bmd,bnd->bmn", normalized_img_embed, normalized_text_embed) # (bsz, #queries, #tokens)
logits = logits.sum(2) / self.temperature # (bsz, #queries)
idx = self._get_src_permutation_idx(indices)
positive_map = torch.zeros_like(logits, dtype=torch.bool)
positive_map[idx] = True
positive_logits = logits.masked_fill(~positive_map, 0)
pos_term = positive_logits.sum(1) # (bsz, )
num_pos = positive_map.sum(1) # (bsz, )
neg_term = logits.logsumexp(1) # (bsz, )
loss_nce = - pos_term / num_pos + neg_term # (bsz, )
losses = {"loss_contrastive_align": loss_nce.mean()}
return losses
def loss_contrastive_align_vid_txt(self, outputs, targets, indices, log=True):
"""encourage higher scores between matched query span and input text"""
normalized_text_embed = outputs["proj_txt_mem"] # (bsz, #tokens, d) text tokens
normalized_img_embed = outputs["proj_queries"] # (bsz, #queries, d)
logits = torch.einsum(
"bmd,bnd->bmn", normalized_img_embed, normalized_text_embed) # (bsz, #queries, #tokens)
logits = logits.sum(2) / self.temperature # (bsz, #queries)
idx = self._get_src_permutation_idx(indices)
positive_map = torch.zeros_like(logits, dtype=torch.bool)
positive_map[idx] = True
positive_logits = logits.masked_fill(~positive_map, 0)
pos_term = positive_logits.sum(1) # (bsz, )
num_pos = positive_map.sum(1) # (bsz, )
neg_term = logits.logsumexp(1) # (bsz, )
loss_nce = - pos_term / num_pos + neg_term # (bsz, )
losses = {"loss_contrastive_align": loss_nce.mean()}
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 # two 1D tensors of the same length
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 loss_event_spans(self, outputs, targets, indices):
# assert 'pred_event_spans' in outputs
## boundary span prediction