-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patheval_model_on_figureqa.py
273 lines (238 loc) · 8.9 KB
/
eval_model_on_figureqa.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
from glob import glob
import json
import os
import re
import shutil
import tarfile
import numpy as np
import pandas as pd
import tensorflow as tf
from tqdm import tqdm
from data.figureqa import FigureQA
from models.cnn_baseline import CNNBaselineModel
from models.rn import RNModel
from models.textonly_baseline import TextOnlyBaselineModel
DEBUG = False
TFDBG = False
if DEBUG and TFDBG:
from tensorflow.python import debug as tf_debug
parser = argparse.ArgumentParser()
parser.add_argument('--data-path', type=str, required=True,
help='parent folder of where the data set is located')
parser.add_argument('--tmp-path', type=str,
help=('tmp directory where to extract data set (optimally '
'on faster storage, if not specified DATA_PATH is '
'used)'))
parser.add_argument('--train-dir', type=str, required=True,
help='training directory of the trained model.')
parser.add_argument('--meta-file', type=str,
help='path to the meta-file from which to load parameters.')
parser.add_argument('--partition', type=str, default='test2',
help='name of the partition to evaluate the model on.')
parser.add_argument('--batchsize', type=int, default=64,
help='Size of the mini-batches.')
args = parser.parse_args()
train_dir = args.train_dir
# load config file (expects unique config file)
config_files = glob(os.path.join(train_dir, '*_on_figureqa_config.json'))
if len(config_files) == 0:
raise IOError('no config file found in TRAIN_DIR')
elif len(config_files) > 1:
raise IOError('more than one config file found in TRAIN_DIR')
config_file = config_files[0]
config = json.load(open(config_file))
model_str = config['model']['name']
if args.meta_file is None:
meta_file = os.path.join(train_dir, 'model_val_best.meta')
assert os.access(meta_file, os.R_OK)
else:
meta_file = args.meta_file
# copy FigureQA to tmp path, if not done before
figureqa_src_path = os.path.join(args.data_path, 'FigureQA')
figureqa_path = os.path.join(args.tmp_path, 'FigureQA')
assert not os.system('mkdir -p {}'.format(figureqa_path))
copy_done_file = os.path.join(figureqa_path, '.done')
same_path = os.path.samefile(figureqa_src_path, figureqa_path)
copy_done = os.path.isfile(copy_done_file)
if not (copy_done or same_path):
for fname in glob(os.path.join(figureqa_src_path, '*.tar.gz')):
shutil.copy2(fname, figureqa_path)
if not copy_done:
for fname in glob(os.path.join(figureqa_path, '*.tar.gz')):
print('extracting {}...'.format(fname))
f = tarfile.open(fname)
f.extractall(path=figureqa_path)
# if figureqa_path not equal to figureqa_src_path, we delete the
# copy of the archive
if not same_path:
os.remove(fname)
assert not os.system('touch {}'.format(copy_done_file))
no_images = model_str == 'TextOnlyBaseline'
if no_images:
im_size = None
else:
im_size = config['im_size']
print('loading {} data...'.format(args.partition))
data_object = FigureQA(
data_path=figureqa_path,
partition=args.partition,
shuffle=False,
load_dict_file='figureqa_dict.json',
im_size=im_size,
no_images=no_images
)
nsamples = len(data_object._questions)
dict_size = len(data_object.tok_to_idx)
if 'test' in args.partition:
# check if answers available. If not, just dump predictions...
try:
answers_df = pd.read_csv(
os.path.join(figureqa_path, args.partition, 'answers.csv'), index_col=0)
answers = answers_df['answer'].as_matrix()
except:
answers = None
else:
# not test partition, so true answers are collected as we iterate through
# the data
answers = []
data = data_object.get_data(batch_size=args.batchsize, return_indices=True,
return_question_strings=True)
# create the session
sess = tf.Session()
if DEBUG and TFDBG:
sess = tf_debug.LocalCLIDebugWrapperSession(
sess, dump_root='/data/home/vmichals/data/tfdbg')
sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)
print('restoring model graph...')
is_training = tf.placeholder(tf.bool, shape=(), name='is_training')
iterator = data.make_one_shot_iterator()
next_batch = iterator.get_next()
if answers == []:
(input_images, input_questions, input_questions_lengths,
answers_batch, question_indices, image_indices, question_ids,
question_strings) = next_batch
iter_answers = True
else:
(input_images, input_questions, input_questions_lengths, question_indices,
image_indices, question_ids, question_strings) = next_batch
iter_answers = False
# instantiate the model
if model_str == 'RN':
model = RNModel(
is_training=is_training,
config=config['model'],
dictionary_size=dict_size
)
elif model_str == 'CNNBaseline':
model = CNNBaselineModel(
is_training=is_training,
config=config['model'],
dictionary_size=dict_size
)
elif model_str == 'TextOnlyBaseline':
model = TextOnlyBaselineModel(
is_training=is_training,
config=config['model'],
dictionary_size=dict_size
)
else:
raise ValueError('unsupported model type found in config file')
input_kwargs = {
'q': input_questions,
'qlen': input_questions_lengths,
}
if model_str != 'TextOnlyBaseline':
input_kwargs['img'] = input_images
# NOTE: workaround, during training inference is nested inside
# the loss variable scope
with tf.variable_scope('loss'):
_, predictions = model.inference(**input_kwargs)
print('create lists of indices for dumping predictions.')
prediction_list = []
q_index_list = []
im_index_list = []
q_id_list = []
q_str_list = []
idx = 0
batch_cnt = 0
nbatches = int(np.ceil(float(nsamples) / args.batchsize))
print('nbatches: {0}'.format(nbatches, ))
print('nsamples: {0}'.format(nsamples, ))
dir(tf.contrib)
saver = tf.train.Saver(max_to_keep=100)
saver.restore(sess, os.path.splitext(meta_file)[0])
if answers is not None:
accuracy = 0.
preds_correct_list = []
# loop over all batches until tf.errors.OutOfRangeError
while True:
try:
print('fetching batch {}/{}'.format(batch_cnt + 1, nbatches))
# fetch predictions
if iter_answers:
(preds, answers_batch_np, q_indices, im_indices, q_ids,
q_strs) = sess.run(
(predictions, answers_batch, question_indices, image_indices,
question_ids, question_strings), feed_dict={
is_training: False,
}
)
answers.extend(answers_batch_np)
else:
(preds, q_indices, im_indices, q_ids, q_strs) = sess.run(
(predictions, question_indices, image_indices,
question_ids, question_strings), feed_dict={
is_training: False,
}
)
nbatch_samples = len(preds)
prediction_list.extend(preds.astype(bool).tolist())
q_index_list.extend(q_indices)
im_index_list.extend(im_indices)
q_id_list.extend(q_ids)
q_str_list.extend(q_strs)
# compare with answers
if answers is not None:
preds_correct = answers[idx:idx+nbatch_samples] == preds.astype(bool)
preds_correct_list.extend(preds_correct)
accuracy = (accuracy * idx + np.sum(preds_correct)) / (
idx + nbatch_samples)
idx += nbatch_samples
batch_cnt += 1
except tf.errors.OutOfRangeError:
break
# if there are ground truth answers, print and save accuracy
if answers is not None:
print('\naccuracy on "{1}": {0}'.format(accuracy, args.partition))
with open('accuracy_{0}_{1}_{2}_{3}.txt'.format(
args.partition,
config['model']['name'],
config['dataset']['name'],
os.path.splitext(os.path.split(meta_file)[1])[0]), 'w') as fid:
json.dump(accuracy, fid)
# dump predictions
lines = ['question_index,image_index,question_id,question_string,answer\n']
for i in range(len(prediction_list)):
lines.append('{},{},{},{},{}\n'.format(q_index_list[i],
im_index_list[i],
q_id_list[i],
q_str_list[i].decode('UTF-8'),
int(prediction_list[i])))
predictions_fname = 'preds_{0}_{1}_{2}_{3}.csv'.format(
args.partition,
config['model']['name'],
config['dataset']['name'],
os.path.splitext(os.path.split(meta_file)[1])[0]
)
with open(predictions_fname, 'w') as fid:
fid.writelines(lines)
if answers is None:
print(
('Dumped {} predictions to {}, to get the accuracy, please send the file'
'to figureqa@microsoft.com with a short comment.').format(
args.partition, predictions_fname)
)
# vim: set ts=4 sw=4 sts=4 expandtab: