-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
971 lines (893 loc) · 48.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
'''
Neural Network models, implemented by PyTorch
'''
import time
import numpy as np
import timm.models.vision_transformer
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import math
import yaml
config = yaml.safe_load(open('./config.yaml', encoding='utf-8'))
# RNNs模型基类,主要是用于指定参数和cell类型
class BaseModel(nn.Module):
def __init__(self, inputDim, hiddenNum, outputDim, layerNum, cell, use_cuda=False):
super(BaseModel, self).__init__()
self.hiddenNum = hiddenNum
self.inputDim = inputDim
self.outputDim = outputDim
self.layerNum = layerNum
self.use_cuda = use_cuda
if cell == "RNN":
self.cell = nn.RNN(input_size=self.inputDim, hidden_size=self.hiddenNum,
num_layers=self.layerNum, dropout=0.0,
nonlinearity="tanh", batch_first=True, )
if cell == "LSTM":
self.cell = nn.LSTM(input_size=self.inputDim, hidden_size=self.hiddenNum,
num_layers=self.layerNum, dropout=0.0,
batch_first=True, )
self.fc = nn.Linear(1024, self.outputDim)
# 标准RNN模型
class RNNModel(BaseModel):
def __init__(self, inputDim, hiddenNum, outputDim, layerNum, cell, use_cuda):
super(RNNModel, self).__init__(inputDim, hiddenNum, outputDim, layerNum, cell, use_cuda)
def forward(self, x):
batchSize = x.size(0)
h0 = Variable(torch.zeros(self.layerNum * 1, batchSize, self.hiddenNum))
if self.use_cuda:
h0 = h0.cuda()
rnnOutput, hn = self.cell(x, h0)
hn = hn.view(batchSize, self.hiddenNum)
fcOutput = self.fc(hn)
return fcOutput
# LSTM模型
class LSTMModel(BaseModel):
def __init__(self, inputDim, hiddenNum, outputDim, layerNum, cell, use_cuda, feature_dim=1):
super(LSTMModel, self).__init__(inputDim, hiddenNum, outputDim, layerNum, cell, use_cuda)
self.input2feature = nn.Sequential(nn.Linear(inputDim, 512), nn.ReLU(), nn.Linear(512, feature_dim), nn.ReLU())
self.alpha = nn.Sequential(nn.Linear(feature_dim, 1), nn.Sigmoid()) # act on single frame
self.beta = nn.Sequential(nn.Linear(feature_dim + hiddenNum, 1), nn.Sigmoid()) # act on single sample
self.lastL = nn.Linear(feature_dim + hiddenNum, outputDim)
def forward(self, x, video_feature: bool = False):
# x shape (bs, seq_len, input_dim)
batchSize = x.size(0)
h0 = Variable(torch.zeros(self.layerNum * 1, batchSize, self.hiddenNum))
c0 = Variable(torch.zeros(self.layerNum * 1, batchSize, self.hiddenNum))
if self.use_cuda:
h0 = h0.cuda()
c0 = c0.cuda()
# fcOutput = self.fc(hn)
rnnOutput, hn = self.cell(x, (h0, c0))
hn = hn[0][-1].view(batchSize, 1, self.hiddenNum) # video level feature
hns = torch.tile(hn, (1, x.size(1), 1)) # (bs, seq_len, hiddenNum)
x = self.input2feature(x) # (bs, seq_len, feature_dim)
aggregation = torch.cat((x, hns), dim=2) # (bs, seq_len, hiddenNum + feature_dim)
alphas = self.alpha(x) # (bs, seq_len, 1) frame level attention weights
betas = self.beta(aggregation) # (bs, seq_len, 1)
alpha_betas = torch.mul(alphas, betas) # (bs, seq_len, 1)
weighted_sum = torch.sum(torch.mul(alpha_betas, aggregation), dim=1) # (bs, hiddenNum + feature_dim)
features = weighted_sum / torch.sum(alpha_betas, dim=1) # (bs, hiddenNum + inputDim)
output = self.lastL(features)
return output
class ORILSTM(BaseModel):
def __init__(self, inputDim, hiddenNum, outputDim, layerNum, cell, use_cuda):
super(ORILSTM, self).__init__(inputDim, hiddenNum, outputDim, layerNum, cell, use_cuda)
def forward(self, x):
batchSize = x.size(0)
h0 = Variable(torch.zeros(self.layerNum * 1, batchSize, self.hiddenNum))
c0 = Variable(torch.zeros(self.layerNum * 1, batchSize, self.hiddenNum))
if self.use_cuda:
h0 = h0.cuda()
c0 = c0.cuda()
rnnOutput, hn = self.cell(x, (h0, c0))
hn = hn[0].view(batchSize, self.hiddenNum)
fcOutput = self.fc(hn)
return fcOutput
class FollowLSTM(BaseModel):
def __init__(self, inputDim, hiddenNum, outputDim, layerNum, cell, use_cuda):
super(FollowLSTM, self).__init__(inputDim, hiddenNum, outputDim, layerNum, cell, use_cuda)
self.alpha = nn.Sequential(nn.Linear(512,1),nn.Sigmoid())
self.beta = nn.Sequential(nn.Linear(1024,1),nn.Sigmoid())
def forward(self, x ):
alphas = self.alpha(x)
batchSize = x.size(0)
h0 = Variable(torch.zeros(self.layerNum * 1, batchSize, self.hiddenNum))
c0 = Variable(torch.zeros(self.layerNum * 1, batchSize, self.hiddenNum))
if self.use_cuda:
h0 = h0.cuda()
c0 = c0.cuda()
rnnOutput, hn = self.cell(x, (h0, c0))
hn = hn[0].view(batchSize, self.hiddenNum)
hx = torch.cat((x,hn[:,None,:].repeat(1,50,1)),dim=2)
betas = self.beta(hx)
hx = torch.mul(hx,alphas*betas).sum(1)/torch.sum(alphas*betas,dim=1)
fcOutput = self.fc(hx)
return fcOutput
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU()
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_planes, planes, stride=1):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3,
stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, self.expansion *
planes, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(self.expansion * planes)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion * planes:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, self.expansion * planes,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(self.expansion * planes)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = F.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, layers):
self.inplanes = 64
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d(1)
self.num_features = 512
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x=''):
f = self.conv1(x)
f = self.bn1(f)
f = self.relu(f)
f = self.maxpool(f)
f = self.layer1(f)
f = self.layer2(f)
f = self.layer3(f)
f = self.layer4(f)
f = self.avgpool(f)
f = f.squeeze(3).squeeze(2) # f[1, 512, 1, 1] ---> f[1, 512]
return f
def ResNet18():
return ResNet(BasicBlock, [2, 2, 2, 2])
def conv3x3(in_planes, out_planes, stride=1):
"3x3 convolution with padding"
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)
class BasicBlock_AT(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock_AT, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU()
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet_AT(nn.Module):
def __init__(self, block, layers, num_classes=1000, end2end=True, at_type=''):
self.inplanes = 64
self.end2end = end2end
super(ResNet_AT, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d(1)
self.dropout = nn.Dropout(0.5)
self.dropout2 = nn.Dropout(0.6)
self.alpha = nn.Sequential(nn.Linear(512, 1),
nn.Sigmoid())
self.beta = nn.Sequential(nn.Linear(1024, 1),
nn.Sigmoid())
self.pred_fc1 = nn.Linear(512, 7)
self.pred_fc2 = nn.Linear(1024, 7)
self.at_type = at_type
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x='', phrase='train', AT_level='first_level', vectors='', vm='', alphas_from1='',
index_matrix=''):
vs = []
alphas = []
assert phrase == 'train' or phrase == 'eval'
assert AT_level == 'first_level' or AT_level == 'second_level' or AT_level == 'pred'
if phrase == 'train':
num_pair = 3
for i in range(num_pair):
f = x[:, :, :, :, i] # x[128,3,224,224]
f = self.conv1(f)
f = self.bn1(f)
f = self.relu(f)
f = self.maxpool(f)
f = self.layer1(f)
f = self.layer2(f)
f = self.layer3(f)
f = self.layer4(f)
f = self.avgpool(f)
f = f.squeeze(3).squeeze(2) # f[1, 512, 1, 1] ---> f[1, 512]
# MN_MODEL(first Level)
vs.append(f)
alphas.append(self.alpha(self.dropout(f)))
vs_stack = torch.stack(vs, dim=2)
alphas_stack = torch.stack(alphas, dim=2)
if self.at_type == 'self-attention':
vm1 = vs_stack.mul(alphas_stack).sum(2).div(alphas_stack.sum(2))
if self.at_type == 'self_relation-attention':
vm1 = vs_stack.mul(alphas_stack).sum(2).div(alphas_stack.sum(2))
betas = []
for i in range(len(vs)):
vs[i] = torch.cat([vs[i], vm1], dim=1)
betas.append(self.beta(self.dropout(vs[i])))
cascadeVs_stack = torch.stack(vs, dim=2)
betas_stack = torch.stack(betas, dim=2)
output = cascadeVs_stack.mul(betas_stack * alphas_stack).sum(2).div((betas_stack * alphas_stack).sum(2))
if self.at_type == 'self-attention':
vm1 = self.dropout(vm1)
pred_score = self.pred_fc1(vm1)
if self.at_type == 'self_relation-attention':
output = self.dropout2(output)
pred_score = self.pred_fc2(output)
return pred_score
if phrase == 'eval':
if AT_level == 'first_level':
f = self.conv1(x)
f = self.bn1(f)
f = self.relu(f)
f = self.maxpool(f)
f = self.layer1(f)
f = self.layer2(f)
f = self.layer3(f)
f = self.layer4(f)
f = self.avgpool(f)
f = f.squeeze(3).squeeze(2) # f[1, 512, 1, 1] ---> f[1, 512]
# MN_MODEL(first Level)
alphas = self.alpha(self.dropout(f))
return f, alphas
if AT_level == 'second_level':
assert self.at_type == 'self_relation-attention'
vms = index_matrix.permute(1, 0).mm(vm) # [381, 21783] -> [21783,381] * [381,512] --> [21783, 512]
vs_cate = torch.cat([vectors, vms], dim=1)
return vs_cate
if AT_level == 'pred':
if self.at_type == 'self-attention':
pred_score = self.pred_fc1(self.dropout(vm))
return pred_score
''' self-attention; relation-attention '''
def resnet18_at(pretrained=False, **kwargs):
# Constructs base a ResNet-18 model.
model = ResNet_AT(BasicBlock_AT, [2, 2, 2, 2], **kwargs)
return model
class EmoTransformer(nn.Module):
def __init__(self, input, nhead, num_layers, batch_first, output_dim): # bs first True
'''
1. embedding layer is may be needed, because more parameters has more fitting capacity.
2. position encoding layer has one more position for cls token, thus, concatenation cls token to input follows embedding.
3. transformer does not care seq_len.
4. after encoder, cls token will be used for classification.
5. encoder has normalization layer, input data normalizing is no needed?
'''
from torch.nn import TransformerEncoder, TransformerEncoderLayer
from dataProcess import config
super(EmoTransformer, self).__init__()
d_model = config['T_proj_dim']
self.input_embedding = nn.Sequential(nn.Linear(input, d_model))
# pe = torch.zeros(25 + 1, d_model) # add cls token
# position = torch.arange(0, 26).unsqueeze(1)
# div_term = torch.exp(torch.arange(0, d_model, 2) * -(math.log(10000.0) / d_model))
# pe[:, 0::2] = torch.sin(position * div_term) # sin
# pe[:, 1::2] = torch.cos(position * div_term) # cos
self.cls_token = nn.Parameter(torch.randn(1, 1, d_model))
# self.pe = pe.unsqueeze(0)
self.pos_embedding = nn.Parameter(torch.randn(1, config['window_size'] + 1, d_model))
self.encoder_layer = TransformerEncoderLayer(d_model=d_model, nhead=nhead, batch_first=batch_first,
dim_feedforward=config['T_forward_dim'],
activation=config['T_activation'])
self.transformer_encoder = TransformerEncoder(self.encoder_layer, num_layers=num_layers)
self.pred = nn.Sequential(nn.Linear(d_model, output_dim))
self.pred1 = nn.Sequential(nn.Linear(d_model, output_dim))
def forward(self, x, att_mask: bool = False, phrase='train', v_s=''):
if phrase == 'train':
tem_m = torch.sum(x, dim=2) # (bs, seq_len)
x = self.input_embedding(x) # (bs,seq_len,d_model)
cls_tokens = torch.tile(self.cls_token, (x.shape[0], 1, 1)) # (bs,1,d_model)
cls_x = torch.cat((cls_tokens, x), dim=1) # (bs,seq_len + 1,d_model)
# make a key padding mask matrix
mask_m = tem_m == 0 # (bs, seq_len)
cls_m = torch.zeros((x.shape[0], 1), dtype=torch.bool).cuda()
mask_m = torch.cat((cls_m, mask_m), dim=1) # (bs, seq_len+1)
cls_x += self.pos_embedding
# cls_x = cls_x + Variable(self.pe[:, :cls_x.size(1)],requires_grad=False).cuda() # (bs,seq_len + 1,d_model)
if att_mask:
x_ = self.transformer_encoder(cls_x, src_key_padding_mask=mask_m) # (bs,seq_len + 1,d_model)
self.transformer_encoder.layers
else:
x_ = self.transformer_encoder(cls_x)
v_feature = x_[:, 0, :] # (bs,1,d_model)
return self.pred1(v_feature)
if phrase == 'mlpIn':
output = self.pred(v_s) # (video, 7)
return output
class AutoEncoder(nn.Module):
def __init__(self):
super(AutoEncoder, self).__init__()
self.AE_encoder = nn.Sequential(nn.Linear(204, 128), nn.ReLU(), nn.Linear(128, 32), nn.ReLU(),
nn.Linear(32, config['AE_mid_dim']))
self.AE_decoder = nn.Sequential(nn.Linear(config['AE_mid_dim'], 32), nn.ReLU(), nn.Linear(32, 128), nn.ReLU(),
nn.Linear(128, 204))
def forward(self, x, use_mid: bool = False):
mid = self.AE_encoder(x)
if use_mid:
return mid
output = self.AE_decoder(mid)
return output
class ViLT(nn.Module):
def __init__(self, model_type_num: int, lms_type_dim: int, rgb_type_dim: int, d_model: int, nhead: int,
forward_dim: int, block_num: int, batch_fist: bool):
super(ViLT, self).__init__()
self.lms_type_project_layer = nn.Linear(lms_type_dim, d_model) # (204, embedding_dim)
self.rgb_type_project_layer = nn.Linear(rgb_type_dim, d_model) # (p*p*c, embedding_dim)
self.model_emd = nn.Embedding(model_type_num, d_model) # (model_type_num, embedding_dim)
self.cls_token = nn.Parameter(torch.randn(1, 1, d_model))
self.second_token = nn.Parameter(torch.randn(1, 1, d_model))
self.lms_pos_emb = nn.Parameter(torch.randn(1, config['window_size'] + 1, d_model))
self.rgb_pos_emb = nn.Parameter(torch.randn(1, config['window_size'] + 1, d_model))
self.encoder_layer = nn.TransformerEncoderLayer(d_model=d_model, nhead=nhead, dim_feedforward=forward_dim,
activation=config['ViLT_activation'], batch_first=batch_fist)
self.encoder = nn.TransformerEncoder(self.encoder_layer, block_num)
self.MLP = nn.Sequential(nn.Linear(config['ViLT_embedding_dim'], config['ViLT_embedding_dim']), nn.Tanh(),
nn.Linear(config['ViLT_embedding_dim'], 7)) # pool + classification
def forward(self, x):
x_rgb, x_lms = torch.split(x, 512, dim=2)
lms_emb = self.lms_type_project_layer(x_lms)
rgb_emb = self.rgb_type_project_layer(x_rgb)
input_emb = torch.cat((torch.tile(self.cls_token, dims=(x.shape[0], 1, 1)), lms_emb, torch.tile(self.second_token, dims=(x.shape[0], 1, 1)), rgb_emb), dim=1)
input_emb += torch.cat((self.lms_pos_emb, self.rgb_pos_emb), dim=1)
output_emb = self.encoder(input_emb)
pred = self.MLP(output_emb[:, 0, :])
return pred
class MultiEmoTransformer(nn.Module):
def __init__(self,lms3dpro, rgbpro, input, nhead, num_layers, batch_first, output_dim): # bs first True
'''
1. embedding layer is may be needed, because more parameters has more fitting capacity.
2. position encoding layer has one more position for cls token, thus, concatenation cls token to input follows embedding.
3. transformer does not care seq_len.
4. after encoder, cls token will be used for classification.
5. encoder has normalization layer, input data normalizing is no needed?
'''
from torch.nn import TransformerEncoder, TransformerEncoderLayer
from dataProcess import config
super(MultiEmoTransformer, self).__init__()
d_model = config['T_proj_dim']
self.lms3d_embedding = nn.Sequential(nn.Linear(config['T_lms3d_dim'], config['T_pro_dim']))
self.rgb_embedding = nn.Sequential(nn.Linear(config['T_rgb_dim'], config['T_pro_dim']))
self.input_embedding = nn.Sequential(nn.Linear(config['T_pro_dim']*2, d_model))
self.cls_token = nn.Parameter(torch.randn(1, 1, d_model))
self.pos_embedding = nn.Parameter(torch.randn(1, config['window_size'] + 1, d_model))
self.encoder_layer = TransformerEncoderLayer(d_model=d_model, nhead=nhead, batch_first=batch_first,
dim_feedforward=config['T_forward_dim'],
activation=config['T_activation'])
self.transformer_encoder = TransformerEncoder(self.encoder_layer, num_layers=num_layers)
self.pred = nn.Sequential(nn.Linear(d_model, output_dim))
def forward(self, lms3d, rgb, att_mask: bool = False):
l = self.lms3d_embedding(lms3d)
r = self.rgb_embedding(rgb)
x = torch.cat((l,r),dim=2)
tem_m = torch.sum(x, dim=2) # (bs, seq_len)
x = self.input_embedding(x) # (bs,seq_len,d_model)
cls_tokens = torch.tile(self.cls_token, (x.shape[0], 1, 1)) # (bs,1,d_model)
cls_x = torch.cat((cls_tokens, x), dim=1) # (bs,seq_len + 1,d_model)
# make a key padding mask matrix
mask_m = tem_m == 0 # (bs, seq_len)
cls_m = torch.zeros((x.shape[0], 1), dtype=torch.bool).cuda()
mask_m = torch.cat((cls_m, mask_m), dim=1) # (bs, seq_len+1)
cls_x += self.pos_embedding
# cls_x = cls_x + Variable(self.pe[:, :cls_x.size(1)],requires_grad=False).cuda() # (bs,seq_len + 1,d_model)
if att_mask:
x_ = self.transformer_encoder(cls_x, src_key_padding_mask=mask_m) # (bs,seq_len + 1,d_model)
# self.transformer_encoder.layers
else:
x_ = self.transformer_encoder(cls_x)
v_feature = x_[:, 0, :] # (bs,1,d_model)
return self.pred(v_feature)
from utils.config import cfg
class MAEEncoder(nn.Module):
def __init__(self, embed_dim=1024, depth=24, num_heads=16,
mlp_ratio=4., norm_layer=nn.LayerNorm, norm_pix_loss=False):
super().__init__()
from timm.models.vision_transformer import Block
# --------------------------------------------------------------------------
self.encoder_embed = nn.Linear(cfg.input_len, embed_dim) # bias = True
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
self.pos_embed = nn.Parameter(torch.zeros(1, cfg.seq_len + 1, embed_dim), requires_grad=False) # fixed sin-cos embedding
self.blocks = nn.ModuleList([
Block(embed_dim, num_heads, mlp_ratio, qkv_bias=True, qk_scale=None, norm_layer=norm_layer, drop=0.1)
for i in range(depth)])
self.pred = nn.Linear(embed_dim, cfg.train_out_dim)
self.norm_pix_loss = norm_pix_loss
self.initialize_weights()
def initialize_weights(self):
import numpy as np
from util.pos_embed import get_1d_sincos_pos_embed_from_grid
pos_embed = get_1d_sincos_pos_embed_from_grid(self.pos_embed.shape[-1], np.arange(cfg.seq_len + 1))
self.pos_embed.data.copy_(torch.from_numpy(pos_embed).float().unsqueeze(0))
def forward(self, x):
# with torch.no_grad():
x = self.encoder_embed(x)
# add pos embed w/o cls token
x = x + self.pos_embed[:, 1:, :] # 传播算法
# append cls token
cls_token = self.cls_token + self.pos_embed[:, :1, :]
cls_tokens = cls_token.expand(x.shape[0], -1, -1)
x = torch.cat((cls_tokens, x), dim=1)
# apply Transformer blocks
for blk in self.blocks:
x = blk(x)
return self.pred(x[:,0,:])
class NormalB16ViT(timm.models.vision_transformer.VisionTransformer):
def __init__(self,weights):
# super().__init__(embed_dim=1280,num_heads=16,patch_size=14) # h-14的设置
super().__init__()
if weights is not None:
self.load_from(weights)
else:
pass
# self.lms_porj = nn.Linear(204, 256)
self.emb_proj = nn.Linear(config['T_input_dim'],self.embed_dim) # 1024->768
self.pos_embed_my = nn.Parameter(torch.zeros(1, config['window_size']+1, self.embed_dim))
self.pos_embed_my.data.copy_(self.pos_embed[:,:config['window_size']+1,:]) # cut position embedding
self.pred = nn.Linear(self.embed_dim,config['T_output_dim'])
pass
@torch.no_grad()
def load_from(self, weights):
def _n2p(w, t=True):
if w.ndim == 4 and w.shape[0] == w.shape[1] == w.shape[2] == 1:
w = w.flatten()
if t:
if w.ndim == 4:
w = w.transpose([3, 2, 0, 1])
elif w.ndim == 3:
w = w.transpose([2, 0, 1])
elif w.ndim == 2:
w = w.transpose([1, 0])
return torch.from_numpy(w)
self.pos_embed.data.copy_(torch.from_numpy(weights['Transformer/posembed_input/pos_embedding']))
self.cls_token.data.copy_(torch.from_numpy(weights['cls']))
for l,b in enumerate(self.blocks):
################ln weight######################
b.norm1.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_0/bias']))
b.norm1.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_0/scale']))
b.norm2.bias.data.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_2/bias']))
b.norm2.weight.data.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_2/scale']))
###############mlp weight######################
b.mlp.fc1.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_0/kernel']).t())
b.mlp.fc1.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_0/bias']).t())
b.mlp.fc2.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_1/kernel']).t())
b.mlp.fc2.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_1/bias']).t())
###############attention weight################
b.attn.qkv.weight.copy_(torch.cat([_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/{n}/kernel'], t=False).flatten(1).T for n in ('query', 'key', 'value')]))
b.attn.qkv.bias.copy_(torch.cat([_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/{n}/bias'], t=False).reshape(-1) for n in ('query', 'key', 'value')]))
b.attn.proj.weight.copy_(_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/out/kernel']).flatten(1))
b.attn.proj.bias.copy_(_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/out/bias']))
@torch.no_grad()
def SinusoidalEncoding(self, seq_len, d_model):
pos_table = np.array([
[pos / np.power(10000, 2 * i / d_model) for i in range(d_model)]
for pos in range(seq_len)])
pos_table[0, 0::2] = np.sin(pos_table[0, 0::2])
pos_table[0, 1::2] = np.cos(pos_table[0, 1::2])
return torch.FloatTensor(pos_table)
def forward(self, x, position_embeddings=None):
# x_l = x[:,:,:204]
# x_r = x[:,:,204:]
# x_l = self.lms_porj(x_l)
# x = torch.cat((x_l,x_r),dim=2)
x = self.emb_proj(x)
if position_embeddings is None:
x = torch.cat((self.cls_token.expand(x.shape[0], -1, -1), x),dim=1) + self.pos_embed_my
else:
cls_token_pe = torch.tile(self.SinusoidalEncoding(1,self.embed_dim),(x.shape[0],1)).cuda()
position_embeddings = torch.from_numpy(position_embeddings).cuda()
x = torch.cat((self.cls_token.expand(x.shape[0], -1, -1), x), dim=1) + torch.cat((cls_token_pe[:, None, :], position_embeddings.expand(x.shape[0], -1, -1)),dim=1)
#x(b,s,d)
x = self.norm_pre(x)
x = self.blocks(x)
x = self.norm(x)
pred = self.pred(x[:,0,:])
return pred
class B16ViT_AB(timm.models.vision_transformer.VisionTransformer):
def __init__(self,weights):
# super().__init__(embed_dim=1280,num_heads=16,patch_size=14) # h-14的设置
super().__init__()
if weights is not None:
self.load_from(weights)
else:
pass
self.lms_proj = nn.Linear(204,config['T_lms3d_dim'],bias=False)
self.emb_proj = nn.Linear(config['T_proj_dim'],self.embed_dim) # 1024->768
self.alpha = nn.Sequential(nn.Linear(self.embed_dim,1),nn.Sigmoid())
self.beta = nn.Sequential(nn.Linear(self.embed_dim*2,1),nn.Sigmoid())
self.pos_embed_my = nn.Parameter(torch.zeros(1, config['window_size']+1, self.embed_dim))
self.pred = nn.Linear(self.embed_dim*2,config['T_output_dim'])
self.dropout = nn.Dropout(0.5)
self.dropout2 = nn.Dropout(0.6)
pass
@torch.no_grad()
def load_from(self, weights):
def _n2p(w, t=True):
if w.ndim == 4 and w.shape[0] == w.shape[1] == w.shape[2] == 1:
w = w.flatten()
if t:
if w.ndim == 4:
w = w.transpose([3, 2, 0, 1])
elif w.ndim == 3:
w = w.transpose([2, 0, 1])
elif w.ndim == 2:
w = w.transpose([1, 0])
return torch.from_numpy(w)
self.pos_embed.data.copy_(torch.from_numpy(weights['Transformer/posembed_input/pos_embedding']))
self.cls_token.data.copy_(torch.from_numpy(weights['cls']))
for l,b in enumerate(self.blocks):
################ln weight######################
b.norm1.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_0/bias']))
b.norm1.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_0/scale']))
b.norm2.bias.data.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_2/bias']))
b.norm2.weight.data.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_2/scale']))
###############mlp weight######################
b.mlp.fc1.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_0/kernel']).t())
b.mlp.fc1.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_0/bias']).t())
b.mlp.fc2.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_1/kernel']).t())
b.mlp.fc2.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_1/bias']).t())
###############attention weight################
b.attn.qkv.weight.copy_(torch.cat([_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/{n}/kernel'], t=False).flatten(1).T for n in ('query', 'key', 'value')]))
b.attn.qkv.bias.copy_(torch.cat([_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/{n}/bias'], t=False).reshape(-1) for n in ('query', 'key', 'value')]))
b.attn.proj.weight.copy_(_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/out/kernel']).flatten(1))
b.attn.proj.bias.copy_(_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/out/bias']))
@torch.no_grad()
def SinusoidalEncoding(self, seq_len, d_model):
pos_table = np.array([
[pos / np.power(10000, 2 * i / d_model) for i in range(d_model)]
for pos in range(seq_len)])
pos_table[0, 0::2] = np.sin(pos_table[0, 0::2])
pos_table[0, 1::2] = np.cos(pos_table[0, 1::2])
return torch.FloatTensor(pos_table)
def forward(self,lms, rgb_fea, position_embeddings=None):
# def forward(self,rgb_fea, position_embeddings=None):
lms_emb = self.lms_proj(lms)
cat_fea = torch.cat((lms_emb, rgb_fea),dim=2)
x0 = self.emb_proj(cat_fea)
if position_embeddings is None:
x = torch.cat((self.cls_token.expand(x0.shape[0], -1, -1), x0),dim=1) + self.pos_embed_my
else:
cls_token_pe = torch.tile(self.SinusoidalEncoding(1,self.embed_dim), (x0.shape[0],1)).cuda()
position_embeddings = torch.from_numpy(position_embeddings).cuda()
x = torch.cat((self.cls_token.expand(x0.shape[0], -1, -1), x0), dim=1) + torch.cat((cls_token_pe[:, None, :], position_embeddings.expand(x0.shape[0], -1, -1)),dim=1)
x = self.norm_pre(x)
x = self.blocks(x)
x = self.norm(x)
v_fea = x[:, 0, :]
alphas = self.alpha(self.dropout(x0))
sum_fea = torch.cat((x0, v_fea[:,None,:].repeat(1, config['window_size'], 1)), dim=2)
betas = self.beta(self.dropout(sum_fea))
denomiter = torch.sum(alphas * betas, dim=1)
hx = torch.mul(sum_fea, alphas * betas).sum(1) / (denomiter+1e-8)
pred = self.pred(self.dropout2(hx))
# pred = self.pred(v_fea)
return pred
class B16ViT_AB_Res(timm.models.vision_transformer.VisionTransformer):
def __init__(self,block,layers):
########################################resnet18 参数##########################
self.inplanes = 64
super(B16ViT_AB_Res, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d(1)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
########################################vit 参数###############################
self.emb_proj = nn.Linear(config['T_input_dim'],self.embed_dim) # 1024->768
self.alpha = nn.Sequential(nn.Linear(self.embed_dim,1),nn.Sigmoid())
self.beta = nn.Sequential(nn.Linear(config['T_proj_dim']*2,1),nn.Sigmoid())
self.pos_embed_my = nn.Parameter(torch.zeros(1, config['window_size']+1, self.embed_dim))
self.pred = nn.Linear(self.embed_dim*2,config['T_output_dim'])
self.blank = torch.zeros((1,512)).cuda()
pass
@torch.no_grad()
def load_from(self, weights):
def _n2p(w, t=True):
if w.ndim == 4 and w.shape[0] == w.shape[1] == w.shape[2] == 1:
w = w.flatten()
if t:
if w.ndim == 4:
w = w.transpose([3, 2, 0, 1])
elif w.ndim == 3:
w = w.transpose([2, 0, 1])
elif w.ndim == 2:
w = w.transpose([1, 0])
return torch.from_numpy(w)
self.pos_embed.data.copy_(torch.from_numpy(weights['Transformer/posembed_input/pos_embedding']))
self.cls_token.data.copy_(torch.from_numpy(weights['cls']))
for l,b in enumerate(self.blocks):
################ln weight######################
b.norm1.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_0/bias']))
b.norm1.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_0/scale']))
b.norm2.bias.data.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_2/bias']))
b.norm2.weight.data.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_2/scale']))
###############mlp weight######################
b.mlp.fc1.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_0/kernel']).t())
b.mlp.fc1.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_0/bias']).t())
b.mlp.fc2.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_1/kernel']).t())
b.mlp.fc2.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_1/bias']).t())
###############attention weight################
b.attn.qkv.weight.copy_(torch.cat([_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/{n}/kernel'], t=False).flatten(1).T for n in ('query', 'key', 'value')]))
b.attn.qkv.bias.copy_(torch.cat([_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/{n}/bias'], t=False).reshape(-1) for n in ('query', 'key', 'value')]))
b.attn.proj.weight.copy_(_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/out/kernel']).flatten(1))
b.attn.proj.bias.copy_(_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/out/bias']))
@torch.no_grad()
def SinusoidalEncoding(self, seq_len, d_model):
pos_table = np.array([
[pos / np.power(10000, 2 * i / d_model) for i in range(d_model)]
for pos in range(seq_len)])
pos_table[0, 0::2] = np.sin(pos_table[0, 0::2])
pos_table[0, 1::2] = np.cos(pos_table[0, 1::2])
return torch.FloatTensor(pos_table)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forwardRes(self, x):
f = self.conv1(x)
f = self.bn1(f)
f = self.relu(f)
f = self.maxpool(f)
f = self.layer1(f)
f = self.layer2(f)
f = self.layer3(f)
f = self.layer4(f)
f = self.avgpool(f)
f = f.squeeze(3).squeeze(2) # f[1, 512, 1, 1] ---> f[1, 512]
return f
def forwardVit(self, rgb, position_embeddings=None):
x0 = self.emb_proj(rgb)
alphas = self.alpha(x0)
with torch.no_grad():
if position_embeddings is None:
x = torch.cat((self.cls_token.expand(x0.shape[0], -1, -1), x0), dim=1) + self.pos_embed_my
else:
cls_token_pe = torch.tile(self.SinusoidalEncoding(1, self.embed_dim), (x0.shape[0], 1)).cuda()
position_embeddings = torch.from_numpy(position_embeddings).cuda()
x = torch.cat((self.cls_token.expand(x0.shape[0], -1, -1), x0), dim=1) + torch.cat(
(cls_token_pe[:, None, :], position_embeddings.expand(x0.shape[0], -1, -1)), dim=1)
x = self.norm_pre(x)
x = self.blocks(x)
x = self.norm(x)
v_fea = x[:, 0, :]
sum_fea = torch.cat((x0, v_fea[:,None,:].repeat(1, 270, 1)), dim=2)
betas = self.beta(sum_fea)
hx = torch.mul(sum_fea, alphas * betas).sum(1) / torch.sum(alphas * betas, dim=1)
pred = self.pred(hx)
return pred
def forward(self,x,pe=None):
vit_x = torch.zeros((0,270,512)).cuda()
for bs_unit in range(x.shape[0]):
real_seq = x[bs_unit]
real_seq_fea = self.forwardRes(real_seq)
real_seq_len = real_seq_fea.shape[0]
EOS_num = 270-real_seq_len
blanks = torch.tile(self.blank,(EOS_num,1))
sup_seq = torch.cat((real_seq_fea, blanks),dim=0)
vit_x = torch.cat((vit_x, sup_seq[None,:,:]),dim=0)
return self.forwardVit(vit_x,position_embeddings=pe)
class MLP_B16ViT_AB(timm.models.vision_transformer.VisionTransformer):
def __init__(self,weights):
# super().__init__(embed_dim=1280,num_heads=16,patch_size=14) # h-14的设置
super().__init__()
if weights is not None:
self.load_from(weights)
else:
pass
self.lms_proj = nn.Linear(204,config['T_lms3d_dim'],bias=False)
self.emb_proj = nn.Sequential(nn.Linear(config['T_proj_dim'],self.embed_dim),nn.GELU(),nn.Linear(self.emb_proj,self.emb_proj)) # 1024->768
self.alpha = nn.Sequential(nn.Linear(self.embed_dim,1),nn.Sigmoid())
self.beta = nn.Sequential(nn.Linear(self.embed_dim*2,1),nn.Sigmoid())
self.pos_embed_my = nn.Parameter(torch.zeros(1, config['window_size']+1, self.embed_dim))
self.pred = nn.Linear(self.embed_dim*2,config['T_output_dim'])
self.dropout = nn.Dropout(0.5)
self.dropout2 = nn.Dropout(0.6)
pass
@torch.no_grad()
def load_from(self, weights):
def _n2p(w, t=True):
if w.ndim == 4 and w.shape[0] == w.shape[1] == w.shape[2] == 1:
w = w.flatten()
if t:
if w.ndim == 4:
w = w.transpose([3, 2, 0, 1])
elif w.ndim == 3:
w = w.transpose([2, 0, 1])
elif w.ndim == 2:
w = w.transpose([1, 0])
return torch.from_numpy(w)
self.pos_embed.data.copy_(torch.from_numpy(weights['Transformer/posembed_input/pos_embedding']))
self.cls_token.data.copy_(torch.from_numpy(weights['cls']))
for l,b in enumerate(self.blocks):
################ln weight######################
b.norm1.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_0/bias']))
b.norm1.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_0/scale']))
b.norm2.bias.data.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_2/bias']))
b.norm2.weight.data.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/LayerNorm_2/scale']))
###############mlp weight######################
b.mlp.fc1.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_0/kernel']).t())
b.mlp.fc1.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_0/bias']).t())
b.mlp.fc2.weight.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_1/kernel']).t())
b.mlp.fc2.bias.copy_(torch.from_numpy(weights[f'Transformer/encoderblock_{l}/MlpBlock_3/Dense_1/bias']).t())
###############attention weight################
b.attn.qkv.weight.copy_(torch.cat([_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/{n}/kernel'], t=False).flatten(1).T for n in ('query', 'key', 'value')]))
b.attn.qkv.bias.copy_(torch.cat([_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/{n}/bias'], t=False).reshape(-1) for n in ('query', 'key', 'value')]))
b.attn.proj.weight.copy_(_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/out/kernel']).flatten(1))
b.attn.proj.bias.copy_(_n2p(weights[f'Transformer/encoderblock_{l}/MultiHeadDotProductAttention_1/out/bias']))
@torch.no_grad()
def SinusoidalEncoding(self, seq_len, d_model):
pos_table = np.array([
[pos / np.power(10000, 2 * i / d_model) for i in range(d_model)]
for pos in range(seq_len)])
pos_table[0, 0::2] = np.sin(pos_table[0, 0::2])
pos_table[0, 1::2] = np.cos(pos_table[0, 1::2])
return torch.FloatTensor(pos_table)
def forward(self,lms, rgb_fea, position_embeddings=None):
# def forward(self,rgb_fea, position_embeddings=None):
lms_emb = self.lms_proj(lms)
cat_fea = torch.cat((lms_emb, rgb_fea),dim=2)
x0 = self.emb_proj(cat_fea)
if position_embeddings is None:
x = torch.cat((self.cls_token.expand(x0.shape[0], -1, -1), x0),dim=1) + self.pos_embed_my
else:
cls_token_pe = torch.tile(self.SinusoidalEncoding(1,self.embed_dim), (x0.shape[0],1)).cuda()
position_embeddings = torch.from_numpy(position_embeddings).cuda()
x = torch.cat((self.cls_token.expand(x0.shape[0], -1, -1), x0), dim=1) + torch.cat((cls_token_pe[:, None, :], position_embeddings.expand(x0.shape[0], -1, -1)),dim=1)
x = self.norm_pre(x)
x = self.blocks(x)
x = self.norm(x)
v_fea = x[:, 0, :]
alphas = self.alpha(self.dropout(x0))
sum_fea = torch.cat((x0, v_fea[:,None,:].repeat(1, config['window_size'], 1)), dim=2)
betas = self.beta(self.dropout(sum_fea))
denomiter = torch.sum(alphas * betas, dim=1)
hx = torch.mul(sum_fea, alphas * betas).sum(1) / (denomiter+1e-8)
pred = self.pred(self.dropout2(hx))
# pred = self.pred(v_fea)
return pred
if __name__ == '__main__':
import dataProcess
cnn_prh = '/home/exp-10086/Project/Emotion-FAN-master/pretrain_model/Resnet18_FER+_pytorch.pth.tar'
vit_pth = '/home/exp-10086/Project/ferdataset/1test_37.pkl'
cnn_check = torch.load(cnn_prh)['state_dict']
vit_check = torch.load(vit_pth)['state_dict']
hybrid = B16ViT_AB_Res(BasicBlock,[2,2,2,2])
model_state = hybrid.state_dict()
for key in cnn_check:
for key in cnn_check:
if ((key == 'module.fc.weight') | (key == 'module.fc.bias')):
continue
else:
model_state[key.replace('module.', '')] = cnn_check[key]
for key in vit_check:
model_state[key] = vit_check[key]
imgin = torch.randn(size=(16,50,3,224,224)).cuda()
hybrid.train()
hybrid.cuda()
pos_encoding = dataProcess.Utils.SinusoidalEncoding(270, config['T_proj_dim'])
y = hybrid(imgin,pe=pos_encoding)
pass