-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.py
More file actions
160 lines (142 loc) · 7.52 KB
/
task.py
File metadata and controls
160 lines (142 loc) · 7.52 KB
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
import tensorflow as tf
import random, time
import os, sys
import numpy as np
import logging
from batch_data import DataBatch
from units.model import TrainModel, BeamSearchInfer
from units.lr_shedule import LRSchedule
from units.visualize import display_result
from setting import *
from units.model_test import *
sys.path.append(PROJECT_ROOT)
os.environ['CUDA_VISIBLE_DEVICES'] = ''
config = tf.ConfigProto(
# device_count={'CPU': 1, 'GPU': 1},
# gpu_options=gpu_option,
allow_soft_placement=True,
log_device_placement=False)
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)-15s %(name)-5s %(levelname)-8s %(message)s',
datefmt='%d %b %Y %H:%M:%S',
filename='./log.txt',
filemode='a+')
class TrainWork:
def __init__(self, train_path=None, validate_path=None, test_path=None,
vocab_path=None, formulas_path=None, image_path=None, batch_size=1):
self.batch_size = batch_size
self.lr_init = 0.1
self.lr_min = 0.00001
self.epochs = 60
self.start_epoch = 34
self.save_id = './saved_models/model-'
self.model = './saved_models/model-25-04-2018--16-56/'
self.data_batch = DataBatch(train_path=train_path, validate_path=validate_path, test_path=test_path,
vocab_path=vocab_path, formulas_path=formulas_path, image_path=image_path,
batch_size=batch_size)
train_data, val_data, test_data, self.vocab_size = self.data_batch.load_data()
if train_data:
self.train_data = self.data_batch.gen_training_data(train_data)
self.total_step = len(self.train_data) * self.epochs
random.shuffle(self.train_data)
if val_data:
self.val_data = self.data_batch.gen_training_data(val_data)
random.shuffle(self.val_data)
if test_data:
self.test_data = self.data_batch.gen_training_data(test_data)
def init_session(self):
self.train_graph = tf.Graph()
with self.train_graph.as_default():
self.train_model = TrainModel(vocab_size=self.vocab_size)
self.train_saver = tf.train.Saver(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES))
def get_checkpoint(self):
"""Get the checkpoint path from the given model output directory"""
ckpt = tf.train.get_checkpoint_state(self.model)
if ckpt and ckpt.model_checkpoint_path:
ckpt_path = ckpt.model_checkpoint_path
else:
raise RuntimeError('No checkpoint file found')
return ckpt_path
def restore_session(self, sess, dir_model):
"""Reload weights into session
Args:
sess: tf.Session()
dir_model: dir with weights
"""
self.train_saver.restore(sess, dir_model)
def train_process(self, restore=False):
lr_schedule = LRSchedule(lr_init=self.lr_init, lr_min=self.lr_min, total_step=self.total_step,
lr_type='pow')
with tf.Session(config=config, graph=self.train_graph) as sess:
if restore:
checkpoint = self.get_checkpoint()
self.restore_session(sess, checkpoint)
else:
init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
sess.run(init_op)
for epoch in range(self.start_epoch, self.epochs):
print('the epoch is:', epoch)
logging.info("the epoch is: %d", epoch)
epoch_start_time = time.time()
random.shuffle(self.train_data)
for j in range(len(self.train_data)):
input_data = self.train_data[j]
feed_dict = {self.train_model.input_image: input_data['input_image'],
self.train_model.ctc_label: input_data['ctc_label'],
self.train_model.ctc_feature_length: input_data['ctc_feature_length'],
self.train_model.att_train_length: input_data['att_train_length'],
self.train_model.att_label: input_data['att_labels'],
self.train_model.max_dec_iteration: [input_data['att_labels'].shape[1]],
self.train_model.learning_rate: lr_schedule.lr}
loss_print, accuracy = self.train_model.train(sess, feed_dict=feed_dict)
batch_no = epoch * len(self.train_data) + j
lr_schedule.update(batch_no=batch_no)
if batch_no % 20 == 0:
print("step: %d/%d, lr: %.6f, loss: %g, acc: %g" % (batch_no, self.total_step,
lr_schedule.lr, loss_print, accuracy))
logging.info("step: %d/%d, lr: %.6f, loss: %g, acc: %g" % (batch_no, self.total_step,
lr_schedule.lr, loss_print,
accuracy))
print("Time for epoch:%f mins" % ((time.time() - epoch_start_time) / 60))
if (epoch + 1) % 2 == 0:
id = self.save_id + time.strftime("%d-%m-%Y--%H-%M")
if not os.path.exists(id):
os.makedirs(id)
self.train_saver.save(sess, id + '/model')
def infer_process(self):
infer_model = BeamSearchInfer(vocab_size=self.vocab_size)
infer_saver = tf.train.Saver()
infer_sess = tf.Session(config=config)
checkpoint = self.get_checkpoint()
infer_saver.restore(infer_sess, checkpoint)
for j in range(len(self.test_data)):
input_data = self.test_data[j]
start = time.time()
feed_dict = {infer_model.input_image: input_data['input_image'],
infer_model.ctc_label: input_data['ctc_label'],
infer_model.ctc_feature_length: input_data['ctc_feature_length'],
infer_model.att_train_length: input_data['att_train_length'],
infer_model.att_label: input_data['att_labels'],
infer_model.max_dec_iteration: [input_data['att_labels'].shape[1]]}
predict_labels = infer_model.predict(infer_sess, feed_dict=feed_dict)
print('predict_labels:', predict_labels[0])
print('att_labels:', input_data['att_labels'][0])
print('the cost time is:', time.time() - start)
display_result(input_data['input_image'][0], input_data['att_labels'][0], predict_labels[0])
if __name__ == "__main__":
# train_path = '../data/chinese_formula_data/label.txt'
# test_path = '../id_data/test_filter.lst'
# test_path = '../chinese_formula_data/tmp.lst'
vocab_path = '../data/chinese_formula_data/vocab.txt'
# image_path = '../data/chinese_formula_data/processed_image'
# formula_path = '../data/chinese_formula_data/label.txt'
test_path = '../data/tmp_data/right_label.txt'
formula_path = '../data/tmp_data/right_label.txt'
image_path = '../data/tmp_data'
mulwork = TrainWork(train_path=None, validate_path=None, test_path=test_path,
vocab_path=vocab_path, image_path=image_path, formulas_path=formula_path, batch_size=1)
# mulwork.init_session()
# mulwork.train_process(restore=True)
mulwork.infer_process()