forked from GT-Vision-Lab/VQA_LSTM_CNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepro.py
executable file
·267 lines (207 loc) · 9.46 KB
/
prepro.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
"""
Preoricess a raw json dataset into hdf5/json files.
Caption: Use spaCy or NLTK or split function to get tokens.
"""
import copy
from random import shuffle, seed
import sys
import os.path
import argparse
import glob
import numpy as np
from scipy.misc import imread, imresize
import scipy.io
import pdb
import string
import h5py
from nltk.tokenize import word_tokenize
import json
import spacy.en
import re
def tokenize(sentence):
return [i for i in re.split(r"([-.\"',:? !\$#@~()*&\^%;\[\]/\\\+<>\n=])", sentence) if i!='' and i!=' ' and i!='\n'];
def prepro_question(imgs, params):
# preprocess all the question
print 'example processed tokens:'
for i,img in enumerate(imgs):
s = img['question']
if params['token_method'] == 'nltk':
txt = word_tokenize(str(s).lower())
elif params['token_method'] == 'spacy':
txt = [token.norm_ for token in params['spacy'](s)]
else:
txt = tokenize(s)
img['processed_tokens'] = txt
if i < 10: print txt
if i % 1000 == 0:
sys.stdout.write("processing %d/%d (%.2f%% done) \r" % (i, len(imgs), i*100.0/len(imgs)) )
sys.stdout.flush()
return imgs
def build_vocab_question(imgs, params):
# build vocabulary for question and answers.
count_thr = params['word_count_threshold']
# count up the number of words
counts = {}
for img in imgs:
for w in img['processed_tokens']:
counts[w] = counts.get(w, 0) + 1
cw = sorted([(count,w) for w,count in counts.iteritems()], reverse=True)
print 'top words and their counts:'
print '\n'.join(map(str,cw[:20]))
# print some stats
total_words = sum(counts.itervalues())
print 'total words:', total_words
bad_words = [w for w,n in counts.iteritems() if n <= count_thr]
vocab = [w for w,n in counts.iteritems() if n > count_thr]
bad_count = sum(counts[w] for w in bad_words)
print 'number of bad words: %d/%d = %.2f%%' % (len(bad_words), len(counts), len(bad_words)*100.0/len(counts))
print 'number of words in vocab would be %d' % (len(vocab), )
print 'number of UNKs: %d/%d = %.2f%%' % (bad_count, total_words, bad_count*100.0/total_words)
# lets now produce the final annotation
# additional special UNK token we will use below to map infrequent words to
print 'inserting the special UNK token'
vocab.append('UNK')
for img in imgs:
txt = img['processed_tokens']
question = [w if counts.get(w,0) > count_thr else 'UNK' for w in txt]
img['final_question'] = question
return imgs, vocab
def apply_vocab_question(imgs, wtoi):
# apply the vocab on test.
for img in imgs:
txt = img['processed_tokens']
question = [w if wtoi.get(w,len(wtoi)+1) != (len(wtoi)+1) else 'UNK' for w in txt]
img['final_question'] = question
return imgs
def get_top_answers(imgs, params):
counts = {}
for img in imgs:
ans = img['ans']
counts[ans] = counts.get(ans, 0) + 1
cw = sorted([(count,w) for w,count in counts.iteritems()], reverse=True)
print 'top answer and their counts:'
print '\n'.join(map(str,cw[:20]))
vocab = []
for i in range(params['num_ans']):
vocab.append(cw[i][1])
return vocab[:params['num_ans']]
def encode_question(imgs, params, wtoi):
max_length = params['max_length']
N = len(imgs)
label_arrays = np.zeros((N, max_length), dtype='uint32')
label_length = np.zeros(N, dtype='uint32')
question_id = np.zeros(N, dtype='uint32')
question_counter = 0
for i,img in enumerate(imgs):
question_id[question_counter] = img['ques_id']
label_length[question_counter] = min(max_length, len(img['final_question'])) # record the length of this sequence
question_counter += 1
for k,w in enumerate(img['final_question']):
if k < max_length:
label_arrays[i,k] = wtoi[w]
return label_arrays, label_length, question_id
def encode_answer(imgs, atoi):
N = len(imgs)
ans_arrays = np.zeros(N, dtype='uint32')
for i, img in enumerate(imgs):
ans_arrays[i] = atoi[img['ans']]
return ans_arrays
def encode_mc_answer(imgs, atoi):
N = len(imgs)
mc_ans_arrays = np.zeros((N, 18), dtype='uint32')
for i, img in enumerate(imgs):
for j, ans in enumerate(img['MC_ans']):
mc_ans_arrays[i,j] = atoi.get(ans, 0)
return mc_ans_arrays
def filter_question(imgs, atoi):
new_imgs = []
for i, img in enumerate(imgs):
if atoi.get(img['ans'],len(atoi)+1) != len(atoi)+1:
new_imgs.append(img)
print 'question number reduce from %d to %d '%(len(imgs), len(new_imgs))
return new_imgs
def get_unqiue_img(imgs):
count_img = {}
N = len(imgs)
img_pos = np.zeros(N, dtype='uint32')
for img in imgs:
count_img[img['img_path']] = count_img.get(img['img_path'], 0) + 1
unique_img = [w for w,n in count_img.iteritems()]
imgtoi = {w:i+1 for i,w in enumerate(unique_img)} # add one for torch, since torch start from 1.
for i, img in enumerate(imgs):
img_pos[i] = imgtoi.get(img['img_path'])
return unique_img, img_pos
def main(params):
if params['token_method'] == 'spacy':
print 'loading spaCy tokenizer for NLP'
params['spacy'] = spacy.en.English(data_dir=params['spacy_data'])
imgs_train = json.load(open(params['input_train_json'], 'r'))
imgs_test = json.load(open(params['input_test_json'], 'r'))
# get top answers
top_ans = get_top_answers(imgs_train, params)
atoi = {w:i+1 for i,w in enumerate(top_ans)}
itoa = {i+1:w for i,w in enumerate(top_ans)}
# filter question, which isn't in the top answers.
imgs_train = filter_question(imgs_train, atoi)
seed(123) # make reproducible
shuffle(imgs_train) # shuffle the order
# tokenization and preprocessing training question
imgs_train = prepro_question(imgs_train, params)
# tokenization and preprocessing testing question
imgs_test = prepro_question(imgs_test, params)
# create the vocab for question
imgs_train, vocab = build_vocab_question(imgs_train, params)
itow = {i+1:w for i,w in enumerate(vocab)} # a 1-indexed vocab translation table
wtoi = {w:i+1 for i,w in enumerate(vocab)} # inverse table
ques_train, ques_length_train, question_id_train = encode_question(imgs_train, params, wtoi)
imgs_test = apply_vocab_question(imgs_test, wtoi)
ques_test, ques_length_test, question_id_test = encode_question(imgs_test, params, wtoi)
# get the unique image for train and test
unique_img_train, img_pos_train = get_unqiue_img(imgs_train)
unique_img_test, img_pos_test = get_unqiue_img(imgs_test)
# get the answer encoding.
A = encode_answer(imgs_train, atoi)
MC_ans_test = encode_mc_answer(imgs_test, atoi)
# create output h5 file for training set.
N = len(imgs_train)
f = h5py.File(params['output_h5'], "w")
f.create_dataset("ques_train", dtype='uint32', data=ques_train)
f.create_dataset("ques_length_train", dtype='uint32', data=ques_length_train)
f.create_dataset("answers", dtype='uint32', data=A)
f.create_dataset("question_id_train", dtype='uint32', data=question_id_train)
f.create_dataset("img_pos_train", dtype='uint32', data=img_pos_train)
f.create_dataset("ques_test", dtype='uint32', data=ques_test)
f.create_dataset("ques_length_test", dtype='uint32', data=ques_length_test)
f.create_dataset("question_id_test", dtype='uint32', data=question_id_test)
f.create_dataset("img_pos_test", dtype='uint32', data=img_pos_test)
f.create_dataset("MC_ans_test", dtype='uint32', data=MC_ans_test)
f.close()
print 'wrote ', params['output_h5']
# create output json file
out = {}
out['ix_to_word'] = itow # encode the (1-indexed) vocab
out['ix_to_ans'] = itoa
out['unique_img_train'] = unique_img_train
out['unique_img_test'] = unique_img_test
json.dump(out, open(params['output_json'], 'w'))
print 'wrote ', params['output_json']
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# input json
parser.add_argument('--input_train_json', required=True, help='input json file to process into hdf5')
parser.add_argument('--input_test_json', required=True, help='input json file to process into hdf5')
parser.add_argument('--num_ans', required=True, type=int, help='number of top answers for the final classifications.')
parser.add_argument('--output_json', default='data_prepro.json', help='output json file')
parser.add_argument('--output_h5', default='data_prepro.h5', help='output h5 file')
# options
parser.add_argument('--max_length', default=26, type=int, help='max length of a caption, in number of words. captions longer than this get clipped.')
parser.add_argument('--word_count_threshold', default=0, type=int, help='only words that occur more than this number of times will be put in vocab')
parser.add_argument('--num_test', default=0, type=int, help='number of test images (to withold until very very end)')
parser.add_argument('--token_method', default='nltk', help='token method. set "spacy" for unigram paraphrasing')
parser.add_argument('--spacy_data', default='spacy_data', help='location of spacy NLP model')
parser.add_argument('--batch_size', default=10, type=int)
args = parser.parse_args()
params = vars(args) # convert to ordinary dict
print 'parsed input parameters:'
print json.dumps(params, indent = 2)
main(params)