-
Notifications
You must be signed in to change notification settings - Fork 256
/
model.py
450 lines (375 loc) · 18.3 KB
/
model.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""Visual Attention Based OCR Model."""
from __future__ import absolute_import
from __future__ import division
import time
import os
import math
import logging
import sys
import distance
import numpy as np
import tensorflow as tf
from PIL import Image
from six.moves import xrange # pylint: disable=redefined-builtin
from .cnn import CNN
from .seq2seq_model import Seq2SeqModel
from ..util.data_gen import DataGen
class Model(object):
def __init__(self,
phase,
visualize,
data_path,
output_dir,
batch_size,
initial_learning_rate,
num_epoch,
steps_per_checkpoint,
model_dir,
target_embedding_size,
attn_num_hidden,
attn_num_layers,
clip_gradients,
max_gradient_norm,
session,
load_model,
gpu_id,
use_gru,
use_distance=True,
max_image_width=160,
max_image_height=60,
max_prediction_length=8,
reg_val=0):
self.use_distance = use_distance
# We need resized width, not the actual width
self.max_original_width = max_image_width
self.max_width = int(math.ceil(1. * max_image_width / max_image_height * DataGen.IMAGE_HEIGHT))
self.encoder_size = int(math.ceil(1. * self.max_width / 4))
self.decoder_size = max_prediction_length + 2
self.buckets = [(self.encoder_size, self.decoder_size)]
gpu_device_id = '/gpu:' + str(gpu_id)
self.gpu_device_id = gpu_device_id
if not os.path.exists(model_dir):
os.makedirs(model_dir)
logging.info('loading data')
# load data
if phase == 'train':
self.s_gen = DataGen(data_path, self.buckets, epochs=num_epoch, max_width=self.max_original_width)
else:
batch_size = 1
self.s_gen = DataGen(data_path, self.buckets, epochs=1, max_width=self.max_original_width)
logging.info('phase: %s' % phase)
logging.info('model_dir: %s' % (model_dir))
logging.info('load_model: %s' % (load_model))
logging.info('output_dir: %s' % (output_dir))
logging.info('steps_per_checkpoint: %d' % (steps_per_checkpoint))
logging.info('batch_size: %d' % (batch_size))
logging.info('num_epoch: %d' % num_epoch)
logging.info('learning_rate: %d' % initial_learning_rate)
logging.info('reg_val: %d' % (reg_val))
logging.info('max_gradient_norm: %f' % max_gradient_norm)
logging.info('clip_gradients: %s' % clip_gradients)
logging.info('max_image_width %f' % max_image_width)
logging.info('max_prediction_length %f' % max_prediction_length)
logging.info('target_embedding_size: %f' % target_embedding_size)
logging.info('attn_num_hidden: %d' % attn_num_hidden)
logging.info('attn_num_layers: %d' % attn_num_layers)
logging.info('visualize: %s' % visualize)
if use_gru:
logging.info('using GRU in the decoder.')
self.reg_val = reg_val
self.sess = session
self.steps_per_checkpoint = steps_per_checkpoint
self.model_dir = model_dir
self.output_dir = output_dir
self.batch_size = batch_size
self.num_epoch = num_epoch
self.global_step = tf.Variable(0, trainable=False)
self.phase = phase
self.visualize = visualize
self.learning_rate = initial_learning_rate
self.clip_gradients = clip_gradients
if phase == 'train':
self.forward_only = False
elif phase == 'test':
self.forward_only = True
else:
assert False, phase
with tf.device(gpu_device_id):
self.height = tf.constant(DataGen.IMAGE_HEIGHT, dtype=tf.int32)
self.height_float = tf.constant(DataGen.IMAGE_HEIGHT, dtype=tf.float64)
self.img_pl = tf.placeholder(tf.string, name='input_image_as_bytes')
self.img_data = tf.cond(
tf.less(tf.rank(self.img_pl), 1),
lambda: tf.expand_dims(self.img_pl, 0),
lambda: self.img_pl
)
self.img_data = tf.map_fn(self._prepare_image, self.img_data, dtype=tf.float32)
num_images = tf.shape(self.img_data)[0]
# TODO: create a mask depending on the image/batch size
self.encoder_masks = []
for i in xrange(self.encoder_size + 1):
self.encoder_masks.append(
tf.tile([[1.]], [num_images, 1])
)
self.decoder_inputs = []
self.target_weights = []
for i in xrange(self.decoder_size + 1):
self.decoder_inputs.append(
tf.tile([0], [num_images])
)
if i < self.decoder_size:
self.target_weights.append(tf.tile([1.], [num_images]))
else:
self.target_weights.append(tf.tile([0.], [num_images]))
cnn_model = CNN(self.img_data, True)
self.conv_output = cnn_model.tf_output()
self.perm_conv_output = tf.transpose(self.conv_output, perm=[1, 0, 2])
self.attention_decoder_model = Seq2SeqModel(
encoder_masks=self.encoder_masks,
encoder_inputs_tensor=self.perm_conv_output,
decoder_inputs=self.decoder_inputs,
target_weights=self.target_weights,
target_vocab_size=len(DataGen.CHARMAP),
buckets=self.buckets,
target_embedding_size=target_embedding_size,
attn_num_layers=attn_num_layers,
attn_num_hidden=attn_num_hidden,
forward_only=self.forward_only,
use_gru=use_gru)
table = tf.contrib.lookup.MutableHashTable(
key_dtype=tf.int64,
value_dtype=tf.string,
default_value="",
checkpoint=True,
)
insert = table.insert(
tf.constant(list(range(len(DataGen.CHARMAP))), dtype=tf.int64),
tf.constant(DataGen.CHARMAP),
)
with tf.control_dependencies([insert]):
num_feed = []
for l in xrange(len(self.attention_decoder_model.output)):
guess = tf.argmax(self.attention_decoder_model.output[l], axis=1)
num_feed.append(guess)
trans_output = tf.transpose(num_feed)
trans_output = tf.map_fn(
lambda m: tf.foldr(
lambda a, x: tf.cond(
tf.equal(x, DataGen.EOS_ID),
lambda: '',
lambda: table.lookup(x) + a
),
m,
initializer=''
),
trans_output,
dtype=tf.string
)
self.prediction = tf.cond(
tf.equal(tf.shape(trans_output)[0], 1),
lambda: trans_output[0],
lambda: trans_output,
)
self.prediction = tf.identity(self.prediction, name='prediction')
if not self.forward_only: # train
self.updates = []
self.summaries_by_bucket = []
params = tf.trainable_variables()
opt = tf.train.AdadeltaOptimizer(learning_rate=initial_learning_rate)
if self.reg_val > 0:
reg_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
logging.info('Adding %s regularization losses', len(reg_losses))
logging.debug('REGULARIZATION_LOSSES: %s', reg_losses)
loss_op = self.reg_val * tf.reduce_sum(reg_losses) + self.attention_decoder_model.loss
else:
loss_op = self.attention_decoder_model.loss
gradients, params = zip(*opt.compute_gradients(loss_op, params))
if self.clip_gradients:
gradients, _ = tf.clip_by_global_norm(gradients, max_gradient_norm)
# Add summaries for loss, variables, gradients, gradient norms and total gradient norm.
summaries = []
summaries.append(tf.summary.scalar("loss", loss_op))
summaries.append(tf.summary.scalar("total_gradient_norm", tf.global_norm(gradients)))
all_summaries = tf.summary.merge(summaries)
self.summaries_by_bucket.append(all_summaries)
# update op - apply gradients
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
self.updates.append(opt.apply_gradients(zip(gradients, params), global_step=self.global_step))
self.saver_all = tf.train.Saver(tf.all_variables())
self.checkpoint_path = os.path.join(self.model_dir, "model.ckpt")
ckpt = tf.train.get_checkpoint_state(model_dir)
if ckpt and load_model:
logging.info("Reading model parameters from %s" % ckpt.model_checkpoint_path)
self.saver_all.restore(self.sess, ckpt.model_checkpoint_path)
else:
logging.info("Created model with fresh parameters.")
self.sess.run(tf.initialize_all_variables())
def test(self):
current_step = 0
num_correct = 0.0
num_total = 0.0
for batch in self.s_gen.gen(1):
current_step += 1
# Get a batch and make a step.
start_time = time.time()
result = self.step(batch, self.forward_only)
curr_step_time = (time.time() - start_time)
if self.visualize:
step_attns = np.array([[a.tolist() for a in step_attn] for step_attn in result['attentions']]).transpose([1, 0, 2])
num_total += 1
output = result['prediction']
ground = batch['labels'][0]
if sys.version_info >= (3,):
output = output.decode('iso-8859-1')
ground = ground.decode('iso-8859-1')
if self.use_distance:
incorrect = distance.levenshtein(output, ground)
incorrect = float(incorrect) / len(ground)
incorrect = min(1, incorrect)
else:
incorrect = 0 if output == ground else 1
num_correct += 1. - incorrect
if self.visualize:
self.visualize_attention(ground, step_attns[0], output, ground, incorrect)
step_accuracy = "{:>4.0%}".format(1. - incorrect)
correctness = step_accuracy + (" ({} vs {})".format(output, ground) if incorrect else " (" + ground + ")")
logging.info('Step {:.0f} ({:.3f}s). Accuracy: {:6.2%}, loss: {:f}, perplexity: {:0<7.6}. {}'.format(
current_step,
curr_step_time,
num_correct / num_total,
result['loss'],
math.exp(result['loss']) if result['loss'] < 300 else float('inf'),
correctness))
def train(self):
step_time = 0.0
loss = 0.0
current_step = 0
writer = tf.summary.FileWriter(self.model_dir, self.sess.graph)
logging.info('Starting the training process.')
for batch in self.s_gen.gen(self.batch_size):
current_step += 1
start_time = time.time()
result = self.step(batch, self.forward_only)
loss += result['loss'] / self.steps_per_checkpoint
curr_step_time = (time.time() - start_time)
step_time += curr_step_time / self.steps_per_checkpoint
# num_correct = 0
# step_outputs = result['prediction']
# grounds = batch['labels']
# for output, ground in zip(step_outputs, grounds):
# if self.use_distance:
# incorrect = distance.levenshtein(output, ground)
# incorrect = float(incorrect) / len(ground)
# incorrect = min(1.0, incorrect)
# else:
# incorrect = 0 if output == ground else 1
# num_correct += 1. - incorrect
writer.add_summary(result['summaries'], current_step)
# precision = num_correct / len(batch['labels'])
step_perplexity = math.exp(result['loss']) if result['loss'] < 300 else float('inf')
# logging.info('Step %i: %.3fs, precision: %.2f, loss: %f, perplexity: %f.'
# % (current_step, curr_step_time, precision*100, result['loss'], step_perplexity))
logging.info('Step %i: %.3fs, loss: %f, perplexity: %f.'
% (current_step, curr_step_time, result['loss'], step_perplexity))
# Once in a while, we save checkpoint, print statistics, and run evals.
if current_step % self.steps_per_checkpoint == 0:
perplexity = math.exp(loss) if loss < 300 else float('inf')
# Print statistics for the previous epoch.
logging.info("Global step %d. Time: %.3f, loss: %f, perplexity: %.2f."
% (self.sess.run(self.global_step), step_time, loss, perplexity))
# Save checkpoint and reset timer and loss.
logging.info("Saving the model at step %d."%current_step)
self.saver_all.save(self.sess, self.checkpoint_path, global_step=self.global_step)
step_time, loss = 0.0, 0.0
# Print statistics for the previous epoch.
perplexity = math.exp(loss) if loss < 300 else float('inf')
logging.info("Global step %d. Time: %.3f, loss: %f, perplexity: %.2f."
% (self.sess.run(self.global_step), step_time, loss, perplexity))
# Save checkpoint and reset timer and loss.
logging.info("Finishing the training and saving the model at step %d." % current_step)
self.saver_all.save(self.sess, self.checkpoint_path, global_step=self.global_step)
# step, read one batch, generate gradients
def step(self, batch, forward_only):
img_data = batch['data']
decoder_inputs = batch['decoder_inputs']
target_weights = batch['target_weights']
# Input feed: encoder inputs, decoder inputs, target_weights, as provided.
input_feed = {}
input_feed[self.img_pl.name] = img_data
for l in xrange(self.decoder_size):
input_feed[self.decoder_inputs[l].name] = decoder_inputs[l]
input_feed[self.target_weights[l].name] = target_weights[l]
# Since our targets are decoder inputs shifted by one, we need one more.
last_target = self.decoder_inputs[self.decoder_size].name
input_feed[last_target] = np.zeros([self.batch_size], dtype=np.int32)
# Output feed: depends on whether we do a backward step or not.
output_feed = [
self.attention_decoder_model.loss, # Loss for this batch.
]
if not forward_only:
output_feed += [self.summaries_by_bucket[0],
self.updates[0]]
else:
output_feed += [self.prediction]
if self.visualize:
output_feed += self.attention_decoder_model.attention_weights_history
outputs = self.sess.run(output_feed, input_feed)
res = {
'loss': outputs[0],
}
if not forward_only:
res['summaries'] = outputs[1]
else:
res['prediction'] = outputs[1]
if self.visualize:
res['attentions'] = outputs[2:]
return res
def visualize_attention(self, filename, attentions, output, label, flag_incorrect):
if flag_incorrect:
output_dir = os.path.join(self.output_dir, 'incorrect')
else:
output_dir = os.path.join(self.output_dir, 'correct')
output_dir = os.path.join(output_dir, filename.replace('/', '_'))
if not os.path.exists(output_dir):
os.makedirs(output_dir)
with open(os.path.join(output_dir, 'word.txt'), 'w') as fword:
fword.write(output+'\n')
fword.write(label)
with open(filename, 'rb') as img_file:
img = Image.open(img_file)
w, h = img.size
mh = 32
mw = math.floor(1. * w / h * mh)
img = img.resize(
(mw, h),
Image.ANTIALIAS)
img_data = np.asarray(img, dtype=np.uint8)
for idx in xrange(len(output)):
output_filename = os.path.join(output_dir, 'image_%d.jpg' % (idx))
attention = attentions[idx][:(int(mw/4)-1)]
attention_orig = np.zeros(mw)
for i in xrange(mw):
if i/4-1 > 0 and i/4-1 < len(attention):
attention_orig[i] = attention[int(i/4)-1]
attention_orig = np.convolve(attention_orig, [0.199547, 0.200226, 0.200454, 0.200226, 0.199547], mode='same')
attention_orig = np.maximum(attention_orig, 0.3)
attention_out = np.zeros((h, mw))
for i in xrange(mw):
attention_out[:, i] = attention_orig[i]
if len(img_data.shape) == 3:
attention_out = attention_out[:, :, np.newaxis]
img_out_data = img_data * attention_out
img_out = Image.fromarray(img_out_data.astype(np.uint8))
img_out.save(output_filename)
def _prepare_image(self, img):
image = tf.image.decode_png(img, channels=1)
dims = tf.shape(image)
width = tf.to_int32(tf.ceil(tf.truediv(dims[1], dims[0]) * self.height_float))
resized = tf.cond(
tf.less_equal(dims[0], self.height),
lambda: tf.to_float(image),
lambda: tf.image.resize_images(image, [self.height, width], method=tf.image.ResizeMethod.BICUBIC),
)
padded = tf.image.pad_to_bounding_box(resized, 0, 0, self.height, self.max_width)
return padded