-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivide.py
322 lines (273 loc) · 10.6 KB
/
divide.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
#%%
# Imports
import argparse
import json
import torch
from torch.utils.data import DataLoader
from dataset import Dictionary, VQAFeatureDataset
from aug_dataset import VQAAugFeatureDataset
import utils
from vqa_debias_loss_functions import *
from tqdm import tqdm
from torch.autograd import Variable
import pickle
import random
import torch
import clip
def parse_args():
parser = argparse.ArgumentParser("Divide Augmentated Samples to high quality or low quality")
parser.add_argument(
'--dataset', default='cpv2',
choices=["v2", "cpv2"],
help="Run on VQA-2.0 instead of VQA-CP 2.0"
)
parser.add_argument(
'--ratio', default=0.5, type=float,
help="High quality data's ratio"
)
args = parser.parse_args()
return args
#%%
args = parse_args()
# get param
dataset = args.dataset
# load clip
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
# Load augmented dataset
if dataset == 'cpv2':
# load data and generate statements
with open('./aug_data/cpv2_other_aug_dataset.pkl', 'rb') as f:
data_other = pickle.load(f)
with open('./aug_data/cpv2_color_aug_dataset.pkl', 'rb') as f:
data_color = pickle.load(f)
with open('./aug_data/cpv2_number_aug_dataset.pkl', 'rb') as f:
data_number = pickle.load(f)
with open('./aug_data/cpv2_paraphrasing_aug_dataset.pkl', 'rb') as f:
data_para = pickle.load(f)
else:
# load data and generate statements
with open('./aug_data/v2_other_aug_dataset.pkl', 'rb') as f:
data_other = pickle.load(f)
with open('./aug_data/v2_color_aug_dataset.pkl', 'rb') as f:
data_color = pickle.load(f)
with open('./aug_data/v2_number_aug_dataset.pkl', 'rb') as f:
data_number = pickle.load(f)
with open('./aug_data/v2_paraphrasing_aug_dataset.pkl', 'rb') as f:
data_para = pickle.load(f)
if dataset == 'cpv2':
cpv2_question_annotation = json.load(open('./data/vqacp_v2_train_annotations.json', 'r'))
qid2qtype = {}
qid2type = {}
for anno in cpv2_question_annotation:
qid = anno['question_id']
qtype = anno['question_type'].lower()
qid2qtype[qid] = qtype
qid2type[qid] = anno['answer_type']
else:
# get question type
v2_question_annotation = json.load(open('./data/v2_mscoco_train2014_annotations.json', 'r'))['annotations']
# %%
qid2qtype = {}
qid2type = {}
for anno in v2_question_annotation:
qid = anno['question_id']
qtype = anno['question_type'].lower()
qid2qtype[qid] = qtype
qid2type[qid] = anno['answer_type']
# load original dataset
if dataset == 'cpv2':
# Can Execute From Here
with open('./aug_data/original_dataset.pkl', 'rb') as f:
original_dataset = pickle.load(f)
else:
# Can Execute From Here
with open('./aug_data/v2_original_dataset.pkl', 'rb') as f:
original_dataset = pickle.load(f)
# 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
#%%
# 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
print("Generate statement for each vq pairs")
for entry in data_number:
question = entry['question']
qtype = question_info[question]['qtype']
ori_noun = question_info[question]['ori_nouns'][0]
ans = entry['answer_text'][0]
statement = question.replace(qtype, '').strip().replace(ori_noun, ans + ' ' + ori_noun)
entry['statement'] = statement
for entry in data_color:
question = entry['question']
qtype = entry['qtype']
ori_noun = entry['ori_nouns'][0]
ans = entry['answer_text'][0]
statement = question.replace(qtype, '').strip().replace(ori_noun, ans + ' ' + ori_noun)
entry['statement'] = statement
for entry in data_other:
question = entry['question']
qtype = question_info[question]['qtype']
ori_noun = question_info[question]['ori_nouns'][0]
ans = entry['answer_text'][0]
statement = question.replace(qtype, '').strip().replace(ori_noun, ans)
entry['statement'] = statement
for entry in data_para:
qtype = entry['qtype']
ans = ''
if len(entry['answer_text']) != 0:
ans = entry['answer_text'][0]
question = entry['question']
statement = question.replace(qtype, ans)
entry['statement'] = statement
if dataset == 'cpv2':
with open('./aug_data/cpv2_other_aug_dataset.pkl', 'wb') as f:
pickle.dump(data_other, f)
with open('./aug_data/cpv2_color_aug_dataset.pkl', 'wb') as f:
pickle.dump(data_color, f)
with open('./aug_data/cpv2_number_aug_dataset.pkl', 'wb') as f:
pickle.dump(data_number, f)
with open('./aug_data/cpv2_paraphrasing_aug_dataset.pkl', 'wb') as f:
pickle.dump(data_para, f)
else:
with open('./aug_data/v2_other_aug_dataset.pkl', 'wb') as f:
pickle.dump(data_other, f)
with open('./aug_data/v2_color_aug_dataset.pkl', 'wb') as f:
pickle.dump(data_color, f)
with open('./aug_data/v2_number_aug_dataset.pkl', 'wb') as f:
pickle.dump(data_number, f)
with open('./aug_data/v2_paraphrasing_aug_dataset.pkl', 'wb') as f:
pickle.dump(data_para, f)
if dataset == 'v2':
with open('./aug_data/v2_imgId_to_clip_feature_dict.pkl', 'rb') as f:
imgId_to_clip_feature_dict = pickle.load(f)
else:
with open('./aug_data/imgId_to_clip_feature_dict.pkl', 'rb') as f:
imgId_to_clip_feature_dict = pickle.load(f)
names = ['other', 'color', 'number', 'paraphrasing']
for name in names:
path = './aug_data/v2_color_aug_dataset.pkl'
if name == 'color':
path = './aug_data/v2_color_aug_dataset.pkl'
elif name == 'other':
path = './aug_data/v2_other_aug_dataset.pkl'
elif name == 'number':
path = './aug_data/v2_number_aug_dataset.pkl'
elif name == 'paraphrasing':
path = './aug_data/v2_paraphrasing_aug_dataset.pkl'
path = path.replace('v2', dataset)
high_quality_path = path.replace('dataset', 'dataset_high_quality')
low_quality_path = path.replace('dataset', 'dataset_low_quality')
#%%
print('\n\nLoad augmented data from: ', path)
print('Save results to: ', high_quality_path, 'and', low_quality_path)
#%%
with open(path, 'rb') as f:
aug_data = pickle.load(f)
# generate statements
unique_statements = {}
for entry in tqdm(aug_data, total=len(aug_data), ncols=80):
statement = entry['statement']
unique_statements[statement] = True
print("Extra statements' CLIP feature.")
statement_feature_dict = {}
batch_size = 256
batch_statements = []
for statements in tqdm(list(unique_statements.keys()), total=len(unique_statements), ncols=80):
# collect batch
batch_statements.append(statements)
if len(batch_statements) >= batch_size:
batch_text_tokens = clip.tokenize(batch_statements).to(device)
with torch.no_grad():
text_features = model.encode_text(batch_text_tokens).cpu()
for i in range(len(batch_statements)):
key = batch_statements[i]
feature = text_features[i]
statement_feature_dict[key] = feature
batch_statements = []
if len(batch_statements) > 0:
batch_text_tokens = clip.tokenize(batch_statements).to(device)
with torch.no_grad():
text_features = model.encode_text(batch_text_tokens).cpu()
for i in range(len(batch_statements)):
key = batch_statements[i]
feature = text_features[i]
statement_feature_dict[key] = feature
batch_statements = []
assert len(statement_feature_dict) == len(unique_statements)
print('Calculate similarity score')
for entry in tqdm(aug_data, total=len(aug_data), ncols=80):
# get image feature
img_id = entry['img_id']
img_feature = imgId_to_clip_feature_dict[img_id]
# get text feature
statement = entry['statement']
text_feature = statement_feature_dict[statement]
# sim
sim = torch.nn.functional.cosine_similarity(text_feature.float(), img_feature.float(), dim=0)
entry['sim'] = sim
#%%
print('Divide and save')
high_quality_entry = []
low_quality_entry = []
# high quality ratio
ratio = args.ratio
if ratio < 0:
if dataset == 'cpv2':
if name == 'color':
# threshold = 0.2065 # for cpv2 color
threshold = 0.2115 # for cpv2 color no missing answer
elif name == 'number':
threshold = 0.2222 # for cpv2 number
elif name == 'other':
# threshold = 0.237 # for cpv2 other
threshold = 0.2100 # for cpv2 other get rid of coco annotation
elif name == 'paraphrasing':
threshold = 0.2315
else:
if name == 'color':
threshold = 0.2077 # for v2 color
elif name == 'number':
threshold = 0.2218 # for v2 number
elif name == 'other':
# threshold = 0.2275 # for v2 other
threshold = 0.2120 # for v2 other get rid of coco annotation and no missing answer
elif name == 'paraphrasing':
threshold = 0.2295
else:
sims = []
for entry in aug_data:
sims.append(entry['sim'])
sorted_sims = sorted(sims)
threshold_idx = int((1 - ratio) * len(sims))
if threshold_idx >= len(sims) - 1:
threshold_idx = len(sims) - 2
threshold = sorted_sims[threshold_idx]
for entry in tqdm(aug_data, total=len(aug_data), ncols=80):
if entry['sim'] > threshold:
high_quality_entry.append(entry)
else:
low_quality_entry.append(entry)
with open(high_quality_path, 'wb') as f:
pickle.dump(high_quality_entry, f)
with open(low_quality_path, 'wb') as f:
pickle.dump(low_quality_entry, f)