-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels_sw.py
More file actions
1319 lines (1071 loc) · 50.3 KB
/
models_sw.py
File metadata and controls
1319 lines (1071 loc) · 50.3 KB
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
import torch.nn as nn
import os
import copy
import math
import pickle
import torch.nn.functional as F
import torch
import torchvision.models as torchmodels
from collections import OrderedDict
from future.utils import iteritems
import numpy as np
# from utils import load_clip_to_cpu
from utils import *
from classes import CLASSES, CUSTOM_TEMPLATES
from thirdparty.clip import clip
import logging
import copy
from cvxopt import matrix
from cvxopt.solvers import qp
from cvxopt import solvers
solvers.options['show_progress'] = False
def get_logger(filename, verbosity=1, name=None):
"""logger function for print logs."""
level_dict = {0: logging.DEBUG, 1: logging.INFO, 2: logging.WARNING}
formatter = logging.Formatter(
"[%(asctime)s][%(filename)s][line:%(lineno)d][%(levelname)s] %(message)s")
logger = logging.getLogger(name)
logger.setLevel(level_dict[verbosity])
fh = logging.FileHandler(filename, "w")
fh.setFormatter(formatter)
logger.addHandler(fh)
sh = logging.StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)
return logger
class PositionEmbeddingSine(nn.Module):
"""
This is a more standard version of the position embedding, very similar to the one
used by the Attention is all you need paper, generalized to work on images.
"""
def __init__(self, num_pos_feats=64, temperature=10000, normalize=False, scale=None):
super().__init__()
self.num_pos_feats = num_pos_feats
self.temperature = temperature
self.normalize = normalize
if scale is not None and normalize is False:
raise ValueError("normalize should be True if scale is passed")
if scale is None:
scale = 2 * math.pi
self.scale = scale
def forward(self, h, w, x):
mask = x.new_zeros((x.shape[0], h, w)).bool()
not_mask = ~mask
y_embed = not_mask.cumsum(1, dtype=torch.float32)
x_embed = not_mask.cumsum(2, dtype=torch.float32)
if self.normalize:
eps = 1e-6
y_embed = y_embed / (y_embed[:, -1:, :] + eps) * self.scale
x_embed = x_embed / (x_embed[:, :, -1:] + eps) * self.scale
dim_t = torch.arange(self.num_pos_feats,
dtype=torch.float32, device=x.device)
dim_t = self.temperature ** (2 * (dim_t // 2) / self.num_pos_feats)
pos_x = x_embed[:, :, :, None] / dim_t
pos_y = y_embed[:, :, :, None] / dim_t
pos_x = torch.stack(
(pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()), dim=4).flatten(3)
pos_y = torch.stack(
(pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()), dim=4).flatten(3)
pos = torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2)
return pos
class PositionEmbeddingLearned(nn.Module):
"""
Absolute pos embedding, learned.
"""
def __init__(self, num_length=50, num_pos_feats=256):
super().__init__()
self.row_embed = nn.Embedding(num_length, num_pos_feats)
self.col_embed = nn.Embedding(num_length, num_pos_feats)
self.reset_parameters()
def reset_parameters(self):
nn.init.uniform_(self.row_embed.weight)
nn.init.uniform_(self.col_embed.weight)
def forward(self, h, w, x):
# h, w = x.shape[-2:]
i = torch.arange(w, device=x.device)
j = torch.arange(h, device=x.device)
x_emb = self.col_embed(i)
y_emb = self.row_embed(j)
pos = torch.cat([
x_emb.unsqueeze(0).repeat(h, 1, 1),
y_emb.unsqueeze(1).repeat(1, w, 1),
], dim=-1).permute(2, 0, 1).unsqueeze(0).repeat(x.shape[0], 1, 1, 1)
return pos
class EncoderLayer(nn.Module):
"Encoder is made up of self-attn and feed forward"
def __init__(self, n_head, d_model, d_ff, dropout, norm_before=True):
super(EncoderLayer, self).__init__()
self.self_attn = MultiHeadAttention(n_head, d_model, dropout)
self.cross_attn = MultiHeadAttention(n_head, d_model, dropout)
self.feed_forward = PositionwiseFeedForward(d_model, d_ff, dropout)
# self.size = size
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.dropout3 = nn.Dropout(dropout)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.norm3 = nn.LayerNorm(d_model)
self.norm4 = nn.LayerNorm(d_model)
self.norm_before = norm_before
def forward_pre(self, x, y, mask=None):
"for conenctions"
x2 = self.norm1(x)
x2 = self.self_attn(x2, x2, x2, mask)
x = x + self.dropout1(x2)
x2 = self.norm2(x)
y2 = self.norm3(y)
x2 = self.cross_attn(x2, y2, y2, mask)
x = x + self.dropout2(x2)
x2 = self.norm4(x)
x2 = self.feed_forward(x2)
out = x + self.dropout3(x2)
return out
def forward_post(self, x, y, mask=None):
"for conenctions"
x2 = self.self_attn(x, x, x, mask)
x = x + self.dropout1(x2)
x = self.norm1(x)
y = self.norm2(y)
x2 = self.cross_attn(x, y, y, mask)
x = x + self.dropout2(x2)
x = self.norm3(x)
x2 = self.feed_forward(x)
x = x + self.dropout3(x2)
out = self.norm4(x)
return out
def forward(self, x, y, mask=None):
if self.norm_before:
return self.forward_pre(x, y, mask)
else:
return self.forward_post(x, y, mask)
class EncoderLayer2(nn.Module):
"Encoder is made up of self-attn and feed forward"
def __init__(self, n_head, d_model, d_ff, dropout, norm_before=True):
super(EncoderLayer2, self).__init__()
self.self_attn = MultiHeadAttention(n_head, d_model, dropout)
self.cross_attn = MultiHeadAttention(n_head, d_model, dropout)
self.feed_forward = PositionwiseFeedForward(d_model, d_ff, dropout)
# self.size = size
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.dropout3 = nn.Dropout(dropout)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.norm3 = nn.LayerNorm(d_model)
self.norm4 = nn.LayerNorm(d_model)
self.norm_before = norm_before
def with_pos_embed(self, tensor, pos):
return tensor if pos is None else tensor + pos
def forward_pre(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
x2 = self.norm1(x)
q = k = self.with_pos_embed(x2, x_pos)
x2 = self.self_attn(q, k, x2, mask)
x = x + self.dropout1(x2)
x2 = self.norm2(x)
y2 = self.norm3(y)
x2 = self.cross_attn(self.with_pos_embed(
x2, x_pos), self.with_pos_embed(y2, y_pos), y2, mask)
x = x + self.dropout2(x2)
x2 = self.norm4(x)
x2 = self.feed_forward(x2)
out = x + self.dropout3(x2)
return out
def forward_post(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
q = k = self.with_pos_embed(x, x_pos)
x2 = self.self_attn(q, k, x, mask)
x = x + self.dropout1(x2)
x = self.norm1(x)
y = self.norm2(y)
x2 = self.cross_attn(self.with_pos_embed(
x, x_pos), self.with_pos_embed(y, y_pos), y, mask)
x = x + self.dropout2(x2)
x = self.norm3(x)
x2 = self.feed_forward(x)
x = x + self.dropout3(x2)
out = self.norm4(x)
return out
def forward(self, x, y, x_pos, y_pos, mask=None):
if self.norm_before:
return self.forward_pre(x, y, x_pos, y_pos, mask)
else:
return self.forward_post(x, y, x_pos, y_pos, mask)
class Encoder_selff(nn.Module):
"Encoder is made up of self-attn and feed forward"
def __init__(self, n_head, d_model, d_ff, dropout, norm_before=True):
super(Encoder_selff, self).__init__()
self.cross_attn = MultiHeadAttention(n_head, d_model, dropout)
self.feed_forward = PositionwiseFeedForward(d_model, d_ff, dropout)
# self.size = size
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.norm1 = nn.LayerNorm(d_model)
# self.norm2 = nn.LayerNorm(d_model)
self.norm3 = nn.LayerNorm(d_model)
self.norm_before = norm_before
def with_pos_embed(self, tensor, pos):
return tensor if pos is None else tensor + pos
def forward_pre(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
x2 = self.norm1(x)
y2 = self.norm3(y)
x2 = self.cross_attn(self.with_pos_embed(
x2, x_pos), self.with_pos_embed(y2, y_pos), y, mask)
x = x + self.dropout1(x2)
return x
def forward_post(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
q = k = self.with_pos_embed(x, x_pos)
x2 = self.cross_attn(q, k, y, mask)
x = x + self.dropout1(x2)
x = self.norm1(x)
x2 = self.feed_forward(x)
x = x + self.dropout2(x2)
out = self.norm2(x)
return x
def forward(self, x, y, x_pos, y_pos, mask=None):
if self.norm_before:
return self.forward_pre(x, y, x_pos, y_pos, mask)
else:
return self.forward_post(x, y, x_pos, y_pos, mask)
class Encoder(nn.Module):
"Encoder is made up of self-attn and feed forward"
def __init__(self, n_head, d_model, d_ff, dropout, norm_before=True):
super(Encoder, self).__init__()
self.cross_attn = MultiHeadAttention(n_head, d_model, dropout)
self.feed_forward = PositionwiseFeedForward(d_model, d_ff, dropout)
# self.size = size
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.norm3 = nn.LayerNorm(d_model)
self.norm_before = norm_before
def with_pos_embed(self, tensor, pos):
return tensor if pos is None else tensor + pos
def forward_pre(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
x2 = self.norm1(x)
y2 = self.norm3(y)
x2 = self.cross_attn(self.with_pos_embed(
x2, x_pos), self.with_pos_embed(y2, y_pos), y, mask)
x = x + self.dropout1(x2)
x2 = self.norm2(x)
x2 = self.feed_forward(x2)
out = x + self.dropout2(x2)
return out
def forward_post(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
q = k = self.with_pos_embed(x, x_pos)
x2 = self.cross_attn(q, k, y, mask)
x = x + self.dropout1(x2)
x = self.norm1(x)
x2 = self.feed_forward(x)
x = x + self.dropout2(x2)
out = self.norm2(x)
return out
def forward(self, x, y, x_pos, y_pos, mask=None):
if self.norm_before:
return self.forward_pre(x, y, x_pos, y_pos, mask)
else:
return self.forward_post(x, y, x_pos, y_pos, mask)
class Decoder(nn.Module):
"Encoder is made up of self-attn and feed forward"
def __init__(self, n_head, d_model, d_ff, dropout, norm_before=True):
super(Decoder, self).__init__()
self.self_attn = MultiHeadAttention(n_head, d_model, dropout)
self.cross_attn = MultiHeadAttention(n_head, d_model, dropout)
self.feed_forward = PositionwiseFeedForward(d_model, d_ff, dropout)
# self.size = size
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.dropout3 = nn.Dropout(dropout)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.norm3 = nn.LayerNorm(d_model)
self.norm4 = nn.LayerNorm(d_model)
self.norm_before = norm_before
def with_pos_embed(self, tensor, pos):
return tensor if pos is None else tensor + pos
def forward_pre(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
x2 = self.norm1(x)
q = k = self.with_pos_embed(x2, x_pos)
x2 = self.self_attn(q, k, x2, mask)
x = x + self.dropout1(x2)
x2 = self.norm2(x)
y2 = self.norm4(y)
x2 = self.cross_attn(self.with_pos_embed(
x2, x_pos), self.with_pos_embed(y2, y_pos), y, mask)
x = x + self.dropout2(x2)
x2 = self.norm3(x)
x2 = self.feed_forward(x2)
out = x + self.dropout3(x2)
return out
def forward_post(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
q = k = self.with_pos_embed(x, x_pos)
x2 = self.self_attn(q, k, x, mask)
x = x + self.dropout1(x2)
x = self.norm1(x)
x2 = self.cross_attn(self.with_pos_embed(
x, x_pos), self.with_pos_embed(y, y_pos), y, mask)
x = x + self.dropout2(x2)
x = self.norm2(x)
x2 = self.feed_forward(x)
x = x + self.dropout3(x2)
out = self.norm3(x)
return out
def forward(self, x, y, x_pos, y_pos, mask=None):
if self.norm_before:
return self.forward_pre(x, y, x_pos, y_pos, mask)
else:
return self.forward_post(x, y, x_pos, y_pos, mask)
class Decoder2(nn.Module):
"Encoder is made up of self-attn and feed forward"
def __init__(self, n_head, d_model, d_ff, dropout, norm_before=True):
super(Decoder2, self).__init__()
self.self_attn = MultiHeadAttention(n_head, d_model, dropout)
self.cross_attn = MultiHeadAttention(n_head, d_model, dropout)
self.feed_forward = PositionwiseFeedForward(d_model, d_ff, dropout)
# self.size = size
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.dropout3 = nn.Dropout(dropout)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.norm3 = nn.LayerNorm(d_model)
self.norm4 = nn.LayerNorm(d_model)
self.norm_before = norm_before
def with_pos_embed(self, tensor, pos):
return tensor if pos is None else tensor + pos
def forward_pre(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
x2 = self.norm1(x)
y2 = self.norm2(y)
x2 = self.cross_attn(self.with_pos_embed(
x2, x_pos), self.with_pos_embed(y2, y_pos), y, mask)
x = x + self.dropout1(x2)
x2 = self.norm3(x)
q = k = self.with_pos_embed(x2, x_pos)
x2 = self.self_attn(q, k, x2, mask)
x = x + self.dropout2(x2)
x2 = self.norm4(x)
x2 = self.feed_forward(x2)
out = x + self.dropout3(x2)
return out
def forward_post(self, x, y, x_pos, y_pos, mask=None):
"for conenctions"
x2 = self.cross_attn(self.with_pos_embed(
x, x_pos), self.with_pos_embed(y, y_pos), y, mask)
x = x + self.dropout1(x2)
x = self.norm1(x)
q = k = self.with_pos_embed(x, x_pos)
x2 = self.self_attn(q, k, x, mask)
x = x + self.dropout2(x2)
x = self.norm2(x)
x2 = self.feed_forward(x)
x = x + self.dropout3(x2)
out = self.norm3(x)
return out
def forward(self, x, y, x_pos, y_pos, mask=None):
if self.norm_before:
return self.forward_pre(x, y, x_pos, y_pos, mask)
else:
return self.forward_post(x, y, x_pos, y_pos, mask)
class MultiHeadAttention(nn.Module):
def __init__(self, h, d_model, dropout=0.1):
"Take in model size and number of heads."
super(MultiHeadAttention, self).__init__()
assert d_model % h == 0
# we assume d_v always equals d_k
self.d_k = d_model // h
self.h = h
self.linears = clones(nn.Linear(d_model, d_model), 4)
self.dropout = nn.Dropout(p=dropout)
def forward(self, query, key, value, mask=None):
if mask is not None:
# same mask applied to all h heads.
mask = mask.unsqueeze(1)
nbatches = query.size(0)
# 1) do all the linear projection in batch from d_model => h * d_k
query, key, value = [l(x).view(nbatches, -1, self.h, self.d_k).transpose(1, 2)
for l, x in zip(self.linears, (query, key, value))]
# 2) Apply attention on all the projected vectors in batch.
x, attn_weights = attention(
query, key, value, mask=mask, dropout=self.dropout)
# 3) concat using a view and apply a final linear
x = x.transpose(1, 2).contiguous().view(
nbatches, -1, self.h * self.d_k)
return self.linears[-1](x)
def attention(query, key, value, mask=None, dropout=None):
"Compute Scaled Dot Product Attention"
d_k = query.size(-1)
scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask == 0, float('-inf'))
p_attn = F.softmax(scores, dim=-1)
if dropout is not None:
p_attn = dropout(p_attn)
return torch.matmul(p_attn, value), p_attn
class Swish(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x)
class PositionwiseFeedForward(nn.Module):
def __init__(self, d_model, d_ff, dropout=0.1):
super(PositionwiseFeedForward, self).__init__()
self.w_1 = nn.Linear(d_model, d_ff)
self.w_2 = nn.Linear(d_ff, d_model)
self.dropout = nn.Dropout(dropout)
self.activation = Swish() # nn.ReLU()
def forward(self, x):
return self.w_2(self.dropout(self.activation(self.w_1(x))))
class SublayerConnection(nn.Module):
"""
A residual connection followed by a layer norm.
Note for code simplicity the norm is first as opposed to last.
"""
def __init__(self, size, dropout):
super(SublayerConnection, self).__init__()
self.norm = LayerNorm(size)
self.dropout = nn.Dropout(dropout)
def forward(self, x, sublayer):
"Apply residual connection to any sublayer with the same size."
return x + self.dropout(sublayer(self.norm(x)))
class LayerNorm(nn.Module):
"Construct a layernorm module."
def __init__(self, features, eps=1e-6):
super(LayerNorm, self).__init__()
self.a_2 = nn.Parameter(torch.ones(features))
self.b_2 = nn.Parameter(torch.zeros(features))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
return self.a_2 * (x - mean) / (std + self.eps) + self.b_2
def weights_init_kaiming(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
nn.init.kaiming_normal_(m.weight, a=0, mode='fan_out')
nn.init.constant_(m.bias, 0.0)
elif classname.find('Conv') != -1:
nn.init.kaiming_normal_(m.weight, a=0, mode='fan_in')
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
elif classname.find('BatchNorm') != -1:
if m.affine:
nn.init.constant_(m.weight, 1.0)
nn.init.constant_(m.bias, 0.0)
def weights_init_classifier(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
nn.init.normal_(m.weight, std=0.001)
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
def clones(module, N):
"Produce N identical layers."
return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])
def convert_weights(state_dict):
"""convert wights when load model."""
tmp_weights = OrderedDict()
for key, params in iteritems(state_dict['model']):
tmp_weights[key] = params
return tmp_weights
class VI_module_w(nn.Module):
def __init__(self, args):
super(VI_module_w, self).__init__()
self.args = args
self.device = f"cuda:{self.args.gpu}"
ft_clip = ['ln_final', 'text_projection']
# load_clip_to_cpu('RN50') clip.load("RN50")
clip_model, _ = clip.load('RN50', device=self.device)
self.image_encoder = clip_model.encode_image
self.text_encoder = clip_model.encode_text
txt_split = 'Photo' # 'Photo' Face, Person
self.classnames = CLASSES
self.templates = CUSTOM_TEMPLATES[txt_split]
self.feature_dim = 1024
self.W = torch.nn.Linear(
self.feature_dim, self.feature_dim, bias=False).to(self.device)
with torch.no_grad():
torch.nn.init.orthogonal_(self.W.weight)
@staticmethod
def set_finetune_parameters(model, ft_layer):
for n, p in model.named_parameters():
if any([n.startswith(l) for l in ft_layer]) or ft_layer == 'all':
p.requires_grad = True
else:
p.requires_grad = False
def forward(self, images_context, labels_cat):
text_inputs = torch.cat(
[clip.tokenize(self.templates.format(c)) for c in self.classnames]) # 26 * 77
text_inputs = text_inputs.to(self.device).detach()
with torch.no_grad():
text_features = self.text_encoder(text_inputs)
text_features = text_features.float()
text_features = text_features / \
text_features.norm(dim=-1, keepdim=True)
with torch.no_grad():
image_features = self.image_encoder(images_context)
image_features = image_features.float()
image_features = image_features / \
image_features.norm(dim=-1, keepdim=True)
bs = image_features.shape[0]
mapped_image_features = self.W(image_features)
# mapped_image_features.expand(len(self.classnames),bs,self.feature_dim)
target_image_features = torch.transpose(mapped_image_features.expand(
len(self.classnames), bs, self.feature_dim), 0, 1)
target_text_features = text_features.expand(
bs, text_features.shape[0], text_features.shape[1])
return target_image_features, target_text_features
class model_GWT(nn.Module):
def __init__(self, args):
super(model_GWT, self).__init__()
self.args = args
h, d_model, d_ff, dropout = 4, 256, 1024, 0.1
# h, d_model, d_ff, dropout = 8, 512, 2048, 0.1
self.num_grid = 7
self.num_grid_gwt = 7 # 2 4 8
num_query = 16
ft_layers = 'all' # ['layer4', 'fc']
model = torchmodels.resnet50(pretrained=True)
self.set_finetune_parameters(model, ft_layers)
model_head = nn.Sequential(model.conv1, model.bn1, model.relu, model.maxpool,
model.layer1, model.layer2, model.layer3, model.layer4)
self.head_proj = nn.Conv2d(
model.fc.in_features, d_model, kernel_size=1)
model = torchmodels.resnet50(pretrained=True)
self.set_finetune_parameters(model, ft_layers)
model_body = nn.Sequential(model.conv1, model.bn1, model.relu, model.maxpool,
model.layer1, model.layer2, model.layer3, model.layer4)
self.body_proj = nn.Conv2d(
model.fc.in_features, d_model, kernel_size=1)
model = torchmodels.resnet50(pretrained=True)
self.set_finetune_parameters(model, ft_layers)
model_context = nn.Sequential(model.conv1, model.bn1, model.relu, model.maxpool,
model.layer1, model.layer2, model.layer3, model.layer4)
self.context_proj = nn.Conv2d(
model.fc.in_features, d_model, kernel_size=1)
# build layers
self.backbone = nn.ModuleDict({
'head': model_head,
'body': model_body,
'context': model_context,
})
self.en_pos = PositionEmbeddingLearned(7, d_model // 2)
self.query_embedding = nn.Embedding(num_query, d_model)
self.encoder_head = clones(Encoder_selff(
h, d_model, d_ff, dropout, norm_before=True), args.num_block1)
self.encoder_body = clones(Encoder_selff(
h, d_model, d_ff, dropout, norm_before=True), args.num_block1)
self.encoder_ctx = clones(Encoder_selff(
h, d_model, d_ff, dropout, norm_before=True), args.num_block1)
self.decoder_head = clones(
Decoder(h, d_model, d_ff, dropout, norm_before=True), args.num_block2)
self.decoder_body = clones(
Decoder(h, d_model, d_ff, dropout, norm_before=True), args.num_block2)
self.decoder_ctx = clones(
Decoder(h, d_model, d_ff, dropout, norm_before=True), args.num_block2)
mid_dim = 2048
mid_dim_gwt = 256
self.en_pos_gwt = PositionEmbeddingLearned(
self.num_grid_gwt, mid_dim_gwt // 2)
num_block1, num_block2 = 3, 3
h = 4
self.query_embedding_gwt = nn.Embedding(num_query, mid_dim_gwt)
self.encoder1 = clones(
Encoder(h, mid_dim_gwt, d_ff, dropout, norm_before=True), num_block1)
self.encoder2 = clones(
Encoder(h, mid_dim_gwt, d_ff, dropout, norm_before=True), num_block1)
self.encoder3 = clones(
Encoder(h, mid_dim_gwt, d_ff, dropout, norm_before=True), num_block1)
self.num_class = args.num_class
self.device = torch.device('cuda:{}'.format(args.gpu[0]))
# 2025_07_05
self.cls_HFE_ctx = nn.Sequential(nn.GELU(), nn.Linear(
7*7*d_model, d_model), nn.GELU(), nn.LayerNorm(d_model), nn.Linear(d_model, args.num_class))
self.cls_HFE_body = nn.Sequential(nn.GELU(), nn.Linear(
7*7*d_model, d_model), nn.GELU(), nn.LayerNorm(d_model), nn.Linear(d_model, args.num_class))
self.cls_HFE_head = nn.Sequential(nn.GELU(), nn.Linear(
7*7*d_model, d_model), nn.GELU(), nn.LayerNorm(d_model), nn.Linear(d_model, args.num_class))
self.cls_global_all = nn.Sequential(nn.GELU(),
nn.LayerNorm(4*d_model),
nn.Linear(4*d_model, d_model),
nn.GELU(),
nn.LayerNorm(d_model),
nn.Linear(d_model, d_model),
nn.GELU(),
nn.LayerNorm(d_model),
nn.Linear(d_model, args.num_class),
)
self.loss_func = DiscreteLoss(
self.args.loss_type, 'dynamic', self.device).to(self.device)
self.ln_after = nn.LayerNorm(mid_dim_gwt)
self.shar_after = nn.Linear(mid_dim_gwt * num_query, mid_dim)
self.cls_en_all = nn.Sequential(nn.GELU(), nn.LayerNorm(
mid_dim), nn.Linear(mid_dim, args.num_class))
ft_clip = ['ln_final', 'text_projection']
clip_model, _ = clip.load('RN50', device=self.device)
for param in clip_model.parameters():
param.requires_grad = False
self.image_encoder = clip_model.encode_image
self.text_encoder = clip_model.encode_text
txt_split = 'Photo'
self.classnames = CLASSES
self.templates = CUSTOM_TEMPLATES[txt_split]
# build layers
self.feature_dim = 1024
self.W = torch.nn.Linear(
self.feature_dim, self.feature_dim, bias=False).to(self.device)
self.W.weight = nn.Parameter(torch.load(
'./checkpoints/VI_weights/w.pth')['model']["W.weight"])
self.VI_ffn = nn.Sequential(nn.GELU(), nn.Linear(self.feature_dim*args.num_class, self.feature_dim*len(CLASSES) // 4), nn.GELU(
), nn.Linear(self.feature_dim*args.num_class//4, mid_dim_gwt), nn.GELU(), nn.Linear(mid_dim_gwt, args.num_class))
@staticmethod
def set_finetune_parameters(model, ft_layer):
for n, p in model.named_parameters():
if any([n.startswith(l) for l in ft_layer]) or ft_layer == 'all':
p.requires_grad = True
else:
p.requires_grad = False
# if 'bn' in n or 'batchnorm' in n:
# p.track_running_stats = False
@staticmethod
def set_finetune_parameters_off(model, ft_layer):
for n, p in model.named_parameters():
if any([n.startswith(l) for l in ft_layer]) or ft_layer == 'all':
p.requires_grad = False
else:
p.requires_grad = True
# if 'bn' in n or 'batchnorm' in n:
# p.track_running_stats = False
@staticmethod
def set_finetune_parameters_pret_all(self, turn='on'):
if turn == 'off':
self.set_finetune_parameters_off(self.backbone['head'], 'all')
self.set_finetune_parameters_off(self.backbone['body'], 'all')
self.set_finetune_parameters_off(self.backbone['context'], 'all')
self.set_finetune_parameters_off(self.context_proj, 'all')
self.set_finetune_parameters_off(self.body_proj, 'all')
self.set_finetune_parameters_off(self.head_proj, 'all')
self.set_finetune_parameters_off(self.query_embedding, 'all')
if turn == 'on':
self.set_finetune_parameters(self.backbone['head'], 'all')
self.set_finetune_parameters(self.backbone['body'], 'all')
self.set_finetune_parameters(self.backbone['context'], 'all')
self.set_finetune_parameters(self.context_proj, 'all')
self.set_finetune_parameters(self.body_proj, 'all')
self.set_finetune_parameters(self.head_proj, 'all')
self.set_finetune_parameters(self.query_embedding, 'all')
@staticmethod
def bilinear_interpolate(im, x, y):
"""
Args:
im: (H, W, C) [y, x]
x: (N)
y: (N)
Returns:
"""
x0 = torch.floor(x).long()
x1 = x0 + 1
y0 = torch.floor(y).long()
y1 = y0 + 1
x0 = torch.clamp(x0, 0, im.shape[1] - 1)
x1 = torch.clamp(x1, 0, im.shape[1] - 1)
y0 = torch.clamp(y0, 0, im.shape[0] - 1)
y1 = torch.clamp(y1, 0, im.shape[0] - 1)
Ia = im[y0, x0]
Ib = im[y1, x0]
Ic = im[y0, x1]
Id = im[y1, x1]
wa = (x1.type_as(x) - x) * (y1.type_as(y) - y)
wb = (x1.type_as(x) - x) * (y - y0.type_as(y))
wc = (x - x0.type_as(x)) * (y1.type_as(y) - y)
wd = (x - x0.type_as(x)) * (y - y0.type_as(y))
ans = torch.t((torch.t(Ia) * wa)) + torch.t(torch.t(Ib) * wb) + \
torch.t(torch.t(Ic) * wc) + torch.t(torch.t(Id) * wd)
return ans
def get_roi_points(self, rois, grid_size):
"""
Args:
rois: [B, 4] (x1y1x2y2)
"""
local_grid_points = self.get_dense_grid_points(
rois[:, [3, 2]] - rois[:, [1, 0]], rois.shape[0], grid_size) # [B, 7*7, yx]
center_points = (rois[:, [3, 2]] + rois[:, [1, 0]]) / 2. # [B, yx]
local_grid_points += center_points.unsqueeze(dim=1)
return local_grid_points # [B, 7*7, yx]
@staticmethod
def get_dense_grid_points(rois, batch_size, grid_size):
faked_features = rois.new_ones((grid_size, grid_size)) # [n, n]
dense_idx = torch.nonzero(faked_features, as_tuple=False)[
None] # (N, 2) [y_idx, x_idx]
dense_idx = dense_idx.repeat(batch_size, 1, 1).float() # (B, 7x7, 2)
grid_points = (dense_idx + 0.5) / grid_size * \
rois.unsqueeze(dim=1) - (rois.unsqueeze(dim=1) / 2) # (B, 7x7, 2)
return grid_points
def vocabulary_informed_output(self, img):
image_features = self.image_encoder(img)
image_features = image_features.float()
image_features = image_features / \
image_features.norm(dim=-1, keepdim=True)
mapped_features = self.W(image_features)
mapped_features = torch.unsqueeze(mapped_features, 1)
mapped_features = mapped_features.expand(-1, 26, -1).clone()
mapped_features_flatten = mapped_features.flatten(start_dim=1)
out_VI = self.VI_ffn(mapped_features_flatten)
out_VI_feature = self.VI_ffn[:-1](mapped_features_flatten)
return out_VI, out_VI_feature
def forward(self, images_context, images_body, images_head, coord_body, coord_head, labels_cat):
ret = {}
bs = images_body.shape[0]
x_head = self.head_proj(
self.backbone['head'](images_head)) # [B, C, H, W]
x_body = self.body_proj(
self.backbone['body'](images_body)) # [B, C, H, W]
x_context = self.context_proj(
self.backbone['context'](images_context)) # [B, C, H, W]
x_head = x_head.flatten(start_dim=2).transpose(
1, 2) # [B, HW, C] [32, 49, 256]
x_body = x_body.flatten(start_dim=2).transpose(
1, 2) # [B, HW, C] same as x_head
x_context = x_context.flatten(start_dim=2).transpose(
1, 2) # [B, HW, C] same as x_head
query_pos = self.query_embedding.weight[None].repeat(bs, 1, 1)
context_pos = self.en_pos(self.num_grid, self.num_grid, x_context).permute(
0, 2, 3, 1) # [B, H, W, C]
body_pos = self.get_roi_points(
coord_body, grid_size=self.num_grid) / 32 # [B, N, 2]
head_pos = self.get_roi_points(
coord_head, grid_size=self.num_grid) / 32 # [B, N, 2]
head_x = head_pos[..., 1].view(-1) # [BN]
head_y = head_pos[..., 0].view(-1) # [BN]
head_pos = self.bilinear_interpolate(context_pos[0], head_x, head_y).view(
bs, self.num_grid ** 2, -1) # [B, N, C]
body_x = body_pos[..., 1].view(-1) # [BN]
body_y = body_pos[..., 0].view(-1) # [BN]
body_pos = self.bilinear_interpolate(context_pos[0], body_x, body_y).view(
bs, self.num_grid ** 2, -1) # [B, N, C]
context_pos = context_pos.view(bs, self.num_grid ** 2, -1)
# 2025-07-18
cls_HFE_head_original = copy.deepcopy(
dict(self.cls_HFE_head.named_parameters()))
cls_HFE_body_original = copy.deepcopy(
dict(self.cls_HFE_body.named_parameters()))
cls_HFE_ctx_original = copy.deepcopy(
dict(self.cls_HFE_ctx.named_parameters()))
for block in self.encoder1:
# [B, HW, C] [B, 49, 256]
x_head = block(x_head, x_context, head_pos, context_pos)
for block in self.encoder2:
# [B, HW, C] [B, 49, 256]
x_body = block(x_body, x_context, body_pos, context_pos)
for block in self.encoder3:
x_context = block(x_context, x_context, context_pos,
context_pos) # [B, HW, C] [B, 49, 256]
x_head = self.ln_after(x_head)
x_body = self.ln_after(x_body)
x_context = self.ln_after(x_context)
ret_emo_process_all = []
# label_VI = []
VI_class = []
loss_head_all = []
loss_body_all = []
loss_ctx_all = []
dim = x_head.shape[-1]
x_head_f_total = x_head.flatten(start_dim=1) # [B, 49 * 256]
x_body_f_total = x_body.flatten(start_dim=1)
x_ctx_f_total = x_context.flatten(start_dim=1)
label_head_total = self.cls_HFE_head(x_head_f_total) # [B, 26]
# [B, 256]
x_head_feature_total = self.cls_HFE_head[:-1](x_head_f_total)
label_body_total = self.cls_HFE_body(x_body_f_total) # [B, 26]
# [B, 256]
x_body_feature_total = self.cls_HFE_body[:-1](x_body_f_total)
label_ctx_total = self.cls_HFE_ctx(x_ctx_f_total) # [B, 26]
x_ctx_feature_total = self.cls_HFE_ctx[:-1](x_ctx_f_total) # [B, 256]
# create temp_feature to store the refined feature
# torch.zeros(bs, dim, requires_grad=False).cuda(device=int(self.args.gpu)) # self.cls_HFE_head[:-1](x_head_f) # [B, 256]
x_head_feature_refined = x_head_feature_total.clone()
# torch.zeros(bs, dim, requires_grad=False).cuda(device=int(self.args.gpu)) # self.cls_HFE_body[:-1](x_body_f) # [B, 256]
x_body_feature_refined = x_body_feature_total.clone()
# torch.zeros(bs, dim, requires_grad=False).cuda(device=int(self.args.gpu)) # self.cls_HFE_ctx[:-1](x_ctx_f) # [B, 256]
x_ctx_feature_refined = x_ctx_feature_total.clone()
VI_out_all, VI_out_feature_all = self.vocabulary_informed_output(
images_context)
ret['label_VI'] = VI_out_all
ret['label_head'] = label_head_total
ret['label_body'] = label_body_total
ret['label_ctx'] = label_ctx_total
# take the 1, range from batch_size (bs)
for bsn in range(bs):
# x_head.flatten(start_dim=1) # [B, 49 * 256]
x_head_f = x_head_f_total[bsn].unsqueeze(0)
label_head = label_head_total[bsn].unsqueeze(0) # self.cls_HFE_head(x_head_f) # [B, 26]
x_body_f = x_body_f_total[bsn].unsqueeze(0) # x_body.flatten(start_dim=1)
label_body = label_body_total[bsn].unsqueeze(0) # self.cls_HFE_body(x_body_f)
x_ctx_f = x_ctx_f_total[bsn].unsqueeze(0) # x_context.flatten(start_dim=1)
label_ctx = label_ctx_total[bsn].unsqueeze(0) # self.cls_HFE_ctx(x_ctx_f)
VI_out = VI_out_all[bsn].unsqueeze(0) # [B, n, c], [B, n]