-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmodel.py
197 lines (149 loc) · 8.64 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
import torch
import torch.nn as nn
import torchvision.models as models
import torch.nn.functional as F
import numpy as np
import math
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class EncoderCNN(nn.Module):
def __init__(self, embed_size):
super(EncoderCNN, self).__init__()
resnet = models.resnet50(pretrained=True)
for param in resnet.parameters():
param.requires_grad_(False)
modules = list(resnet.children())[:-1]
self.resnet = nn.Sequential(*modules)
self.embed = nn.Linear(resnet.fc.in_features, embed_size)
def forward(self, images):
features = self.resnet(images)
features = features.view(features.size(0), -1)
features = self.embed(features)
return features
class DecoderRNN(nn.Module):
def __init__(self, embed_size, hidden_size, vocab_size):
''' Initialize the layers of this model.'''
super().__init__()
# Keep track of hidden_size for initialization of hidden state
self.hidden_size = hidden_size
# Embedding layer that turns words into a vector of a specified size
self.word_embeddings = nn.Embedding(vocab_size, embed_size)
# The LSTM takes embedded word vectors (of a specified size) as input
# and outputs hidden states of size hidden_dim
self.lstm = nn.LSTM(input_size=embed_size, \
hidden_size=hidden_size, # LSTM hidden units
num_layers=1, # number of LSTM layer
bias=True, # use bias weights b_ih and b_hh
batch_first=True, # input & output will have batch size as 1st dimension
dropout=0, # Not applying dropout
bidirectional=False, # unidirectional LSTM
)
# The linear layer that maps the hidden state output dimension
# to the number of words we want as output, vocab_size
self.linear = nn.Linear(hidden_size, vocab_size)
# initialize the hidden state
# self.hidden = self.init_hidden()
def init_hidden(self, batch_size):
""" At the start of training, we need to initialize a hidden state;
there will be none because the hidden state is formed based on previously seen data.
So, this function defines a hidden state with all zeroes
The axes semantics are (num_layers, batch_size, hidden_dim)
"""
return (torch.zeros((1, batch_size, self.hidden_size), device=device), \
torch.zeros((1, batch_size, self.hidden_size), device=device))
def forward(self, features, captions):
""" Define the feedforward behavior of the model """
# Discard the <end> word to avoid predicting when <end> is the input of the RNN
captions = captions[:, :-1]
# Initialize the hidden state
batch_size = features.shape[0] # features is of shape (batch_size, embed_size)
self.hidden = self.init_hidden(self.batch_size)
# Create embedded word vectors for each word in the captions
embeddings = self.word_embeddings(captions) # embeddings new shape : (batch_size, captions length - 1, embed_size)
# Stack the features and captions
embeddings = torch.cat((features.unsqueeze(1), embeddings), dim=1) # embeddings new shape : (batch_size, caption length, embed_size)
# Get the output and hidden state by passing the lstm over our word embeddings
# the lstm takes in our embeddings and hidden state
lstm_out, self.hidden = self.lstm(embeddings, self.hidden) # lstm_out shape : (batch_size, caption length, hidden_size)
# Fully connected layer
outputs = self.linear(lstm_out) # outputs shape : (batch_size, caption length, vocab_size)
return outputs
## Greedy search
def sample(self, inputs):
" accepts pre-processed image tensor (inputs) and returns predicted sentence (list of tensor ids of length max_len) "
output = []
batch_size = inputs.shape[0] # batch_size is 1 at inference, inputs shape : (1, 1, embed_size)
hidden = self.init_hidden(batch_size) # Get initial hidden state of the LSTM
while True:
lstm_out, hidden = self.lstm(inputs, hidden) # lstm_out shape : (1, 1, hidden_size)
outputs = self.linear(lstm_out) # outputs shape : (1, 1, vocab_size)
outputs = outputs.squeeze(1) # outputs shape : (1, vocab_size)
_, max_indice = torch.max(outputs, dim=1) # predict the most likely next word, max_indice shape : (1)
output.append(max_indice.cpu().numpy()[0].item()) # storing the word predicted
if (max_indice == 1):
# We predicted the <end> word, so there is no further prediction to do
break
## Prepare to embed the last predicted word to be the new input of the lstm
inputs = self.word_embeddings(max_indice) # inputs shape : (1, embed_size)
inputs = inputs.unsqueeze(1) # inputs shape : (1, 1, embed_size)
return output
## Beam search implementation (Attempt)
def beam_search_sample(self, inputs, beam=3):
output = []
batch_size = inputs.shape[0] # batch_size is 1 at inference, inputs shape : (1, 1, embed_size)
hidden = self.init_hidden(batch_size) # Get initial hidden state of the LSTM
# sequences[0][0] : index of start word
# sequences[0][1] : probability of the word predicted
# sequences[0][2] : hidden state related of the last word
sequences = [[[torch.Tensor([0])], 1.0, hidden]]
max_len = 20
## Step 1
# Predict the first word <start>
outputs, hidden = DecoderRNN.get_outputs(self, inputs, hidden)
_, max_indice = torch.max(outputs, dim=1) # predict the most likely next word, max_indice shape : (1)
output.append(max_indice.cpu().numpy()[0].item()) # storing the word predicted
# inputs = DecoderRNN.get_next_word_input(self, max_indice)
l = 0
while len(sequences[0][0]) < max_len:
print("l:", l)
l+= 1
temp = []
for seq in sequences:
# print("seq[0]: ", seq[0])
inputs = seq[0][-1] # last word index in seq
inputs = inputs.type(torch.cuda.LongTensor)
print("inputs : ", inputs)
# Embed the input word
inputs = self.word_embeddings(inputs) # inputs shape : (1, embed_size)
inputs = inputs.unsqueeze(1) # inputs shape : (1, 1, embed_size)
# retrieve the hidden state
hidden = seq[2]
preds, hidden = DecoderRNN.get_outputs(self, inputs, hidden)
# Getting the top <beam_index>(n) predictions
softmax_score = F.log_softmax(outputs, dim=1) # Define a function to sort the cumulative score
sorted_score, indices = torch.sort(-softmax_score, dim=1)
word_preds = indices[0][:beam]
best_scores = sorted_score[0][:beam]
# Creating a new list so as to put them via the model again
for i, w in enumerate(word_preds):
# print("seq[0]: ", seq[0][0][:].cpu().numpy().item())
next_cap, prob = seq[0][0].cpu().numpy().tolist(), seq[1]
next_cap.append(w)
print("next_cap : ", next_cap)
prob *best_scores[i].cpu().item()
temp.append([next_cap, prob])
sequences = temp
# Order according to proba
ordered = sorted(sequences, key=lambda tup: tup[1])
# Getting the top words
sequences = ordered[:beam]
print("sequences: ", sequences)
def get_outputs(self, inputs, hidden):
lstm_out, hidden = self.lstm(inputs, hidden) # lstm_out shape : (1, 1, hidden_size)
outputs = self.linear(lstm_out) # outputs shape : (1, 1, vocab_size)
outputs = outputs.squeeze(1) # outputs shape : (1, vocab_size)
return outputs, hidden
def get_next_word_input(self, max_indice):
## Prepare to embed the last predicted word to be the new input of the lstm
inputs = self.word_embeddings(max_indice) # inputs shape : (1, embed_size)
inputs = inputs.unsqueeze(1) # inputs shape : (1, 1, embed_size)
return inputs