-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_original_dataset.py
218 lines (180 loc) · 6.63 KB
/
process_original_dataset.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
# This code is to prepare Original IQA Triplets and extract meaningful nouns
# Original IQA Triplets:
# <question_id, img_id, question, answer>
import os
import json
import pickle
import utils
import numpy as np
import torch
from tqdm import tqdm
from collections import defaultdict
import argparse
import spacy
import nltk
import nltk.stem as ns
from nltk.corpus import wordnet as wn
def parse_args():
parser = argparse.ArgumentParser("Assign Label For Low Quality/High Quality DATA")
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
#### Prepare Three Sources
# - Original IQA Triplets
# - MSCOCO Annotations
# - Faster RCNN Detector Results
print("Prepare Original IQA Triplets")
if dataset == 'cpv2':
# open file of questions and answers
answer_path = os.path.join('data', 'cp-cache', 'train_target.pkl')
question_path = os.path.join('data', 'vqacp_v2_train_questions.json')
with open(question_path) as f:
questions = json.load(f)
with open(answer_path, 'rb') as f:
answers = pickle.load(f)
else:
# open file of questions and answers
answer_path = os.path.join('data', 'cache', 'train_target.pkl')
question_path = os.path.join('data', 'v2_OpenEnded_mscoco_train2014_questions.json')
with open(question_path) as f:
questions = json.load(f)["questions"]
with open(answer_path, 'rb') as f:
answers = pickle.load(f)
# sort by question id
questions.sort(key=lambda x: x['question_id'])
answers.sort(key=lambda x: x['question_id'])
utils.assert_eq(len(questions), len(answers))
# load label to answer file
if dataset == 'cpv2':
cache_file = os.path.join('data', 'cp-cache', 'trainval_label2ans.pkl')
label2ans = pickle.load(open(cache_file, 'rb'))
else:
cache_file = os.path.join('data', 'cache', 'trainval_label2ans.pkl')
label2ans = pickle.load(open(cache_file, 'rb'))
original_dataset = []
# read data into memory
for question, answer in tqdm(zip(questions, answers), total=len(questions), ncols=80):
if answer["labels"] is None:
raise ValueError()
utils.assert_eq(question['question_id'], answer['question_id'])
utils.assert_eq(question['image_id'], answer['image_id'])
q_id = question['question_id']
img_id = question['image_id']
q_text = question['question']
ans = []
score = []
for i in range(len(answer['labels'])):
ans.append(label2ans[answer['labels'][i]])
score.append(answer['scores'][i])
entry = {
'q_id': q_id,
'img_id': img_id,
'question': q_text,
'answer_text': ans,
'scores': score
}
original_dataset.append(entry)
# show exsample
print('Example: ', original_dataset[1])
##### Prepare Faster RCNN Detection Data (Filtered By Confidence of Detection)
print("\n\nPrepare Faster RCNN Detection Data")
# load vocabs of faster rcnn data result
data_path = './faster_rcnn_vocabs'
# Load classes
classes = ['__background__']
with open(os.path.join(data_path, 'objects_vocab.txt')) as f:
for object in f.readlines():
classes.append(object.split(',')[0].lower().strip())
# Load attributes
attributes = ['__no_attribute__']
with open(os.path.join(data_path, 'attributes_vocab.txt')) as f:
for att in f.readlines():
attributes.append(att.split(',')[0].lower().strip())
# prepare faster rcnn detection data
for entry in tqdm(original_dataset, total=len(original_dataset), ncols=100):
img_id = entry['img_id']
rcnn_feature = torch.load('data/rcnn_feature/' + str(img_id) + '.pth', encoding='iso-8859-1')
roi = rcnn_feature['spatial_feature']
bbox = roi[:, :4]
cls_score = rcnn_feature['cls_score']
attr_score = rcnn_feature['attr_score']
attr_thresh = 0.4
conf_thresh = 0.8
object_texts = []
attribute_texts = []
for i in range(36):
obj_idx = int(cls_score[i][0])
if obj_idx > 0 and cls_score[i][1] > conf_thresh:
object_texts.append(classes[obj_idx])
attr_idx = int(attr_score[i][0])
if attr_idx > 0:
attribute_texts.append(attributes[attr_idx])
else:
attribute_texts.append('')
entry['objects'] = object_texts
entry['attributes'] = attribute_texts
# show example
print('Example with faster rcnn detection results: ', original_dataset[1])
print("\n\nExtract Nouns of Question")
# 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
# 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']
nlp = spacy.load("en_core_web_sm")
lemmatizer = ns.WordNetLemmatizer()
for entry in tqdm(original_dataset, total=len(original_dataset), ncols=100):
question = handle(entry['question'])
qid = entry['q_id']
qtype = qid2qtype[qid]
meaningful_nouns = []
ori_nouns = []
# 1. remove question type
question = question.strip()
# 2. use spaCy POS to get meaningful words
doc = nlp(question)
qtype_doc = nlp(qtype)
for i in range(len(qtype_doc), len(doc)):
token = doc[i]
if token.pos_ != 'NOUN' and token.pos_ != 'PROPN':
continue
# 3. tokenize singular and plural forms
text = lemmatizer.lemmatize(token.text, pos=wn.NOUN)
# 4. remove photo and picture
if text == 'photo' or text == 'picture':
continue
meaningful_nouns.append(text)
ori_nouns.append(token.text)
entry['nouns'] = meaningful_nouns
entry['ori_nouns'] = ori_nouns
# example
print('Example with meaningful nouns: ', original_dataset[1])
print('Dataset Size: ', len(original_dataset))
if dataset == 'cpv2':
print('Saving to aug_data/original_dataset.pkl')
with open('./aug_data/original_dataset.pkl', 'wb') as f:
pickle.dump(original_dataset, f)
else:
print('Saving to aug_data/v2_original_dataset.pkl')
with open('./aug_data/v2_original_dataset.pkl', 'wb') as f:
pickle.dump(original_dataset, f)