-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_color.py
317 lines (273 loc) · 9.15 KB
/
generate_color.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
#%%
# imports
import os
import json
import pickle
import utils
import numpy as np
import torch
from tqdm import tqdm
from collections import defaultdict
import argparse
#%%
def parse_args():
parser = argparse.ArgumentParser("Generate other augmented answers.")
parser.add_argument(
'--dataset', default='cpv2',
choices=["v2", "cpv2"],
help="Run on VQA-2.0 instead of VQA-CP 2.0"
)
args = parser.parse_args()
return args
args = parse_args()
dataset = args.dataset
#%%
# get question type
if dataset == 'cpv2':
data_question_annotation = json.load(open('./data/vqacp_v2_train_annotations.json', 'r'))
else:
data_question_annotation = json.load(open('./data/v2_mscoco_train2014_annotations.json', 'r'))['annotations']
qid2qtype = {}
qid2type = {}
for anno in data_question_annotation:
qid = anno['question_id']
qtype = anno['question_type'].lower()
qid2qtype[qid] = qtype
qid2type[qid] = anno['answer_type']
if dataset == 'cpv2':
print('Load original data from: original_dataset.pkl')
with open('./aug_data/original_dataset.pkl', 'rb') as f:
original_dataset = pickle.load(f)
else:
print('Load original data from: v2_original_dataset.pkl')
with open('./aug_data/v2_original_dataset.pkl', 'rb') as f:
original_dataset = pickle.load(f)
print('Dataset size: ', len(original_dataset))
#%%
# handle sentence function
def handle(sentence:str):
sentence = sentence.lower()
sentence = sentence.replace(',', '').replace('?', '').replace('\'s', ' \'s').\
replace('-',' ').replace('.','').replace('"', '').replace('n\'t', ' not').\
replace('$', ' dollar ')
return sentence
#%% md
### Speed Up Techs
#%%
# 1. create exist qa triplets dict
print('1. Create exist qa triplets dict')
exist_triplets_dict = defaultdict(dict)
for entry in tqdm(original_dataset, ncols=100, total=len(original_dataset)):
exist_triplets_dict[handle(entry['question'])][entry['img_id']] = True
# 2. collect question information
print('2. Collect question information')
question_info = {}
for i in tqdm(range(len(original_dataset)), ncols=100, total=len(original_dataset)):
entry = original_dataset[i]
question = handle(entry['question'])
if question_info.get(question, None) is not None:
question_info[question]['entry_idxs'].append(i)
continue
info = {
'nouns': entry['nouns'],
'ori_nouns': entry['ori_nouns'],
'qtype': qid2qtype[entry['q_id']],
'type': qid2type[entry['q_id']],
'entry_idxs': [i],
'returned_imgs': [],
}
question_info[question] = info
# 3. create obj_dict for every entry
print('3. Create obj_dict for every entry')
for i in tqdm(range(len(original_dataset)), ncols=100, total=len(original_dataset)):
entry = original_dataset[i]
obj_dict = {}
for obj in entry['objects']:
obj_dict[obj] = True
entry['obj_dict'] = obj_dict
# 4. collect image info
print('4. Collect image info')
image_info = {}
for i in tqdm(range(len(original_dataset)), ncols=100, total=len(original_dataset)):
entry = original_dataset[i]
img_id = entry['img_id']
if image_info.get(img_id, None) is not None:
image_info[img_id]['entry_idxs'].append(i)
else:
info = {
'objects': entry['objects'],
'attributes': entry['attributes'],
'entry_idxs': [i],
'obj_dict': entry['obj_dict']
}
image_info[img_id] = info
# 5. create object to image_id
print('5. Create object to image image id')
obj2imgIds = defaultdict(list)
for img_id, info in tqdm(image_info.items(), ncols=100, total=len(image_info)):
obj_dict = info['obj_dict']
for obj in list(obj_dict.keys()):
obj2imgIds[obj].append(img_id)
#%% md
#### Simple AUG(Color Question)
#%%
# 6. find color questions
print('6. Find Color questions')
color_qs = {}
for question in list(question_info.keys()):
qtype = question_info[question]['qtype']
if 'color' in qtype.split():
color_qs[qtype] = True
color_qs.keys()
#%%
# 1. find color questions and only 1 noun
questions = list(question_info.keys())
color_questions = []
for question in questions:
# color question
qtype = question_info[question]['qtype']
if 'color' not in qtype.split():
continue
# only consider one noun
if len(question_info[question]['nouns']) != 1:
continue
color_questions.append(question)
#%%
# 2. find color attributes
color_attr_count_dict = defaultdict(int)
for entry in original_dataset:
question = handle(entry['question'])
qtype = question_info[question]['qtype']
if 'color' not in qtype.split():
continue
for ans in entry['answer_text']:
color_attr_count_dict[ans] += 1
color_attr_dict = {}
for key, value in color_attr_count_dict.items():
if value < 20:
continue
color_attr_dict[key] = True
color_attr_dict.keys()
valid_color_questions = []
for question in color_questions:
idxs = question_info[question]['entry_idxs']
nouns = question_info[question]['nouns']
if len(nouns) == 0:
continue
noun = nouns[0]
valid = True
for idx in idxs:
entry = original_dataset[idx]
objs = entry['objects']
attrs = entry['attributes']
answer = entry['answer_text']
colors = []
for i in range(len(objs)):
if objs[i] != noun:
continue
if attrs[i] == '':
continue
colors.append(attrs[i])
flag = False
for color in colors:
if color in entry['answer_text']:
flag = True
break
if not flag:
valid = False
break
if valid:
valid_color_questions.append(question)
print('Color Question Count: ', len(valid_color_questions))
#%%
print('6. Pair image-question pairs')
count = 0
for question in tqdm(valid_color_questions, total=len(valid_color_questions), ncols=80):
info = question_info[question]
nouns = info['nouns']
returned_imgs = {}
for noun in nouns:
for img_id in obj2imgIds[noun]:
if exist_triplets_dict[question].get(img_id, False):
continue
returned_imgs[img_id] = True
info['returned_imgs'] = list(returned_imgs.keys())
count = count + len(returned_imgs)
print('Color VQ Pairs:', count)
#%%
# Assign Answers
print('7. Assign initial answers and save')
color_aug_dataset = []
for question in tqdm(valid_color_questions, total=len(valid_color_questions), ncols=80):
info = question_info[question]
nouns = info['nouns']
returned_imgs = info['returned_imgs']
ori_noun = info['ori_nouns'][0]
for img_id in returned_imgs:
img_info = image_info[img_id]
attrs = img_info['attributes']
objs = img_info['objects']
# assign answer
noun = nouns[0]
ans = ''
for i in range(len(objs)):
if objs[i] != noun:
continue
if attrs[i] != '' and color_attr_dict.get(attrs[i], False):
ans = attrs[i]
break
if ans == '':
continue
newEntry = {
'q_id': 'color_aug_' + str(len(valid_color_questions)), # assign new question id
'img_id': img_id,
'question': question,
'answer_text': [ans],
'scores': [1.0],
'objects': img_info['objects'],
'nouns': nouns,
'qtype': info['qtype'],
'ori_nouns': info['ori_nouns']
}
# second step verification
valid = True
for idx in img_info['entry_idxs']:
entry_j = original_dataset[idx]
qid_j = entry_j['q_id']
if 'color' in qid2qtype[qid_j].split():
nouns_j = entry_j['nouns']
if not (len(nouns_j) == 1 and nouns_j[0] == noun):
continue
ans = newEntry['answer_text'][0]
if ans not in entry_j['answer_text']:
valid = False
break
if not valid:
continue
color_aug_dataset.append(newEntry)
# create more IQA triplets about colors
for i in range(len(objs)):
if objs[i] == noun or objs[i] == ori_noun:
continue
if attrs[i] == '' or not color_attr_dict.get(attrs[i], False):
continue
newEntry = {
'q_id': 'color_aug_' + str(len(color_aug_dataset)), # assign new question id
'img_id': img_id,
'question': question.replace(' ' + ori_noun, ' ' + objs[i]),
'answer_text': [attrs[i]],
'scores': [1.0],
'objects': img_info['objects'],
'nouns': nouns,
'qtype': info['qtype'],
'ori_nouns': [objs[i]]
}
color_aug_dataset.append(newEntry)
print('color augmented dataset:', len(color_aug_dataset))
#%%
if dataset == 'cpv2':
with open('./aug_data/cpv2_color_aug_dataset.pkl', 'wb') as f:
pickle.dump(color_aug_dataset, f)
else:
with open('./aug_data/v2_color_aug_dataset.pkl', 'wb') as f:
pickle.dump(color_aug_dataset, f)