-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathesposallesSequenceCTC.py
165 lines (148 loc) · 9.24 KB
/
esposallesSequenceCTC.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
import tensorflow as tf
from tensorflow.contrib import rnn
import numpy as np
import time
import esposallesData
class SeqLearn():
def __init__(self, n_classes, datasets):
self.trainImg, self.seqLen_train, self.trainLabel, self.validationImg, self.seqLen_validation, self.validationLabel, self.testImg, self.seqLen_test, self.testLabel = datasets
self.n_examples = len(self.trainImg)
self.n_examples_t = len(self.validationImg)
self.n_features = esposallesData.IMG_HEIGHT
if esposallesData.TEXTLINE:
self.batch_size = 8
else:
self.batch_size = 64
self.n_classes = n_classes
self.n_hidden = 100
self.n_layers = 1
self.learning_rate = 1e-3
self.n_epochs = 800
self.n_batches_per_epoch = int(self.n_examples/self.batch_size)
self.n_batches_per_epoch_t = int(self.n_examples_t/self.batch_size)
self.summary = tf.Summary()
self.summary_writer = tf.summary.FileWriter('ler_epoch_tensorboard')
self.model()
self.saver = tf.train.Saver()
def sparse_tuple_from(self, sequences, dtype=np.int32):
# Create a sparse representention of x.
# Args:
# sequences: a list of lists of type dtype where each element is a sequence
# Returns:
# A tuple with (indices, values, shape)
indices = []
values = []
for n, seq in enumerate(sequences):
indices.extend(zip([n]*len(seq), range(len(seq))))
values.extend(seq)
indices = np.asarray(indices, dtype=np.int64)
values = np.asarray(values, dtype=dtype)
shape = np.asarray([len(sequences), np.asarray(indices).max(0)[1]+1], dtype=np.int64)
return indices, values, shape
def model(self):
self.x = tf.placeholder(tf.float32, [None, self.n_features, None]) # (batch_size, n_features, time_steps)
self.y = tf.sparse_placeholder(tf.int32)
self.seqLen = tf.placeholder(tf.int32, [None])
# <CNN>
batch_s = tf.shape(self.x)[0]
conv = tf.reshape(self.x, shape=[batch_s, self.n_features, -1, 1])
w_conv = tf.Variable(tf.random_normal([3, 3, 1, 32])) # 3,3 better
b_conv = tf.Variable(tf.constant(0., shape=[32]))
conv = tf.nn.conv2d(conv, w_conv, strides=[1, 1, 1, 1], padding='SAME')
conv = tf.nn.bias_add(conv, b_conv)
conv = tf.nn.relu(conv)
conv = tf.nn.max_pool(conv, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # (batch_s, n_features/2, time_s/2, 32)
w_conv2 = tf.Variable(tf.random_normal([3, 3, 32, 64])) # 3,3 better
b_conv2 = tf.Variable(tf.constant(0., shape=[64]))
conv2 = tf.nn.conv2d(conv, w_conv2, strides=[1, 1, 1, 1], padding='SAME')
conv2 = tf.nn.bias_add(conv2, b_conv2)
conv2 = tf.nn.relu(conv2)
conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # (batch_s, n_features/4, time_s/4, 64)
w_conv3 = tf.Variable(tf.random_normal([3, 3, 64, 128])) # 3,3 better
b_conv3 = tf.Variable(tf.constant(0., shape=[128]))
conv3 = tf.nn.conv2d(conv2, w_conv3, strides=[1, 1, 1, 1], padding='SAME')
conv3 = tf.nn.bias_add(conv3, b_conv3)
conv3 = tf.nn.relu(conv3)
#conv3 = tf.nn.max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # (batch_s, n_features/8, time_s/8, 128)
xx = tf.transpose(conv3, (2, 0, 1, 3)) # (time/4, batch, features/4, channels==128)
xx = tf.reshape(xx, [-1, batch_s, int(self.n_features*128/4)]) # (time/4, batch, features/4 * 128)
# </CNN>
lstm_fw_cell = rnn.BasicLSTMCell(self.n_hidden)
lstm_bw_cell = rnn.BasicLSTMCell(self.n_hidden)
outputs, states = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, xx, self.seqLen, dtype=tf.float32, time_major=True)
outputs = tf.concat(outputs, 2) # (time_step_new, batch, features_new*2)
outputs = tf.reshape(outputs, [-1, self.n_hidden*2])
weight2 = tf.Variable(tf.random_normal([self.n_hidden*2, self.n_classes+1]))
bias2 = tf.Variable(tf.constant(0., shape=[self.n_classes+1]))
pred = tf.matmul(outputs, weight2) + bias2
self.pred = tf.reshape(pred, [-1, batch_s, self.n_classes+1])
loss = tf.nn.ctc_loss(self.y, self.pred, self.seqLen)
self.cost = tf.reduce_mean(loss)
#self.loss_summary = tf.summary.scalar('loss', self.cost) ###
#self.optimizer = tf.train.MomentumOptimizer(self.learning_rate, 0.9).minimize(self.cost)
self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.cost)
self.decoded, log_prob = tf.nn.ctc_greedy_decoder(self.pred, self.seqLen)
self.decoded_long, log_prob = tf.nn.ctc_greedy_decoder(self.pred, self.seqLen, merge_repeated=False)
self.mistake_num = tf.edit_distance(tf.cast(self.decoded[0], tf.int32), self.y, normalize=False)
#self.error_rate_summary = tf.summary.scalar('error rate', self.label_error_rate) ###
#self.merged_summary = tf.summary.merge([self.loss_summary, self.error_rate_summary]) ###
def train(self, test_flag=True):
with tf.Session() as sess:
tf.global_variables_initializer().run()
#self.saver.restore(sess, 'models/model_textline.ckpt-763')
#print('@@@@Model restored@@@@')
for epoch in range(self.n_epochs+1):
train_cost = mistake_num = y_label_len = 0
start = time.time()
for batch in range(self.n_batches_per_epoch):
batch_x = np.array(self.trainImg[batch*self.batch_size: (batch+1)*self.batch_size])
batch_train_seqLen = self.seqLen_train[batch*self.batch_size: (batch+1)*self.batch_size]
batch_y = self.trainLabel[batch*self.batch_size: (batch+1)*self.batch_size]
label_len = sum([len(i) for i in batch_y])
batch_y = self.sparse_tuple_from(batch_y)
feed = {self.x: batch_x, self.y: batch_y, self.seqLen: batch_train_seqLen}
batch_cost, _, prediction = sess.run([self.cost, self.optimizer, self.pred], feed_dict=feed)
train_cost += batch_cost * self.batch_size
mistake_num += sess.run(self.mistake_num, feed_dict=feed).sum()
y_label_len += label_len
train_cost /= self.n_examples
train_cer = mistake_num / y_label_len
self.summary.value.add(tag='train_cer', simple_value=train_cer)
print('epoch {}/{}, train_cost={:.3f}, train_cer={:.3f}, time={:.3f}'.format(epoch, self.n_epochs, train_cost, train_cer, time.time()-start))
if esposallesData.TEXTLINE:
save_path = self.saver.save(sess, 'models/model_textline.ckpt', global_step=epoch)
else:
save_path = self.saver.save(sess, 'models/model_word.ckpt', global_step=epoch)
print('Model saved in file:', save_path)
with open('train_cer.log', 'a') as f:
f.write(str(train_cer))
f.write(' ')
if test_flag:
mistake_num_t = y_label_len_t = 0
start_t = time.time()
for bat in range(self.n_batches_per_epoch_t):
batch_x_t = np.array(self.validationImg[bat*self.batch_size: (bat+1)*self.batch_size])
batch_validation_seqLen = self.seqLen_validation[bat*self.batch_size: (bat+1)*self.batch_size]
batch_y_t = self.validationLabel[bat*self.batch_size: (bat+1)*self.batch_size]
label_len_t = sum([len(i) for i in batch_y_t])
batch_y_t = self.sparse_tuple_from(batch_y_t)
feed_t = {self.x: batch_x_t, self.y: batch_y_t, self.seqLen: batch_validation_seqLen}
mistake_num_t += sess.run(self.mistake_num, feed_dict=feed_t).sum()
y_label_len_t += label_len_t
test_cer = mistake_num_t / y_label_len_t
self.summary.value.add(tag='test_cer', simple_value=test_cer)
print('###TEST### test_cer={:.3f}, time={:.3f}'.format(test_cer, time.time()-start_t))
with open('test_cer.log', 'a') as f:
f.write(str(test_cer))
f.write(' ')
self.summary_writer.add_summary(self.summary, epoch)
def proper_seq_len(seqLen, timeRatio):
return [int(l/timeRatio) for l in seqLen]
if __name__ == '__main__':
# labelNum, (trainImg, seqLen_train, trainLabel), (validationImg, seqLen_validation, validationLabel), (testImg, seqLen_test, testLabel) = esposallesData.getData(1280, 10, 4)
labelNum, (trainImg, seqLen_train, trainLabel), (validationImg, seqLen_validation, validationLabel), (testImg, seqLen_test, testLabel) = esposallesData.getData(None, None, None)
seqLen_train = proper_seq_len(seqLen_train, 4)
seqLen_validation = proper_seq_len(seqLen_validation, 4)
seqLen_test = proper_seq_len(seqLen_test, 4)
model = SeqLearn(labelNum, [trainImg, seqLen_train, trainLabel, validationImg, seqLen_validation, validationLabel, testImg, seqLen_test, testLabel])
model.train()