-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.py
More file actions
251 lines (206 loc) · 10 KB
/
Model.py
File metadata and controls
251 lines (206 loc) · 10 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
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
import numpy as np
import tensorflow as tf
'''
Handwritten text recognition model written by Harald Scheidl:
https://github.com/githubharald/SimpleHTR
'''
class DecoderType:
BestPath = 0
WordBeamSearch = 1
class Model:
# model constants
batchSize = 50
imgSize = (128, 32)
maxTextLen = 32
def __init__(self, charList, decoderType=DecoderType.BestPath,
mustRestore=False, dump=False):
self.dump = dump
self.charList = charList
self.decoderType = decoderType
self.mustRestore = mustRestore
self.snapID = 0
# Whether to use normalization over a batch or a population
self.is_train = tf.placeholder(tf.bool, name='is_train')
# input image batch
self.inputImgs = tf.placeholder(tf.float32, shape=(
None, Model.imgSize[0], Model.imgSize[1]))
# setup CNN, RNN and CTC
self.setupCNN()
self.setupRNN()
self.setupCTC()
# setup optimizer to train NN
self.batchesTrained = 0
self.learningRate = tf.placeholder(tf.float32, shape=[])
self.update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(self.update_ops):
self.optimizer = tf.train.RMSPropOptimizer(
self.learningRate).minimize(self.loss)
# initialize TF
(self.sess, self.saver) = self.setupTF()
def setupCNN(self):
cnnIn4d = tf.expand_dims(input=self.inputImgs, axis=3)
# list of parameters for the layers
kernelVals = [5, 5, 3, 3, 3]
featureVals = [1, 32, 64, 128, 128, 256]
strideVals = poolVals = [(2, 2), (2, 2), (1, 2), (1, 2), (1, 2)]
numLayers = len(strideVals)
# create layers
pool = cnnIn4d # input to first CNN layer
for i in range(numLayers):
kernel = tf.Variable(tf.truncated_normal(
[kernelVals[i], kernelVals[i], featureVals[i],
featureVals[i + 1]], stddev=0.1))
conv = tf.nn.conv2d(pool, kernel, padding='SAME',
strides=(1, 1, 1, 1))
conv_norm = tf.layers.batch_normalization(conv,
training=self.is_train)
relu = tf.nn.relu(conv_norm)
pool = tf.nn.max_pool(relu, (1, poolVals[i][0], poolVals[i][1], 1),
(1, strideVals[i][0], strideVals[i][1], 1),
'VALID')
self.cnnOut4d = pool
def setupRNN(self):
rnnIn3d = tf.squeeze(self.cnnOut4d, axis=[2])
# basic cells which is used to build RNN
numHidden = 256
cells = [
tf.contrib.rnn.LSTMCell(num_units=numHidden, state_is_tuple=True)
for _ in range(2)] # 2 layers
# stack basic cells
stacked = tf.contrib.rnn.MultiRNNCell(cells, state_is_tuple=True)
# bidirectional RNN
# BxTxF -> BxTx2H
((fw, bw), _) = tf.nn.bidirectional_dynamic_rnn(cell_fw=stacked,
cell_bw=stacked,
inputs=rnnIn3d,
dtype=rnnIn3d.dtype)
# BxTxH + BxTxH -> BxTx2H -> BxTx1X2H
concat = tf.expand_dims(tf.concat([fw, bw], 2), 2)
# project output to chars (including blank): BxTx1x2H -> BxTx1xC -> BxTxC
kernel = tf.Variable(
tf.truncated_normal([1, 1, numHidden * 2, len(self.charList) + 1],
stddev=0.1))
self.rnnOut3d = tf.squeeze(
tf.nn.atrous_conv2d(value=concat, filters=kernel, rate=1,
padding='SAME'), axis=[2])
def setupCTC(self):
# BxTxC -> TxBxC
self.ctcIn3dTBC = tf.transpose(self.rnnOut3d, [1, 0, 2])
# ground truth text as sparse tensor
self.gtTexts = tf.SparseTensor(
tf.placeholder(tf.int64, shape=[None, 2]),
tf.placeholder(tf.int32, [None]), tf.placeholder(tf.int64, [2]))
# calc loss for batch
self.seqLen = tf.placeholder(tf.int32, [None])
self.loss = tf.reduce_mean(
tf.nn.ctc_loss(labels=self.gtTexts, inputs=self.ctcIn3dTBC,
sequence_length=self.seqLen,
ctc_merge_repeated=True))
# calc loss for each element to compute label probability
self.savedCtcInput = tf.placeholder(tf.float32,
shape=[Model.maxTextLen, None,
len(self.charList) + 1])
self.lossPerElement = tf.nn.ctc_loss(labels=self.gtTexts,
inputs=self.savedCtcInput,
sequence_length=self.seqLen,
ctc_merge_repeated=True)
# decoder: either best path decoding or beam search decoding
if self.decoderType == DecoderType.BestPath:
self.decoder = tf.nn.ctc_greedy_decoder(inputs=self.ctcIn3dTBC,
sequence_length=self.seqLen)
elif self.decoderType == DecoderType.WordBeamSearch:
# import compiled word beam search operation (see https://github.com/githubharald/CTCWordBeamSearch)
word_beam_search_module = tf.load_op_library('./TFWordBeamSearch.so')
# prepare information about language (dictionary, characters in dataset, characters forming words)
chars = str().join(self.charList)
with open('model/wordCharList.txt', "rb") as f:
byte = f.read(1)
if byte != "":
byte = f.read()
myString = byte.decode("Windows-1255")
wordChars = myString.splitlines()[0]
corpus = open('data/corpus.txt').read()
# decode using the "Words" mode of word beam search
self.decoder = word_beam_search_module.word_beam_search(
tf.nn.softmax(self.ctcIn3dTBC, axis=2), 50, 'Words', 0.0,
corpus.encode('utf8'), chars.encode('utf8'),
wordChars.encode('utf8'))
def setupTF(self):
sess = tf.Session() # TF session
saver = tf.train.Saver(max_to_keep=1) # saver saves model to file
modelDir = 'model/'
latestSnapshot = tf.train.latest_checkpoint(
modelDir) # is there a saved model?
# if model must be restored (for inference), there must be a snapshot
if self.mustRestore and not latestSnapshot:
raise Exception('No saved model found in: ' + modelDir)
# load saved model if available
if latestSnapshot:
saver.restore(sess, latestSnapshot)
else:
sess.run(tf.global_variables_initializer())
return (sess, saver)
def toSparse(self, texts):
indices = []
values = []
shape = [len(texts), 0] # last entry must be max(labelList[i])
# go over all texts
for (batchElement, text) in enumerate(texts):
# convert to string of label (i.e. class-ids)
labelStr = [self.charList.index(c) for c in text]
# sparse tensor must have size of max. label-string
if len(labelStr) > shape[1]:
shape[1] = len(labelStr)
# put each label into sparse tensor
for (i, label) in enumerate(labelStr):
indices.append([batchElement, i])
values.append(label)
return (indices, values, shape)
def decoderOutputToText(self, ctcOutput, batchSize):
# contains string of labels for each batch element
encodedLabelStrs = [[] for i in range(batchSize)]
# word beam search: label strings terminated by blank
if self.decoderType == DecoderType.WordBeamSearch:
blank = len(self.charList)
for b in range(batchSize):
for label in ctcOutput[b]:
if label == blank:
break
encodedLabelStrs[b].append(label)
# TF decoders: label strings are contained in sparse tensor
else:
# ctc returns tuple, first element is SparseTensor
decoded = ctcOutput[0][0]
# go over all indices and save mapping: batch -> values
idxDict = {b: [] for b in range(batchSize)}
for (idx, idx2d) in enumerate(decoded.indices):
label = decoded.values[idx]
batchElement = idx2d[0] # index according to [b,t]
encodedLabelStrs[batchElement].append(label)
# map labels to chars for all batch elements
return [str().join([self.charList[c] for c in labelStr]) for labelStr in
encodedLabelStrs]
def inferBatch(self, batch, calcProbability=False, probabilityOfGT=False):
# decode, optionally save RNN output
numBatchElements = len(batch.imgs)
evalRnnOutput = self.dump or calcProbability
evalList = [self.decoder] + ([self.ctcIn3dTBC] if evalRnnOutput else [])
feedDict = {self.inputImgs: batch.imgs,
self.seqLen: [Model.maxTextLen] * numBatchElements,
self.is_train: False}
evalRes = self.sess.run(evalList, feedDict)
decoded = evalRes[0]
texts = self.decoderOutputToText(decoded, numBatchElements)
# feed RNN output and recognized text into CTC loss to compute labeling probability
probs = None
if calcProbability:
sparse = self.toSparse(
batch.gtTexts) if probabilityOfGT else self.toSparse(texts)
ctcInput = evalRes[1]
evalList = self.lossPerElement
feedDict = {self.savedCtcInput: ctcInput, self.gtTexts: sparse,
self.seqLen: [Model.maxTextLen] * numBatchElements,
self.is_train: False}
lossVals = self.sess.run(evalList, feedDict)
probs = np.exp(-lossVals)
return (texts, probs)