-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformer.py
More file actions
637 lines (489 loc) · 29.8 KB
/
Copy pathtransformer.py
File metadata and controls
637 lines (489 loc) · 29.8 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
import torch.nn.functional as F
from torch.nn import TransformerEncoder, TransformerEncoderLayer
import torch
import torch.nn as nn
from torchvision import models
from params import RunningParams
from cross_vit import CrossTransformer, Transformer
from einops import rearrange, repeat
from einops.layers.torch import Rearrange
import torchvision
RunningParams = RunningParams()
class BinaryMLP(nn.Module):
def __init__(self, input_dim, hidden_dim, dropout=0.0): # Set default dropout to 0.2
super().__init__()
self.net = nn.Sequential(
nn.Linear(input_dim, 512),
nn.BatchNorm1d(512),
nn.GELU(),
nn.Linear(512, hidden_dim),
nn.BatchNorm1d(hidden_dim), # Make sure to use `hidden_dim` instead of fixed number
nn.GELU(),
nn.Linear(hidden_dim, 2),
)
def forward(self, x):
return self.net(x)
if RunningParams.CUB_TRAINING is True:
class Transformer_AdvisingNetwork(nn.Module):
def __init__(self):
print("Using training network for Birds (CUB-200)")
super(Transformer_AdvisingNetwork, self).__init__()
from iNat_resnet import ResNet_AvgPool_classifier, Bottleneck
resnet = ResNet_AvgPool_classifier(Bottleneck, [3, 4, 6, 4])
my_model_state_dict = torch.load(
f'{RunningParams.prj_dir}/pretrained_models/cub-200/iNaturalist_pretrained_RN50_85.83.pth')
if RunningParams.resnet == 50 and RunningParams.RN50_INAT is False:
resnet = models.resnet50(pretrained=True)
resnet.fc = nn.Sequential(nn.Linear(2048, 200)).cuda()
my_model_state_dict = torch.load(
f'{RunningParams.prj_dir}/pretrained_models/cub-200/imagenet_pretrained_resnet50_cub_200way_top1acc_63.pth')
elif RunningParams.resnet == 34:
resnet = models.resnet34(pretrained=True)
resnet.fc = nn.Sequential(nn.Linear(512, 200)).cuda()
my_model_state_dict = torch.load(
f'{RunningParams.prj_dir}/pretrained_models/cub-200/imagenet_pretrained_resnet34_cub_200way_top1acc_62_81.pth')
elif RunningParams.resnet == 18:
resnet = models.resnet18(pretrained=True)
resnet.fc = nn.Sequential(nn.Linear(512, 200)).cuda()
my_model_state_dict = torch.load(
f'{RunningParams.prj_dir}/pretrained_models/cub-200/imagenet_pretrained_resnet18_cub_200way_top1acc_60_22.pth')
resnet.load_state_dict(my_model_state_dict, strict=True)
if RunningParams.resnet == 34 or RunningParams.resnet == 18 or (
RunningParams.resnet == 50 and RunningParams.RN50_INAT is False):
resnet.fc = resnet.fc[0]
conv_features = list(resnet.children())[:RunningParams.conv_layer-6] # delete the last fc layer
self.conv_layers = nn.Sequential(*conv_features)
self.pooling_layer = nn.AdaptiveAvgPool2d(output_size=(1, 1))
class transformer_feat_embedder(nn.Module):
def __init__(self, num_patches, dim):
super().__init__()
self.pos_embedding = nn.Parameter(torch.randn(1, num_patches + 1, dim))
self.cls_token = nn.Parameter(torch.randn(1, 1, dim))
def forward(self, feat, k_value):
b, n, _ = feat.shape
cls_tokens = repeat(self.cls_token, '() n d -> b n d', b=b)
x = torch.cat((cls_tokens, feat), dim=1)
x += self.pos_embedding[:, :(n + 1)]
return x
self.transformer_feat_embedder = transformer_feat_embedder(RunningParams.feat_map_size[RunningParams.conv_layer],
RunningParams.conv_layer_size[RunningParams.conv_layer])
transformer_depth = RunningParams.N
cross_transformer_depth = RunningParams.M
feat_dim = RunningParams.conv_layer_size[RunningParams.conv_layer]
self.transformer = Transformer(dim=feat_dim, depth=transformer_depth, heads=8, dim_head=64, mlp_dim=512, dropout=0.0)
self.cross_transformer = CrossTransformer(sm_dim=feat_dim, lg_dim=feat_dim, depth=cross_transformer_depth, heads=8,
dim_head=64, dropout=0.0)
print('Using transformer with depth {} and 8 heads'.format(transformer_depth))
print('Using cross_transformer with depth {} and 8 heads'.format(cross_transformer_depth))
self.branch3 = BinaryMLP(
2 * RunningParams.conv_layer_size[RunningParams.conv_layer] + 2, 32)
self.agg_branch = nn.Linear(2, 1).cuda()
# initialize all fc layers to xavier
# TODO: Check if this initialization destroy the information from the pretrained conv layers
for m in self.modules():
if isinstance(m, nn.Linear):
torch.nn.init.xavier_normal_(m.weight, gain=1)
def forward(self, images, explanations, scores):
# Process the input images
input_spatial_feats = self.conv_layers(images)
input_feat = self.pooling_layer(input_spatial_feats).squeeze()
input_spatial_feats = input_spatial_feats.flatten(start_dim=2) # bsxcx49
# Process the nearest neighbors
explanations = explanations.squeeze()
if len(explanations.shape) == 3:
explanations = explanations.unsqueeze(0)
explanation_spatial_feats = self.conv_layers(explanations)
explanation_spatial_feats = explanation_spatial_feats.flatten(start_dim=2)
# change from 2048x49 -> 49x2048
# 49 tokens for an image
input_spatial_feats = torch.transpose(input_spatial_feats, 1, 2)
explanation_spatial_feats = torch.transpose(explanation_spatial_feats, 1, 2)
sep_token = torch.zeros([explanations.shape[0], 1], requires_grad=False).cuda()
transformer_encoder_depth = RunningParams.L
# Add the cls token and positional embedding --> 50x2048
input_spatial_feats = self.transformer_feat_embedder(input_spatial_feats, 1)
explanation_spatial_feats = self.transformer_feat_embedder(explanation_spatial_feats, 1)
# TODO: as the output of the first layer does not propagate to the second layer, then transformer_encoder_depth should be 1
for _ in range(transformer_encoder_depth):
# Self-attention --> 50x2048; both cls and image tokens are transformed.
if self.transformer is not None:
input_spt_feats = self.transformer(input_spatial_feats)
exp_spt_feats = self.transformer(explanation_spatial_feats)
else:
input_spt_feats = input_spatial_feats
exp_spt_feats = explanation_spatial_feats
if self.cross_transformer is not None:
# Cross-attention --> 50x2048; only the cls tokens are transformed. Image tokens are kept the same.
input_spatial_feats, explanation_spatial_feats, i2e_attn, e2i_attn = self.cross_transformer(input_spt_feats, exp_spt_feats)
else:
input_spatial_feats = input_spt_feats
explanation_spatial_feats = exp_spt_feats
input_emb, exp_emb = input_spatial_feats, explanation_spatial_feats
input_emb, exp_emb = input_emb.squeeze(), exp_emb.squeeze()
# Extracting the cls token --> 1x2048
input_cls, exp_cls = map(lambda t: t[:, 0], (input_emb, exp_emb))
pairwise_feats = []
x = self.branch3(
torch.cat([sep_token, input_cls, sep_token, exp_cls], dim=1))
output3 = x
output3 = self.agg_branch(output3)
output = output3
return output, input_feat, None, None
class CNN_AdvisingNetwork(nn.Module):
def __init__(self):
print("Using training network for Birds (CUB-200)")
super(CNN_AdvisingNetwork, self).__init__()
from iNat_resnet import ResNet_AvgPool_classifier, Bottleneck
resnet = ResNet_AvgPool_classifier(Bottleneck, [3, 4, 6, 4])
my_model_state_dict = torch.load(
f'{RunningParams.prj_dir}/pretrained_models/cub-200/iNaturalist_pretrained_RN50_85.83.pth')
resnet.load_state_dict(my_model_state_dict, strict=True)
conv_features = list(resnet.children())[:RunningParams.conv_layer-6] # delete the last fc layer
self.conv_layers = nn.Sequential(*conv_features)
self.pooling_layer = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.branch3 = BinaryMLP(
2 * RunningParams.conv_layer_size[RunningParams.conv_layer] + 2, 32)
self.agg_branch = nn.Linear(2, 1).cuda()
# initialize all fc layers to xavier
for m in self.modules():
if isinstance(m, nn.Linear):
torch.nn.init.xavier_normal_(m.weight, gain=1)
def forward(self, images, explanations, scores):
# Process the input images
input_spatial_feats = self.conv_layers(images)
input_feat = self.pooling_layer(input_spatial_feats).squeeze()
# Process the nearest neighbors
explanations = explanations.squeeze()
explanation_spatial_feats = self.conv_layers(explanations)
################################
exp_feat = self.pooling_layer(explanation_spatial_feats).squeeze()
################################
sep_token = torch.zeros([explanations.shape[0], 1], requires_grad=False).cuda()
################################
x = self.branch3(
torch.cat([sep_token, input_feat, sep_token, exp_feat], dim=1))
################################
output3 = self.agg_branch(x)
output = output3
return output, input_feat, None, None
class ViT_AdvisingNetwork(nn.Module):
def __init__(self):
print("Using training ViT network for Birds (CUB-200)")
super(ViT_AdvisingNetwork, self).__init__()
import timm
class CustomViT(nn.Module):
def __init__(self, base_model):
super(CustomViT, self).__init__()
self.base_model = base_model
def forward(self, x):
# Get the features from the base ViT model
x = self.base_model.forward_features(x)
# Extract the CLS token (first token)
cls_token = x[:, 0]
# Pass the features through the classifier
output = self.base_model.head(cls_token)
return output, cls_token
# Initialize the base model and load the trained weights
base_model = timm.create_model('vit_base_patch16_224', pretrained=False, num_classes=200)
model_path = "./vit_base_patch16_224_cub_200way.pth"
state_dict = torch.load(model_path, map_location=torch.device("cuda"))
new_state_dict = {k.replace("module.", ""): v for k, v in state_dict.items()}
base_model.load_state_dict(new_state_dict)
# Wrap the base model in the custom model
self.feature_extractor = CustomViT(base_model)
# 768 is the dimension of ViT-B-16
self.branch3 = BinaryMLP(
2 * 768 + 2, 32)
self.agg_branch = nn.Linear(2, 1).cuda()
# initialize branch3 and agg_branch only
for m in self.modules():
if isinstance(m, nn.Linear) and not isinstance(m, CustomViT):
torch.nn.init.xavier_normal_(m.weight, gain=1)
def forward(self, images, explanations, scores):
# Process the input images
_, input_cls = self.feature_extractor(images)
# Process the nearest neighbors
explanations = explanations.squeeze()
_, explanation_cls = self.feature_extractor(explanations)
sep_token = torch.zeros([explanations.shape[0], 1], requires_grad=False).cuda()
################################
x = self.branch3(
torch.cat([sep_token, input_cls, sep_token, explanation_cls], dim=1))
################################
output3 = self.agg_branch(x)
output = output3
return output, None, None, None
elif RunningParams.CARS_TRAINING is True:
class Transformer_AdvisingNetwork(nn.Module):
def __init__(self):
print("Using training network for Cars")
super(Transformer_AdvisingNetwork, self).__init__()
################################################################
import torchvision
if RunningParams.resnet == 50:
model = torchvision.models.resnet50(pretrained=True).cuda()
elif RunningParams.resnet == 34:
model = torchvision.models.resnet34(pretrained=True).cuda()
elif RunningParams.resnet == 18:
model = torchvision.models.resnet18(pretrained=True).cuda()
model.fc = nn.Linear(model.fc.in_features, 196)
my_model_state_dict = torch.load(
f'{RunningParams.prj_dir}/pretrained_models/cars-196/model_best_rn{RunningParams.resnet}.pth.tar', map_location=torch.device('cpu'))
model.load_state_dict(my_model_state_dict['state_dict'], strict=True)
################################################################
conv_features = list(model.children())[:RunningParams.conv_layer - 6] # delete the last fc layer
self.conv_layers = nn.Sequential(*conv_features)
self.pooling_layer = nn.AdaptiveAvgPool2d(output_size=(1, 1))
class transformer_feat_embedder(nn.Module):
def __init__(self, num_patches, dim):
super().__init__()
self.pos_embedding = nn.Parameter(torch.randn(1, num_patches + 1, dim))
self.cls_token = nn.Parameter(torch.randn(1, 1, dim))
def forward(self, feat, k_value):
b, n, _ = feat.shape
cls_tokens = repeat(self.cls_token, '() n d -> b n d', b=b)
x = torch.cat((cls_tokens, feat), dim=1)
x += self.pos_embedding[:, :(n + 1)]
return x
self.transformer_feat_embedder = transformer_feat_embedder(
RunningParams.feat_map_size[RunningParams.conv_layer],
RunningParams.conv_layer_size[RunningParams.conv_layer])
transformer_depth = RunningParams.N
cross_transformer_depth = RunningParams.M
feat_dim = RunningParams.conv_layer_size[RunningParams.conv_layer]
self.transformer = Transformer(dim=feat_dim, depth=transformer_depth, heads=8, dim_head=64, mlp_dim=512,
dropout=0.0)
self.cross_transformer = CrossTransformer(sm_dim=feat_dim, lg_dim=feat_dim, depth=cross_transformer_depth,
heads=8,
dim_head=64, dropout=0.0)
print('Using transformer with depth {} and 8 heads'.format(transformer_depth))
print('Using cross_transformer with depth {} and 8 heads'.format(cross_transformer_depth))
self.branch3 = BinaryMLP(
2 * RunningParams.conv_layer_size[RunningParams.conv_layer] + 2, 32)
self.agg_branch = nn.Linear(2, 1).cuda()
# initialize all fc layers to xavier
for m in self.modules():
if isinstance(m, nn.Linear):
torch.nn.init.xavier_normal_(m.weight, gain=1)
def forward(self, images, explanations, scores):
# Process the input images
input_spatial_feats = self.conv_layers(images)
input_feat = self.pooling_layer(input_spatial_feats).squeeze()
input_spatial_feats = input_spatial_feats.flatten(start_dim=2) # bsxcx49
# Process the nearest neighbors
explanations = explanations.squeeze()
explanation_spatial_feats = self.conv_layers(explanations)
explanation_spatial_feats = explanation_spatial_feats.flatten(start_dim=2)
# change from 2048x49 -> 49x2048
# 49 tokens for an image
input_spatial_feats = torch.transpose(input_spatial_feats, 1, 2)
explanation_spatial_feats = torch.transpose(explanation_spatial_feats, 1, 2)
sep_token = torch.zeros([explanations.shape[0], 1], requires_grad=False).cuda()
transformer_encoder_depth = RunningParams.L
# Add the cls token and positional embedding --> 50x2048
input_spatial_feats = self.transformer_feat_embedder(input_spatial_feats, 1)
explanation_spatial_feats = self.transformer_feat_embedder(explanation_spatial_feats, 1)
# TODO: as the output of the first layer does not propagate to the second layer, then transformer_encoder_depth should be 1
for _ in range(transformer_encoder_depth):
# Self-attention --> 50x2048; both cls and image tokens are transformed.
input_spt_feats = self.transformer(input_spatial_feats)
exp_spt_feats = self.transformer(explanation_spatial_feats)
# Cross-attention --> 50x2048; only the cls tokens are transformed. Image tokens are kept the same.
input_spatial_feats, explanation_spatial_feats, i2e_attn, e2i_attn = self.cross_transformer(
input_spt_feats, exp_spt_feats)
input_emb, exp_emb = input_spatial_feats, explanation_spatial_feats
input_emb, exp_emb = input_emb.squeeze(), exp_emb.squeeze()
# Extracting the cls token --> 1x2048
input_cls, exp_cls = map(lambda t: t[:, 0], (input_emb, exp_emb))
pairwise_feats = []
x = self.branch3(
torch.cat([sep_token, input_cls, sep_token, exp_cls], dim=1))
output3 = x
output3 = self.agg_branch(output3)
output = output3
return output, input_feat, None, None
elif RunningParams.DOGS_TRAINING is True:
class Transformer_AdvisingNetwork(nn.Module):
def __init__(self):
print("Using training network for Stanford Dogs")
super(Transformer_AdvisingNetwork, self).__init__()
################################################################
import torchvision
if RunningParams.resnet == 50:
model = torchvision.models.resnet50(pretrained=True).cuda()
model.fc = nn.Linear(2048, 120)
model.fc = nn.Sequential(nn.Linear(2048, 120)).cuda()
elif RunningParams.resnet == 34:
model = torchvision.models.resnet34(pretrained=True).cuda()
model.fc = nn.Linear(512, 120)
elif RunningParams.resnet == 18:
model = torchvision.models.resnet18(pretrained=True).cuda()
model.fc = nn.Linear(512, 120)
print(f'{RunningParams.prj_dir}/pretrained_models/dogs-120/resnet{RunningParams.resnet}_stanford_dogs.pth')
my_model_state_dict = torch.load(
f'{RunningParams.prj_dir}/pretrained_models/dogs-120/resnet{RunningParams.resnet}_stanford_dogs.pth',
map_location='cuda'
)
new_state_dict = {k.replace("model.", ""): v for k, v in my_model_state_dict.items()}
################################################################
conv_features = list(model.children())[:RunningParams.conv_layer - 6] # delete the last fc layer
self.conv_layers = nn.Sequential(*conv_features)
self.pooling_layer = nn.AdaptiveAvgPool2d(output_size=(1, 1))
class transformer_feat_embedder(nn.Module):
def __init__(self, num_patches, dim):
super().__init__()
self.pos_embedding = nn.Parameter(torch.randn(1, num_patches + 1, dim))
self.cls_token = nn.Parameter(torch.randn(1, 1, dim))
def forward(self, feat, k_value):
if k_value == 1:
b, n, _ = feat.shape
cls_tokens = repeat(self.cls_token, '() n d -> b n d', b=b)
x = torch.cat((cls_tokens, feat), dim=1)
else:
b, k, n, _ = feat.shape
cls_tokens = repeat(self.cls_token, '() n d -> b k n d', b=b, k=k)
x = torch.cat((cls_tokens, feat), dim=2)
x += self.pos_embedding[:, :(n + 1)]
return x
self.transformer_feat_embedder = transformer_feat_embedder(
RunningParams.feat_map_size[RunningParams.conv_layer],
RunningParams.conv_layer_size[RunningParams.conv_layer])
transformer_depth = RunningParams.N
cross_transformer_depth = RunningParams.M
feat_dim = RunningParams.conv_layer_size[RunningParams.conv_layer]
self.transformer = Transformer(dim=feat_dim, depth=transformer_depth, heads=8, dim_head=64, mlp_dim=512,
dropout=0.0)
self.cross_transformer = CrossTransformer(sm_dim=feat_dim, lg_dim=feat_dim, depth=cross_transformer_depth,
heads=8,
dim_head=64, dropout=0.0)
print('Using transformer with depth {} and 8 heads'.format(transformer_depth))
print('Using cross_transformer with depth {} and 8 heads'.format(cross_transformer_depth))
self.branch3 = BinaryMLP(
2 * RunningParams.conv_layer_size[RunningParams.conv_layer] + 2, 32)
if RunningParams.k_value == 1:
self.agg_branch = nn.Linear(2, 1).cuda()
else:
self.agg_branch = nn.Linear(RunningParams.k_value*2, 1).cuda()
#TODO?? Only initialize branch3 and agg_branch
# initialize all fc layers to xavier
# for m in self.modules():
# if isinstance(m, nn.Linear):
# torch.nn.init.xavier_normal_(m.weight, gain=1)
# initialize branch3 and agg_branch only with Xavier initialization
for m in self.modules():
# Check if the module is a Linear layer and not in conv_layers
if isinstance(m, nn.Linear) and m not in self.conv_layers.modules():
torch.nn.init.xavier_normal_(m.weight, gain=1)
def forward(self, images, explanations, scores):
# Process the input images
input_spatial_feats = self.conv_layers(images)
input_feat = self.pooling_layer(input_spatial_feats).squeeze()
input_spatial_feats = input_spatial_feats.flatten(start_dim=2) # bsxcx49
# Process the nearest neighbors
if RunningParams.k_value == 1:
explanations = explanations.squeeze()
explanation_spatial_feats = self.conv_layers(explanations)
explanation_spatial_feats = explanation_spatial_feats.flatten(start_dim=2)
else: # K > 1
explanation_spatial_feats = []
for sample_idx in range(RunningParams.k_value):
data = explanations[:, sample_idx, :] # TODO: I traced to be at least correct here
explanation_spatial_feat = self.conv_layers(data)
explanation_spatial_feat = explanation_spatial_feat.flatten(start_dim=2) #
explanation_spatial_feats.append(explanation_spatial_feat)
# bsxKx2048x49
explanation_spatial_feats = torch.stack(explanation_spatial_feats, dim=1)
# change from 2048x49 -> 49x2048
# 49 tokens for an image
input_spatial_feats = torch.transpose(input_spatial_feats, 1, 2)
if RunningParams.k_value == 1:
explanation_spatial_feats = torch.transpose(explanation_spatial_feats, 1, 2)
else:
explanation_spatial_feats = torch.transpose(explanation_spatial_feats, 2, 3) # 4x3x49x2048
sep_token = torch.zeros([explanations.shape[0], 1], requires_grad=False).cuda()
transformer_encoder_depth = RunningParams.L
if RunningParams.k_value == 1:
# Add the cls token and positional embedding --> 50x2048
input_spatial_feats = self.transformer_feat_embedder(input_spatial_feats, 1)
explanation_spatial_feats = self.transformer_feat_embedder(explanation_spatial_feats, 1)
# TODO: as the output of the first layer does not propagate to the second layer, then transformer_encoder_depth should be 1
for _ in range(transformer_encoder_depth):
# Self-attention --> 50x2048; both cls and image tokens are transformed.
input_spt_feats = self.transformer(input_spatial_feats)
exp_spt_feats = self.transformer(explanation_spatial_feats)
# Cross-attention --> 50x2048; only the cls tokens are transformed. Image tokens are kept the same.
input_spatial_feats, explanation_spatial_feats, i2e_attn, e2i_attn = self.cross_transformer(
input_spt_feats, exp_spt_feats)
input_emb, exp_emb = input_spatial_feats, explanation_spatial_feats
input_emb, exp_emb = input_emb.squeeze(), exp_emb.squeeze()
# Extracting the cls token --> 1x2048
input_cls, exp_cls = map(lambda t: t[:, 0], (input_emb, exp_emb))
else:
# Clone the input tensor K times along the second dimension
input_spatial_feats = input_spatial_feats.unsqueeze(1).repeat(1, RunningParams.k_value, 1, 1). \
view(input_spatial_feats.shape[0], RunningParams.k_value, 49,
RunningParams.conv_layer_size[RunningParams.conv_layer])
input_spatial_feats = self.transformer_feat_embedder(input_spatial_feats,
RunningParams.k_value) # bsx50x2048
explanation_spatial_feats = self.transformer_feat_embedder(explanation_spatial_feats,
RunningParams.k_value) # bsxKx50x2048
i2e_attns = []
e2i_attns = []
for depth_idx in range(transformer_encoder_depth):
input_list = []
explanation_list = []
for prototype_idx in range(RunningParams.k_value):
input = input_spatial_feats[:, prototype_idx, :]
explanation = explanation_spatial_feats[:, prototype_idx, :]
input = self.transformer(input)
explanation = self.transformer(explanation)
# TODO: Thử remove self-attention
# TODO: với VIT hiện tại ko có model về patch (local) mà chỉ có global info
# input = input_spatial_feats
# explanation = explanation
# Cross-attention --> bsx50x2048; only the cls tokens are transformed. Image tokens are kept the same.
inp, exp, i2e_attn, e2i_attn = self.cross_transformer(input, explanation)
# Extract attention from the last layer (i.e. closest to classification head)
if depth_idx == transformer_encoder_depth - 1:
i2e_attns.append(i2e_attn)
e2i_attns.append(e2i_attn)
input_list.append(inp)
explanation_list.append(exp)
input_spatial_feats = torch.stack(input_list, dim=1)
explanation_spatial_feats = torch.stack(explanation_list, dim=1)
input_emb = input_spatial_feats
input_emb = input_emb.squeeze()
if RunningParams.k_value == 1:
input_cls = input_emb[:, 0]
else:
input_cls = input_emb[:, :, 0]
exp_emb = explanation_spatial_feats
exp_emb = exp_emb.squeeze()
if RunningParams.k_value == 1:
exp_cls = exp_emb[:, 0]
else:
exp_cls = exp_emb[:, :, 0]
i2e_attns = torch.cat(i2e_attns, dim=2)
e2i_attns = torch.cat(e2i_attns, dim=2)
pairwise_feats = []
if RunningParams.k_value == 1:
x = self.branch3(
torch.cat([sep_token, input_cls, sep_token, exp_cls], dim=1))
output3 = x
else:
for prototype_idx in range(0, RunningParams.k_value):
x = self.branch3(
torch.cat(
[sep_token, input_cls[:, prototype_idx], sep_token, exp_cls[:, prototype_idx]],
dim=1))
pairwise_feats.append(x)
output3 = torch.cat(pairwise_feats, dim=1)
output3 = self.agg_branch(output3)
output = output3
if RunningParams.k_value == 1:
return output, input_feat, None, None
else:
return output, input_feat, i2e_attns, e2i_attns
else:
print('Failed creating the model! Exiting...')
exit(-1)