-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
134 lines (106 loc) · 5.13 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
resnet = models.resnet50(pretrained=True)
for param in resnet.parameters():
param.requires_grad_(False)
modules = list(resnet.children())[:-2] # extracting the last conv layer from the model
self.resnet = nn.Sequential(*modules)
def forward(self, imgs):
features = self.resnet(imgs)
features = features.permute(0, 2, 3, 1) # batch x 7 x 7 x 2048
features = features.view(features.size(0), -1, features.size(-1)) # batch x 49 x 2048
return features
class Attention(nn.Module):
def __init__(self, encoder_dims, decoder_dims, attention_dims):
super(Attention, self).__init__()
self.attention_dims = attention_dims # size of attention network
self.U = nn.Linear(encoder_dims, attention_dims) # a^(t)
self.W = nn.Linear(decoder_dims, attention_dims) # s^(t` - 1)
self.A = nn.Linear(attention_dims, 1) # cvt the attention dims back to 1
def forward(self, features, hidden):
u_as = self.U(features)
w_as = self.W(hidden)
combined_state = torch.tanh(u_as + w_as.unsqueeze(1))
attention_score = self.A(combined_state)
attention_score = attention_score.squeeze(2)
alpha = F.softmax(attention_score, dim=1)
attention_weights = features * alpha.unsqueeze(2) # batch x num_timesteps (49) x features
attention_weights = attention_weights.sum(dim=1)
return alpha, attention_weights
class Decoder(nn.Module):
def __init__(self, embed_size, vocab_size, attention_dim, encoder_dim, decoder_dim, fc_dims, p=0.3,
embeddings=None):
super().__init__()
self.vocab_size = vocab_size
self.attention_dim = attention_dim
self.decoder_dim = decoder_dim
self.embedding = nn.Embedding(vocab_size, embedding_dim=embed_size)
self.attention = Attention(encoder_dim, decoder_dim, attention_dim)
self.init_h = nn.Linear(encoder_dim, decoder_dim)
self.init_c = nn.Linear(encoder_dim, decoder_dim)
self.lstm = nn.LSTMCell(encoder_dim + embed_size, decoder_dim, bias=True)
self.fcn1 = nn.Linear(decoder_dim, vocab_size)
self.fcn2 = nn.Linear(fc_dims, vocab_size)
self.drop = nn.Dropout(p)
if embeddings is not None:
self.load_pretrained_embed(embeddings)
def forward(self, features, captions):
seq_length = len(captions[0]) - 1 # Exclude the last one
batch_size = captions.size(0)
num_timesteps = features.size(1)
embed = self.embedding(captions)
h, c = self.init_hidden_state(features) # initialize h and c for LSTM
preds = torch.zeros(batch_size, seq_length, self.vocab_size).to(device)
alphas = torch.zeros(batch_size, seq_length, num_timesteps).to(device)
for s in range(seq_length):
alpha, context = self.attention(features, h)
lstm_inp = torch.cat((embed[:, s], context), dim=1)
h, c = self.lstm(lstm_inp, (h, c))
out = self.drop(self.fcn1(h))
preds[:, s] = out
alphas[:, s] = alpha
return preds, alphas
def gen_captions(self, features, max_len=20, vocab=None):
h, c = self.init_hidden_state(features)
alphas = []
captions = []
word = torch.tensor(vocab.stoi["<SOS>"]).view(1, -1).to(device)
embed = self.embedding(word)
for i in range(max_len):
alpha, context = self.attention(features, h)
alphas.append(alpha.cpu().detach().numpy())
lstm_inp = torch.cat((embed[:, 0], context), dim=1)
h, c = self.lstm(lstm_inp, (h, c))
out = self.drop(self.fcn1(h))
word_out_idx = torch.argmax(out, dim=1)
captions.append(word_out_idx.item())
if vocab.itos[word_out_idx.item()] == "<EOS>":
break
embed = self.embedding(word_out_idx.unsqueeze(0))
return [vocab.itos[word] for word in captions], alphas
def load_pretrained_embed(self, embeddings):
self.embedding.weight = nn.Parameter(embeddings)
for p in self.embedding.parameters():
p.requires_grad = True
def init_hidden_state(self, encoder_output):
mean_encoder_out = encoder_output.mean(dim=1)
h = self.init_h(mean_encoder_out)
c = self.init_c(mean_encoder_out)
return h, c
class EncoderDecoder(nn.Module):
def __init__(self, embed_size, vocab_size, attention_dim, encoder_dim, decoder_dim, fc_dims, p=0.3,
embeddings=None):
super().__init__()
self.EncoderCNN = Encoder()
self.DecoderLSTM = Decoder(embed_size, vocab_size, attention_dim, encoder_dim, decoder_dim, fc_dims, p,
embeddings)
def forward(self, imgs, caps):
features = self.EncoderCNN(imgs)
out = self.DecoderLSTM(features, caps)
return out