forked from tesseract-ocr/tesseract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlstmtrainer.cpp
1345 lines (1290 loc) · 54 KB
/
lstmtrainer.cpp
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////
// File: lstmtrainer.cpp
// Description: Top-level line trainer class for LSTM-based networks.
// Author: Ray Smith
// Created: Fir May 03 09:14:06 PST 2013
//
// (C) Copyright 2013, Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////
// Include automatically generated configuration file if running autoconf.
#ifdef HAVE_CONFIG_H
#include "config_auto.h"
#endif
#include "lstmtrainer.h"
#include <string>
#include "allheaders.h"
#include "boxread.h"
#include "ctc.h"
#include "imagedata.h"
#include "input.h"
#include "networkbuilder.h"
#include "ratngs.h"
#include "recodebeam.h"
#ifdef INCLUDE_TENSORFLOW
#include "tfnetwork.h"
#endif
#include "tprintf.h"
#include "callcpp.h"
namespace tesseract {
// Min actual error rate increase to constitute divergence.
const double kMinDivergenceRate = 50.0;
// Min iterations since last best before acting on a stall.
const int kMinStallIterations = 10000;
// Fraction of current char error rate that sub_trainer_ has to be ahead
// before we declare the sub_trainer_ a success and switch to it.
const double kSubTrainerMarginFraction = 3.0 / 128;
// Factor to reduce learning rate on divergence.
const double kLearningRateDecay = sqrt(0.5);
// LR adjustment iterations.
const int kNumAdjustmentIterations = 100;
// How often to add data to the error_graph_.
const int kErrorGraphInterval = 1000;
// Number of training images to train between calls to MaintainCheckpoints.
const int kNumPagesPerBatch = 100;
// Min percent error rate to consider start-up phase over.
const int kMinStartedErrorRate = 75;
// Error rate at which to transition to stage 1.
const double kStageTransitionThreshold = 10.0;
// Confidence beyond which the truth is more likely wrong than the recognizer.
const double kHighConfidence = 0.9375; // 15/16.
// Fraction of weight sign-changing total to constitute a definite improvement.
const double kImprovementFraction = 15.0 / 16.0;
// Fraction of last written best to make it worth writing another.
const double kBestCheckpointFraction = 31.0 / 32.0;
// Scale factor for display of target activations of CTC.
const int kTargetXScale = 5;
const int kTargetYScale = 100;
LSTMTrainer::LSTMTrainer()
: training_data_(0),
file_reader_(LoadDataFromFile),
file_writer_(SaveDataToFile),
checkpoint_reader_(
NewPermanentTessCallback(this, &LSTMTrainer::ReadTrainingDump)),
checkpoint_writer_(
NewPermanentTessCallback(this, &LSTMTrainer::SaveTrainingDump)),
sub_trainer_(NULL) {
EmptyConstructor();
debug_interval_ = 0;
}
LSTMTrainer::LSTMTrainer(FileReader file_reader, FileWriter file_writer,
CheckPointReader checkpoint_reader,
CheckPointWriter checkpoint_writer,
const char* model_base, const char* checkpoint_name,
int debug_interval, inT64 max_memory)
: training_data_(max_memory),
file_reader_(file_reader),
file_writer_(file_writer),
checkpoint_reader_(checkpoint_reader),
checkpoint_writer_(checkpoint_writer),
sub_trainer_(NULL) {
EmptyConstructor();
if (file_reader_ == NULL) file_reader_ = LoadDataFromFile;
if (file_writer_ == NULL) file_writer_ = SaveDataToFile;
if (checkpoint_reader_ == NULL) {
checkpoint_reader_ =
NewPermanentTessCallback(this, &LSTMTrainer::ReadTrainingDump);
}
if (checkpoint_writer_ == NULL) {
checkpoint_writer_ =
NewPermanentTessCallback(this, &LSTMTrainer::SaveTrainingDump);
}
debug_interval_ = debug_interval;
model_base_ = model_base;
checkpoint_name_ = checkpoint_name;
}
LSTMTrainer::~LSTMTrainer() {
delete align_win_;
delete target_win_;
delete ctc_win_;
delete recon_win_;
delete checkpoint_reader_;
delete checkpoint_writer_;
delete sub_trainer_;
}
// Tries to deserialize a trainer from the given file and silently returns
// false in case of failure.
bool LSTMTrainer::TryLoadingCheckpoint(const char* filename) {
GenericVector<char> data;
if (!(*file_reader_)(filename, &data)) return false;
tprintf("Loaded file %s, unpacking...\n", filename);
return checkpoint_reader_->Run(data, this);
}
// Initializes the character set encode/decode mechanism.
// train_flags control training behavior according to the TrainingFlags
// enum, including character set encoding.
// script_dir is required for TF_COMPRESS_UNICHARSET, and, if provided,
// fully initializes the unicharset from the universal unicharsets.
// Note: Call before InitNetwork!
void LSTMTrainer::InitCharSet(const UNICHARSET& unicharset,
const STRING& script_dir, int train_flags) {
EmptyConstructor();
training_flags_ = train_flags;
ccutil_.unicharset.CopyFrom(unicharset);
null_char_ = GetUnicharset().has_special_codes() ? UNICHAR_BROKEN
: GetUnicharset().size();
SetUnicharsetProperties(script_dir);
}
// Initializes the character set encode/decode mechanism directly from a
// previously setup UNICHARSET and UnicharCompress.
// ctc_mode controls how the truth text is mapped to the network targets.
// Note: Call before InitNetwork!
void LSTMTrainer::InitCharSet(const UNICHARSET& unicharset,
const UnicharCompress& recoder) {
EmptyConstructor();
int flags = TF_COMPRESS_UNICHARSET;
training_flags_ = static_cast<TrainingFlags>(flags);
ccutil_.unicharset.CopyFrom(unicharset);
recoder_ = recoder;
null_char_ = GetUnicharset().has_special_codes() ? UNICHAR_BROKEN
: GetUnicharset().size();
RecodedCharID code;
recoder_.EncodeUnichar(null_char_, &code);
null_char_ = code(0);
// Space should encode as itself.
recoder_.EncodeUnichar(UNICHAR_SPACE, &code);
ASSERT_HOST(code(0) == UNICHAR_SPACE);
}
// Initializes the trainer with a network_spec in the network description
// net_flags control network behavior according to the NetworkFlags enum.
// There isn't really much difference between them - only where the effects
// are implemented.
// For other args see NetworkBuilder::InitNetwork.
// Note: Be sure to call InitCharSet before InitNetwork!
bool LSTMTrainer::InitNetwork(const STRING& network_spec, int append_index,
int net_flags, float weight_range,
float learning_rate, float momentum) {
// Call after InitCharSet.
ASSERT_HOST(GetUnicharset().size() > SPECIAL_UNICHAR_CODES_COUNT);
weight_range_ = weight_range;
learning_rate_ = learning_rate;
momentum_ = momentum;
int num_outputs = null_char_ == GetUnicharset().size()
? null_char_ + 1
: GetUnicharset().size();
if (IsRecoding()) num_outputs = recoder_.code_range();
if (!NetworkBuilder::InitNetwork(num_outputs, network_spec, append_index,
net_flags, weight_range, &randomizer_,
&network_)) {
return false;
}
network_str_ += network_spec;
tprintf("Built network:%s from request %s\n",
network_->spec().string(), network_spec.string());
tprintf("Training parameters:\n Debug interval = %d,"
" weights = %g, learning rate = %g, momentum=%g\n",
debug_interval_, weight_range_, learning_rate_, momentum_);
return true;
}
// Initializes a trainer from a serialized TFNetworkModel proto.
// Returns the global step of TensorFlow graph or 0 if failed.
int LSTMTrainer::InitTensorFlowNetwork(const std::string& tf_proto) {
#ifdef INCLUDE_TENSORFLOW
delete network_;
TFNetwork* tf_net = new TFNetwork("TensorFlow");
training_iteration_ = tf_net->InitFromProtoStr(tf_proto);
if (training_iteration_ == 0) {
tprintf("InitFromProtoStr failed!!\n");
return 0;
}
network_ = tf_net;
ASSERT_HOST(recoder_.code_range() == tf_net->num_classes());
return training_iteration_;
#else
tprintf("TensorFlow not compiled in! -DINCLUDE_TENSORFLOW\n");
return 0;
#endif
}
// Resets all the iteration counters for fine tuning or traininng a head,
// where we want the error reporting to reset.
void LSTMTrainer::InitIterations() {
sample_iteration_ = 0;
training_iteration_ = 0;
learning_iteration_ = 0;
prev_sample_iteration_ = 0;
best_error_rate_ = 100.0;
best_iteration_ = 0;
worst_error_rate_ = 0.0;
worst_iteration_ = 0;
stall_iteration_ = kMinStallIterations;
improvement_steps_ = kMinStallIterations;
perfect_delay_ = 0;
last_perfect_training_iteration_ = 0;
for (int i = 0; i < ET_COUNT; ++i) {
best_error_rates_[i] = 100.0;
worst_error_rates_[i] = 0.0;
error_buffers_[i].init_to_size(kRollingBufferSize_, 0.0);
error_rates_[i] = 100.0;
}
error_rate_of_last_saved_best_ = kMinStartedErrorRate;
}
// If the training sample is usable, grid searches for the optimal
// dict_ratio/cert_offset, and returns the results in a string of space-
// separated triplets of ratio,offset=worderr.
Trainability LSTMTrainer::GridSearchDictParams(
const ImageData* trainingdata, int iteration, double min_dict_ratio,
double dict_ratio_step, double max_dict_ratio, double min_cert_offset,
double cert_offset_step, double max_cert_offset, STRING* results) {
sample_iteration_ = iteration;
NetworkIO fwd_outputs, targets;
Trainability result =
PrepareForBackward(trainingdata, &fwd_outputs, &targets);
if (result == UNENCODABLE || result == HI_PRECISION_ERR || dict_ == NULL)
return result;
// Encode/decode the truth to get the normalization.
GenericVector<int> truth_labels, ocr_labels, xcoords;
ASSERT_HOST(EncodeString(trainingdata->transcription(), &truth_labels));
// NO-dict error.
RecodeBeamSearch base_search(recoder_, null_char_, SimpleTextOutput(), NULL);
base_search.Decode(fwd_outputs, 1.0, 0.0, RecodeBeamSearch::kMinCertainty,
NULL);
base_search.ExtractBestPathAsLabels(&ocr_labels, &xcoords);
STRING truth_text = DecodeLabels(truth_labels);
STRING ocr_text = DecodeLabels(ocr_labels);
double baseline_error = ComputeWordError(&truth_text, &ocr_text);
results->add_str_double("0,0=", baseline_error);
RecodeBeamSearch search(recoder_, null_char_, SimpleTextOutput(), dict_);
for (double r = min_dict_ratio; r < max_dict_ratio; r += dict_ratio_step) {
for (double c = min_cert_offset; c < max_cert_offset;
c += cert_offset_step) {
search.Decode(fwd_outputs, r, c, RecodeBeamSearch::kMinCertainty, NULL);
search.ExtractBestPathAsLabels(&ocr_labels, &xcoords);
truth_text = DecodeLabels(truth_labels);
ocr_text = DecodeLabels(ocr_labels);
// This is destructive on both strings.
double word_error = ComputeWordError(&truth_text, &ocr_text);
if ((r == min_dict_ratio && c == min_cert_offset) ||
!std::isfinite(word_error)) {
STRING t = DecodeLabels(truth_labels);
STRING o = DecodeLabels(ocr_labels);
tprintf("r=%g, c=%g, truth=%s, ocr=%s, wderr=%g, truth[0]=%d\n", r, c,
t.string(), o.string(), word_error, truth_labels[0]);
}
results->add_str_double(" ", r);
results->add_str_double(",", c);
results->add_str_double("=", word_error);
}
}
return result;
}
// Provides output on the distribution of weight values.
void LSTMTrainer::DebugNetwork() {
network_->DebugWeights();
}
// Loads a set of lstmf files that were created using the lstm.train config to
// tesseract into memory ready for training. Returns false if nothing was
// loaded.
bool LSTMTrainer::LoadAllTrainingData(const GenericVector<STRING>& filenames) {
training_data_.Clear();
return training_data_.LoadDocuments(filenames, "eng", CacheStrategy(),
file_reader_);
}
// Keeps track of best and locally worst char error_rate and launches tests
// using tester, when a new min or max is reached.
// Writes checkpoints at appropriate times and builds and returns a log message
// to indicate progress. Returns false if nothing interesting happened.
bool LSTMTrainer::MaintainCheckpoints(TestCallback tester, STRING* log_msg) {
PrepareLogMsg(log_msg);
double error_rate = CharError();
int iteration = learning_iteration();
if (iteration >= stall_iteration_ &&
error_rate > best_error_rate_ * (1.0 + kSubTrainerMarginFraction) &&
best_error_rate_ < kMinStartedErrorRate && !best_trainer_.empty()) {
// It hasn't got any better in a long while, and is a margin worse than the
// best, so go back to the best model and try a different learning rate.
StartSubtrainer(log_msg);
}
SubTrainerResult sub_trainer_result = STR_NONE;
if (sub_trainer_ != NULL) {
sub_trainer_result = UpdateSubtrainer(log_msg);
if (sub_trainer_result == STR_REPLACED) {
// Reset the inputs, as we have overwritten *this.
error_rate = CharError();
iteration = learning_iteration();
PrepareLogMsg(log_msg);
}
}
bool result = true; // Something interesting happened.
GenericVector<char> rec_model_data;
if (error_rate < best_error_rate_) {
SaveRecognitionDump(&rec_model_data);
log_msg->add_str_double(" New best char error = ", error_rate);
*log_msg += UpdateErrorGraph(iteration, error_rate, rec_model_data, tester);
// If sub_trainer_ is not NULL, either *this beat it to a new best, or it
// just overwrote *this. In either case, we have finished with it.
delete sub_trainer_;
sub_trainer_ = NULL;
stall_iteration_ = learning_iteration() + kMinStallIterations;
if (TransitionTrainingStage(kStageTransitionThreshold)) {
log_msg->add_str_int(" Transitioned to stage ", CurrentTrainingStage());
}
checkpoint_writer_->Run(NO_BEST_TRAINER, this, &best_trainer_);
if (error_rate < error_rate_of_last_saved_best_ * kBestCheckpointFraction) {
STRING best_model_name = DumpFilename();
if (!(*file_writer_)(best_trainer_, best_model_name)) {
*log_msg += " failed to write best model:";
} else {
*log_msg += " wrote best model:";
error_rate_of_last_saved_best_ = best_error_rate_;
}
*log_msg += best_model_name;
}
} else if (error_rate > worst_error_rate_) {
SaveRecognitionDump(&rec_model_data);
log_msg->add_str_double(" New worst char error = ", error_rate);
*log_msg += UpdateErrorGraph(iteration, error_rate, rec_model_data, tester);
if (worst_error_rate_ > best_error_rate_ + kMinDivergenceRate &&
best_error_rate_ < kMinStartedErrorRate && !best_trainer_.empty()) {
// Error rate has ballooned. Go back to the best model.
*log_msg += "\nDivergence! ";
// Copy best_trainer_ before reading it, as it will get overwritten.
GenericVector<char> revert_data(best_trainer_);
if (checkpoint_reader_->Run(revert_data, this)) {
LogIterations("Reverted to", log_msg);
ReduceLearningRates(this, log_msg);
} else {
LogIterations("Failed to Revert at", log_msg);
}
// If it fails again, we will wait twice as long before reverting again.
stall_iteration_ = iteration + 2 * (iteration - learning_iteration());
// Re-save the best trainer with the new learning rates and stall
// iteration.
checkpoint_writer_->Run(NO_BEST_TRAINER, this, &best_trainer_);
}
} else {
// Something interesting happened only if the sub_trainer_ was trained.
result = sub_trainer_result != STR_NONE;
}
if (checkpoint_writer_ != NULL && file_writer_ != NULL &&
checkpoint_name_.length() > 0) {
// Write a current checkpoint.
GenericVector<char> checkpoint;
if (!checkpoint_writer_->Run(FULL, this, &checkpoint) ||
!(*file_writer_)(checkpoint, checkpoint_name_)) {
*log_msg += " failed to write checkpoint.";
} else {
*log_msg += " wrote checkpoint.";
}
}
*log_msg += "\n";
return result;
}
// Builds a string containing a progress message with current error rates.
void LSTMTrainer::PrepareLogMsg(STRING* log_msg) const {
LogIterations("At", log_msg);
log_msg->add_str_double(", Mean rms=", error_rates_[ET_RMS]);
log_msg->add_str_double("%, delta=", error_rates_[ET_DELTA]);
log_msg->add_str_double("%, char train=", error_rates_[ET_CHAR_ERROR]);
log_msg->add_str_double("%, word train=", error_rates_[ET_WORD_RECERR]);
log_msg->add_str_double("%, skip ratio=", error_rates_[ET_SKIP_RATIO]);
*log_msg += "%, ";
}
// Appends <intro_str> iteration learning_iteration()/training_iteration()/
// sample_iteration() to the log_msg.
void LSTMTrainer::LogIterations(const char* intro_str, STRING* log_msg) const {
*log_msg += intro_str;
log_msg->add_str_int(" iteration ", learning_iteration());
log_msg->add_str_int("/", training_iteration());
log_msg->add_str_int("/", sample_iteration());
}
// Returns true and increments the training_stage_ if the error rate has just
// passed through the given threshold for the first time.
bool LSTMTrainer::TransitionTrainingStage(float error_threshold) {
if (best_error_rate_ < error_threshold &&
training_stage_ + 1 < num_training_stages_) {
++training_stage_;
return true;
}
return false;
}
// Writes to the given file. Returns false in case of error.
bool LSTMTrainer::Serialize(TFile* fp) const {
if (!LSTMRecognizer::Serialize(fp)) return false;
if (fp->FWrite(&learning_iteration_, sizeof(learning_iteration_), 1) != 1)
return false;
if (fp->FWrite(&prev_sample_iteration_, sizeof(prev_sample_iteration_), 1) !=
1)
return false;
if (fp->FWrite(&perfect_delay_, sizeof(perfect_delay_), 1) != 1) return false;
if (fp->FWrite(&last_perfect_training_iteration_,
sizeof(last_perfect_training_iteration_), 1) != 1)
return false;
for (int i = 0; i < ET_COUNT; ++i) {
if (!error_buffers_[i].Serialize(fp)) return false;
}
if (fp->FWrite(&error_rates_, sizeof(error_rates_), 1) != 1) return false;
if (fp->FWrite(&training_stage_, sizeof(training_stage_), 1) != 1)
return false;
uinT8 amount = serialize_amount_;
if (fp->FWrite(&amount, sizeof(amount), 1) != 1) return false;
if (amount == LIGHT) return true; // We are done.
if (fp->FWrite(&best_error_rate_, sizeof(best_error_rate_), 1) != 1)
return false;
if (fp->FWrite(&best_error_rates_, sizeof(best_error_rates_), 1) != 1)
return false;
if (fp->FWrite(&best_iteration_, sizeof(best_iteration_), 1) != 1)
return false;
if (fp->FWrite(&worst_error_rate_, sizeof(worst_error_rate_), 1) != 1)
return false;
if (fp->FWrite(&worst_error_rates_, sizeof(worst_error_rates_), 1) != 1)
return false;
if (fp->FWrite(&worst_iteration_, sizeof(worst_iteration_), 1) != 1)
return false;
if (fp->FWrite(&stall_iteration_, sizeof(stall_iteration_), 1) != 1)
return false;
if (!best_model_data_.Serialize(fp)) return false;
if (!worst_model_data_.Serialize(fp)) return false;
if (amount != NO_BEST_TRAINER && !best_trainer_.Serialize(fp)) return false;
GenericVector<char> sub_data;
if (sub_trainer_ != NULL && !SaveTrainingDump(LIGHT, sub_trainer_, &sub_data))
return false;
if (!sub_data.Serialize(fp)) return false;
if (!best_error_history_.Serialize(fp)) return false;
if (!best_error_iterations_.Serialize(fp)) return false;
if (fp->FWrite(&improvement_steps_, sizeof(improvement_steps_), 1) != 1)
return false;
return true;
}
// Reads from the given file. Returns false in case of error.
// If swap is true, assumes a big/little-endian swap is needed.
bool LSTMTrainer::DeSerialize(bool swap, TFile* fp) {
if (!LSTMRecognizer::DeSerialize(swap, fp)) return false;
if (fp->FRead(&learning_iteration_, sizeof(learning_iteration_), 1) != 1) {
// Special case. If we successfully decoded the recognizer, but fail here
// then it means we were just given a recognizer, so issue a warning and
// allow it.
tprintf("Warning: LSTMTrainer deserialized an LSTMRecognizer!\n");
learning_iteration_ = 0;
network_->SetEnableTraining(TS_RE_ENABLE);
return true;
}
if (fp->FRead(&prev_sample_iteration_, sizeof(prev_sample_iteration_), 1) !=
1)
return false;
if (fp->FRead(&perfect_delay_, sizeof(perfect_delay_), 1) != 1) return false;
if (fp->FRead(&last_perfect_training_iteration_,
sizeof(last_perfect_training_iteration_), 1) != 1)
return false;
for (int i = 0; i < ET_COUNT; ++i) {
if (!error_buffers_[i].DeSerialize(swap, fp)) return false;
}
if (fp->FRead(&error_rates_, sizeof(error_rates_), 1) != 1) return false;
if (fp->FRead(&training_stage_, sizeof(training_stage_), 1) != 1)
return false;
uinT8 amount;
if (fp->FRead(&amount, sizeof(amount), 1) != 1) return false;
if (amount == LIGHT) return true; // Don't read the rest.
if (fp->FRead(&best_error_rate_, sizeof(best_error_rate_), 1) != 1)
return false;
if (fp->FRead(&best_error_rates_, sizeof(best_error_rates_), 1) != 1)
return false;
if (fp->FRead(&best_iteration_, sizeof(best_iteration_), 1) != 1)
return false;
if (fp->FRead(&worst_error_rate_, sizeof(worst_error_rate_), 1) != 1)
return false;
if (fp->FRead(&worst_error_rates_, sizeof(worst_error_rates_), 1) != 1)
return false;
if (fp->FRead(&worst_iteration_, sizeof(worst_iteration_), 1) != 1)
return false;
if (fp->FRead(&stall_iteration_, sizeof(stall_iteration_), 1) != 1)
return false;
if (!best_model_data_.DeSerialize(swap, fp)) return false;
if (!worst_model_data_.DeSerialize(swap, fp)) return false;
if (amount != NO_BEST_TRAINER && !best_trainer_.DeSerialize(swap, fp))
return false;
GenericVector<char> sub_data;
if (!sub_data.DeSerialize(swap, fp)) return false;
delete sub_trainer_;
if (sub_data.empty()) {
sub_trainer_ = NULL;
} else {
sub_trainer_ = new LSTMTrainer();
if (!ReadTrainingDump(sub_data, sub_trainer_)) return false;
}
if (!best_error_history_.DeSerialize(swap, fp)) return false;
if (!best_error_iterations_.DeSerialize(swap, fp)) return false;
if (fp->FRead(&improvement_steps_, sizeof(improvement_steps_), 1) != 1)
return false;
return true;
}
// De-serializes the saved best_trainer_ into sub_trainer_, and adjusts the
// learning rates (by scaling reduction, or layer specific, according to
// NF_LAYER_SPECIFIC_LR).
void LSTMTrainer::StartSubtrainer(STRING* log_msg) {
delete sub_trainer_;
sub_trainer_ = new LSTMTrainer();
if (!checkpoint_reader_->Run(best_trainer_, sub_trainer_)) {
*log_msg += " Failed to revert to previous best for trial!";
delete sub_trainer_;
sub_trainer_ = NULL;
} else {
log_msg->add_str_int(" Trial sub_trainer_ from iteration ",
sub_trainer_->training_iteration());
// Reduce learning rate so it doesn't diverge this time.
sub_trainer_->ReduceLearningRates(this, log_msg);
// If it fails again, we will wait twice as long before reverting again.
int stall_offset =
learning_iteration() - sub_trainer_->learning_iteration();
stall_iteration_ = learning_iteration() + 2 * stall_offset;
sub_trainer_->stall_iteration_ = stall_iteration_;
// Re-save the best trainer with the new learning rates and stall iteration.
checkpoint_writer_->Run(NO_BEST_TRAINER, sub_trainer_, &best_trainer_);
}
}
// While the sub_trainer_ is behind the current training iteration and its
// training error is at least kSubTrainerMarginFraction better than the
// current training error, trains the sub_trainer_, and returns STR_UPDATED if
// it did anything. If it catches up, and has a better error rate than the
// current best, as well as a margin over the current error rate, then the
// trainer in *this is replaced with sub_trainer_, and STR_REPLACED is
// returned. STR_NONE is returned if the subtrainer wasn't good enough to
// receive any training iterations.
SubTrainerResult LSTMTrainer::UpdateSubtrainer(STRING* log_msg) {
double training_error = CharError();
double sub_error = sub_trainer_->CharError();
double sub_margin = (training_error - sub_error) / sub_error;
if (sub_margin >= kSubTrainerMarginFraction) {
log_msg->add_str_double(" sub_trainer=", sub_error);
log_msg->add_str_double(" margin=", 100.0 * sub_margin);
*log_msg += "\n";
// Catch up to current iteration.
int end_iteration = training_iteration();
while (sub_trainer_->training_iteration() < end_iteration &&
sub_margin >= kSubTrainerMarginFraction) {
int target_iteration =
sub_trainer_->training_iteration() + kNumPagesPerBatch;
while (sub_trainer_->training_iteration() < target_iteration) {
sub_trainer_->TrainOnLine(this, false);
}
STRING batch_log = "Sub:";
sub_trainer_->PrepareLogMsg(&batch_log);
batch_log += "\n";
tprintf("UpdateSubtrainer:%s", batch_log.string());
*log_msg += batch_log;
sub_error = sub_trainer_->CharError();
sub_margin = (training_error - sub_error) / sub_error;
}
if (sub_error < best_error_rate_ &&
sub_margin >= kSubTrainerMarginFraction) {
// The sub_trainer_ has won the race to a new best. Switch to it.
GenericVector<char> updated_trainer;
SaveTrainingDump(LIGHT, sub_trainer_, &updated_trainer);
ReadTrainingDump(updated_trainer, this);
log_msg->add_str_int(" Sub trainer wins at iteration ",
training_iteration());
*log_msg += "\n";
return STR_REPLACED;
}
return STR_UPDATED;
}
return STR_NONE;
}
// Reduces network learning rates, either for everything, or for layers
// independently, according to NF_LAYER_SPECIFIC_LR.
void LSTMTrainer::ReduceLearningRates(LSTMTrainer* samples_trainer,
STRING* log_msg) {
if (network_->TestFlag(NF_LAYER_SPECIFIC_LR)) {
int num_reduced = ReduceLayerLearningRates(
kLearningRateDecay, kNumAdjustmentIterations, samples_trainer);
log_msg->add_str_int("\nReduced learning rate on layers: ", num_reduced);
} else {
ScaleLearningRate(kLearningRateDecay);
log_msg->add_str_double("\nReduced learning rate to :", learning_rate_);
}
*log_msg += "\n";
}
// Considers reducing the learning rate independently for each layer down by
// factor(<1), or leaving it the same, by double-training the given number of
// samples and minimizing the amount of changing of sign of weight updates.
// Even if it looks like all weights should remain the same, an adjustment
// will be made to guarantee a different result when reverting to an old best.
// Returns the number of layer learning rates that were reduced.
int LSTMTrainer::ReduceLayerLearningRates(double factor, int num_samples,
LSTMTrainer* samples_trainer) {
enum WhichWay {
LR_DOWN, // Learning rate will go down by factor.
LR_SAME, // Learning rate will stay the same.
LR_COUNT // Size of arrays.
};
// Epsilon is so small that it may as well be zero, but still positive.
const double kEpsilon = 1.0e-30;
GenericVector<STRING> layers = EnumerateLayers();
int num_layers = layers.size();
GenericVector<int> num_weights;
num_weights.init_to_size(num_layers, 0);
GenericVector<double> bad_sums[LR_COUNT];
GenericVector<double> ok_sums[LR_COUNT];
for (int i = 0; i < LR_COUNT; ++i) {
bad_sums[i].init_to_size(num_layers, 0.0);
ok_sums[i].init_to_size(num_layers, 0.0);
}
double momentum_factor = 1.0 / (1.0 - momentum_);
GenericVector<char> orig_trainer;
SaveTrainingDump(LIGHT, this, &orig_trainer);
for (int i = 0; i < num_layers; ++i) {
Network* layer = GetLayer(layers[i]);
num_weights[i] = layer->IsTraining() ? layer->num_weights() : 0;
}
int iteration = sample_iteration();
for (int s = 0; s < num_samples; ++s) {
// Which way will we modify the learning rate?
for (int ww = 0; ww < LR_COUNT; ++ww) {
// Transfer momentum to learning rate and adjust by the ww factor.
float ww_factor = momentum_factor;
if (ww == LR_DOWN) ww_factor *= factor;
// Make a copy of *this, so we can mess about without damaging anything.
LSTMTrainer copy_trainer;
copy_trainer.ReadTrainingDump(orig_trainer, ©_trainer);
// Clear the updates, doing nothing else.
copy_trainer.network_->Update(0.0, 0.0, 0);
// Adjust the learning rate in each layer.
for (int i = 0; i < num_layers; ++i) {
if (num_weights[i] == 0) continue;
copy_trainer.ScaleLayerLearningRate(layers[i], ww_factor);
}
copy_trainer.SetIteration(iteration);
// Train on the sample, but keep the update in updates_ instead of
// applying to the weights.
const ImageData* trainingdata =
copy_trainer.TrainOnLine(samples_trainer, true);
if (trainingdata == NULL) continue;
// We'll now use this trainer again for each layer.
GenericVector<char> updated_trainer;
SaveTrainingDump(LIGHT, ©_trainer, &updated_trainer);
for (int i = 0; i < num_layers; ++i) {
if (num_weights[i] == 0) continue;
LSTMTrainer layer_trainer;
layer_trainer.ReadTrainingDump(updated_trainer, &layer_trainer);
Network* layer = layer_trainer.GetLayer(layers[i]);
// Update the weights in just the layer, and also zero the updates
// matrix (to epsilon).
layer->Update(0.0, kEpsilon, 0);
// Train again on the same sample, again holding back the updates.
layer_trainer.TrainOnLine(trainingdata, true);
// Count the sign changes in the updates in layer vs in copy_trainer.
float before_bad = bad_sums[ww][i];
float before_ok = ok_sums[ww][i];
layer->CountAlternators(*copy_trainer.GetLayer(layers[i]),
&ok_sums[ww][i], &bad_sums[ww][i]);
float bad_frac =
bad_sums[ww][i] + ok_sums[ww][i] - before_bad - before_ok;
if (bad_frac > 0.0f)
bad_frac = (bad_sums[ww][i] - before_bad) / bad_frac;
}
}
++iteration;
}
int num_lowered = 0;
for (int i = 0; i < num_layers; ++i) {
if (num_weights[i] == 0) continue;
Network* layer = GetLayer(layers[i]);
float lr = GetLayerLearningRate(layers[i]);
double total_down = bad_sums[LR_DOWN][i] + ok_sums[LR_DOWN][i];
double total_same = bad_sums[LR_SAME][i] + ok_sums[LR_SAME][i];
double frac_down = bad_sums[LR_DOWN][i] / total_down;
double frac_same = bad_sums[LR_SAME][i] / total_same;
tprintf("Layer %d=%s: lr %g->%g%%, lr %g->%g%%", i, layer->name().string(),
lr * factor, 100.0 * frac_down, lr, 100.0 * frac_same);
if (frac_down < frac_same * kImprovementFraction) {
tprintf(" REDUCED\n");
ScaleLayerLearningRate(layers[i], factor);
++num_lowered;
} else {
tprintf(" SAME\n");
}
}
if (num_lowered == 0) {
// Just lower everything to make sure.
for (int i = 0; i < num_layers; ++i) {
if (num_weights[i] > 0) {
ScaleLayerLearningRate(layers[i], factor);
++num_lowered;
}
}
}
return num_lowered;
}
// Converts the string to integer class labels, with appropriate null_char_s
// in between if not in SimpleTextOutput mode. Returns false on failure.
/* static */
bool LSTMTrainer::EncodeString(const STRING& str, const UNICHARSET& unicharset,
const UnicharCompress* recoder, bool simple_text,
int null_char, GenericVector<int>* labels) {
if (str.string() == NULL || str.length() <= 0) {
tprintf("Empty truth string!\n");
return false;
}
int err_index;
GenericVector<int> internal_labels;
labels->truncate(0);
if (!simple_text) labels->push_back(null_char);
if (unicharset.encode_string(str.string(), true, &internal_labels, NULL,
&err_index)) {
bool success = true;
for (int i = 0; i < internal_labels.size(); ++i) {
if (recoder != NULL) {
// Re-encode labels via recoder.
RecodedCharID code;
int len = recoder->EncodeUnichar(internal_labels[i], &code);
if (len > 0) {
for (int j = 0; j < len; ++j) {
labels->push_back(code(j));
if (!simple_text) labels->push_back(null_char);
}
} else {
success = false;
err_index = 0;
break;
}
} else {
labels->push_back(internal_labels[i]);
if (!simple_text) labels->push_back(null_char);
}
}
if (success) return true;
}
tprintf("Encoding of string failed! Failure bytes:");
while (err_index < str.length()) {
tprintf(" %x", str[err_index++]);
}
tprintf("\n");
return false;
}
// Performs forward-backward on the given trainingdata.
// Returns a Trainability enum to indicate the suitability of the sample.
Trainability LSTMTrainer::TrainOnLine(const ImageData* trainingdata,
bool batch) {
NetworkIO fwd_outputs, targets;
Trainability trainable =
PrepareForBackward(trainingdata, &fwd_outputs, &targets);
++sample_iteration_;
if (trainable == UNENCODABLE || trainable == NOT_BOXED) {
return trainable; // Sample was unusable.
}
bool debug = debug_interval_ > 0 &&
training_iteration() % debug_interval_ == 0;
// Run backprop on the output.
NetworkIO bp_deltas;
if (network_->IsTraining() &&
(trainable != PERFECT ||
training_iteration() >
last_perfect_training_iteration_ + perfect_delay_)) {
network_->Backward(debug, targets, &scratch_space_, &bp_deltas);
network_->Update(learning_rate_, batch ? -1.0f : momentum_,
training_iteration_ + 1);
}
#ifndef GRAPHICS_DISABLED
if (debug_interval_ == 1 && debug_win_ != NULL) {
delete debug_win_->AwaitEvent(SVET_CLICK);
}
#endif // GRAPHICS_DISABLED
// Roll the memory of past means.
RollErrorBuffers();
return trainable;
}
// Prepares the ground truth, runs forward, and prepares the targets.
// Returns a Trainability enum to indicate the suitability of the sample.
Trainability LSTMTrainer::PrepareForBackward(const ImageData* trainingdata,
NetworkIO* fwd_outputs,
NetworkIO* targets) {
if (trainingdata == NULL) {
tprintf("Null trainingdata.\n");
return UNENCODABLE;
}
// Ensure repeatability of random elements even across checkpoints.
bool debug = debug_interval_ > 0 &&
training_iteration() % debug_interval_ == 0;
GenericVector<int> truth_labels;
if (!EncodeString(trainingdata->transcription(), &truth_labels)) {
tprintf("Can't encode transcription: %s\n",
trainingdata->transcription().string());
return UNENCODABLE;
}
int w = 0;
while (w < truth_labels.size() &&
(truth_labels[w] == UNICHAR_SPACE || truth_labels[w] == null_char_))
++w;
if (w == truth_labels.size()) {
tprintf("Blank transcription: %s\n",
trainingdata->transcription().string());
return UNENCODABLE;
}
float image_scale;
NetworkIO inputs;
bool invert = trainingdata->boxes().empty();
if (!RecognizeLine(*trainingdata, invert, debug, invert, 0.0f, &image_scale,
&inputs, fwd_outputs)) {
tprintf("Image not trainable\n");
return UNENCODABLE;
}
targets->Resize(*fwd_outputs, network_->NumOutputs());
LossType loss_type = OutputLossType();
if (loss_type == LT_SOFTMAX) {
if (!ComputeTextTargets(*fwd_outputs, truth_labels, targets)) {
tprintf("Compute simple targets failed!\n");
return UNENCODABLE;
}
} else if (loss_type == LT_CTC) {
if (!ComputeCTCTargets(truth_labels, fwd_outputs, targets)) {
tprintf("Compute CTC targets failed!\n");
return UNENCODABLE;
}
} else {
tprintf("Logistic outputs not implemented yet!\n");
return UNENCODABLE;
}
GenericVector<int> ocr_labels;
GenericVector<int> xcoords;
LabelsFromOutputs(*fwd_outputs, 0.0f, &ocr_labels, &xcoords);
// CTC does not produce correct target labels to begin with.
if (loss_type != LT_CTC) {
LabelsFromOutputs(*targets, 0.0f, &truth_labels, &xcoords);
}
if (!DebugLSTMTraining(inputs, *trainingdata, *fwd_outputs, truth_labels,
*targets)) {
tprintf("Input width was %d\n", inputs.Width());
return UNENCODABLE;
}
STRING ocr_text = DecodeLabels(ocr_labels);
STRING truth_text = DecodeLabels(truth_labels);
targets->SubtractAllFromFloat(*fwd_outputs);
if (debug_interval_ != 0) {
tprintf("Iteration %d: BEST OCR TEXT : %s\n", training_iteration(),
ocr_text.string());
}
double char_error = ComputeCharError(truth_labels, ocr_labels);
double word_error = ComputeWordError(&truth_text, &ocr_text);
double delta_error = ComputeErrorRates(*targets, char_error, word_error);
if (debug_interval_ != 0) {
tprintf("File %s page %d %s:\n", trainingdata->imagefilename().string(),
trainingdata->page_number(), delta_error == 0.0 ? "(Perfect)" : "");
}
if (delta_error == 0.0) return PERFECT;
if (targets->AnySuspiciousTruth(kHighConfidence)) return HI_PRECISION_ERR;
return TRAINABLE;
}
// Writes the trainer to memory, so that the current training state can be
// restored.
bool LSTMTrainer::SaveTrainingDump(SerializeAmount serialize_amount,
const LSTMTrainer* trainer,
GenericVector<char>* data) const {
TFile fp;
fp.OpenWrite(data);
trainer->serialize_amount_ = serialize_amount;
return trainer->Serialize(&fp);
}
// Reads previously saved trainer from memory.
bool LSTMTrainer::ReadTrainingDump(const GenericVector<char>& data,
LSTMTrainer* trainer) {
return trainer->ReadSizedTrainingDump(&data[0], data.size());
}
bool LSTMTrainer::ReadSizedTrainingDump(const char* data, int size) {
TFile fp;
fp.Open(data, size);
return DeSerialize(false, &fp);
}
// Writes the recognizer to memory, so that it can be used for testing later.
void LSTMTrainer::SaveRecognitionDump(GenericVector<char>* data) const {
TFile fp;
fp.OpenWrite(data);
network_->SetEnableTraining(TS_TEMP_DISABLE);
ASSERT_HOST(LSTMRecognizer::Serialize(&fp));
network_->SetEnableTraining(TS_RE_ENABLE);
}
// Reads and returns a previously saved recognizer from memory.
LSTMRecognizer* LSTMTrainer::ReadRecognitionDump(
const GenericVector<char>& data) {
TFile fp;
fp.Open(&data[0], data.size());
LSTMRecognizer* recognizer = new LSTMRecognizer;
ASSERT_HOST(recognizer->DeSerialize(false, &fp));
return recognizer;
}
// Returns a suitable filename for a training dump, based on the model_base_,
// the iteration and the error rates.
STRING LSTMTrainer::DumpFilename() const {
STRING filename;
filename.add_str_double(model_base_.string(), best_error_rate_);
filename.add_str_int("_", best_iteration_);
filename += ".lstm";
return filename;
}
// Fills the whole error buffer of the given type with the given value.
void LSTMTrainer::FillErrorBuffer(double new_error, ErrorTypes type) {
for (int i = 0; i < kRollingBufferSize_; ++i)
error_buffers_[type][i] = new_error;
error_rates_[type] = 100.0 * new_error;
}
// Factored sub-constructor sets up reasonable default values.
void LSTMTrainer::EmptyConstructor() {
align_win_ = NULL;
target_win_ = NULL;
ctc_win_ = NULL;
recon_win_ = NULL;
checkpoint_iteration_ = 0;
serialize_amount_ = FULL;
training_stage_ = 0;
num_training_stages_ = 2;
InitIterations();
}
// Sets the unicharset properties using the given script_dir as a source of
// script unicharsets. If the flag TF_COMPRESS_UNICHARSET is true, also sets
// up the recoder_ to simplify the unicharset.
void LSTMTrainer::SetUnicharsetProperties(const STRING& script_dir) {
tprintf("Setting unichar properties\n");
for (int s = 0; s < GetUnicharset().get_script_table_size(); ++s) {
if (strcmp("NULL", GetUnicharset().get_script_from_script_id(s)) == 0)
continue;
// Load the unicharset for the script if available.
STRING filename = script_dir + "/" +
GetUnicharset().get_script_from_script_id(s) +
".unicharset";
UNICHARSET script_set;
GenericVector<char> data;
if ((*file_reader_)(filename, &data) &&
script_set.load_from_inmemory_file(&data[0], data.size())) {
tprintf("Setting properties for script %s\n",
GetUnicharset().get_script_from_script_id(s));
ccutil_.unicharset.SetPropertiesFromOther(script_set);
}
}