forked from ocropus-archive/DUP-ocropy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocropus-rtrain
executable file
·339 lines (281 loc) · 11.2 KB
/
ocropus-rtrain
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
#!/usr/bin/env python
from __future__ import print_function
import random as pyrandom
import re
import os.path
import traceback
import argparse
import sys
import numpy as np
import matplotlib.pyplot as plt
import ocrolib
import ocrolib.lstm as lstm
from ocrolib import lineest
np.seterr(divide='raise',over='raise',invalid='raise',under='ignore')
parser = argparse.ArgumentParser("train an RNN recognizer")
# line normalization
parser.add_argument("-e","--lineest",default="center",
help="type of text line estimator, default: %(default)s")
parser.add_argument("-E","--nolineest",action="store_true",
help="don't perform line estimation and load .dew.png file")
parser.add_argument("-l","--height",default=48,type=int,
help="set the default height for line estimation, default: %(default)s")
parser.add_argument("--dewarp",action="store_true",
help="only perform line estimation and output .dew.png file")
# character set
parser.add_argument("-c","--codec",default=[],nargs='*',
help="construct a codec from the input text")
# learning
parser.add_argument("-C","--clstm",action="store_true",
help="use C++ LSTM")
parser.add_argument("-r","--lrate",type=float,default=1e-4,
help="LSTM learning rate, default: %(default)s")
parser.add_argument("-S","--hiddensize",type=int,default=100,
help="# LSTM state units, default: %(default)s")
parser.add_argument("-o","--output",default=None,
help="LSTM model file")
parser.add_argument("-F","--savefreq",type=int,default=1000,
help="LSTM save frequency, default: %(default)s")
parser.add_argument("--strip",action="store_false",
help="strip the model before saving")
parser.add_argument("-N","--ntrain",type=int,default=1000000,
help="# lines to train before stopping, default: %(default)s")
parser.add_argument("-t","--tests",default=None,
help="test cases for error estimation")
parser.add_argument('--unidirectional',action="store_true",
help="use only unidirectional LSTM")
parser.add_argument("--updates",action="store_true",
help="verbose LSTM updates")
parser.add_argument('--load',default=None,
help="start training with a previously trained model")
parser.add_argument('--start',default=-1,type=int,
help="manually set the number of already learned lines, which influences the naming and stoping condition, default: %(default)s which will then be overriden by the value saved in the network")
# debugging
parser.add_argument("-X","--exec",default="None",dest="execute",
help="execute before anything else (usually used for imports)")
parser.add_argument("-v","--verbose",action="store_true")
parser.add_argument("-d","--display",type=int,default=0,
help="display output for every nth iteration, where n=DISPLAY, default: %(default)s")
parser.add_argument("-m","--movie",default=None)
parser.add_argument("-M","--moviesample",default=None)
parser.add_argument("-q","--quiet",action="store_true")
parser.add_argument("-Q","--nocheck",action="store_true")
parser.add_argument("-p","--pad",type=int,default=16)
# add file
parser.add_argument("-f","--file",default=None,help="path to file listing input files, one per line")
parser.add_argument("files",nargs="*")
args = parser.parse_args()
inputs = ocrolib.glob_all(args.files)
if args.file is not None:
print("getting training data from file")
with open(args.file) as file:
for l in file:
inputs.append(l.rstrip())
if len(inputs)==0:
parser.print_help()
sys.exit(0)
print("# inputs", len(inputs))
# pre-execute any python commands
exec args.execute
# make sure movie mode is used correctly
if args.movie is not None:
if args.display<2:
print("you must set --display to some number greater than 1")
sys.exit(0)
if args.moviesample is None:
args.moviesample = inputs[0]
# make sure an output file has been set
if args.output is None:
print("you must give an output file with %d in it, or a prefix")
sys.exit(0)
if not "%" in args.output:
if args.clstm:
oname = args.output+"-%08d.h5"
else:
oname = args.output+"-%08d.pyrnn"
else:
oname = args.output
# get a separate test set, if present
tests = None
if args.tests is not None:
tests = ocrolib.glob_all(args.tests.split(":"))
print("# tests", len(tests) if tests is not None else "None")
# load the line normalizer
if args.lineest=="center":
lnorm = lineest.CenterNormalizer()
else:
raise Exception(args.lineest+": unknown line normalizer")
lnorm.setHeight(args.height)
# The `codec` maps between strings and arrays of integers.
if args.codec!=[]:
print("# building codec")
codec = lstm.Codec()
charset = set()
print(args.codec)
for fname in ocrolib.glob_all(args.codec):
transcript = ocrolib.read_text(fname)
l = list(lstm.normalize_nfkc(transcript))
charset = charset.union(l)
charset = sorted(list(charset))
charset = [c for c in charset if c>" " and c!="~"]
else:
print("# using default codec")
charset = sorted(list(set(list(lstm.ascii_labels) + list(ocrolib.chars.default))))
charset = [""," ","~",]+[c for c in charset if c not in [" ","~"]]
print("# charset size", len(charset), end=' ')
if len(charset)<200:
print("[" + "".join(charset) + "]")
else:
s = "".join(charset)
print("[" + s[:20], "...", s[-20:] + "]")
codec = lstm.Codec().init(charset)
# Load an existing network or construct a new one
# Somewhat convoluted logic for dealing with old style Python
# modules and new style C++ LSTM networks.
def save_lstm(fname,network):
if args.clstm:
network.lstm.save(fname)
else:
if args.strip:
network.clear_log()
for x in network.walk(): x.preSave()
ocrolib.save_object(fname,network)
if args.strip:
for x in network.walk(): x.postLoad()
def load_lstm(fname):
if args.clstm:
network = lstm.SeqRecognizer(args.height,args.hiddensize,
codec=codec,
normalize=lstm.normalize_nfkc)
import clstm
mylstm = clstm.make_BIDILSTM()
mylstm.init(network.No,args.hiddensize,network.Ni)
mylstm.load(fname)
network.lstm = clstm.CNetwork(mylstm)
return network
else:
network = ocrolib.load_object(last_save)
network.upgrade()
for x in network.walk(): x.postLoad()
return network
if args.load:
print("# loading", args.load)
last_save = args.load
network = load_lstm(args.load)
else:
last_save = None
network = lstm.SeqRecognizer(args.height,args.hiddensize,
codec=codec,
normalize=lstm.normalize_nfkc)
if args.clstm:
import clstm
mylstm = clstm.make_BIDILSTM()
mylstm.init(network.No,args.hiddensize,network.Ni)
network.lstm = clstm.CNetwork(mylstm)
if getattr(network,"lnorm",None) is None:
network.lnorm = lnorm
network.upgrade()
if network.last_trial%100==99: network.last_trial += 1
print("# last_trial", network.last_trial)
# set up the learning rate
network.setLearningRate(args.lrate,0.9)
if args.updates: network.lstm.verbose = 1
# used for plotting
plt.ion()
plt.rc('xtick',labelsize=7)
plt.rc('ytick',labelsize=7)
plt.rcParams.update({"font.size":7})
def cleandisp(s):
return re.sub('[$]',r'#',s)
def plot_network_info(network,transcript,pred,gta):
plt.subplot(511)
plt.imshow(line.T,cmap=plt.cm.gray)
plt.title(cleandisp(transcript))
plt.subplot(512)
plt.gca().set_xticks([])
plt.imshow(network.outputs.T[1:],vmin=0,cmap=plt.cm.hot)
plt.title(cleandisp(pred[:len(transcript)]))
plt.subplot(513)
plt.imshow(network.aligned.T[1:],vmin=0,cmap=plt.cm.hot)
plt.title(cleandisp(gta[:len(transcript)]))
plt.subplot(514)
plt.plot(network.outputs[:,0],color='yellow',linewidth=3,alpha=0.5)
plt.plot(network.outputs[:,1],color='green',linewidth=3,alpha=0.5)
plt.plot(np.amax(network.outputs[:,2:],axis=1),color='blue',linewidth=3,alpha=0.5)
plt.plot(network.aligned[:,0],color='orange',linestyle='dashed',alpha=0.7)
plt.plot(network.aligned[:,1],color='green',linestyle='dashed',alpha=0.5)
plt.plot(np.amax(network.aligned[:,2:],axis=1),color='blue',linestyle='dashed',alpha=0.5)
plt.subplot(515)
plt.gca().set_yscale('log')
r = 10000
errs = network.errors(range=r,smooth=100)
xs = np.arange(len(errs))+network.last_trial-len(errs)
plt.plot(xs,errs,color='black')
plt.plot(xs,network.errors(range=r),color='black',alpha=0.4)
plt.plot(xs,network.cerrors(range=r,smooth=100),color='red',linestyle='dashed')
start = args.start if args.start>=0 else network.last_trial
for trial in range(start,args.ntrain):
network.last_trial = trial+1
do_display = (args.display>0 and trial%args.display==0)
do_update = 1
if args.movie and do_display:
fname = args.moviesample
do_update = 0
else:
fname = pyrandom.sample(inputs,1)[0]
base,_ = ocrolib.allsplitext(fname)
try:
line = ocrolib.read_image_gray(fname)
transcript = ocrolib.read_text(base+".gt.txt")
except IOError as e:
print("ERROR", e)
continue
if not args.nolineest:
assert "dew.png" not in fname,"don't dewarp already dewarped lines"
network.lnorm.measure(np.amax(line)-line)
line = network.lnorm.normalize(line,cval=np.amax(line))
else:
assert "dew.png" in fname,"input must already be dewarped"
if line.size<10 or np.amax(line)==np.amin(line):
print("EMPTY-INPUT")
continue
line = line * 1.0/np.amax(line)
line = np.amax(line)-line
line = line.T
if args.pad>0:
w = line.shape[1]
line = np.vstack([np.zeros((args.pad,w)),line,np.zeros((args.pad,w))])
cs = np.array(codec.encode(transcript),'i')
try:
pcs = network.trainSequence(line,cs,update=do_update,key=fname)
except FloatingPointError as e:
print("# oops, got FloatingPointError", e)
traceback.print_exc()
network = load_lstm(last_save)
continue
except lstm.RangeError as e:
continue
pred = "".join(codec.decode(pcs))
acs = lstm.translate_back(network.aligned)
gta = "".join(codec.decode(acs))
if not args.quiet:
print("%d %.2f %s" % (trial, network.error, line.shape), fname)
print(" TRU:", repr(transcript))
print(" ALN:", repr(gta[:len(transcript)+5]))
print(" OUT:", repr(pred[:len(transcript)+5]))
pred = re.sub(' ','_',pred)
gta = re.sub(' ','_',gta)
if (trial+1)%args.savefreq==0:
ofile = oname%(trial+1)+".gz"
print("# saving", ofile)
save_lstm(ofile,network)
last_save = ofile
if do_display:
plt.figure("training",figsize=(1400//75,800//75),dpi=75)
plt.clf()
plt.gcf().canvas.set_window_title(args.output)
plot_network_info(network,transcript,pred,gta)
plt.ginput(1,0.01)
if args.movie is not None:
plt.draw()
plt.savefig("%s-%08d.png"%(args.movie,trial),bbox_inches=0)