forked from sreekanth-sreekumar/ml-final-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlate_fusion_model.py
39 lines (26 loc) · 1.25 KB
/
late_fusion_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
import torch
import torch.nn as nn
class LateFusionModel(nn.Module):
def __init__(self, resnet_model, bert_model):
super(LateFusionModel, self).__init__()
resnet_feature_size = resnet_model.fc.in_features
self.resnet = resnet_model
self.resnet.fc = nn.Identity()
# Freeze resnet
for param in self.resnet.parameters():
param.requires_grad = False
# bert_model.config.output_hidden_states = True
self.bert = bert_model.bert
bert_feature_size = bert_model.classifier.in_features
self.bert.eval()
# Freeze bert
for param in self.bert.parameters():
param.requires_grad = False
# Create the last linear layer
self.linear = nn.Linear(bert_feature_size + resnet_feature_size, 1)
def forward(self, inp):
inp = {x: inp[x].to(next(self.parameters()).device) for x in inp}
bert_output = self.bert(inp['input_ids'].squeeze(dim=1), attention_mask=inp['attention_masks'].squeeze(dim=1))
cls_vector = bert_output.pooler_output
resnet_output = self.resnet(inp['image'])
return self.linear(torch.cat((cls_vector, resnet_output), dim=1))