/
tr_read.py
289 lines (249 loc) · 9.35 KB
/
tr_read.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from vqaTools.vqa import VQA
from collections import Counter
from stemmer import stem
import random
import os
import tensorflow as tf
from tensorflow.contrib import learn
import numpy as np
import pickle
import os
# Config of the dir
dataDir = '.'
versionType = 'v2_'
taskType = 'OpenEnded'
dataType = 'mscoco'
dataSubType = 'train2014' # Replace it to 'val2014' for validation
annFile = '%s/Annotations/%s%s_%s_annotations.json' % (dataDir,
versionType, dataType, dataSubType)
quesFile = '%s/Questions/%s%s_%s_%s_questions.json' % (dataDir,
versionType, taskType, dataType, dataSubType)
imgDir = '%s/Images/%s/%s/' % (dataDir, dataType, dataSubType)
gtDir = '%s/QuestionTypes/abstract_v002_question_types.txt' % dataDir
trDir = '%s/TR/%s_im.tfrecord' % (dataDir, dataSubType)
pkl_file = 'qa.pkl'
# Config of the data
RESIZE_SIZE = 256
BATCH_SIZE = 128
NUM_THREADS = 32
FIXED_NUM = 2048
class data_vqa:
""" Data class of VQA dataset. """
def __init__(
self,
resize_size=RESIZE_SIZE,
batch_size=BATCH_SIZE,
num_threads=NUM_THREADS,
fixed_num=FIXED_NUM,
):
""" Initlization """
print '[__init__]'
self.fixed_num = fixed_num
# Ininlize the offical json processing api
if os.path.isfile(pkl_file):
print '[info] init with saved pkl file.'
load = open(pkl_file, 'rb')
self.imgid_dict = pickle.load(load)
self.question_processed = pickle.load(load)
self.confidence = pickle.load(load)
self.answers = pickle.load(load)
self.answer_dict = pickle.load(load)
self.max_len_question = pickle.load(load)
load.close()
else:
print '[info] init without saved pkl file.'
self.data = VQA(annFile, quesFile)
self.data_ids = self.data.getQuesIds()
self.data_len = len(self.data_ids)
print(self.data_len)
self.copy_data()
del self.data
del self.data_ids
self.question_processed = self.process_question(\
self.questions,
self.max_len_question)
del self.questions
self.build_dict_question()
self.build_dict_answer()
save = open(pkl_file, 'wb')
pickle.dump(self.imgid_dict, save, -1)
pickle.dump(self.question_processed, save, -1)
pickle.dump(self.confidence, save, -1)
pickle.dump(self.answers, save, -1)
pickle.dump(self.answer_dict, save, -1)
pickle.dump(self.max_len_question, save, -1)
save.close()
print '[info]saved pkl file.'
# Build the reader of the tfrecord file
# The tfrecord file is generated by tr.write.py
feature = {'image': tf.FixedLenFeature([], tf.string),
'imgid': tf.FixedLenFeature([], tf.int64)}
filename_queue = tf.train.string_input_producer([trDir])
reader = tf.TFRecordReader()
(_, serialized_example) = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
features=feature)
image = tf.decode_raw(features['image'], tf.uint8)
image = tf.cast(image, tf.float32)
image = image / 255.
imgid = tf.cast(features['imgid'], tf.int32)
image = tf.reshape(image, [resize_size, resize_size, 3])
(self.op_images, self.op_imgids) = \
tf.train.shuffle_batch([image, imgid],
batch_size=batch_size,
capacity=20480,
num_threads=num_threads,
min_after_dequeue=10240)
def copy_data(self):
""" Copy the data from the official json api """
print ' [copy_data]'
self.answers = [[self.data.qa[data_id]['answers'][i]['answer'
].encode('ascii', 'ignore').lower() for i in
range(10)] for data_id in self.data_ids]
self.confidence = [[(lambda x: (1. if x == 'yes'
else 0.5))(self.data.qa[data_id]['answers'
][i]['answer_confidence'].encode('ascii',
'ignore')) for i in range(10)]
for data_id in self.data_ids]
self.imgids = [self.data.qa[data_id]['image_id'] for data_id in
self.data_ids]
self.questions = \
[self.preprocessing(self.data.qqa[ques_id]['question'])
for ques_id in self.data_ids]
self.max_len_question = max([len(question.split())
for question in self.questions])
print self.max_len_question
def build_dict_question(self):
""" Build the mapping from image's imgid to index of
image's questions index """
print ' [build_dict_question]'
self.imgid_dict = {}
imgid_set = list(set(self.imgids))
for imgid in imgid_set:
self.imgid_dict[imgid] = []
for i in range(self.data_len):
imgid = self.imgids[i]
self.imgid_dict[imgid].append(i)
def test_question(self):
print ' [test_question]'
chars = set()
for question in self.questions:
chars.update(question)
char_list = list(chars)
print len(char_list)
def build_dict_answer(self):
""" Build the mapping from answer's char set to id """
print ' [build_dict_answer]'
answer_list = []
for answers in self.answers:
for answer in answers:
answer_list.append(answer)
counts = Counter(answer_list)
top_n = counts.most_common(self.fixed_num)
fixed_list = [elem[0] for elem in top_n]
# print(fixed_list)
total = 0
for elem in top_n:
total += elem[1]
print top_n[self.fixed_num - 1][1]
print total
print len(answer_list)
self.answer_dict = dict((c, i) for (i, c) in
enumerate(fixed_list))
def preprocessing(self, text):
""" Replace the unusual character in the text """
to_replace = [
'!',
'#',
'%',
'$',
"'",
'&',
')',
'(',
'+',
'*',
'-',
',',
'/',
'.',
'1',
'0',
'3',
'2',
'5',
'4',
'7',
'6',
'9',
'8',
';',
':',
'?',
'_',
'^',
]
lowered = text.encode('ascii', 'ignore').lower()
replacing = lowered
for char_to_replace in to_replace:
replacing = replacing.replace(char_to_replace, ' '
+ char_to_replace + ' ')
stemming = ' '
splited = replacing.split()
# return replacing
return stemming.join([stem(item) for item in splited])
def tokenization(self, stentance, preprocess=True):
""" Split the stentance into words """
if preprocess == True:
stentance = self.preprocessing(stentance)
splited = stentance.split()
return splited
def process_question(self, sentences, max_len_question):
""" Preprocessing the question data """
print ' [process_question]'
question_list = []
for sentence in sentences:
splited = sentence.split()
for word in splited:
question_list.append(word)
counts = Counter(question_list)
top_n = counts.most_common(self.fixed_num)
fixed_list = [elem[0] for elem in top_n]
# print(fixed_list)
total = 0
for elem in top_n:
total += elem[1]
print top_n[self.fixed_num - 1][1]
print total
print len(question_list)
self.question_dict = dict((c, i) for (i, c) in
enumerate(fixed_list))
processed_question = []
for sentence in sentences:
splited = sentence.split()
processed_sentence = []
for word in splited:
processed_sentence.append(self.question_dict.get(word,
self.fixed_num))
processed_sentence = processed_sentence + [self.fixed_num] \
* (max_len_question - len(splited))
processed_question.append(processed_sentence)
return processed_question
def get_batch(self, imgids):
""" Get the next batch of data """
questions = []
answers = []
confidences = []
# (images, imgids) = sess.run([self.op_images, self.op_imgids])
for imgid in imgids:
index = random.choice(self.imgid_dict[imgid])
questions.append(self.question_processed[index])
answer_to_choice = random.choice(range(10))
confidences.append(self.confidence[index][answer_to_choice])
answer = self.answers[index][answer_to_choice]
answers.append(self.answer_dict.get(answer, self.fixed_num))
return (np.array(questions), np.array(answers),
np.array(confidences))
data = data_vqa()