-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
234 lines (195 loc) · 6.7 KB
/
filter.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
# %%
# Imports
import argparse
import json
import torch
from torch.utils.data import DataLoader
# from new_dataset import Dictionary, VQAFeatureDataset
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 os
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import random
def parse_args():
parser = argparse.ArgumentParser("CLIP-based filtering: filter out less-efficient augmented samples.")
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()
dataset = args.dataset
# %%
import torch
import clip
from PIL import Image
import pickle
# %%
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
# load dataset
if dataset == 'v2':
print('1.Load from v2_all_aug_dataset.pkl')
with open('./aug_data/v2_all_aug_dataset.pkl', 'rb') as f:
all_dataset = pickle.load(f)
else:
print('1.Load from cpv2_all_aug_dataset.pkl')
with open('./aug_data/cpv2_all_aug_dataset.pkl', 'rb') as f:
all_dataset = pickle.load(f)
print('Augmentated dataset size: ', len(all_dataset))
# load original dataset
if dataset == 'v2':
print('2.Load from v2_original_dataset.pkl')
with open('./aug_data/v2_original_dataset.pkl', 'rb') as f:
original_dataset = pickle.load(f)
else:
print('2.Load from original_dataset.pkl')
with open('./aug_data/original_dataset.pkl', 'rb') as f:
original_dataset = pickle.load(f)
print('Original 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
#%%
print('3. Collect Question Information')
from tqdm import tqdm
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'],
'entry_idxs': [i],
'returned_imgs': [],
}
question_info[question] = info
# collect all image information
print('4. Collect Image Information')
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 None:
info = {
# 'annotations': entry['annotations'],
'objects': entry['objects'],
'attributes': entry['attributes'],
# 'img_path': entry['img_path']
}
if dataset == 'v2':
info['img_path'] = entry['img_path']
image_info[img_id] = info
# load img id to feature
print('5. Load CLIP image features')
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)
# collect all nouns
unique_statements = {}
for entry in tqdm(all_dataset, total=len(all_dataset), ncols=80):
if 'nouns' not in entry.keys():
nouns = question_info[entry['question']]['nouns']
else:
nouns = entry['nouns']
for noun in nouns:
unique_statements['a photo of a ' + noun] = True
#%%
# get text feature
print('6. Extra CLIP text feature for each question')
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)
import numpy as np
#%%
print('7. Calculate similarity between image and question')
for entry in tqdm(all_dataset, total=len(all_dataset), ncols=80):
# get image feature
img_id = entry['img_id']
img_feature = imgId_to_clip_feature_dict[img_id]
# get text feature
sims = []
if 'nouns' not in entry.keys():
nouns = question_info[entry['question']]['nouns']
else:
nouns = entry['nouns']
for noun in nouns:
statement = 'a photo of a ' + noun
text_feature = statement_feature_dict[statement]
# sim
sim = torch.nn.functional.cosine_similarity(text_feature.float(), img_feature.float(), dim=0).cpu().numpy()
sims.append(sim)
if len(nouns) == 0:
sim_mean = 0
else:
sim_mean = np.array(sims).mean()
entry['sim_mean'] = sim_mean
ratio = args.ratio
if ratio < 0:
ratio = 0.5
sims = []
for entry in all_dataset:
sims.append(entry['sim_mean'])
sorted_sims = sorted(sims)
threshold_idx = int((1 - ratio) * len(sims))
if threshold_idx >= len(sims) - 1:
threshold_idx = len(sims) - 2
thre = sorted_sims[threshold_idx]
print('8. Determine thresh: ', thre)
print('9. Filter and save!')
high_entries = []
for entry in all_dataset:
if entry['sim_mean'] > thre:
high_entries.append(entry)
print('Filter ratio:', len(high_entries) / len(all_dataset))
if dataset == 'cpv2':
with open('./aug_data/cpv2_total_aug_dataset.pkl', 'wb') as f:
pickle.dump(high_entries, f)
else:
with open('./aug_data/v2_total_aug_dataset.pkl', 'wb') as f:
pickle.dump(high_entries, f)