-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModelWrapper.py
More file actions
281 lines (207 loc) · 8.97 KB
/
Copy pathModelWrapper.py
File metadata and controls
281 lines (207 loc) · 8.97 KB
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
279
280
import json
import logging
import torch
from torch.autograd import Variable
from torchvision import transforms
from PIL import Image
import nltk
import numpy as np
from models import VQGModel, QAModel, VQAModel, TopKDecoder
from VisualGenomeQA import load_vocab, Vocabulary
class ModelWrapper(object):
""" Wrapper class for sequence generation models
This class provides interface for pytorch models.
Attributes:
- model(nn.Module): pytorch model instance
Methods:
- init_model: initialize a model from a config file
- predict:
"""
def __init__(self, Model, config_path):
"""
Args:
- Model: sequence generation model class
- config_path: path to json file which contains model configs
"""
self.logger = logging.getLogger(__name__)
self.model = self.init_model(Model, config_path)
def init_model(self, Model, config_path):
with open(config_path) as c:
config = json.load(c)
# Get model config
model_config = config[Model.__name__]
self.logger.debug('%s config:' % (Model.__name__))
self.logger.debug(model_config)
# Load vocab
self.logger.debug('Loading vocab from %s...' %(model_config['vocab_path']))
self.vocab = load_vocab(model_config['vocab_path'])
# Initialize model
self.logger.debug('Initializing model...')
model = Model(len(self.vocab),
model_config['max_len'],
model_config['hidden_size'],
self.vocab(self.vocab.sos),
self.vocab(self.vocab.eos))
# Load model weights
model.load_state_dict(torch.load(model_config['model_path']))
self.beam_size = model_config['beam_size']
model.decoder = TopKDecoder(model.decoder, self.beam_size)
model.eval()
if torch.cuda.is_available():
model.cuda()
self.logger.debug('Done')
return model
def decode_topk(self, *args):
""" Given a list of inputs, output list of topk outputs for each
input. (k = self.beam_size)
Returns:
topk_outputs(list): list of topk outputs for each input.
tokk_probs(np.array): array containing probabilities for
topk_outputs (batch_size, beam_size).
"""
topk_outputs = []
softmax_list, _, other = self.model(*args)
topk_length = other['topk_length']
topk_sequence = other['topk_sequence']
for i in range(args[0].size(0)):
outputs = []
for k in range(self.beam_size):
length = topk_length[i][k]
sequence = [seq[i, k] for seq in topk_sequence]
tgt_id_seq = [sequence[di].data[0] for di in range(length)]
tgt_seq = [self.vocab.idx2word[tok] for tok in tgt_id_seq]
outputs.append(' '.join(tgt_seq[:-1]))
topk_outputs.append(outputs)
scores = other['score']
topk_probs = torch.exp(scores).cpu().numpy()
return topk_outputs, topk_probs
def predict(self, *args, **kwargs):
raise NotImplementedError()
class VQGWrapper(ModelWrapper):
""" Wrapper class for VQGModel"""
def __init__(self, config_path):
super(VQGWrapper, self).__init__(VQGModel, config_path)
# transformation applied to input images
self.transform = transforms.Compose([
transforms.Scale(244),
transforms.CenterCrop(244),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
def predict(self, img_paths):
"""
Given a list of images, generate topk questions for each image
Args:
img_paths([str]) - list of image paths
Output:
topk_questions ([[str]]) - list of list of questions
"""
images_var = []
# Create input variable of images
for img_path in img_paths:
image = Image.open(img_path)
image_tensor = Variable(self.transform(image))
images_var.append(image_tensor)
images_var = torch.stack(images_var)
if torch.cuda.is_available():
images_var = images_var.cuda()
# Run the model
topk_questions, topk_probs = self.decode_topk(images_var)
return topk_questions, topk_probs
class QAWrapper(ModelWrapper):
""" Wrapper class for QAModel"""
def __init__(self, config_path):
super(QAWrapper, self).__init__(QAModel, config_path)
def predict(self, questions):
""" Given a list of questions, generate topk answers for each question
Args:
answers[[str]] - list of topk answers for each question
"""
# Tokenize question
questions = [nltk.tokenize.word_tokenize(str(question).lower())\
for question in questions]
# RNN requires input sequences to be sorted by length
questions.sort(key = lambda q: len(q), reverse=True)
input_lengths = [len(q) for q in questions]
max_length = max(input_lengths)
# Create input variable of questions
questions_var = torch.zeros(len(questions), max_length).long()
for i, tokens in enumerate(questions):
question = [self.vocab(token) for token in tokens]
end = len(question)
question_tensor = torch.Tensor(question).long()
questions_var[i, :end] = question_tensor[:end]
questions_var = Variable(questions_var)
if torch.cuda.is_available():
questions_var = questions_var.cuda()
# Run the model
topk_answers, topk_probs = self.decode_topk(questions_var, input_lengths)
questions = [' '.join(tokens) for tokens in questions]
return questions, topk_answers, topk_probs
def rank(self, questions):
""" Rank a list of questions by sum of topk answer probs
Args:
questions[str]
"""
questions, topk_answers, topk_probs = self.predict(questions)
# Rank questions by sum of topk answer probs
# LOW sum of topk answer probs means
# HIGH question uncertainty
questions_topk_probs = zip(questions, topk_probs)
questions_topk_probs.sort(key = lambda s: sum(s[1]))
return [question for question, _ in questions_topk_probs]
class VQAWrapper(ModelWrapper):
""" Wrapper class for VQAModel"""
def __init__(self, config_path):
super(VQAWrapper, self).__init__(VQAModel, config_path)
# transformation applied to input images
self.transform = transforms.Compose([
transforms.Scale(244),
transforms.CenterCrop(244),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
def predict(self, img_questions):
""" Given a list of image-question pairs, generate topk answers for
each image-question pair
Args:
img_questions ([tuple(img_path, question)])
"""
img_paths = [pair[0] for pair in img_questions]
questions = [pair[1] for pair in img_questions]
# Tokenize question
questions = [nltk.tokenize.word_tokenize(str(question).lower())\
for question in questions]
# RNN requires input sequences to be sorted by length
# Sort question and index pairs
indices = range(len(questions))
qis = zip(questions, indices)
qis.sort(key=lambda qi: len(qi[0]), reverse=True)
questions, indices = zip(*qis)
input_lengths = [len(q) for q in questions]
max_length = max(input_lengths)
# Create input variable of images
images_var = []
for img_path in img_paths:
image = Image.open(img_path)
image_tensor = Variable(self.transform(image))
images_var.append(image_tensor)
images_var = torch.stack(images_var)
# Create input variable of questions
questions_var = torch.zeros(len(questions), max_length).long()
for i, tokens in enumerate(questions):
question = [self.vocab(token) for token in tokens]
end = len(question)
question_tensor = torch.Tensor(question).long()
questions_var[i, :end] = question_tensor[:end]
questions_var = Variable(questions_var)
if torch.cuda.is_available():
images_var = images_var.cuda()
questions_var = questions_var.cuda()
# Run the model
topk_answers, topk_probs = self.decode_topk(images_var, questions_var,
input_lengths)
# Resort questions using indices
qis = zip(questions, indices)
qis.sort(key=lambda qi: qi[1])
questions, indices = zip(*qis)
questions = [' '.join(tokens) for tokens in questions]
return questions, topk_answers, topk_probs