-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·215 lines (168 loc) · 8.72 KB
/
main.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
import datetime
import logging
import os
import time
import cv2
import numpy as np
import tensorflow as tf
import cnn_lstm_otc_ocr
import utils
import helper
os.environ["CUDA_VISIBLE_DEVICES"]="0"
FLAGS = utils.FLAGS
logger = logging.getLogger('Traing for OCR using CNN+LSTM+CTC')
logger.setLevel(logging.INFO)
def train(train_dir=None, val_dir=None, mode='train'):
model = cnn_lstm_otc_ocr.LSTMOCR(mode)
model.build_graph()
print('loading train data, please wait---------------------')
train_feeder = utils.DataIterator(data_dir=train_dir)
print('get image: ', train_feeder.size)
print('loading validation data, please wait---------------------')
val_feeder = utils.DataIterator(data_dir=val_dir)
print('get image: ', val_feeder.size)
num_train_samples = train_feeder.size # 100000
num_batches_per_epoch = int(num_train_samples / FLAGS.batch_size) # example: 100000/100
num_val_samples = val_feeder.size
num_batches_per_epoch_val = int(num_val_samples / FLAGS.batch_size) # example: 10000/100
shuffle_idx_val = np.random.permutation(num_val_samples)
with tf.device('/gpu:0'):
config = tf.ConfigProto(allow_soft_placement=True)
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver(tf.global_variables(), max_to_keep=100)
train_writer = tf.summary.FileWriter(FLAGS.log_dir + '/train', sess.graph)
if FLAGS.restore:
ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
if ckpt:
# the global_step will restore sa well
saver.restore(sess, ckpt)
print('restore from the checkpoint{0}'.format(ckpt))
print('=============================begin training=============================')
for cur_epoch in range(FLAGS.num_epochs):
shuffle_idx = np.random.permutation(num_train_samples)
train_cost = 0
start_time = time.time()
batch_time = time.time()
# the tracing part
for cur_batch in range(num_batches_per_epoch):
if (cur_batch + 1) % 100 == 0:
print('batch', cur_batch, ': time', time.time() - batch_time)
batch_time = time.time()
indexs = [shuffle_idx[i % num_train_samples] for i in
range(cur_batch * FLAGS.batch_size, (cur_batch + 1) * FLAGS.batch_size)]
batch_inputs, batch_seq_len, batch_labels = \
train_feeder.input_index_generate_batch(indexs)
# batch_inputs,batch_seq_len,batch_labels=utils.gen_batch(FLAGS.batch_size)
feed = {model.inputs: batch_inputs,
model.labels: batch_labels,
model.seq_len: batch_seq_len}
# if summary is needed
# batch_cost,step,train_summary,_ = sess.run([cost,global_step,merged_summay,optimizer],feed)
summary_str, batch_cost, step, _ = \
sess.run([model.merged_summay, model.cost, model.global_step,
model.train_op], feed)
# calculate the cost
train_cost += batch_cost * FLAGS.batch_size
train_writer.add_summary(summary_str, step)
# save the checkpoint
if step % FLAGS.save_steps == 1:
if not os.path.isdir(FLAGS.checkpoint_dir):
os.mkdir(FLAGS.checkpoint_dir)
logger.info('save the checkpoint of{0}', format(step))
saver.save(sess, os.path.join(FLAGS.checkpoint_dir, 'ocr-model'),
global_step=step)
# train_err += the_err * FLAGS.batch_size
# do validation
if step % FLAGS.validation_steps == 0:
acc_batch_total = 0
lastbatch_err = 0
lr = 0
for j in xrange(num_batches_per_epoch_val):
indexs_val = [shuffle_idx_val[i % num_val_samples] for i in
range(j * FLAGS.batch_size, (j + 1) * FLAGS.batch_size)]
val_inputs, val_seq_len, val_labels = \
val_feeder.input_index_generate_batch(indexs_val)
val_feed = {model.inputs: val_inputs,
model.labels: val_labels,
model.seq_len: val_seq_len}
dense_decoded, lastbatch_err, lr = \
sess.run([model.dense_decoded, model.lrn_rate],
val_feed)
# print the decode result
ori_labels = val_feeder.the_label(indexs_val)
acc = utils.accuracy_calculation(ori_labels, dense_decoded,
ignore_value=-1, isPrint=True)
acc_batch_total += acc
accuracy = (acc_batch_total * FLAGS.batch_size) / num_val_samples
avg_train_cost = train_cost / ((cur_batch + 1) * FLAGS.batch_size)
# train_err /= num_train_samples
now = datetime.datetime.now()
log = "{}/{} {}:{}:{} Epoch {}/{}, " \
"accuracy = {:.3f},avg_train_cost = {:.3f}, " \
"lastbatch_err = {:.3f}, time = {:.3f},lr={:.8f}"
print(log.format(now.month, now.day, now.hour, now.minute, now.second,
cur_epoch + 1, FLAGS.num_epochs, accuracy, avg_train_cost,
lastbatch_err, time.time() - start_time, lr))
def infer(img_path, mode='infer'):
# imgList = load_img_path('/home/yang/Downloads/FILE/ml/imgs/image_contest_level_1_validate/')
imgList = helper.load_img_path(img_path)
print(imgList[:5])
model = cnn_lstm_otc_ocr.LSTMOCR(mode)
model.build_graph()
total_steps = len(imgList) / FLAGS.batch_size
config = tf.ConfigProto(allow_soft_placement=True)
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver(tf.global_variables(), max_to_keep=100)
ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
if ckpt:
saver.restore(sess, ckpt)
print('restore from ckpt{}'.format(ckpt))
else:
print('cannot restore')
decoded_expression = []
for curr_step in xrange(total_steps):
imgs_input = []
seq_len_input = []
for img in imgList[curr_step * FLAGS.batch_size: (curr_step + 1) * FLAGS.batch_size]:
im = cv2.imread(img, 0).astype(np.float32) / 255.
im = np.reshape(im, [FLAGS.image_height, FLAGS.image_width, FLAGS.image_channel])
def get_input_lens(seqs):
length = np.array([FLAGS.max_stepsize for _ in seqs], dtype=np.int64)
return seqs, length
inp, seq_len = get_input_lens(np.array([im]))
imgs_input.append(im)
seq_len_input.append(seq_len)
imgs_input = np.asarray(imgs_input)
seq_len_input = np.asarray(seq_len_input)
seq_len_input = np.reshape(seq_len_input, [-1])
feed = {model.inputs: imgs_input,
model.seq_len: seq_len_input}
dense_decoded_code = sess.run(model.dense_decoded, feed)
for item in dense_decoded_code:
expression = ''
for i in item:
if i == -1:
expression += ''
else:
expression += utils.decode_maps[i]
decoded_expression.append(expression)
with open('./result.txt', 'a') as f:
for code in decoded_expression:
f.write(code + '\n')
def main(_):
if FLAGS.num_gpus == 0:
dev = '/cpu:0'
elif FLAGS.num_gpus == 1:
dev = '/gpu:0'
else:
raise ValueError('Only support 0 or 1 gpu.')
with tf.device(dev):
if FLAGS.mode == 'train':
train(FLAGS.train_dir, FLAGS.val_dir, FLAGS.mode)
elif FLAGS.mode == 'infer':
infer(FLAGS.infer_dir, FLAGS.mode)
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run()