-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset_TDIUC.py
632 lines (554 loc) · 25.3 KB
/
dataset_TDIUC.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
"""
This code is modified from Hengyuan Hu's repository.
https://github.com/hengyuan-hu/bottom-up-attention-vqa
"""
from __future__ import print_function
import os
import json
import _pickle as cPickle
import numpy as np
import utils
import torch
from torch.utils.data import Dataset
import tools.compute_softscore
import itertools
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore",category=FutureWarning)
import h5py
COUNTING_ONLY = False
# Following Trott et al. (ICLR 2018)
# Interpretable Counting for Visual Question Answering
def is_howmany(q, a, label2ans):
if 'how many' in q.lower() or \
('number of' in q.lower() and 'number of the' not in q.lower()) or \
'amount of' in q.lower() or \
'count of' in q.lower():
if a is None or answer_filter(a, label2ans):
return True
else:
return False
else:
return False
def answer_filter(answers, label2ans, max_num=10):
for ans in answers['labels']:
if label2ans[ans].isdigit() and max_num >= int(label2ans[ans]):
return True
return False
class Dictionary(object):
def __init__(self, word2idx=None, idx2word=None):
if word2idx is None:
word2idx = {}
if idx2word is None:
idx2word = []
self.word2idx = word2idx
self.idx2word = idx2word
@property
def ntoken(self):
return len(self.word2idx)
@property
def padding_idx(self):
return len(self.word2idx)
def tokenize(self, sentence, add_word):
sentence = sentence.lower()
sentence = sentence.replace(',', '').replace('?', '').replace('\'s', ' \'s')
words = sentence.split()
tokens = []
if add_word:
for w in words:
tokens.append(self.add_word(w))
else:
for w in words:
# the least frequent word (`bebe`) as UNK for Visual Genome dataset
tokens.append(self.word2idx.get(w, self.padding_idx-1))
return tokens
def dump_to_file(self, path):
cPickle.dump([self.word2idx, self.idx2word], open(path, 'wb'))
print('dictionary dumped to %s' % path)
@classmethod
def load_from_file(cls, path):
print('loading dictionary from %s' % path)
word2idx, idx2word = cPickle.load(open(path, 'rb'))
d = cls(word2idx, idx2word)
return d
def add_word(self, word):
if word not in self.word2idx:
self.idx2word.append(word)
self.word2idx[word] = len(self.idx2word) - 1
return self.word2idx[word]
def __len__(self):
return len(self.idx2word)
def _create_entry(img, question, answer, answer_type):
if None!=answer:
answer.pop('image_id')
answer.pop('question_id')
entry = {
'question_id' : question['question_id'],
'image_id' : question['image_id'],
'image' : img,
'question' : question['question'],
'answer' : answer,
'answer_type': answer_type}
return entry
def is_json(myjson):
try:
json_object = json.loads(myjson)
except ValueError:
return False
return True
def _load_dataset(dataroot, name, img_id2val, label2ans):
"""Load entries
img_id2val: dict {img_id -> val} val can be used to retrieve image or features
dataroot: root path of dataset
name: 'train', 'val', 'test-dev2015', test2015'
"""
question_path = os.path.join(
dataroot, 'OpenEnded_mscoco_%s_questions.json' % \
(name + '2014' if 'test'!=name[:4] else name))
questions = sorted(json.load(open(question_path))['questions'],
key=lambda x: x['question_id'])
# Anotation
anotation_path = os.path.join(
dataroot, 'mscoco_%s_annotations.json' % \
(name + '2014' if 'test' != name[:4] else name))
if 'test'!=name[:4]: # train, val
flag=1
else:
flag=0
if flag==1:
anotations = sorted(json.load(open(anotation_path))['annotations'],
key=lambda x: x['question_id'])
if 'test'!=name[:4]: # train, val
answer_path = os.path.join(dataroot, 'cache', '%s_target.pkl' % name)
answers = cPickle.load(open(answer_path, 'rb'))
answers = sorted(answers, key=lambda x: x['question_id'])
utils.assert_eq(len(questions), len(answers))
entries = []
for question, answer, anotation in zip(questions, answers, anotations):
utils.assert_eq(question['question_id'], answer['question_id'])
utils.assert_eq(question['image_id'], answer['image_id'])
utils.assert_eq(question['question_id'], anotation['question_id'])
utils.assert_eq(question['image_id'], anotation['image_id'])
img_id = question['image_id']
#map onehot
map_anotation = ['object_presence', 'object_recognition', 'counting', 'color', 'attribute', 'activity_recognition', \
'sport_recognition', 'positional_reasoning', 'scene_recognition', 'sentiment_understanding', \
'utility_affordance','absurd']
anotation_onehot = (1.0*(np.array(map_anotation)== anotation['question_type'])).tolist()
if not COUNTING_ONLY or is_howmany(question['question'], answer, label2ans):
entries.append(_create_entry(img_id2val[img_id], question, answer, anotation_onehot))
else: # test2015
entries = []
for question in questions:
img_id = question['image_id']
if not COUNTING_ONLY or is_howmany(question['question'], None, None):
entries.append(_create_entry(img_id2val[img_id], question, None, torch.zeros(12)))
return entries
def _load_visualgenome(dataroot, name, img_id2val, label2ans, adaptive=True, q_type_map_link = 'question_type_mapping.txt'):
"""Load entries
img_id2val: dict {img_id -> val} val can be used to retrieve image or features
dataroot: root path of dataset
name: 'train', 'val'
"""
question_path = os.path.join(dataroot, 'question_answers.json')
image_data_path = os.path.join(dataroot, 'image_data.json')
ans2label_path = os.path.join(dataroot, 'cache', 'trainval_ans2label.pkl')
cache_path = os.path.join(dataroot, 'cache', 'vg_%s%s_target.pkl' % (name, '_adaptive' if adaptive else ''))
# Mapping file loading
with open(q_type_map_link, 'r') as infile:
inp = infile.readlines()
qT_map = [] #qT_map: mapping file
for i in inp:
label = np.array(i[:-1].split(' ')).astype(int)[1]
qT_map.append(label)
#Main phase
if os.path.isfile(cache_path):
entries = cPickle.load(open(cache_path, 'rb'))
else:
entries = []
ans2label = cPickle.load(open(ans2label_path, 'rb'))
vgq = json.load(open(question_path, 'r'))
_vgv = json.load(open(image_data_path, 'r')) #108,077
vgv = {}
for _v in _vgv:
if None != _v['coco_id']:
vgv[_v['id']] = _v['coco_id']
counts = [0, 0, 0, 0] # used image, used question, total question, out-of-split
for vg in vgq:
coco_id = vgv.get(vg['id'], None)
if None != coco_id:
counts[0] += 1
img_idx = img_id2val.get(coco_id, None)
if None == img_idx:
counts[3] += 1
for q in vg['qas']:
counts[2] += 1
_answer = tools.compute_softscore.preprocess_answer(q['answer'])
label = ans2label.get(_answer, None)
if None != label and None != img_idx:
counts[1] += 1
answer_type = torch.tensor([0.0,0.0,0.0])
answer_type[[int(label)]]+=1.0
answer = {
'labels': [label],
'scores': [1.]}
entry = {
'question_id' : q['id'],
'image_id' : coco_id,
'image' : img_idx,
'question' : q['question'],
'answer' : answer,
'answer_type' : answer_type}
if not COUNTING_ONLY or is_howmany(q['question'], answer, label2ans):
entries.append(entry)
print('Loading VisualGenome %s' % name)
print('\tUsed COCO images: %d/%d (%.4f)' % \
(counts[0], len(_vgv), counts[0]/len(_vgv)))
print('\tOut-of-split COCO images: %d/%d (%.4f)' % \
(counts[3], counts[0], counts[3]/counts[0]))
print('\tUsed VG questions: %d/%d (%.4f)' % \
(counts[1], counts[2], counts[1]/counts[2]))
with open(cache_path, 'wb') as f:
cPickle.dump(entries, open(cache_path, 'wb'))
return entries
def _find_coco_id(vgv, vgv_id):
for v in vgv:
if v['id']==vgv_id:
return v['coco_id']
return None
class VQAFeatureDataset(Dataset):
def __init__(self, name, args, dictionary, dataroot='data', adaptive=False, max_boxes=100, question_len=12):
super(VQAFeatureDataset, self).__init__()
assert name in ['train', 'val', 'test-dev2015', 'test2015']
dataroot = args.TDIUC_dir
ans2label_path = os.path.join(dataroot, 'cache', 'trainval_ans2label.pkl')
label2ans_path = os.path.join(dataroot, 'cache', 'trainval_label2ans.pkl')
self.ans2label = cPickle.load(open(ans2label_path, 'rb'))
self.label2ans = cPickle.load(open(label2ans_path, 'rb'))
self.num_ans_candidates = len(self.ans2label)
# Get the number of answer type class
lines = open(args.question_type_mapping, 'r')
get_lines = lines.readlines()
candidate_qt = set([line.split(' ')[1].strip() for line in get_lines])
self.num_qts_candidates = len(candidate_qt) #number of answer type class
# End get the number of answer type class
self.dictionary = dictionary
self.adaptive = adaptive
self.img_id2idx = cPickle.load(
open(os.path.join(dataroot, '%s%s_imgid2idx.pkl' % (name, '' if self.adaptive else '36')), 'rb'))
h5_path = os.path.join(dataroot, '%s%s.hdf5' % (name, '' if self.adaptive else '36'))
self.entries = _load_dataset(dataroot, name, self.img_id2idx, self.label2ans)
print('loading features from h5 file', h5_path)
with h5py.File(h5_path, 'r') as hf:
self.features = np.array(hf.get('image_features'))
self.spatials = np.array(hf.get('spatial_features'))
if self.adaptive:
self.pos_boxes = np.array(hf.get('pos_boxes'))
self.max_boxes = max_boxes
#self.entries = _load_dataset(dataroot, name, self.img_id2idx, self.label2ans)
self.tokenize(question_len)
self.tensorize()
self.v_dim = self.features.size(1 if self.adaptive else 2)
self.s_dim = self.spatials.size(1 if self.adaptive else 2)
def tokenize(self, max_length=12):
"""Tokenizes the questions.
This will add q_token in each entry of the dataset.
-1 represent nil, and should be treated as padding_idx in embedding
"""
for entry in self.entries:
tokens = self.dictionary.tokenize(entry['question'], False)
tokens = tokens[:max_length]
if len(tokens) < max_length:
# Note here we pad in front of the sentence
padding = [self.dictionary.padding_idx] * (max_length - len(tokens))
tokens = tokens + padding
utils.assert_eq(len(tokens), max_length)
entry['q_token'] = tokens
def tensorize(self):
self.features = torch.from_numpy(self.features)
self.spatials = torch.from_numpy(self.spatials)
for entry in self.entries:
question = torch.from_numpy(np.array(entry['q_token']))
entry['q_token'] = question
answer = entry['answer']
if None!=answer:
labels = np.array(answer['labels'])
scores = np.array(answer['scores'], dtype=np.float32)
if len(labels):
labels = torch.from_numpy(labels)
scores = torch.from_numpy(scores)
entry['answer']['labels'] = labels
entry['answer']['scores'] = scores
else:
entry['answer']['labels'] = None
entry['answer']['scores'] = None
def __getitem__(self, index):
entry = self.entries[index]
if not self.adaptive:
features = self.features[entry['image']]
spatials = self.spatials[entry['image']]
else:
features = self.features[self.pos_boxes[entry['image']][0]:self.pos_boxes[entry['image']][1],:]
if features.size(0) > self.max_boxes:
features = features[:self.max_boxes]
spatials = self.spatials[self.pos_boxes[entry['image']][0]:self.pos_boxes[entry['image']][1],:]
if spatials.size(0) > self.max_boxes:
spatials = spatials[:self.max_boxes]
question = entry['q_token']
question_id = entry['question_id']
answer = entry['answer']
answer_type = torch.tensor(entry['answer_type'])
if None!=answer:
labels = answer['labels']
scores = answer['scores']
target = torch.zeros(self.num_ans_candidates)
if labels is not None:
target.scatter_(0, labels, scores)
return features, spatials, question, target, answer_type
else:
return features, spatials, question, question_id, answer_type
def __len__(self):
return len(self.entries)
class VisualGenomeFeatureDataset(Dataset):
def __init__(self, name, features, spatials, dictionary, dataroot='data', adaptive=False, pos_boxes=None, max_boxes=100, question_len=12):
super(VisualGenomeFeatureDataset, self).__init__()
# do not use test split images!
assert name in ['train', 'val']
ans2label_path = os.path.join(dataroot, 'cache', 'trainval_ans2label.pkl')
label2ans_path = os.path.join(dataroot, 'cache', 'trainval_label2ans.pkl')
self.ans2label = cPickle.load(open(ans2label_path, 'rb'))
self.label2ans = cPickle.load(open(label2ans_path, 'rb'))
self.num_ans_candidates = len(self.ans2label)
self.dictionary = dictionary
self.adaptive = adaptive
self.img_id2idx = cPickle.load(
open(os.path.join(dataroot, '%s%s_imgid2idx.pkl' % (name, '' if self.adaptive else '36')), 'rb'))
self.features = features
self.spatials = spatials
if self.adaptive:
self.pos_boxes = pos_boxes
self.max_boxes = max_boxes
self.entries = _load_visualgenome(dataroot, name, self.img_id2idx, self.label2ans)
self.tokenize(question_len)
self.tensorize()
self.v_dim = self.features.size(1 if self.adaptive else 2)
self.s_dim = self.spatials.size(1 if self.adaptive else 2)
def tokenize(self, max_length=12):
"""Tokenizes the questions.
This will add q_token in each entry of the dataset.
-1 represent nil, and should be treated as padding_idx in embedding
"""
for entry in self.entries:
tokens = self.dictionary.tokenize(entry['question'], False)
tokens = tokens[:max_length]
if len(tokens) < max_length:
# Note here we pad in front of the sentence
padding = [self.dictionary.padding_idx] * (max_length - len(tokens))
tokens = tokens + padding
utils.assert_eq(len(tokens), max_length)
entry['q_token'] = tokens
def tensorize(self):
for entry in self.entries:
question = torch.from_numpy(np.array(entry['q_token']))
entry['q_token'] = question
answer = entry['answer']
labels = np.array(answer['labels'])
scores = np.array(answer['scores'], dtype=np.float32)
if len(labels):
labels = torch.from_numpy(labels)
scores = torch.from_numpy(scores)
entry['answer']['labels'] = labels
entry['answer']['scores'] = scores
else:
entry['answer']['labels'] = None
entry['answer']['scores'] = None
def __getitem__(self, index):
entry = self.entries[index]
if not self.adaptive:
features = self.features[entry['image']]
spatials = self.spatials[entry['image']]
else:
features = self.features[self.pos_boxes[entry['image']][0]:self.pos_boxes[entry['image']][1],:]
if features.size(0) > self.max_boxes:
features = features[0:self.max_boxes]
spatials = self.spatials[self.pos_boxes[entry['image']][0]:self.pos_boxes[entry['image']][1],:]
if spatials.size(0) > self.max_boxes:
spatials = spatials[0:self.max_boxes]
question = entry['q_token']
question_id = entry['question_id']
answer = entry['answer']
labels = answer['labels']
scores = answer['scores']
target = torch.zeros(self.num_ans_candidates)
if labels is not None:
target.scatter_(0, labels, scores)
return features, spatials, question, target
def __len__(self):
return len(self.entries)
class VQAVGFeatureDataset(Dataset):
def __init__(self, name, dictionary, dataroot='data', adaptive=False):
super(VQAVGFeatureDataset, self).__init__()
assert name in ['train', 'val', 'test-dev2015', 'test2015']
ans2label_path = os.path.join(dataroot, 'cache', 'trainval_ans2label.pkl')
label2ans_path = os.path.join(dataroot, 'cache', 'trainval_label2ans.pkl')
self.ans2label = cPickle.load(open(ans2label_path, 'rb'))
self.label2ans = cPickle.load(open(label2ans_path, 'rb'))
self.num_ans_candidates = len(self.ans2label)
self.dictionary = dictionary
self.adaptive = adaptive
self.img_id2idx = cPickle.load(
open(os.path.join(dataroot, '%s%s_imgid2idx.pkl' % (name, '' if self.adaptive else '36')), 'rb'))
h5_path = os.path.join(dataroot, '%s%s.hdf5' % (name, '' if self.adaptive else '36'))
print('loading features from h5 file')
with h5py.File(h5_path, 'r') as hf:
self.features = np.array(hf.get('image_features'))
self.spatials = np.array(hf.get('spatial_features'))
if self.adaptive:
self.pos_boxes = np.array(hf.get('pos_boxes'))
self.entries = _load_visualgenome(dataroot, name, self.img_id2idx, self.label2ans)
self.tokenize()
self.tensorize()
self.v_dim = self.features.size(1 if self.adaptive else 2)
self.s_dim = self.spatials.size(1 if self.adaptive else 2)
def tokenize(self, max_length=12):
"""Tokenizes the questions.
This will add q_token in each entry of the dataset.
-1 represent nil, and should be treated as padding_idx in embedding
"""
for entry in self.entries:
tokens = self.dictionary.tokenize(entry['question'], False)
tokens = tokens[:max_length]
if len(tokens) < max_length:
# Note here we pad in front of the sentence
padding = [self.dictionary.padding_idx] * (max_length - len(tokens))
tokens = tokens + padding
utils.assert_eq(len(tokens), max_length)
entry['q_token'] = tokens
def tensorize(self):
self.features = torch.from_numpy(self.features)
count = 0
for bbox in self.pos_boxes:
if (bbox[1]-bbox[0]) > 48:
count+=1
self.spatials = torch.from_numpy(self.spatials)
for entry in self.entries:
question = torch.from_numpy(np.array(entry['q_token']))
entry['q_token'] = question
answer = entry['answer']
if None!=answer:
labels = np.array(answer['labels'])
scores = np.array(answer['scores'], dtype=np.float32)
if len(labels):
labels = torch.from_numpy(labels)
scores = torch.from_numpy(scores)
entry['answer']['labels'] = labels
entry['answer']['scores'] = scores
else:
entry['answer']['labels'] = None
entry['answer']['scores'] = None
def __getitem__(self, index):
entry = self.entries[index]
if not self.adaptive:
features = self.features[entry['image']]
spatials = self.spatials[entry['image']]
else:
features = self.features[self.pos_boxes[entry['image']][0]:self.pos_boxes[entry['image']][1],:]
if features.size(0) > 50:
features = features[0:49]
spatials = self.spatials[self.pos_boxes[entry['image']][0]:self.pos_boxes[entry['image']][1],:]
# if spatials.size(0) > 50:
# spatials = spatials[0:49]
question = entry['q_token']
question_id = entry['question_id']
answer = entry['answer']
if None!=answer:
labels = answer['labels']
scores = answer['scores']
target = torch.zeros(self.num_ans_candidates)
if labels is not None:
target.scatter_(0, labels, scores)
return features, spatials, question, target
else:
return features, spatials, question, question_id
def __len__(self):
return len(self.entries)
def tfidf_from_questions(names, args, dictionary, dataroot='data', target=['vqa', 'vg']):
inds = [[], []] # rows, cols for uncoalesce sparse matrix
df = dict()
N = len(dictionary)
if args.use_TDIUC:
dataroot = args.TDIUC_dir
def populate(inds, df, text):
tokens = dictionary.tokenize(text, True)
for t in tokens:
df[t] = df.get(t, 0) + 1
combin = list(itertools.combinations(tokens, 2))
for c in combin:
if c[0] < N:
inds[0].append(c[0]); inds[1].append(c[1])
if c[1] < N:
inds[0].append(c[1]); inds[1].append(c[0])
if 'vqa' in target: # VQA 2.0
for name in names:
assert name in ['train', 'val', 'test-dev2015', 'test2015']
question_path = os.path.join(
dataroot, 'v2_OpenEnded_mscoco_%s_questions.json' % \
(name + '2014' if 'test'!=name[:4] else name))
questions = json.load(open(question_path))['questions']
for question in questions:
populate(inds, df, question['question'])
if 'vg' in target: # Visual Genome
question_path = os.path.join(dataroot, 'question_answers.json')
vgq = json.load(open(question_path, 'r'))
for vg in vgq:
for q in vg['qas']:
populate(inds, df, q['question'])
if 'cap' in target: # MSCOCO Caption
for split in ['train2017', 'val2017']:
captions = json.load(open('data/annotations/captions_%s.json' % split, 'r'))
for caps in captions['annotations']:
populate(inds, df, caps['caption'])
# TF-IDF
vals = [1] * len(inds[1])
for idx, col in enumerate(inds[1]):
assert df[col] >= 1, 'document frequency should be greater than zero!'
vals[col] /= df[col]
# Make stochastic matrix
def normalize(inds, vals):
z = dict()
for row, val in zip(inds[0], vals):
z[row] = z.get(row, 0) + val
for idx, row in enumerate(inds[0]):
vals[idx] /= z[row]
return vals
vals = normalize(inds, vals)
tfidf = torch.sparse.FloatTensor(torch.LongTensor(inds), torch.FloatTensor(vals))
tfidf = tfidf.coalesce()
# Latent word embeddings
emb_dim = 300
glove_file = os.path.join(args.TDIUC_dir, 'glove', 'glove.6B.%dd.txt' % emb_dim) if args.use_TDIUC else 'data/glove/glove.6B.%dd.txt' % emb_dim
weights, word2emb = utils.create_glove_embedding_init(dictionary.idx2word[N:], glove_file)
print('tf-idf stochastic matrix (%d x %d) is generated.' % (tfidf.size(0), tfidf.size(1)))
return tfidf, weights
if __name__=='__main__':
dictionary = Dictionary.load_from_file('data/dictionary.pkl')
tfidf, weights = tfidf_from_questions(['train', 'val', 'test2015'], dictionary, )
if __name__=='__main2__':
from torch.utils.data import DataLoader
dictionary = Dictionary.load_from_file('data/dictionary.pkl')
train_dset = VQAFeatureDataset('val', dictionary, adaptive=True)
# name = 'train'
# eval_dset = VQAFeatureDataset(name, dictionary)
# vg_dset = VisualGenomeFeatureDataset(name, eval_dset.features, eval_dset.spatials, dictionary)
# train_loader = DataLoader(vg_dset, 10, shuffle=True, num_workers=1)
loader = DataLoader(train_dset, 10, shuffle=True, num_workers=1, collate_fn=utils.trim_collate)
for i, (v, b, q, a) in enumerate(loader):
print(v.size())
# VisualGenome Train
# Used COCO images: 51487/108077 (0.4764)
# Out-of-split COCO images: 17464/51487 (0.3392)
# Used VG questions: 325311/726932 (0.4475)
# VisualGenome Val
# Used COCO images: 51487/108077 (0.4764)
# Out-of-split COCO images: 34023/51487 (0.6608)
# Used VG questions: 166409/726932 (0.2289)