-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdataset.py
More file actions
284 lines (250 loc) · 11.1 KB
/
dataset.py
File metadata and controls
284 lines (250 loc) · 11.1 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
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
import os
import numpy as np
import pandas as pd
from tqdm import tqdm
from nltk.tokenize import word_tokenize
from utils.vocabulary import Vocabulary
from utils.vqa.vqa import VQA
class DataSet(object):
def __init__(self,
image_files,
question_word_idxs,
question_lens,
question_ids,
batch_size,
answer_idxs = None,
is_train = False,
shuffle = False):
self.image_files = np.array(image_files)
self.question_word_idxs = np.array(question_word_idxs)
self.question_lens = np.array(question_lens)
self.question_ids = np.array(question_ids)
self.answer_idxs = np.array(answer_idxs)
self.batch_size = batch_size
self.is_train = is_train
self.shuffle = shuffle
self.setup()
def setup(self):
""" Setup the dataset. """
self.count = len(self.question_ids)
self.num_batches = int(np.ceil(self.count * 1.0 / self.batch_size))
self.fake_count = self.num_batches * self.batch_size - self.count
self.idxs = list(range(self.count))
self.reset()
def reset(self):
""" Reset the dataset. """
self.current_idx = 0
if self.shuffle:
np.random.shuffle(self.idxs)
def next_batch(self):
""" Fetch the next batch. """
assert self.has_next_batch()
if self.has_full_next_batch():
start, end = self.current_idx, self.current_idx + self.batch_size
current_idxs = self.idxs[start:end]
else:
start, end = self.current_idx, self.count
current_idxs = self.idxs[start:end]
current_idxs += list(np.random.choice(self.count, self.fake_count))
image_files = self.image_files[current_idxs]
question_word_idxs = self.question_word_idxs[current_idxs]
question_lens = self.question_lens[current_idxs]
if self.is_train:
answer_idxs = self.answer_idxs[current_idxs]
self.current_idx += self.batch_size
return image_files, question_word_idxs, question_lens, answer_idxs
else:
self.current_idx += self.batch_size
return image_files, question_word_idxs, question_lens
def has_next_batch(self):
""" Determine whether there is a batch left. """
return self.current_idx < self.count
def has_full_next_batch(self):
""" Determine whether there is a full batch left. """
return self.current_idx + self.batch_size <= self.count
def prepare_train_data(config):
""" Prepare the data for training the model. """
vqa = VQA(config.train_answer_file, config.train_question_file)
vqa.filter_by_ques_len(config.max_question_length)
vqa.filter_by_ans_len(1)
print("Reading the questions and answers...")
annotations = process_vqa(vqa,
'COCO_train2014',
config.train_image_dir,
config.temp_train_annotation_file)
image_files = annotations['image_file'].values
questions = annotations['question'].values
question_ids = annotations['question_id'].values
answers = annotations['answer'].values
print("Questions and answers read.")
print("Number of questions = %d" %(len(question_ids)))
print("Building the vocabulary...")
vocabulary = Vocabulary()
if not os.path.exists(config.vocabulary_file):
for question in tqdm(questions):
vocabulary.add_words(word_tokenize(question))
for answer in tqdm(answers):
vocabulary.add_words(word_tokenize(answer))
vocabulary.compute_frequency()
vocabulary.save(config.vocabulary_file)
else:
vocabulary.load(config.vocabulary_file)
print("Vocabulary built.")
print("Number of words = %d" %(vocabulary.size))
config.vocabulary_size = vocabulary.size
print("Processing the questions and answers...")
if not os.path.exists(config.temp_train_data_file):
question_word_idxs, question_lens = process_questions(questions,
vocabulary,
config)
answer_idxs = process_answers(answers, vocabulary)
data = {'question_word_idxs': question_word_idxs,
'question_lens': question_lens,
'answer_idxs': answer_idxs}
np.save(config.temp_train_data_file, data)
else:
data = np.load(config.temp_train_data_file).item()
question_word_idxs = data['question_word_idxs']
question_lens = data['question_lens']
answer_idxs = data['answer_idxs']
print("Questions and answers processed.")
print("Building the dataset...")
dataset = DataSet(image_files,
question_word_idxs,
question_lens,
question_ids,
config.batch_size,
answer_idxs,
True,
True)
print("Dataset built.")
return dataset, config
def prepare_eval_data(config):
""" Prepare the data for evaluating the model. """
vqa = VQA(config.eval_answer_file, config.eval_question_file)
vqa.filter_by_ques_len(config.max_question_length)
vqa.filter_by_ans_len(1)
print("Reading the questions...")
annotations = process_vqa(vqa,
'COCO_val2014',
config.eval_image_dir,
config.temp_eval_annotation_file)
image_files = annotations['image_file'].values
questions = annotations['question'].values
question_ids = annotations['question_id'].values
print("Questions read.")
print("Number of questions = %d" %(len(question_ids)))
print("Building the vocabulary...")
if os.path.exists(config.vocabulary_file):
vocabulary = Vocabulary(config.vocabulary_file)
else:
vocabulary = build_vocabulary(config)
print("Vocabulary built.")
print("Number of words = %d" %(vocabulary.size))
config.vocabulary_size = vocabulary.size
print("Processing the questions...")
if not os.path.exists(config.temp_eval_data_file):
question_word_idxs, question_lens = process_questions(questions,
vocabulary,
config)
data = {'question_word_idxs': question_word_idxs,
'question_lens': question_lens}
np.save(config.temp_eval_data_file, data)
else:
data = np.load(config.temp_eval_data_file).item()
question_word_idxs = data['question_word_idxs']
question_lens = data['question_lens']
print("Questions processed.")
print("Building the dataset...")
dataset = DataSet(image_files,
question_word_idxs,
question_lens,
question_ids,
config.batch_size)
print("Dataset built.")
return vqa, dataset, vocabulary, config
def prepare_test_data(config):
""" Prepare the data for testing the model. """
print("Reading the questions...")
annotations = pd.read_csv(config.test_question_file)
images = annotations['image'].unique()
image_files = [os.path.join(config.test_image_dir, f) for f in images]
temp = pd.DataFrame({'image': images, 'image_file': image_files})
annotations = pd.merge(annotations, temp)
annotations.to_csv(config.temp_test_info_file)
image_files = annotations['image_file'].values
questions = annotations['question'].values
question_ids = annotations['question_id'].values
print("Questions read.")
print("Number of questions = %d" %(len(question_ids)))
print("Building the vocabulary...")
if os.path.exists(config.vocabulary_file):
vocabulary = Vocabulary(config.vocabulary_file)
else:
vocabulary = build_vocabulary(config)
print("Vocabulary built.")
print("Number of words = %d" %(vocabulary.size))
config.vocabulary_size = vocabulary.size
print("Processing the questions...")
question_word_idxs, question_lens = process_questions(questions,
vocabulary,
config)
print("Questions processed.")
print("Building the dataset...")
dataset = DataSet(image_files,
question_word_idxs,
question_lens,
question_ids,
config.batch_size)
print("Dataset built.")
return dataset, vocabulary, config
def process_vqa(vqa, label, image_dir, annotation_file):
""" Build a temporary annotation file for training or evaluation. """
question_ids = list(vqa.qa.keys())
image_ids = [vqa.qa[k]['image_id'] for k in question_ids]
image_files = [os.path.join(image_dir, label+"_000000"+("%06d" %k)+".jpg")
for k in image_ids]
questions = [vqa.qqa[k]['question'] for k in question_ids]
answers = [vqa.qa[k]['best_answer'] for k in question_ids]
annotations = pd.DataFrame({'question_id': question_ids,
'image_id': image_ids,
'image_file': image_files,
'question': questions,
'answer': answers})
annotations.to_csv(annotation_file)
return annotations
def process_questions(questions, vocabulary, config):
""" Tokenize the questions and translate each token into its index \
in the vocabulary, and get the number of tokens. """
question_word_idxs = []
question_lens = []
for q in tqdm(questions):
word_idxs = vocabulary.process_sentence(q)
current_length = len(word_idxs)
current_word_idxs = np.zeros((config.max_question_length), np.int32)
current_word_idxs[:current_length] = np.array(word_idxs)
question_word_idxs.append(current_word_idxs)
question_lens.append(current_length)
return np.array(question_word_idxs), np.array(question_lens)
def process_answers(answers, vocabulary):
""" Translate the answers into their indicies in the vocabulary. """
answer_idxs = []
for answer in tqdm(answers):
answer_idxs.append(vocabulary.word_to_idx(word_tokenize(answer)[0]))
return np.array(answer_idxs)
def build_vocabulary(config):
""" Build the vocabulary from the training data and save it to a file. """
vqa = VQA(config.train_answer_file, config.train_question_file)
vqa.filter_by_ques_len(config.max_question_length)
vqa.filter_by_ans_len(1)
question_ids = list(vqa.qa.keys())
questions = [vqa.qqa[k]['question'] for k in question_ids]
answers = [vqa.qa[k]['best_answer'] for k in question_ids]
vocabulary = Vocabulary()
for question in tqdm(questions):
vocabulary.add_words(word_tokenize(question))
for answer in tqdm(answers):
vocabulary.add_words(word_tokenize(answer))
vocabulary.compute_frequency()
vocabulary.save(config.vocabulary_file)
return vocabulary