/
main.py
executable file
·395 lines (336 loc) · 16.8 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
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
# encoding: utf-8
'''
@author: ZiqiLiu
@file: main.py
@time: 2017/5/18 上午11:03
@desc:
'''
# -*- coding:utf-8 -*-
# !/usr/bin/python
import os
import sys
import time
import pickle
import signal
import traceback
from args import parse_args
import numpy as np
import tensorflow as tf
from glob import glob
from tensorflow.python.framework import graph_util
from reader import read_dataset
from utils.common import check_dir, path_join
from utils.prediction import evaluate, ctc_predict, ctc_decode
from octbit.octbit_graph import GraphRewriter
from utils.wer import WERCalculator
DEBUG = False
class Runner(object):
def __init__(self, config):
self.config = config
self.epoch = 0
self.wer_cal = WERCalculator([0, -1])
def run(self, TrainingModel):
graph = tf.Graph()
with graph.as_default(), tf.Session() as sess:
self.data = read_dataset(self.config)
if config.mode == 'train':
print('building training model....')
with tf.variable_scope("model"):
self.train_model = TrainingModel(self.config,
self.data.batch_input_queue(),
is_train=True)
self.train_model.config.show()
print('building valid model....')
with tf.variable_scope("model", reuse=True):
self.valid_model = TrainingModel(self.config,
self.data.valid_queue(),
is_train=False)
else:
with tf.variable_scope("model", reuse=False):
self.valid_model = TrainingModel(self.config,
self.data.valid_queue(),
is_train=False)
saver = tf.train.Saver()
# restore from stored models
files = glob(path_join(self.config.model_path, '*.ckpt.*'))
if len(files) > 0:
saver.restore(sess, path_join(self.config.model_path,
self.config.model_name))
print(('Model restored from:' + self.config.model_path))
else:
print("Model doesn't exist.\nInitializing........")
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
tf.Graph.finalize(graph)
st_time = time.time()
if os.path.exists(path_join(self.config.model_path, 'best.pkl')):
with open(path_join(self.config.model_path, 'best.pkl'),
'rb') as f:
best_miss, best_false = pickle.load(f)
print('best miss', best_miss, 'best false', best_false)
else:
print('best not exist')
check_dir(self.config.model_path)
if self.config.mode == 'train':
best_miss = 1
best_false = 1
accu_loss = 0
epoch_step = config.tfrecord_size * self.data.train_file_size // config.batch_size
if self.config.reset_global:
sess.run(self.train_model.reset_global_step)
def handler_stop_signals(signum, frame):
global run
run = False
if not DEBUG:
print(
'training shut down, total setp %s, the model will be save in %s' % (
step, self.config.model_path))
saver.save(sess, save_path=(
path_join(self.config.model_path, 'latest.ckpt')))
print('best miss rate:%f\tbest false rate %f' % (
best_miss, best_false))
sys.exit(0)
signal.signal(signal.SIGINT, handler_stop_signals)
signal.signal(signal.SIGTERM, handler_stop_signals)
best_list = []
best_threshold = 0.08
best_count = 0
# (miss,false,step,best_count)
last_time = time.time()
try:
sess.run([self.data.noise_stage_op,
self.data.noise_filequeue_enqueue_op,
self.train_model.stage_op,
self.train_model.input_filequeue_enqueue_op,
self.valid_model.stage_op,
self.valid_model.input_filequeue_enqueue_op])
va = tf.trainable_variables()
for i in va:
print(i.name)
while self.epoch < self.config.max_epoch:
_, _, _, _, _, l, lr, step, grads = sess.run(
[self.train_model.train_op,
self.data.noise_stage_op,
self.data.noise_filequeue_enqueue_op,
self.train_model.stage_op,
self.train_model.input_filequeue_enqueue_op,
self.train_model.loss,
self.train_model.learning_rate,
self.train_model.global_step,
self.train_model.grads
])
epoch = step // epoch_step
accu_loss += l
if epoch > self.epoch:
self.epoch = epoch
print('accumulated loss', accu_loss)
saver.save(sess, save_path=(
path_join(self.config.model_path,
'latest.ckpt')))
print('latest.ckpt save in %s' % (
path_join(self.config.model_path,
'latest.ckpt')))
accu_loss = 0
if step % config.valid_step == 0:
print('epoch time ', (time.time() - last_time) / 60)
last_time = time.time()
miss_count = 0
false_count = 0
target_count = 0
wer = 0
valid_batch = self.data.valid_file_size * config.tfrecord_size // config.batch_size
text = ""
for i in range(valid_batch):
softmax, correctness, labels, _, _ = sess.run(
[self.valid_model.softmax,
self.valid_model.correctness,
self.valid_model.labels,
self.valid_model.stage_op,
self.valid_model.input_filequeue_enqueue_op])
np.set_printoptions(precision=4,
threshold=np.inf,
suppress=True)
decode_output = [ctc_decode(s) for s in softmax]
for i in decode_output:
text += str(i) + '\n'
text += str(labels) + '\n'
text += '=' * 20 + '\n'
result = [ctc_predict(seq, config.label_seqs)
for seq in
decode_output]
miss, target, false_accept = evaluate(
result, correctness.tolist())
miss_count += miss
target_count += target
false_count += false_accept
wer += self.wer_cal.cal_batch_wer(labels,
decode_output).sum()
# print(miss_count, false_count)
with open('./valid.txt', 'w') as f:
f.write(text)
miss_rate = miss_count / target_count
false_accept_rate = false_count / (
self.data.validation_size - target_count)
print('--------------------------------')
print('epoch %d' % self.epoch)
print('training loss:' + str(l))
print('learning rate:', lr, 'global step', step)
print('miss rate:' + str(miss_rate))
print('flase_accept_rate:' + str(false_accept_rate))
print(miss_count, '/', target_count)
print('wer', wer / self.data.validation_size)
if miss_rate + false_accept_rate < best_miss + best_false:
best_miss = miss_rate
best_false = false_accept_rate
saver.save(sess,
save_path=(path_join(
self.config.model_path,
'best.ckpt')))
with open(path_join(
self.config.model_path, 'best.pkl'),
'wb') as f:
best_tuple = (best_miss, best_false)
pickle.dump(best_tuple, f)
if miss_rate + false_accept_rate < best_threshold:
best_count += 1
print('best_count', best_count)
best_list.append((miss_rate,
false_accept_rate, step,
best_count))
saver.save(sess,
save_path=(path_join(
self.config.model_path,
'best' + str(
best_count) + '.ckpt')))
print(
'training finished, total epoch %d, the model will be save in %s' % (
self.epoch, self.config.model_path))
saver.save(sess, save_path=(
path_join(self.config.model_path, 'latest.ckpt')))
print('best miss rate:%f\tbest false rate"%f' % (
best_miss, best_false))
except tf.errors.OutOfRangeError:
print('Done training -- epoch limit reached')
except Exception as e:
print(e)
traceback.print_exc()
finally:
with open('best_list.pkl', 'wb') as f:
pickle.dump(best_list, f)
print('total time:%f hours' % (
(time.time() - st_time) / 3600))
# When done, ask the threads to stop.
else:
with open(
config.rawdata_path + 'valid/' + "ctc_valid.pkl.sorted",
'rb') as f:
pkl = pickle.load(f)
miss_count = 0
false_count = 0
target_count = 0
valid_batch = self.data.valid_file_size * config.tfrecord_size // config.batch_size
for i in range(valid_batch):
# if i > 7:
# break
ind = 14
softmax, ctc_input, correctness, labels, _, _ = sess.run(
[self.valid_model.softmax,
self.valid_model.nn_outputs,
self.valid_model.correctness,
self.valid_model.labels,
self.valid_model.stage_op,
self.valid_model.input_filequeue_enqueue_op])
np.set_printoptions(precision=4,
threshold=np.inf,
suppress=True)
correctness = correctness.tolist()
decode_output = [ctc_decode(s) for s in softmax]
result = [ctc_predict(seq, config.label_seqs)
for seq in
decode_output]
for k, r in enumerate(result):
if r != correctness[k]:
name = pkl[i * config.batch_size + k][0]
print("scp liuziqi@10.8.0.62:/ssd/keyword_raw/valid/%s ./"%name)
# print(pkl[i * config.batch_size + k])
# print(decode_output[k])
# print(labels[k])
with open('logits.txt', 'w') as f:
f.write(str(ctc_input[k]))
miss, target, false_accept = evaluate(
result, correctness)
miss_count += miss
target_count += target
false_count += false_accept
print('--------------------------------')
print('miss rate: %d/%d' % (miss_count, target_count))
print('flase_accept_rate: %d/%d' % (
false_count, self.data.validation_size - target_count))
def build_graph(self, DeployModel):
check_dir(self.config.graph_path)
config_path = path_join(self.config.graph_path, 'config.pkl')
graph_path = path_join(self.config.graph_path, self.config.graph_name)
import pickle
pickle.dump(self.config, open(config_path, 'wb'))
with tf.Graph().as_default(), tf.Session(config=tf.ConfigProto(
allow_soft_placement=True)) as session:
with tf.variable_scope("model"):
model = DeployModel(config=config)
print('Graph build finished')
variable_names = [n.name for n in
tf.get_default_graph().as_graph_def().node]
for n in variable_names:
print(n)
saver = tf.train.Saver()
saver.restore(session, save_path=path_join(self.config.model_path,
'latest.ckpt'))
print("model restored from %s" % config.model_path)
frozen_graph_def = graph_util.convert_variables_to_constants(
session, session.graph.as_graph_def(),
['model/inputX', 'model/rnn_initial_states',
'model/rnn_states', 'model/softmax', 'model/logit'])
tf.train.write_graph(
frozen_graph_def,
self.config.graph_path,
self.config.graph_name,
as_text=False,
)
try:
tf.import_graph_def(frozen_graph_def, name="")
except Exception as e:
print("!!!!Import graph meet error: ", e)
exit()
print('graph saved in %s' % graph_path)
print("====octbit graph====")
rewriter = GraphRewriter(frozen_graph_def, mode="octbit")
octbit_graph = rewriter.rewrite(
['model/inputX', 'model/rnn_initial_states',
'model/rnn_states', 'model/softmax', 'model/logit'])
try:
tf.import_graph_def(octbit_graph, name="")
except Exception as e:
print("!!!!Import octbit graph meet error: ", e)
exit()
tf.train.write_graph(
octbit_graph,
self.config.graph_path,
self.config.graph_name[:-3] + '_octbit.pb',
as_text=False,
)
if __name__ == '__main__':
config, model = parse_args()
if model == 'rnn':
from models import rnn_ctc
TrainingModel = rnn_ctc.GRU
DeployModel = rnn_ctc.DeployModel
elif model == 'attention':
from models import attention_ctc
TrainingModel = attention_ctc.Attention
DeployModel = attention_ctc.DeployModel
else:
raise Exception('model %s not defined!' % model)
runner = Runner(config)
if config.mode == 'build':
runner.build_graph(DeployModel)
else:
runner.run(TrainingModel)