forked from ChiuYeeLau/VQA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_preprocessing.py
More file actions
executable file
·69 lines (49 loc) · 2.04 KB
/
data_preprocessing.py
File metadata and controls
executable file
·69 lines (49 loc) · 2.04 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
import json
import os
import argparse
import pdb
def main(params):
train = []
test = []
imdir='v7w_%s.jpg'
print 'Loading annotations and questions...'
data = json.load(open('dataset_v7w_%s.json' %(params['data_set']), 'r'))["images"]
# pdb.set_trace()
pdb.set_trace()
for image in data:
# print image.keys()
for QA in image['qa_pairs']:
correct_ans = QA['answer']
question_id = QA['qa_id']
image_path = imdir%(QA["image_id"])
question = QA['question']
# add correct answer
if image['split'] == 'test':
test.append({'ques_id': question_id, 'img_path': image_path, 'question': question, 'MC_ans': correct_ans, 'ans': 1})
else:
train.append({'ques_id': question_id, 'img_path': image_path, 'question': question, 'MC_ans': correct_ans, 'ans': 1})
mc_ans = QA['multiple_choices']
if len(mc_ans) != 3:
print len(mc_ans)
# add wrong answers
for wrong_ans in mc_ans:
if image['split'] == 'test':
test.append({'ques_id': question_id, 'img_path': image_path, 'question': question, 'MC_ans': wrong_ans, 'ans': 0})
else:
train.append({'ques_id': question_id, 'img_path': image_path, 'question': question, 'MC_ans': wrong_ans, 'ans': 0})
# subtype = 'val2014'
# for i in range(len(val_anno['annotations'])):
# ans = val_anno['annotations'][i]['multiple_choice_answer']
# question_id = val_anno['annotations'][i]['question_id']
# image_path = imdir%(subtype, subtype, val_anno['annotations'][i]['image_id'])
# question = val_ques['questions'][i]['question']
# mc_ans = val_ques['questions'][i]['multiple_choices']
# test.append({'ques_id': question_id, 'img_path': image_path, 'question': question, 'MC_ans': mc_ans})
json.dump(train, open('vqa_raw_train.json', 'w'))
json.dump(test, open('vqa_raw_test.json', 'w'))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--data_set', default = 'telling',help = 'which data set, telling or pointing')
args = parser.parse_args()
params = vars(args) # convert to ordinary dict
main(params)