-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcmp_nn_vqa_model.py
278 lines (216 loc) · 11 KB
/
cmp_nn_vqa_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
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
import cPickle as pickle
from torch.autograd import Variable
import torch.nn.functional as F
import torch
import numpy as np
use_gpu = torch.cuda.is_available()
if use_gpu:
torch.cuda.manual_seed(1)
else:
torch.manual_seed(1)
class FindModule(torch.nn.Module):
def __init__(self, D_img, D_txt, D_map):
super(FindModule, self).__init__()
self.squeeze_wordvec_to_map_d = torch.nn.Linear(D_txt, D_map)
self.squeeze_image_to_map_d = torch.nn.Conv2d(
D_img, D_map, 1, stride=1, padding=0, bias=True)
self.squeeze_to_attention = torch.nn.Conv2d(
D_map, 1, 1, stride=1, padding=0, bias=True)
def forward(self, wordvec, image_feat):
wordvec_mapped = self.squeeze_wordvec_to_map_d(wordvec)
wordvec_mapped = wordvec_mapped.view((1, -1, 1, 1))
image_feat_mapped = self.squeeze_image_to_map_d(image_feat)
elementwise_multiplication = torch.mul(image_feat_mapped,
wordvec_mapped)
normalized_elementwise_multiplication = F.normalize(
elementwise_multiplication, p=2, dim=1)
attention_grid = self.squeeze_to_attention(
normalized_elementwise_multiplication)
return attention_grid
class AndModule(torch.nn.Module):
def __init__(self):
super(AndModule, self).__init__()
def forward(self, attention_grid1, attention_grid2):
return torch.min(attention_grid1, attention_grid2)
class OrModule(torch.nn.Module):
def __init__(self):
super(OrModule, self).__init__()
def forward(self, attention_grid1, attention_grid2):
return torch.max(attention_grid1, attention_grid2)
class DescribeModule(torch.nn.Module):
def __init__(self, D_txt, D_img, D_map, D_hidden, question_vocab_size,
D_ans_choices, question_word_embeddings_np):
super(DescribeModule, self).__init__()
self.squeeze_wordvec_to_map_d = torch.nn.Linear(D_txt, D_map)
self.squeeze_attended_image_to_map_d = torch.nn.Linear(D_img, D_map)
self.map_to_answer_choices = torch.nn.Linear(D_map, D_ans_choices)
# How to use this in Describe Module!??
# LSTM here
self.LSTMEncoder = LSTMEncoder(question_word_embeddings_np, D_txt,
D_hidden, question_vocab_size)
def forward(self, attention_grid, module_wordvec, image_feat,
token_sequence_tensor):
wordvec_mapped = self.squeeze_wordvec_to_map_d(module_wordvec)
wordvec_mapped = wordvec_mapped.view((1, -1))
softmax_2d = torch.nn.Softmax2d()
normalized_attention_grid = softmax_2d(attention_grid)
attended_image_feat = torch.mul(normalized_attention_grid, image_feat)
image_prob_vector = attended_image_feat.sum(2).sum(
2
) # sum over height and width. Retain depth (to sum attention probabilities).
attention_feat_mapped = self.squeeze_attended_image_to_map_d(
image_prob_vector)
elementwise_multiplication = torch.mul(attention_feat_mapped,
wordvec_mapped)
# LSTM here
last_hidden_state = self.LSTMEncoder(token_sequence_tensor)
# LSTM here
elementwise_multiplication = torch.mul(elementwise_multiplication,
last_hidden_state)
normalized_elementwise_multiplication = F.normalize(
elementwise_multiplication, p=2, dim=1)
scores = self.map_to_answer_choices(
normalized_elementwise_multiplication)
return scores
# softmax = torch.nn.Softmax()
# return softmax(scores)
class TransformModule(torch.nn.Module):
def __init__(self, D_txt, D_img, D_map):
super(TransformModule, self).__init__()
self.squeeze_wordvec_to_map_d = torch.nn.Linear(D_txt, D_map)
self.squeeze_attended_image_to_map_d = torch.nn.Linear(D_img, D_map)
self.squeeze_image_to_map_d = torch.nn.Conv2d(
D_img, D_map, 1, stride=1, padding=0, bias=True)
self.squeeze_to_attention = torch.nn.Conv2d(
D_map, 1, 1, stride=1, padding=0, bias=True)
def forward(self, input_attention_grid, wordvec, image_feat):
wordvec_mapped = self.squeeze_wordvec_to_map_d(wordvec)
wordvec_mapped = wordvec_mapped.view((1, -1, 1, 1))
image_feat_mapped = self.squeeze_image_to_map_d(image_feat)
softmax_2d = torch.nn.Softmax2d()
normalized_attention_grid = softmax_2d(input_attention_grid)
attended_image_feat = torch.mul(normalized_attention_grid, image_feat)
image_prob_vector = attended_image_feat.sum(2).sum(
2
) # sum over height and width. Retain depth (to sum attention probabilities).
attention_feat_mapped = self.squeeze_attended_image_to_map_d(
image_prob_vector)
attention_feat_mapped = attention_feat_mapped.view((1, -1, 1, 1))
elementwise_multiplication = torch.mul(attention_feat_mapped,
wordvec_mapped)
elementwise_multiplication = torch.mul(elementwise_multiplication,
image_feat_mapped)
normalized_elementwise_multiplication = F.normalize(
elementwise_multiplication, p=2, dim=1)
output_attention_grid = self.squeeze_to_attention(
normalized_elementwise_multiplication)
return output_attention_grid
class LSTMEncoder(torch.nn.Module):
def __init__(self, question_work_embeddings_np, embedding_dim, hidden_dim,
vocab_size):
super(LSTMEncoder, self).__init__()
self.hidden_dim = hidden_dim
self.question_word_embeddings = torch.nn.Embedding(
vocab_size, embedding_dim)
self.question_word_embeddings.weight = torch.nn.Parameter(
torch.from_numpy(question_work_embeddings_np))
self.question_word_embeddings.weight.requires_grad = False
self.lstm = torch.nn.LSTM(embedding_dim, hidden_dim)
self.hidden = self.init_hidden()
def init_hidden(self):
if use_gpu:
return (torch.autograd.Variable(
torch.zeros(1, 1, self.hidden_dim)).cuda(),
torch.autograd.Variable(
torch.zeros(1, 1, self.hidden_dim)).cuda())
else:
return (torch.autograd.Variable(torch.zeros(1, 1, self.hidden_dim)),
torch.autograd.Variable(torch.zeros(1, 1, self.hidden_dim)))
def forward(self, input_token_ids_sequence):
embeds = self.question_word_embeddings(input_token_ids_sequence)
lstm_out, self.hidden = self.lstm(
embeds.view(input_token_ids_sequence.size()[1], 1, -1), self.hidden)
return self.hidden[0].view(1, -1)
class DynamicModularNet(torch.nn.Module):
def __init__(self, D_img, D_txt, D_map, D_hidden,
question_word_embeddings_np, question_vocab_size,
module_vocab_size, ans_choices_size):
super(DynamicModularNet, self).__init__()
self.module_word_embeddings = torch.nn.Embedding(
module_vocab_size, D_txt)
self.Find = FindModule(D_img, D_txt, D_map)
self.Describe = DescribeModule(D_txt, D_img, D_map, D_hidden,
question_vocab_size, ans_choices_size,
question_word_embeddings_np)
self.Transform = TransformModule(D_txt, D_img, D_map)
self.And = AndModule()
# self.Or = OrModule()
def forward(self, Layout, image_feat, token_sequence_tensor,
module_vocab_dict):
# Have idea from here: https://github.com/dasguptar/treelstm.pytorch/blob/master/model.py
if Layout[0] == 'Find':
# Find
root_type, root_label = 'Find', Layout[1]
if root_label not in module_vocab_dict.word_list:
root_label = '<unk>'
root_label_id = module_vocab_dict.word2idx(root_label)
# label_vec = torch.index_select(embedding_matrix, 0, torch.LongTensor([root_label_id]) )
if use_gpu:
label_vec = self.module_word_embeddings(
Variable(
torch.LongTensor([root_label_id]).cuda(),
requires_grad=False)).view(-1)
else:
label_vec = self.module_word_embeddings(
Variable(
torch.LongTensor([root_label_id]),
requires_grad=False)).view(-1)
return self.Find(label_vec, image_feat)
else:
# print Layout
LayoutRoot = Layout[0]
LayoutSubtree = Layout[1]
root_type, root_label = LayoutRoot
if root_type == "Describe":
if root_label not in module_vocab_dict.word_list:
root_label = '<unk>'
root_label_id = module_vocab_dict.word2idx(root_label)
if use_gpu:
label_vec = self.module_word_embeddings(
Variable(
torch.LongTensor([root_label_id]).cuda(),
requires_grad=False)).view(-1)
else:
label_vec = self.module_word_embeddings(
Variable(
torch.LongTensor([root_label_id]),
requires_grad=False)).view(-1)
subtree_attention = self.forward(LayoutSubtree, image_feat,
token_sequence_tensor,
module_vocab_dict)
return self.Describe(subtree_attention, label_vec, image_feat,
token_sequence_tensor)
if root_type == "Transform":
if root_label not in module_vocab_dict.word_list:
root_label = '<unk>'
root_label_id = module_vocab_dict.word2idx(root_label)
if use_gpu:
label_vec = self.module_word_embeddings(
Variable(
torch.LongTensor([root_label_id]).cuda(),
requires_grad=False)).view(-1)
else:
label_vec = self.module_word_embeddings(
Variable(
torch.LongTensor([root_label_id]),
requires_grad=False)).view(-1)
subtree_attention = self.forward(LayoutSubtree, image_feat,
token_sequence_tensor,
module_vocab_dict)
return self.Transform(subtree_attention, label_vec, image_feat)
if root_type == "And":
child_attentions = [
self.forward(sub_node, image_feat, module_vocab_dict)
for sub_node in LayoutSubtree
]
return self.And(child_attentions[0], child_attentions[1])