forked from parksunwoo/show_attend_and_tell_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
100 lines (79 loc) · 3.5 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
'''
Source code for an attention based image caption generation system described
in:
Show, Attend and Tell: Neural Image Caption Generation with Visual Attention
International Conference for Machine Learning (2015)
http://arxiv.org/abs/1502.03044
'''
import torch
import torch.nn as nn
import torchvision.models as models
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence
class EncoderCNN(nn.Module):
def __init__(self, embed_size):
super(EncoderCNN, self).__init__()
vggnet = models.vgg19(pretrained=True)
modules = list(vggnet.children())[:-1]
self.vggnet = nn.Sequential(*modules)
self.linear = nn.Linear(vggnet.classifier[6].in_features, embed_size)
self.bn = nn.BatchNorm1d(embed_size, momentum=0.01)
self.init_weights()
def init_weights(self):
self.linear.weight.data.normal_(0.0, 0.2)
self.linear.bias.data.fill_(0)
def forward(self, images):
features = self.vggnet(images)
features = Variable(features.data)
print (features)
features = features.view(features.size(0), -1)
features = self.bn(self.linear(features))
return features
class AttnDecoderRNN(nn.Module):
def __init__(self, embed_size, hidden_size, vocab_size, num_layers):
super(AttnDecoderRNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.embed = nn.Embedding(vocab_size, embed_size)
self.attn = nn.Linear(hidden_size, hidden_size)
self.lstm = nn.LSTM(embed_size, hidden_size, num_layers, batch_first=True)
self.linear = nn.Linear(hidden_size, vocab_size)
self.init_weights()
def init_weights(self):
self.embed.weight.data.uniform_(-0.1, 0.1)
self.linear.weight.data.uniform_(-0.1, 0.1)
self.linear.bias.data.fill_(0)
def init_hidden(self):
return cuda_variable(torch.zeros(self.num_layers, 1, self.hidden_size))
def forward(self, captions, last_hidden, encoder_hiddens, lengths):
embeddings = self.embed(captions)
embeddings = torch.cat((last_hidden.unsqueeze(1), embeddings), 1)
packed = pack_padded_sequence(embeddings, lengths, batch_first=True)
hiddens, _ = self.lstm(packed)
rnn_output = self.linear(hiddens[0])
print (rnn_output)
attn_weights = self._get_att_weight(rnn_output.squeeze(0), encoder_hiddens)
context = attn_weights.bmm(encoder_hiddens.transpose(0,1))
rnn_output = rnn_output.squeeze(0)
context = context.squeeze(1)
output = self.out(torch.cat((rnn_output, context), 1))
return output
def _get_att_weight(self, hidden, encoder_hiddens):
seq_len = len(encoder_hiddens)
# Create variable to store attention energies
attn_scores = cuda_variable(torch.zeros(seq_len)) # B x 1 x S
# Calculate energies for each encoder hidden
for i in range(seq_len):
attn_scores[i] = self.get_att_score(hidden, encoder_hiddens[i])
# Normalize scores to weights in range 0 to 1,
# resize to 1 x 1 x seq_len
# print("att_scores", attn_scores.size())
return nn.softmax(attn_scores).view(1, 1, -1)
def get_att_score(self, hidden, encoder_hidden):
score = self.attn(encoder_hidden)
return torch.dot(hidden.view(-1), score.view(-1))
def cuda_variable(tensor):
if torch.cuda.is_available():
return Variable(tensor.cuda())
else:
return Variable(tensor)