-
Notifications
You must be signed in to change notification settings - Fork 1
/
VQA_graph_dataset.py
413 lines (343 loc) · 14.7 KB
/
VQA_graph_dataset.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from __future__ import absolute_import, division, print_function
import os
import json
import numpy as np
import zarr
import pandas as pd
from torch.utils.data import Dataset
from torch.utils.data import dataloader
from utils import normalize_adj
try:
import cPickle as pickle
except:
import pickle as pickle
class VQA_Graph_Dataset(Dataset):
def __init__(self, data_dir, emb_dim=300, train=True):
# Set parameters
self.data_dir = data_dir # directory where the data is stored
self.emb_dim = emb_dim # question embedding dimension
self.train = train # train (True) or eval (False) mode
self.seqlen = 14 # maximum question sequence length
# Load training question dictionary
q_dict = pickle.load(
open(os.path.join(data_dir, 'question_graph/train_data/vqa_trainval_q_dict.p'), 'rb'))
self.q_itow = q_dict['itow']
self.q_wtoi = q_dict['wtoi']
self.q_words = len(self.q_itow) + 1
# Load training answer dictionary
a_dict = pickle.load(
open(os.path.join(data_dir, 'question_graph/train_data/vqa_trainval_a_dict.p'), 'rb'))
self.a_itow = a_dict['itow']
self.a_wtoi = a_dict['wtoi']
self.n_answers = len(self.a_itow) + 1
# Load image features and bounding boxes
self.i_feat = zarr.open(os.path.join(
data_dir, 'vg_100/trainval.zarr'), mode='r')
self.bbox = zarr.open(os.path.join(
data_dir, 'vg_100/trainval_boxes.zarr'), mode='r')
self.sizes = pd.read_csv(os.path.join(
data_dir, 'vg_100/trainval_image_size.csv'))
self.visual_graph = pickle.load(
open(os.path.join(data_dir, 'visual_graph/vg_100/trainval_visual_graph_1_03.pkl'), 'rb'))
# Load questions
if train:
self.vqa = json.load(
open(os.path.join(data_dir, 'question_graph/train_data/vqa_train_q_graph.json')))
else:
self.vqa = json.load(
open(os.path.join(data_dir, 'question_graph/train_data/vqa_val_q_graph.json')))
self.n_questions = len(self.vqa)
print('Loading done')
self.feat_dim = self.i_feat[list(self.i_feat.keys())[
0]].shape[1] + 4 # + bbox
self.init_pretrained_wemb(emb_dim)
print('init_pretrained_wemb over!')
def init_pretrained_wemb(self, emb_dim):
"""
From blog.keras.io
Initialises words embeddings with pre-trained GLOVE embeddings
"""
embeddings_index = {}
f = open(os.path.join(self.data_dir, 'glove.6B.') +
str(emb_dim) + 'd.txt')
for line in f:
#values = line.split()
values = line.strip().split()
word = values[0]
try:
coefs = np.asarray(values[1:], dtype=np.float32)
except:
continue
embeddings_index[word] = coefs
f.close()
# question embedding dict
count = 0
embedding_mat_q = np.zeros((self.q_words, emb_dim), dtype=np.float32)
for word, i in self.q_wtoi.items():
embedding_v = embeddings_index.get(word)
if embedding_v is not None:
embedding_mat_q[i] = embedding_v
else:
count += 1
print('Unkonwn question words: ',count)
self.pretrained_wemb_q = embedding_mat_q
def __len__(self):
return self.n_questions
def __getitem__(self, idx):
# question sample
qlen = len(self.vqa[idx]['question_toked'])
q = [0] * 100
for i, w in enumerate(self.vqa[idx]['question_toked']):
try:
q[i] = self.q_wtoi[w.lower()]
except:
q[i] = 0 # validation questions may contain unseen word
# soft label answers
a = np.zeros(self.n_answers, dtype=np.float32)
for w, c in self.vqa[idx]['answers_w_scores']:
try:
a[self.a_wtoi[w]] = c
except:
continue
# number of votes for each answer
n_votes = np.zeros(self.n_answers, dtype=np.float32)
for w, c in self.vqa[idx]['answers']:
try:
n_votes[self.a_wtoi[w]] = c
except:
continue
# id of the question
qid = self.vqa[idx]['question_id']
# image sample
iid = self.vqa[idx]['image_id']
img = self.i_feat[str(iid)]
vg_boxes = np.asarray(self.bbox[str(iid)])
vg_edges = np.asarray(self.visual_graph[str(iid)])
imsize = self.sizes[str(iid)]
if np.logical_not(np.isfinite(img)).sum() > 0:
raise ValueError
# number of image objects
K = img.shape[0]
# question graph of question(rely on the nlp Dependency parser result )
#question_token = self.vqa[idx]['question_toked']
question_graph_nodes = self.vqa[idx]['question_parser_graph_nodes']
qglen = np.ones(self.seqlen).astype(np.int)
qg_nodes = np.zeros([self.seqlen, 10]).astype(np.int)
for i,node_id in enumerate(question_graph_nodes):
if i>= self.seqlen:
break
qglen[int(node_id)] = min(len(question_graph_nodes[node_id]), 10)
for j, w in enumerate(question_graph_nodes[node_id]):
if j>=10:
break
try:
qg_nodes[int(node_id)][j] = self.q_wtoi[w.lower()]
except:
qg_nodes[int(node_id)][j] = 0
qg_edges = np.asarray(self.vqa[idx]['question_A_Matrix'])
edges_num = qg_edges.shape[0]
if edges_num >= 14:
qg_edges = qg_edges[:14,:14]
else:
qg_edges_pad1 = np.zeros([edges_num, 14-edges_num])
qg_edges_pad2 = np.zeros([14-edges_num, 14])
qg_edges = np.concatenate([qg_edges, qg_edges_pad1], axis=1)
qg_edges = np.concatenate([qg_edges, qg_edges_pad2], axis=0)
# scale bounding boxes by image dimensions
for i in range(K):
bbox = vg_boxes[i]
bbox[0] /= imsize[0]
bbox[1] /= imsize[1]
bbox[2] /= imsize[0]
bbox[3] /= imsize[1]
vg_boxes[i] = bbox
qg_edges = normalize_adj(qg_edges,'DAD')
vg_edges = normalize_adj(vg_edges,'DAD')
# question graph nodes not sorted
qg_nodes = np.asarray(qg_nodes)
qg_edges = np.asarray(qg_edges).astype(np.float)
qglen = np.asarray(qglen)
# format variables
q = np.asarray(q)
a = np.asarray(a).reshape(-1)
n_votes = np.asarray(n_votes).reshape(-1)
qid = np.asarray(qid).reshape(-1)
# padding vg feat
vg_pad = np.zeros([100-K, self.feat_dim])
i = np.concatenate([img, vg_boxes], axis=1)
vg_nodes = np.concatenate([i, vg_pad], axis=0)
vg_edges = np.asarray(vg_edges).astype(np.float)
return q, a, n_votes, qid, vg_nodes, vg_edges, qg_nodes, qg_edges, qglen, qlen
class VQA_Graph_Dataset_Test(Dataset):
def __init__(self, data_dir, emb_dim=300, train=True):
self.data_dir = data_dir
self.emb_dim = emb_dim
self.train = train
self.seqlen = 14 # hard set based on paper
q_dict = pickle.load(
open(os.path.join(data_dir, 'question_graph/trainval_data/vqa_all_aug_q_dict.p'), 'rb'))
self.q_itow = q_dict['itow']
self.q_wtoi = q_dict['wtoi']
self.q_words = len(self.q_itow) + 1
a_dict = pickle.load(
open(os.path.join(data_dir, 'question_graph/train_data/vqa_trainval_a_dict.p'), 'rb'))
self.a_itow = a_dict['itow']
self.a_wtoi = a_dict['wtoi']
self.n_answers = len(self.a_itow) + 1
if train:
# Data augment with Visual Genome
self.vqa = json.load(open(os.path.join(data_dir, 'question_graph/trainval_data/vqa_trainval_q_graph.json'))) + json.load(open(os.path.join(data_dir, 'question_graph/trainval_data/vg_aug_train_q_graph.json'))) + json.load(open(os.path.join(data_dir, 'question_graph/trainval_data/vg_aug_val_q_graph.json')))
# No data augment with VG
#self.vqa = json.load(open(os.path.join(data_dir, 'question_graph/trainval_data/vqa_trainval_final_3129_q_graph.json')))
self.i_feat = zarr.open(os.path.join(
data_dir, 'vg_100/trainval.zarr'), mode='r')
self.bbox = zarr.open(os.path.join(
data_dir, 'vg_100/trainval_boxes.zarr'), mode='r')
self.sizes = pd.read_csv(os.path.join(
data_dir, 'vg_100/trainval_image_size.csv'))
self.visual_graph = pickle.load(
open(os.path.join(data_dir, 'visual_graph/vg_100/trainval_visual_graph_1_03.pkl'), 'rb'))
else:
self.vqa = json.load(
open(os.path.join(data_dir, 'question_graph/trainval_data/vqa_test_q_graph.json')))
#open(os.path.join(data_dir, 'vqa_test_toked.json')))
self.i_feat = zarr.open(os.path.join(
data_dir, 'vg_100/test.zarr'), mode='r')
self.bbox = zarr.open(os.path.join(
data_dir, 'vg_100/test_boxes.zarr'), mode='r')
self.sizes = pd.read_csv(os.path.join(
data_dir, 'vg_100/test_image_size.csv'))
self.visual_graph = pickle.load(
open(os.path.join(data_dir, 'visual_graph/vg_100/test_visual_graph_1_03.pkl'), 'rb'))
self.n_questions = len(self.vqa)
print("question loader over!")
self.feat_dim = self.i_feat[list(self.i_feat.keys())[
0]].shape[1] + 4 # + bbox
self.init_pretrained_wemb(emb_dim)
print('init_pretrained_wemb over!')
print('Loading done')
def init_pretrained_wemb(self, emb_dim):
"""
From blog.keras.io
Initialises words embeddings with pre-trained GLOVE embeddings
"""
embeddings_index = {}
f = open(os.path.join(self.data_dir, 'glove.6B.') +
str(emb_dim) + 'd.txt')
for line in f:
values = line.split()
word = values[0]
try:
coefs = np.asarray(values[1:], dtype=np.float32)
except:
continue
embeddings_index[word] = coefs
f.close()
# question embedding dict
count = 0
embedding_mat_q = np.zeros((self.q_words, emb_dim), dtype=np.float32)
for word, i in self.q_wtoi.items():
embedding_v = embeddings_index.get(word)
if embedding_v is not None:
embedding_mat_q[i] = embedding_v
else:
count += 1
print('Unkonwn question words: ',count)
self.pretrained_wemb_q = embedding_mat_q
def __len__(self):
return self.n_questions
def __getitem__(self, idx):
# question sample
qlen = len(self.vqa[idx]['question_toked'])
q = [0] * 100
for i, w in enumerate(self.vqa[idx]['question_toked']):
try:
q[i] = self.q_wtoi[w.lower()]
except:
q[i] = 0 # validation questions may contain unseen word
# soft label answers
if self.train:
a = np.zeros(self.n_answers, dtype=np.float32)
for w, c in self.vqa[idx]['answers_w_scores']:
try:
a[self.a_wtoi[w]] = c
except:
continue
a = np.asarray(a).reshape(-1)
else:
# return 0's for unknown test set answers
a = 0
# votes
if self.train:
n_votes = np.zeros(self.n_answers, dtype=np.float32)
for w, c in self.vqa[idx]['answers']:
try:
n_votes[self.a_wtoi[w]] = c
except:
continue
n_votes = np.asarray(n_votes).reshape(-1)
else:
# return 0's for unknown test set answers
n_votes = 0
# id of the question
qid = self.vqa[idx]['question_id']
# image sample
iid = self.vqa[idx]['image_id']
img = self.i_feat[str(iid)]
vg_boxes = np.asarray(self.bbox[str(iid)])
vg_edges = np.asarray(self.visual_graph[str(iid)])
imsize = self.sizes[str(iid)]
if np.logical_not(np.isfinite(img)).sum() > 0:
raise ValueError
# number of image objects
#K = 36
K = img.shape[0]
# question graph of question(rely on the nlp Dependency parser result )
#question_token = self.vqa[idx]['question_toked']
question_graph_nodes = self.vqa[idx]['question_parser_graph_nodes']
qglen = np.ones(self.seqlen).astype(np.int)
qg_nodes = np.zeros([self.seqlen, 10]).astype(np.int)
for i,node_id in enumerate(question_graph_nodes):
if i>= self.seqlen:
break
qglen[int(node_id)] = min(len(question_graph_nodes[node_id]), 10)
for j, w in enumerate(question_graph_nodes[node_id]):
if j>=10:
break
try:
qg_nodes[int(node_id)][j] = self.q_wtoi[w.lower()]
except:
qg_nodes[int(node_id)][j] = 0
qg_edges = np.asarray(self.vqa[idx]['question_A_Matrix'])
edges_num = qg_edges.shape[0]
if edges_num >= 14:
qg_edges = qg_edges[:14,:14]
else:
qg_edges_pad1 = np.zeros([edges_num, 14-edges_num])
qg_edges_pad2 = np.zeros([14-edges_num, 14])
qg_edges = np.concatenate([qg_edges, qg_edges_pad1], axis=1)
qg_edges = np.concatenate([qg_edges, qg_edges_pad2], axis=0)
qg_edges = normalize_adj(qg_edges,'DAD')
vg_edges = normalize_adj(vg_edges,'DAD')
# scale bounding boxes by image dimensions
for i in range(K):
bbox = vg_boxes[i]
bbox[0] /= imsize[0]
bbox[1] /= imsize[1]
bbox[2] /= imsize[0]
bbox[3] /= imsize[1]
vg_boxes[i] = bbox
qg_nodes = np.asarray(qg_nodes)
qg_edges = np.asarray(qg_edges).astype(np.float)
qglen = np.asarray(qglen)
# format variables
q = np.asarray(q)
a = np.asarray(a).reshape(-1)
n_votes = np.asarray(n_votes).reshape(-1)
qid = np.asarray(qid).reshape(-1)
# padding vg feat
vg_pad = np.zeros([100-K, self.feat_dim])
i = np.concatenate([img, vg_boxes], axis=1)
vg_nodes = np.concatenate([i, vg_pad], axis=0)
vg_edges = np.asarray(vg_edges).astype(np.float)
return q, a, n_votes, qid, vg_nodes, vg_edges, qg_nodes, qg_edges, qglen, qlen