tesseract  4.0.0-1-g2a2b
tesseract::LSTMTrainer Class Reference

#include <lstmtrainer.h>

Inheritance diagram for tesseract::LSTMTrainer:
tesseract::LSTMRecognizer

Public Member Functions

 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_t max_memory)
 
virtual ~LSTMTrainer ()
 
bool TryLoadingCheckpoint (const char *filename, const char *old_traineddata)
 
void InitCharSet (const std::string &traineddata_path)
 
void InitCharSet (const TessdataManager &mgr)
 
bool InitNetwork (const STRING &network_spec, int append_index, int net_flags, float weight_range, float learning_rate, float momentum, float adam_beta)
 
int InitTensorFlowNetwork (const std::string &tf_proto)
 
void InitIterations ()
 
double ActivationError () const
 
double CharError () const
 
const double * error_rates () const
 
double best_error_rate () const
 
int best_iteration () const
 
int learning_iteration () const
 
int32_t improvement_steps () const
 
void set_perfect_delay (int delay)
 
const GenericVector< char > & best_trainer () const
 
double NewSingleError (ErrorTypes type) const
 
double LastSingleError (ErrorTypes type) const
 
const DocumentCachetraining_data () const
 
DocumentCachemutable_training_data ()
 
Trainability 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)
 
void DebugNetwork ()
 
bool LoadAllTrainingData (const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, bool randomly_rotate)
 
bool MaintainCheckpoints (TestCallback tester, STRING *log_msg)
 
bool MaintainCheckpointsSpecific (int iteration, const GenericVector< char > *train_model, const GenericVector< char > *rec_model, TestCallback tester, STRING *log_msg)
 
void PrepareLogMsg (STRING *log_msg) const
 
void LogIterations (const char *intro_str, STRING *log_msg) const
 
bool TransitionTrainingStage (float error_threshold)
 
int CurrentTrainingStage () const
 
bool Serialize (SerializeAmount serialize_amount, const TessdataManager *mgr, TFile *fp) const
 
bool DeSerialize (const TessdataManager *mgr, TFile *fp)
 
void StartSubtrainer (STRING *log_msg)
 
SubTrainerResult UpdateSubtrainer (STRING *log_msg)
 
void ReduceLearningRates (LSTMTrainer *samples_trainer, STRING *log_msg)
 
int ReduceLayerLearningRates (double factor, int num_samples, LSTMTrainer *samples_trainer)
 
bool EncodeString (const STRING &str, GenericVector< int > *labels) const
 
const ImageDataTrainOnLine (LSTMTrainer *samples_trainer, bool batch)
 
Trainability TrainOnLine (const ImageData *trainingdata, bool batch)
 
Trainability PrepareForBackward (const ImageData *trainingdata, NetworkIO *fwd_outputs, NetworkIO *targets)
 
bool SaveTrainingDump (SerializeAmount serialize_amount, const LSTMTrainer *trainer, GenericVector< char > *data) const
 
bool ReadTrainingDump (const GenericVector< char > &data, LSTMTrainer *trainer) const
 
bool ReadSizedTrainingDump (const char *data, int size, LSTMTrainer *trainer) const
 
bool ReadLocalTrainingDump (const TessdataManager *mgr, const char *data, int size)
 
void SetupCheckpointInfo ()
 
bool SaveTraineddata (const STRING &filename)
 
void SaveRecognitionDump (GenericVector< char > *data) const
 
STRING DumpFilename () const
 
void FillErrorBuffer (double new_error, ErrorTypes type)
 
std::vector< int > MapRecoder (const UNICHARSET &old_chset, const UnicharCompress &old_recoder) const
 
- Public Member Functions inherited from tesseract::LSTMRecognizer
 LSTMRecognizer ()
 
 ~LSTMRecognizer ()
 
int NumOutputs () const
 
int training_iteration () const
 
int sample_iteration () const
 
double learning_rate () const
 
LossType OutputLossType () const
 
bool SimpleTextOutput () const
 
bool IsIntMode () const
 
bool IsRecoding () const
 
bool IsTensorFlow () const
 
GenericVector< STRINGEnumerateLayers () const
 
NetworkGetLayer (const STRING &id) const
 
float GetLayerLearningRate (const STRING &id) const
 
void ScaleLearningRate (double factor)
 
void ScaleLayerLearningRate (const STRING &id, double factor)
 
void ConvertToInt ()
 
const UNICHARSETGetUnicharset () const
 
const UnicharCompressGetRecoder () const
 
const DictGetDict () const
 
void SetIteration (int iteration)
 
int NumInputs () const
 
int null_char () const
 
bool Load (const char *lang, TessdataManager *mgr)
 
bool Serialize (const TessdataManager *mgr, TFile *fp) const
 
bool DeSerialize (const TessdataManager *mgr, TFile *fp)
 
bool LoadCharsets (const TessdataManager *mgr)
 
bool LoadRecoder (TFile *fp)
 
bool LoadDictionary (const char *lang, TessdataManager *mgr)
 
void RecognizeLine (const ImageData &image_data, bool invert, bool debug, double worst_dict_cert, const TBOX &line_box, PointerVector< WERD_RES > *words, int lstm_choice_mode=0)
 
void OutputStats (const NetworkIO &outputs, float *min_output, float *mean_output, float *sd)
 
bool RecognizeLine (const ImageData &image_data, bool invert, bool debug, bool re_invert, bool upside_down, float *scale_factor, NetworkIO *inputs, NetworkIO *outputs)
 
STRING DecodeLabels (const GenericVector< int > &labels)
 
void DisplayForward (const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window)
 
void LabelsFromOutputs (const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)
 

Static Public Member Functions

static bool EncodeString (const STRING &str, const UNICHARSET &unicharset, const UnicharCompress *recoder, bool simple_text, int null_char, GenericVector< int > *labels)
 

Protected Member Functions

void InitCharSet ()
 
void SetNullChar ()
 
void EmptyConstructor ()
 
bool DebugLSTMTraining (const NetworkIO &inputs, const ImageData &trainingdata, const NetworkIO &fwd_outputs, const GenericVector< int > &truth_labels, const NetworkIO &outputs)
 
void DisplayTargets (const NetworkIO &targets, const char *window_name, ScrollView **window)
 
bool ComputeTextTargets (const NetworkIO &outputs, const GenericVector< int > &truth_labels, NetworkIO *targets)
 
bool ComputeCTCTargets (const GenericVector< int > &truth_labels, NetworkIO *outputs, NetworkIO *targets)
 
double ComputeErrorRates (const NetworkIO &deltas, double char_error, double word_error)
 
double ComputeRMSError (const NetworkIO &deltas)
 
double ComputeWinnerError (const NetworkIO &deltas)
 
double ComputeCharError (const GenericVector< int > &truth_str, const GenericVector< int > &ocr_str)
 
double ComputeWordError (STRING *truth_str, STRING *ocr_str)
 
void UpdateErrorBuffer (double new_error, ErrorTypes type)
 
void RollErrorBuffers ()
 
STRING UpdateErrorGraph (int iteration, double error_rate, const GenericVector< char > &model_data, TestCallback tester)
 
- Protected Member Functions inherited from tesseract::LSTMRecognizer
void SetRandomSeed ()
 
void DisplayLSTMOutput (const GenericVector< int > &labels, const GenericVector< int > &xcoords, int height, ScrollView *window)
 
void DebugActivationPath (const NetworkIO &outputs, const GenericVector< int > &labels, const GenericVector< int > &xcoords)
 
void DebugActivationRange (const NetworkIO &outputs, const char *label, int best_choice, int x_start, int x_end)
 
void LabelsViaReEncode (const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
 
void LabelsViaSimpleText (const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
 
const char * DecodeLabel (const GenericVector< int > &labels, int start, int *end, int *decoded)
 
const char * DecodeSingleLabel (int label)
 

Protected Attributes

ScrollViewalign_win_
 
ScrollViewtarget_win_
 
ScrollViewctc_win_
 
ScrollViewrecon_win_
 
int debug_interval_
 
int checkpoint_iteration_
 
STRING model_base_
 
STRING checkpoint_name_
 
bool randomly_rotate_
 
DocumentCache training_data_
 
STRING best_model_name_
 
int num_training_stages_
 
FileReader file_reader_
 
FileWriter file_writer_
 
CheckPointReader checkpoint_reader_
 
CheckPointWriter checkpoint_writer_
 
double best_error_rate_
 
double best_error_rates_ [ET_COUNT]
 
int best_iteration_
 
double worst_error_rate_
 
double worst_error_rates_ [ET_COUNT]
 
int worst_iteration_
 
int stall_iteration_
 
GenericVector< char > best_model_data_
 
GenericVector< char > worst_model_data_
 
GenericVector< char > best_trainer_
 
LSTMTrainersub_trainer_
 
float error_rate_of_last_saved_best_
 
int training_stage_
 
GenericVector< double > best_error_history_
 
GenericVector< int > best_error_iterations_
 
int32_t improvement_steps_
 
int learning_iteration_
 
int prev_sample_iteration_
 
int perfect_delay_
 
int last_perfect_training_iteration_
 
GenericVector< double > error_buffers_ [ET_COUNT]
 
double error_rates_ [ET_COUNT]
 
TessdataManager mgr_
 
- Protected Attributes inherited from tesseract::LSTMRecognizer
Networknetwork_
 
CCUtil ccutil_
 
UnicharCompress recoder_
 
STRING network_str_
 
int32_t training_flags_
 
int32_t training_iteration_
 
int32_t sample_iteration_
 
int32_t null_char_
 
float learning_rate_
 
float momentum_
 
float adam_beta_
 
TRand randomizer_
 
NetworkScratch scratch_space_
 
Dictdict_
 
RecodeBeamSearchsearch_
 
ScrollViewdebug_win_
 

Static Protected Attributes

static const int kRollingBufferSize_ = 1000
 

Detailed Description

Definition at line 89 of file lstmtrainer.h.

Constructor & Destructor Documentation

◆ LSTMTrainer() [1/2]

tesseract::LSTMTrainer::LSTMTrainer ( )

Definition at line 73 of file lstmtrainer.cpp.

74  : randomly_rotate_(false),
75  training_data_(0),
82  sub_trainer_(nullptr) {
84  debug_interval_ = 0;
85 }
DocumentCache training_data_
Definition: lstmtrainer.h:414
bool SaveTrainingDump(SerializeAmount serialize_amount, const LSTMTrainer *trainer, GenericVector< char > *data) const
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
LSTMTrainer * sub_trainer_
Definition: lstmtrainer.h:450
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
bool ReadTrainingDump(const GenericVector< char > &data, LSTMTrainer *trainer) const
Definition: lstmtrainer.h:291
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
CheckPointReader checkpoint_reader_
Definition: lstmtrainer.h:424
CheckPointWriter checkpoint_writer_
Definition: lstmtrainer.h:425

◆ LSTMTrainer() [2/2]

tesseract::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_t  max_memory 
)

Definition at line 87 of file lstmtrainer.cpp.

92  : randomly_rotate_(false),
93  training_data_(max_memory),
94  file_reader_(file_reader),
95  file_writer_(file_writer),
96  checkpoint_reader_(checkpoint_reader),
97  checkpoint_writer_(checkpoint_writer),
98  sub_trainer_(nullptr),
99  mgr_(file_reader) {
101  if (file_reader_ == nullptr) file_reader_ = LoadDataFromFile;
102  if (file_writer_ == nullptr) file_writer_ = SaveDataToFile;
103  if (checkpoint_reader_ == nullptr) {
106  }
107  if (checkpoint_writer_ == nullptr) {
110  }
111  debug_interval_ = debug_interval;
112  model_base_ = model_base;
113  checkpoint_name_ = checkpoint_name;
114 }
DocumentCache training_data_
Definition: lstmtrainer.h:414
bool SaveTrainingDump(SerializeAmount serialize_amount, const LSTMTrainer *trainer, GenericVector< char > *data) const
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
LSTMTrainer * sub_trainer_
Definition: lstmtrainer.h:450
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
TessdataManager mgr_
Definition: lstmtrainer.h:483
bool ReadTrainingDump(const GenericVector< char > &data, LSTMTrainer *trainer) const
Definition: lstmtrainer.h:291
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
CheckPointReader checkpoint_reader_
Definition: lstmtrainer.h:424
CheckPointWriter checkpoint_writer_
Definition: lstmtrainer.h:425

◆ ~LSTMTrainer()

tesseract::LSTMTrainer::~LSTMTrainer ( )
virtual

Definition at line 116 of file lstmtrainer.cpp.

116  {
117  delete align_win_;
118  delete target_win_;
119  delete ctc_win_;
120  delete recon_win_;
121  delete checkpoint_reader_;
122  delete checkpoint_writer_;
123  delete sub_trainer_;
124 }
LSTMTrainer * sub_trainer_
Definition: lstmtrainer.h:450
ScrollView * align_win_
Definition: lstmtrainer.h:397
ScrollView * ctc_win_
Definition: lstmtrainer.h:401
ScrollView * recon_win_
Definition: lstmtrainer.h:403
ScrollView * target_win_
Definition: lstmtrainer.h:399
CheckPointReader checkpoint_reader_
Definition: lstmtrainer.h:424
CheckPointWriter checkpoint_writer_
Definition: lstmtrainer.h:425

Member Function Documentation

◆ ActivationError()

double tesseract::LSTMTrainer::ActivationError ( ) const
inline

Definition at line 136 of file lstmtrainer.h.

136  {
137  return error_rates_[ET_DELTA];
138  }
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ best_error_rate()

double tesseract::LSTMTrainer::best_error_rate ( ) const
inline

Definition at line 143 of file lstmtrainer.h.

143  {
144  return best_error_rate_;
145  }

◆ best_iteration()

int tesseract::LSTMTrainer::best_iteration ( ) const
inline

Definition at line 146 of file lstmtrainer.h.

146  {
147  return best_iteration_;
148  }

◆ best_trainer()

const GenericVector<char>& tesseract::LSTMTrainer::best_trainer ( ) const
inline

Definition at line 152 of file lstmtrainer.h.

152 { return best_trainer_; }
GenericVector< char > best_trainer_
Definition: lstmtrainer.h:447

◆ CharError()

double tesseract::LSTMTrainer::CharError ( ) const
inline

Definition at line 139 of file lstmtrainer.h.

◆ ComputeCharError()

double tesseract::LSTMTrainer::ComputeCharError ( const GenericVector< int > &  truth_str,
const GenericVector< int > &  ocr_str 
)
protected

Definition at line 1187 of file lstmtrainer.cpp.

1188  {
1189  GenericVector<int> label_counts;
1190  label_counts.init_to_size(NumOutputs(), 0);
1191  int truth_size = 0;
1192  for (int i = 0; i < truth_str.size(); ++i) {
1193  if (truth_str[i] != null_char_) {
1194  ++label_counts[truth_str[i]];
1195  ++truth_size;
1196  }
1197  }
1198  for (int i = 0; i < ocr_str.size(); ++i) {
1199  if (ocr_str[i] != null_char_) {
1200  --label_counts[ocr_str[i]];
1201  }
1202  }
1203  int char_errors = 0;
1204  for (int i = 0; i < label_counts.size(); ++i) {
1205  char_errors += abs(label_counts[i]);
1206  }
1207  if (truth_size == 0) {
1208  return (char_errors == 0) ? 0.0 : 1.0;
1209  }
1210  return static_cast<double>(char_errors) / truth_size;
1211 }
int size() const
Definition: genericvector.h:71
void init_to_size(int size, const T &t)

◆ ComputeCTCTargets()

bool tesseract::LSTMTrainer::ComputeCTCTargets ( const GenericVector< int > &  truth_labels,
NetworkIO outputs,
NetworkIO targets 
)
protected

Definition at line 1119 of file lstmtrainer.cpp.

1120  {
1121  // Bottom-clip outputs to a minimum probability.
1122  CTC::NormalizeProbs(outputs);
1123  return CTC::ComputeCTCTargets(truth_labels, null_char_,
1124  outputs->float_array(), targets);
1125 }
static void NormalizeProbs(NetworkIO *probs)
Definition: ctc.h:36
static bool ComputeCTCTargets(const GenericVector< int > &truth_labels, int null_char, const GENERIC_2D_ARRAY< float > &outputs, NetworkIO *targets)
Definition: ctc.cpp:55

◆ ComputeErrorRates()

double tesseract::LSTMTrainer::ComputeErrorRates ( const NetworkIO deltas,
double  char_error,
double  word_error 
)
protected

Definition at line 1130 of file lstmtrainer.cpp.

1131  {
1133  // Delta error is the fraction of timesteps with >0.5 error in the top choice
1134  // score. If zero, then the top choice characters are guaranteed correct,
1135  // even when there is residue in the RMS error.
1136  double delta_error = ComputeWinnerError(deltas);
1137  UpdateErrorBuffer(delta_error, ET_DELTA);
1138  UpdateErrorBuffer(word_error, ET_WORD_RECERR);
1139  UpdateErrorBuffer(char_error, ET_CHAR_ERROR);
1140  // Skip ratio measures the difference between sample_iteration_ and
1141  // training_iteration_, which reflects the number of unusable samples,
1142  // usually due to unencodable truth text, or the text not fitting in the
1143  // space for the output.
1144  double skip_count = sample_iteration_ - prev_sample_iteration_;
1145  UpdateErrorBuffer(skip_count, ET_SKIP_RATIO);
1146  return delta_error;
1147 }
void UpdateErrorBuffer(double new_error, ErrorTypes type)
double ComputeWinnerError(const NetworkIO &deltas)
double ComputeRMSError(const NetworkIO &deltas)

◆ ComputeRMSError()

double tesseract::LSTMTrainer::ComputeRMSError ( const NetworkIO deltas)
protected

Definition at line 1150 of file lstmtrainer.cpp.

1150  {
1151  double total_error = 0.0;
1152  int width = deltas.Width();
1153  int num_classes = deltas.NumFeatures();
1154  for (int t = 0; t < width; ++t) {
1155  const float* class_errs = deltas.f(t);
1156  for (int c = 0; c < num_classes; ++c) {
1157  double error = class_errs[c];
1158  total_error += error * error;
1159  }
1160  }
1161  return sqrt(total_error / (width * num_classes));
1162 }

◆ ComputeTextTargets()

bool tesseract::LSTMTrainer::ComputeTextTargets ( const NetworkIO outputs,
const GenericVector< int > &  truth_labels,
NetworkIO targets 
)
protected

Definition at line 1099 of file lstmtrainer.cpp.

1101  {
1102  if (truth_labels.size() > targets->Width()) {
1103  tprintf("Error: transcription %s too long to fit into target of width %d\n",
1104  DecodeLabels(truth_labels).string(), targets->Width());
1105  return false;
1106  }
1107  for (int i = 0; i < truth_labels.size() && i < targets->Width(); ++i) {
1108  targets->SetActivations(i, truth_labels[i], 1.0);
1109  }
1110  for (int i = truth_labels.size(); i < targets->Width(); ++i) {
1111  targets->SetActivations(i, null_char_, 1.0);
1112  }
1113  return true;
1114 }
int size() const
Definition: genericvector.h:71
STRING DecodeLabels(const GenericVector< int > &labels)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ ComputeWinnerError()

double tesseract::LSTMTrainer::ComputeWinnerError ( const NetworkIO deltas)
protected

Definition at line 1169 of file lstmtrainer.cpp.

1169  {
1170  int num_errors = 0;
1171  int width = deltas.Width();
1172  int num_classes = deltas.NumFeatures();
1173  for (int t = 0; t < width; ++t) {
1174  const float* class_errs = deltas.f(t);
1175  for (int c = 0; c < num_classes; ++c) {
1176  float abs_delta = fabs(class_errs[c]);
1177  // TODO(rays) Filtering cases where the delta is very large to cut out
1178  // GT errors doesn't work. Find a better way or get better truth.
1179  if (0.5 <= abs_delta)
1180  ++num_errors;
1181  }
1182  }
1183  return static_cast<double>(num_errors) / width;
1184 }

◆ ComputeWordError()

double tesseract::LSTMTrainer::ComputeWordError ( STRING truth_str,
STRING ocr_str 
)
protected

Definition at line 1215 of file lstmtrainer.cpp.

1215  {
1216  using StrMap = std::unordered_map<std::string, int, std::hash<std::string>>;
1217  GenericVector<STRING> truth_words, ocr_words;
1218  truth_str->split(' ', &truth_words);
1219  if (truth_words.empty()) return 0.0;
1220  ocr_str->split(' ', &ocr_words);
1221  StrMap word_counts;
1222  for (int i = 0; i < truth_words.size(); ++i) {
1223  std::string truth_word(truth_words[i].string());
1224  StrMap::iterator it = word_counts.find(truth_word);
1225  if (it == word_counts.end())
1226  word_counts.insert(std::make_pair(truth_word, 1));
1227  else
1228  ++it->second;
1229  }
1230  for (int i = 0; i < ocr_words.size(); ++i) {
1231  std::string ocr_word(ocr_words[i].string());
1232  StrMap::iterator it = word_counts.find(ocr_word);
1233  if (it == word_counts.end())
1234  word_counts.insert(std::make_pair(ocr_word, -1));
1235  else
1236  --it->second;
1237  }
1238  int word_recall_errs = 0;
1239  for (StrMap::const_iterator it = word_counts.begin(); it != word_counts.end();
1240  ++it) {
1241  if (it->second > 0) word_recall_errs += it->second;
1242  }
1243  return static_cast<double>(word_recall_errs) / truth_words.size();
1244 }
int size() const
Definition: genericvector.h:71
void split(const char c, GenericVector< STRING > *splited)
Definition: strngs.cpp:284
bool empty() const
Definition: genericvector.h:90

◆ CurrentTrainingStage()

int tesseract::LSTMTrainer::CurrentTrainingStage ( ) const
inline

Definition at line 211 of file lstmtrainer.h.

211 { return training_stage_; }

◆ DebugLSTMTraining()

bool tesseract::LSTMTrainer::DebugLSTMTraining ( const NetworkIO inputs,
const ImageData trainingdata,
const NetworkIO fwd_outputs,
const GenericVector< int > &  truth_labels,
const NetworkIO outputs 
)
protected

Definition at line 1029 of file lstmtrainer.cpp.

1033  {
1034  const STRING& truth_text = DecodeLabels(truth_labels);
1035  if (truth_text.string() == nullptr || truth_text.length() <= 0) {
1036  tprintf("Empty truth string at decode time!\n");
1037  return false;
1038  }
1039  if (debug_interval_ != 0) {
1040  // Get class labels, xcoords and string.
1041  GenericVector<int> labels;
1042  GenericVector<int> xcoords;
1043  LabelsFromOutputs(outputs, &labels, &xcoords);
1044  STRING text = DecodeLabels(labels);
1045  tprintf("Iteration %d: ALIGNED TRUTH : %s\n",
1046  training_iteration(), text.string());
1047  if (debug_interval_ > 0 && training_iteration() % debug_interval_ == 0) {
1048  tprintf("TRAINING activation path for truth string %s\n",
1049  truth_text.string());
1050  DebugActivationPath(outputs, labels, xcoords);
1051  DisplayForward(inputs, labels, xcoords, "LSTMTraining", &align_win_);
1052  if (OutputLossType() == LT_CTC) {
1053  DisplayTargets(fwd_outputs, "CTC Outputs", &ctc_win_);
1054  DisplayTargets(outputs, "CTC Targets", &target_win_);
1055  }
1056  }
1057  }
1058  return true;
1059 }
const char * string() const
Definition: strngs.cpp:196
STRING DecodeLabels(const GenericVector< int > &labels)
ScrollView * align_win_
Definition: lstmtrainer.h:397
void DebugActivationPath(const NetworkIO &outputs, const GenericVector< int > &labels, const GenericVector< int > &xcoords)
ScrollView * ctc_win_
Definition: lstmtrainer.h:401
void DisplayForward(const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
LossType OutputLossType() const
Definition: strngs.h:45
ScrollView * target_win_
Definition: lstmtrainer.h:399
int32_t length() const
Definition: strngs.cpp:191
void DisplayTargets(const NetworkIO &targets, const char *window_name, ScrollView **window)
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)

◆ DebugNetwork()

void tesseract::LSTMTrainer::DebugNetwork ( )

Definition at line 293 of file lstmtrainer.cpp.

293  {
295 }
virtual void DebugWeights()
Definition: network.h:218

◆ DeSerialize()

bool tesseract::LSTMTrainer::DeSerialize ( const TessdataManager mgr,
TFile fp 
)

Definition at line 468 of file lstmtrainer.cpp.

468  {
469  if (!LSTMRecognizer::DeSerialize(mgr, fp)) return false;
470  if (!fp->DeSerialize(&learning_iteration_)) {
471  // Special case. If we successfully decoded the recognizer, but fail here
472  // then it means we were just given a recognizer, so issue a warning and
473  // allow it.
474  tprintf("Warning: LSTMTrainer deserialized an LSTMRecognizer!\n");
477  return true;
478  }
479  if (!fp->DeSerialize(&prev_sample_iteration_)) return false;
480  if (!fp->DeSerialize(&perfect_delay_)) return false;
481  if (!fp->DeSerialize(&last_perfect_training_iteration_)) return false;
482  for (int i = 0; i < ET_COUNT; ++i) {
483  if (!error_buffers_[i].DeSerialize(fp)) return false;
484  }
485  if (!fp->DeSerialize(&error_rates_[0], countof(error_rates_))) return false;
486  if (!fp->DeSerialize(&training_stage_)) return false;
487  uint8_t amount;
488  if (!fp->DeSerialize(&amount)) return false;
489  if (amount == LIGHT) return true; // Don't read the rest.
490  if (!fp->DeSerialize(&best_error_rate_)) return false;
491  if (!fp->DeSerialize(&best_error_rates_[0], countof(best_error_rates_))) return false;
492  if (!fp->DeSerialize(&best_iteration_)) return false;
493  if (!fp->DeSerialize(&worst_error_rate_)) return false;
494  if (!fp->DeSerialize(&worst_error_rates_[0], countof(worst_error_rates_))) return false;
495  if (!fp->DeSerialize(&worst_iteration_)) return false;
496  if (!fp->DeSerialize(&stall_iteration_)) return false;
497  if (!best_model_data_.DeSerialize(fp)) return false;
498  if (!worst_model_data_.DeSerialize(fp)) return false;
499  if (amount != NO_BEST_TRAINER && !best_trainer_.DeSerialize(fp)) return false;
500  GenericVector<char> sub_data;
501  if (!sub_data.DeSerialize(fp)) return false;
502  delete sub_trainer_;
503  if (sub_data.empty()) {
504  sub_trainer_ = nullptr;
505  } else {
506  sub_trainer_ = new LSTMTrainer();
507  if (!ReadTrainingDump(sub_data, sub_trainer_)) return false;
508  }
509  if (!best_error_history_.DeSerialize(fp)) return false;
510  if (!best_error_iterations_.DeSerialize(fp)) return false;
511  return fp->DeSerialize(&improvement_steps_);
512 }
double worst_error_rates_[ET_COUNT]
Definition: lstmtrainer.h:438
LSTMTrainer * sub_trainer_
Definition: lstmtrainer.h:450
bool DeSerialize(bool swap, FILE *fp)
double best_error_rates_[ET_COUNT]
Definition: lstmtrainer.h:432
constexpr size_t countof(T const (&)[N]) noexcept
Definition: serialis.h:43
GenericVector< char > best_trainer_
Definition: lstmtrainer.h:447
virtual void SetEnableTraining(TrainingState state)
Definition: network.cpp:110
GenericVector< double > best_error_history_
Definition: lstmtrainer.h:457
bool ReadTrainingDump(const GenericVector< char > &data, LSTMTrainer *trainer) const
Definition: lstmtrainer.h:291
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
GenericVector< int > best_error_iterations_
Definition: lstmtrainer.h:458
bool empty() const
Definition: genericvector.h:90
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
GenericVector< char > best_model_data_
Definition: lstmtrainer.h:444
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
GenericVector< char > worst_model_data_
Definition: lstmtrainer.h:445
GenericVector< double > error_buffers_[ET_COUNT]
Definition: lstmtrainer.h:479
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ DisplayTargets()

void tesseract::LSTMTrainer::DisplayTargets ( const NetworkIO targets,
const char *  window_name,
ScrollView **  window 
)
protected

Definition at line 1062 of file lstmtrainer.cpp.

1063  {
1064 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics.
1065  int width = targets.Width();
1066  int num_features = targets.NumFeatures();
1067  Network::ClearWindow(true, window_name, width * kTargetXScale, kTargetYScale,
1068  window);
1069  for (int c = 0; c < num_features; ++c) {
1070  int color = c % (ScrollView::GREEN_YELLOW - 1) + 2;
1071  (*window)->Pen(static_cast<ScrollView::Color>(color));
1072  int start_t = -1;
1073  for (int t = 0; t < width; ++t) {
1074  double target = targets.f(t)[c];
1075  target *= kTargetYScale;
1076  if (target >= 1) {
1077  if (start_t < 0) {
1078  (*window)->SetCursor(t - 1, 0);
1079  start_t = t;
1080  }
1081  (*window)->DrawTo(t, target);
1082  } else if (start_t >= 0) {
1083  (*window)->DrawTo(t, 0);
1084  (*window)->DrawTo(start_t - 1, 0);
1085  start_t = -1;
1086  }
1087  }
1088  if (start_t >= 0) {
1089  (*window)->DrawTo(width, 0);
1090  (*window)->DrawTo(start_t - 1, 0);
1091  }
1092  }
1093  (*window)->Update();
1094 #endif // GRAPHICS_DISABLED
1095 }
const int kTargetXScale
Definition: lstmtrainer.cpp:70
const int kTargetYScale
Definition: lstmtrainer.cpp:71
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
Definition: network.cpp:306

◆ DumpFilename()

STRING tesseract::LSTMTrainer::DumpFilename ( ) const

Definition at line 940 of file lstmtrainer.cpp.

940  {
941  STRING filename;
943  filename.add_str_int("_", best_iteration_);
944  filename += ".checkpoint";
945  return filename;
946 }
const char * string() const
Definition: strngs.cpp:196
void add_str_double(const char *str, double number)
Definition: strngs.cpp:389
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
Definition: strngs.h:45

◆ EmptyConstructor()

void tesseract::LSTMTrainer::EmptyConstructor ( )
protected

Definition at line 1014 of file lstmtrainer.cpp.

1014  {
1015  align_win_ = nullptr;
1016  target_win_ = nullptr;
1017  ctc_win_ = nullptr;
1018  recon_win_ = nullptr;
1020  training_stage_ = 0;
1022  InitIterations();
1023 }
ScrollView * align_win_
Definition: lstmtrainer.h:397
ScrollView * ctc_win_
Definition: lstmtrainer.h:401
ScrollView * recon_win_
Definition: lstmtrainer.h:403
ScrollView * target_win_
Definition: lstmtrainer.h:399

◆ EncodeString() [1/2]

bool tesseract::LSTMTrainer::EncodeString ( const STRING str,
GenericVector< int > *  labels 
) const
inline

Definition at line 246 of file lstmtrainer.h.

246  {
247  return EncodeString(str, GetUnicharset(), IsRecoding() ? &recoder_ : nullptr,
248  SimpleTextOutput(), null_char_, labels);
249  }
const UNICHARSET & GetUnicharset() const
bool EncodeString(const STRING &str, GenericVector< int > *labels) const
Definition: lstmtrainer.h:246

◆ EncodeString() [2/2]

bool tesseract::LSTMTrainer::EncodeString ( const STRING str,
const UNICHARSET unicharset,
const UnicharCompress recoder,
bool  simple_text,
int  null_char,
GenericVector< int > *  labels 
)
static

Definition at line 718 of file lstmtrainer.cpp.

720  {
721  if (str.string() == nullptr || str.length() <= 0) {
722  tprintf("Empty truth string!\n");
723  return false;
724  }
725  int err_index;
726  GenericVector<int> internal_labels;
727  labels->truncate(0);
728  if (!simple_text) labels->push_back(null_char);
729  std::string cleaned = unicharset.CleanupString(str.string());
730  if (unicharset.encode_string(cleaned.c_str(), true, &internal_labels, nullptr,
731  &err_index)) {
732  bool success = true;
733  for (int i = 0; i < internal_labels.size(); ++i) {
734  if (recoder != nullptr) {
735  // Re-encode labels via recoder.
736  RecodedCharID code;
737  int len = recoder->EncodeUnichar(internal_labels[i], &code);
738  if (len > 0) {
739  for (int j = 0; j < len; ++j) {
740  labels->push_back(code(j));
741  if (!simple_text) labels->push_back(null_char);
742  }
743  } else {
744  success = false;
745  err_index = 0;
746  break;
747  }
748  } else {
749  labels->push_back(internal_labels[i]);
750  if (!simple_text) labels->push_back(null_char);
751  }
752  }
753  if (success) return true;
754  }
755  tprintf("Encoding of string failed! Failure bytes:");
756  while (err_index < cleaned.size()) {
757  tprintf(" %x", cleaned[err_index++]);
758  }
759  tprintf("\n");
760  return false;
761 }
int size() const
Definition: genericvector.h:71
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
Definition: unicharset.cpp:258
const char * string() const
Definition: strngs.cpp:196
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
static std::string CleanupString(const char *utf8_str)
Definition: unicharset.h:241
int push_back(T object)
void truncate(int size)
int32_t length() const
Definition: strngs.cpp:191

◆ error_rates()

const double* tesseract::LSTMTrainer::error_rates ( ) const
inline

Definition at line 140 of file lstmtrainer.h.

140  {
141  return error_rates_;
142  }
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ FillErrorBuffer()

void tesseract::LSTMTrainer::FillErrorBuffer ( double  new_error,
ErrorTypes  type 
)

Definition at line 949 of file lstmtrainer.cpp.

949  {
950  for (int i = 0; i < kRollingBufferSize_; ++i)
951  error_buffers_[type][i] = new_error;
952  error_rates_[type] = 100.0 * new_error;
953 }
static const int kRollingBufferSize_
Definition: lstmtrainer.h:478
GenericVector< double > error_buffers_[ET_COUNT]
Definition: lstmtrainer.h:479
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ GridSearchDictParams()

Trainability tesseract::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 
)

Definition at line 243 of file lstmtrainer.cpp.

246  {
247  sample_iteration_ = iteration;
248  NetworkIO fwd_outputs, targets;
249  Trainability result =
250  PrepareForBackward(trainingdata, &fwd_outputs, &targets);
251  if (result == UNENCODABLE || result == HI_PRECISION_ERR || dict_ == nullptr)
252  return result;
253 
254  // Encode/decode the truth to get the normalization.
255  GenericVector<int> truth_labels, ocr_labels, xcoords;
256  ASSERT_HOST(EncodeString(trainingdata->transcription(), &truth_labels));
257  // NO-dict error.
258  RecodeBeamSearch base_search(recoder_, null_char_, SimpleTextOutput(), nullptr);
259  base_search.Decode(fwd_outputs, 1.0, 0.0, RecodeBeamSearch::kMinCertainty,
260  nullptr);
261  base_search.ExtractBestPathAsLabels(&ocr_labels, &xcoords);
262  STRING truth_text = DecodeLabels(truth_labels);
263  STRING ocr_text = DecodeLabels(ocr_labels);
264  double baseline_error = ComputeWordError(&truth_text, &ocr_text);
265  results->add_str_double("0,0=", baseline_error);
266 
267  RecodeBeamSearch search(recoder_, null_char_, SimpleTextOutput(), dict_);
268  for (double r = min_dict_ratio; r < max_dict_ratio; r += dict_ratio_step) {
269  for (double c = min_cert_offset; c < max_cert_offset;
270  c += cert_offset_step) {
271  search.Decode(fwd_outputs, r, c, RecodeBeamSearch::kMinCertainty, nullptr);
272  search.ExtractBestPathAsLabels(&ocr_labels, &xcoords);
273  truth_text = DecodeLabels(truth_labels);
274  ocr_text = DecodeLabels(ocr_labels);
275  // This is destructive on both strings.
276  double word_error = ComputeWordError(&truth_text, &ocr_text);
277  if ((r == min_dict_ratio && c == min_cert_offset) ||
278  !std::isfinite(word_error)) {
279  STRING t = DecodeLabels(truth_labels);
280  STRING o = DecodeLabels(ocr_labels);
281  tprintf("r=%g, c=%g, truth=%s, ocr=%s, wderr=%g, truth[0]=%d\n", r, c,
282  t.string(), o.string(), word_error, truth_labels[0]);
283  }
284  results->add_str_double(" ", r);
285  results->add_str_double(",", c);
286  results->add_str_double("=", word_error);
287  }
288  }
289  return result;
290 }
const char * string() const
Definition: strngs.cpp:196
STRING DecodeLabels(const GenericVector< int > &labels)
double ComputeWordError(STRING *truth_str, STRING *ocr_str)
Trainability PrepareForBackward(const ImageData *trainingdata, NetworkIO *fwd_outputs, NetworkIO *targets)
void add_str_double(const char *str, double number)
Definition: strngs.cpp:389
LIST search(LIST list, void *key, int_compare is_equal)
Definition: oldlist.cpp:366
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
bool EncodeString(const STRING &str, GenericVector< int > *labels) const
Definition: lstmtrainer.h:246
static const float kMinCertainty
Definition: recodebeam.h:222
Definition: strngs.h:45
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ improvement_steps()

int32_t tesseract::LSTMTrainer::improvement_steps ( ) const
inline

Definition at line 150 of file lstmtrainer.h.

150 { return improvement_steps_; }

◆ InitCharSet() [1/3]

void tesseract::LSTMTrainer::InitCharSet ( const std::string &  traineddata_path)
inline

Definition at line 109 of file lstmtrainer.h.

109  {
110  ASSERT_HOST(mgr_.Init(traineddata_path.c_str()));
111  InitCharSet();
112  }
TessdataManager mgr_
Definition: lstmtrainer.h:483
bool Init(const char *data_file_name)
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ InitCharSet() [2/3]

void tesseract::LSTMTrainer::InitCharSet ( const TessdataManager mgr)
inline

Definition at line 113 of file lstmtrainer.h.

113  {
114  mgr_ = mgr;
115  InitCharSet();
116  }
TessdataManager mgr_
Definition: lstmtrainer.h:483

◆ InitCharSet() [3/3]

void tesseract::LSTMTrainer::InitCharSet ( )
protected

Definition at line 992 of file lstmtrainer.cpp.

992  {
995  // Initialize the unicharset and recoder.
996  if (!LoadCharsets(&mgr_)) {
997  ASSERT_HOST(
998  "Must provide a traineddata containing lstm_unicharset and"
999  " lstm_recoder!\n" != nullptr);
1000  }
1001  SetNullChar();
1002 }
bool LoadCharsets(const TessdataManager *mgr)
TessdataManager mgr_
Definition: lstmtrainer.h:483
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ InitIterations()

void tesseract::LSTMTrainer::InitIterations ( )

Definition at line 218 of file lstmtrainer.cpp.

218  {
219  sample_iteration_ = 0;
223  best_error_rate_ = 100.0;
224  best_iteration_ = 0;
225  worst_error_rate_ = 0.0;
226  worst_iteration_ = 0;
229  perfect_delay_ = 0;
231  for (int i = 0; i < ET_COUNT; ++i) {
232  best_error_rates_[i] = 100.0;
233  worst_error_rates_[i] = 0.0;
235  error_rates_[i] = 100.0;
236  }
238 }
double worst_error_rates_[ET_COUNT]
Definition: lstmtrainer.h:438
const int kMinStartedErrorRate
Definition: lstmtrainer.cpp:60
static const int kRollingBufferSize_
Definition: lstmtrainer.h:478
double best_error_rates_[ET_COUNT]
Definition: lstmtrainer.h:432
void init_to_size(int size, const T &t)
const int kMinStallIterations
Definition: lstmtrainer.cpp:47
float error_rate_of_last_saved_best_
Definition: lstmtrainer.h:452
GenericVector< double > error_buffers_[ET_COUNT]
Definition: lstmtrainer.h:479
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ InitNetwork()

bool tesseract::LSTMTrainer::InitNetwork ( const STRING network_spec,
int  append_index,
int  net_flags,
float  weight_range,
float  learning_rate,
float  momentum,
float  adam_beta 
)

Definition at line 171 of file lstmtrainer.cpp.

174  {
175  mgr_.SetVersionString(mgr_.VersionString() + ":" + network_spec.string());
176  adam_beta_ = adam_beta;
178  momentum_ = momentum;
179  SetNullChar();
180  if (!NetworkBuilder::InitNetwork(recoder_.code_range(), network_spec,
181  append_index, net_flags, weight_range,
182  &randomizer_, &network_)) {
183  return false;
184  }
185  network_str_ += network_spec;
186  tprintf("Built network:%s from request %s\n",
187  network_->spec().string(), network_spec.string());
188  tprintf(
189  "Training parameters:\n Debug interval = %d,"
190  " weights = %g, learning rate = %g, momentum=%g\n",
191  debug_interval_, weight_range, learning_rate_, momentum_);
192  tprintf("null char=%d\n", null_char_);
193  return true;
194 }
void SetVersionString(const std::string &v_str)
const char * string() const
Definition: strngs.cpp:196
virtual STRING spec() const
Definition: network.h:141
TessdataManager mgr_
Definition: lstmtrainer.h:483
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
double learning_rate() const
static bool InitNetwork(int num_outputs, STRING network_spec, int append_index, int net_flags, float weight_range, TRand *randomizer, Network **network)
std::string VersionString() const

◆ InitTensorFlowNetwork()

int tesseract::LSTMTrainer::InitTensorFlowNetwork ( const std::string &  tf_proto)

Definition at line 198 of file lstmtrainer.cpp.

198  {
199 #ifdef INCLUDE_TENSORFLOW
200  delete network_;
201  TFNetwork* tf_net = new TFNetwork("TensorFlow");
202  training_iteration_ = tf_net->InitFromProtoStr(tf_proto);
203  if (training_iteration_ == 0) {
204  tprintf("InitFromProtoStr failed!!\n");
205  return 0;
206  }
207  network_ = tf_net;
208  ASSERT_HOST(recoder_.code_range() == tf_net->num_classes());
209  return training_iteration_;
210 #else
211  tprintf("TensorFlow not compiled in! -DINCLUDE_TENSORFLOW\n");
212  return 0;
213 #endif
214 }
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ LastSingleError()

double tesseract::LSTMTrainer::LastSingleError ( ErrorTypes  type) const
inline

Definition at line 160 of file lstmtrainer.h.

160  {
161  return error_buffers_[type]
164  }
static const int kRollingBufferSize_
Definition: lstmtrainer.h:478
GenericVector< double > error_buffers_[ET_COUNT]
Definition: lstmtrainer.h:479

◆ learning_iteration()

int tesseract::LSTMTrainer::learning_iteration ( ) const
inline

Definition at line 149 of file lstmtrainer.h.

149 { return learning_iteration_; }

◆ LoadAllTrainingData()

bool tesseract::LSTMTrainer::LoadAllTrainingData ( const GenericVector< STRING > &  filenames,
CachingStrategy  cache_strategy,
bool  randomly_rotate 
)

Definition at line 300 of file lstmtrainer.cpp.

302  {
303  randomly_rotate_ = randomly_rotate;
305  return training_data_.LoadDocuments(filenames, cache_strategy, file_reader_);
306 }
DocumentCache training_data_
Definition: lstmtrainer.h:414
bool LoadDocuments(const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, FileReader reader)
Definition: imagedata.cpp:572

◆ LogIterations()

void tesseract::LSTMTrainer::LogIterations ( const char *  intro_str,
STRING log_msg 
) const

Definition at line 412 of file lstmtrainer.cpp.

412  {
413  *log_msg += intro_str;
414  log_msg->add_str_int(" iteration ", learning_iteration());
415  log_msg->add_str_int("/", training_iteration());
416  log_msg->add_str_int("/", sample_iteration());
417 }
int learning_iteration() const
Definition: lstmtrainer.h:149
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379

◆ MaintainCheckpoints()

bool tesseract::LSTMTrainer::MaintainCheckpoints ( TestCallback  tester,
STRING log_msg 
)

Definition at line 312 of file lstmtrainer.cpp.

312  {
313  PrepareLogMsg(log_msg);
314  double error_rate = CharError();
315  int iteration = learning_iteration();
316  if (iteration >= stall_iteration_ &&
317  error_rate > best_error_rate_ * (1.0 + kSubTrainerMarginFraction) &&
319  // It hasn't got any better in a long while, and is a margin worse than the
320  // best, so go back to the best model and try a different learning rate.
321  StartSubtrainer(log_msg);
322  }
323  SubTrainerResult sub_trainer_result = STR_NONE;
324  if (sub_trainer_ != nullptr) {
325  sub_trainer_result = UpdateSubtrainer(log_msg);
326  if (sub_trainer_result == STR_REPLACED) {
327  // Reset the inputs, as we have overwritten *this.
328  error_rate = CharError();
329  iteration = learning_iteration();
330  PrepareLogMsg(log_msg);
331  }
332  }
333  bool result = true; // Something interesting happened.
334  GenericVector<char> rec_model_data;
335  if (error_rate < best_error_rate_) {
336  SaveRecognitionDump(&rec_model_data);
337  log_msg->add_str_double(" New best char error = ", error_rate);
338  *log_msg += UpdateErrorGraph(iteration, error_rate, rec_model_data, tester);
339  // If sub_trainer_ is not nullptr, either *this beat it to a new best, or it
340  // just overwrote *this. In either case, we have finished with it.
341  delete sub_trainer_;
342  sub_trainer_ = nullptr;
345  log_msg->add_str_int(" Transitioned to stage ", CurrentTrainingStage());
346  }
349  STRING best_model_name = DumpFilename();
350  if (!(*file_writer_)(best_trainer_, best_model_name)) {
351  *log_msg += " failed to write best model:";
352  } else {
353  *log_msg += " wrote best model:";
355  }
356  *log_msg += best_model_name;
357  }
358  } else if (error_rate > worst_error_rate_) {
359  SaveRecognitionDump(&rec_model_data);
360  log_msg->add_str_double(" New worst char error = ", error_rate);
361  *log_msg += UpdateErrorGraph(iteration, error_rate, rec_model_data, tester);
364  // Error rate has ballooned. Go back to the best model.
365  *log_msg += "\nDivergence! ";
366  // Copy best_trainer_ before reading it, as it will get overwritten.
367  GenericVector<char> revert_data(best_trainer_);
368  if (checkpoint_reader_->Run(revert_data, this)) {
369  LogIterations("Reverted to", log_msg);
370  ReduceLearningRates(this, log_msg);
371  } else {
372  LogIterations("Failed to Revert at", log_msg);
373  }
374  // If it fails again, we will wait twice as long before reverting again.
375  stall_iteration_ = iteration + 2 * (iteration - learning_iteration());
376  // Re-save the best trainer with the new learning rates and stall
377  // iteration.
379  }
380  } else {
381  // Something interesting happened only if the sub_trainer_ was trained.
382  result = sub_trainer_result != STR_NONE;
383  }
384  if (checkpoint_writer_ != nullptr && file_writer_ != nullptr &&
385  checkpoint_name_.length() > 0) {
386  // Write a current checkpoint.
387  GenericVector<char> checkpoint;
388  if (!checkpoint_writer_->Run(FULL, this, &checkpoint) ||
389  !(*file_writer_)(checkpoint, checkpoint_name_)) {
390  *log_msg += " failed to write checkpoint.";
391  } else {
392  *log_msg += " wrote checkpoint.";
393  }
394  }
395  *log_msg += "\n";
396  return result;
397 }
bool TransitionTrainingStage(float error_threshold)
STRING UpdateErrorGraph(int iteration, double error_rate, const GenericVector< char > &model_data, TestCallback tester)
const int kMinStartedErrorRate
Definition: lstmtrainer.cpp:60
int learning_iteration() const
Definition: lstmtrainer.h:149
const double kMinDivergenceRate
Definition: lstmtrainer.cpp:45
LSTMTrainer * sub_trainer_
Definition: lstmtrainer.h:450
int CurrentTrainingStage() const
Definition: lstmtrainer.h:211
void PrepareLogMsg(STRING *log_msg) const
void ReduceLearningRates(LSTMTrainer *samples_trainer, STRING *log_msg)
GenericVector< char > best_trainer_
Definition: lstmtrainer.h:447
void SaveRecognitionDump(GenericVector< char > *data) const
void add_str_double(const char *str, double number)
Definition: strngs.cpp:389
virtual R Run(A1, A2)=0
void LogIterations(const char *intro_str, STRING *log_msg) const
double CharError() const
Definition: lstmtrainer.h:139
bool empty() const
Definition: genericvector.h:90
void StartSubtrainer(STRING *log_msg)
SubTrainerResult UpdateSubtrainer(STRING *log_msg)
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
STRING DumpFilename() const
const int kMinStallIterations
Definition: lstmtrainer.cpp:47
Definition: strngs.h:45
float error_rate_of_last_saved_best_
Definition: lstmtrainer.h:452
const double kSubTrainerMarginFraction
Definition: lstmtrainer.cpp:50
const double kStageTransitionThreshold
Definition: lstmtrainer.cpp:62
CheckPointReader checkpoint_reader_
Definition: lstmtrainer.h:424
virtual R Run(A1, A2, A3)=0
int32_t length() const
Definition: strngs.cpp:191
CheckPointWriter checkpoint_writer_
Definition: lstmtrainer.h:425
const double kBestCheckpointFraction
Definition: lstmtrainer.cpp:68

◆ MaintainCheckpointsSpecific()

bool tesseract::LSTMTrainer::MaintainCheckpointsSpecific ( int  iteration,
const GenericVector< char > *  train_model,
const GenericVector< char > *  rec_model,
TestCallback  tester,
STRING log_msg 
)

◆ MapRecoder()

std::vector< int > tesseract::LSTMTrainer::MapRecoder ( const UNICHARSET old_chset,
const UnicharCompress old_recoder 
) const

Definition at line 957 of file lstmtrainer.cpp.

958  {
959  int num_new_codes = recoder_.code_range();
960  int num_new_unichars = GetUnicharset().size();
961  std::vector<int> code_map(num_new_codes, -1);
962  for (int c = 0; c < num_new_codes; ++c) {
963  int old_code = -1;
964  // Find all new unichar_ids that recode to something that includes c.
965  // The <= is to include the null char, which may be beyond the unicharset.
966  for (int uid = 0; uid <= num_new_unichars; ++uid) {
967  RecodedCharID codes;
968  int length = recoder_.EncodeUnichar(uid, &codes);
969  int code_index = 0;
970  while (code_index < length && codes(code_index) != c) ++code_index;
971  if (code_index == length) continue;
972  // The old unicharset must have the same unichar.
973  int old_uid =
974  uid < num_new_unichars
975  ? old_chset.unichar_to_id(GetUnicharset().id_to_unichar(uid))
976  : old_chset.size() - 1;
977  if (old_uid == INVALID_UNICHAR_ID) continue;
978  // The encoding of old_uid at the same code_index is the old code.
979  RecodedCharID old_codes;
980  if (code_index < old_recoder.EncodeUnichar(old_uid, &old_codes)) {
981  old_code = old_codes(code_index);
982  break;
983  }
984  }
985  code_map[c] = old_code;
986  }
987  return code_map;
988 }
const UNICHARSET & GetUnicharset() const
int EncodeUnichar(int unichar_id, RecodedCharID *code) const
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:209
int size() const
Definition: unicharset.h:336

◆ mutable_training_data()

DocumentCache* tesseract::LSTMTrainer::mutable_training_data ( )
inline

Definition at line 168 of file lstmtrainer.h.

168 { return &training_data_; }
DocumentCache training_data_
Definition: lstmtrainer.h:414

◆ NewSingleError()

double tesseract::LSTMTrainer::NewSingleError ( ErrorTypes  type) const
inline

Definition at line 154 of file lstmtrainer.h.

154  {
156  }
static const int kRollingBufferSize_
Definition: lstmtrainer.h:478
GenericVector< double > error_buffers_[ET_COUNT]
Definition: lstmtrainer.h:479

◆ PrepareForBackward()

Trainability tesseract::LSTMTrainer::PrepareForBackward ( const ImageData trainingdata,
NetworkIO fwd_outputs,
NetworkIO targets 
)

Definition at line 798 of file lstmtrainer.cpp.

800  {
801  if (trainingdata == nullptr) {
802  tprintf("Null trainingdata.\n");
803  return UNENCODABLE;
804  }
805  // Ensure repeatability of random elements even across checkpoints.
806  bool debug = debug_interval_ > 0 &&
808  GenericVector<int> truth_labels;
809  if (!EncodeString(trainingdata->transcription(), &truth_labels)) {
810  tprintf("Can't encode transcription: '%s' in language '%s'\n",
811  trainingdata->transcription().string(),
812  trainingdata->language().string());
813  return UNENCODABLE;
814  }
815  bool upside_down = false;
816  if (randomly_rotate_) {
817  // This ensures consistent training results.
818  SetRandomSeed();
819  upside_down = randomizer_.SignedRand(1.0) > 0.0;
820  if (upside_down) {
821  // Modify the truth labels to match the rotation:
822  // Apart from space and null, increment the label. This is changes the
823  // script-id to the same script-id but upside-down.
824  // The labels need to be reversed in order, as the first is now the last.
825  for (int c = 0; c < truth_labels.size(); ++c) {
826  if (truth_labels[c] != UNICHAR_SPACE && truth_labels[c] != null_char_)
827  ++truth_labels[c];
828  }
829  truth_labels.reverse();
830  }
831  }
832  int w = 0;
833  while (w < truth_labels.size() &&
834  (truth_labels[w] == UNICHAR_SPACE || truth_labels[w] == null_char_))
835  ++w;
836  if (w == truth_labels.size()) {
837  tprintf("Blank transcription: %s\n",
838  trainingdata->transcription().string());
839  return UNENCODABLE;
840  }
841  float image_scale;
842  NetworkIO inputs;
843  bool invert = trainingdata->boxes().empty();
844  if (!RecognizeLine(*trainingdata, invert, debug, invert, upside_down,
845  &image_scale, &inputs, fwd_outputs)) {
846  tprintf("Image not trainable\n");
847  return UNENCODABLE;
848  }
849  targets->Resize(*fwd_outputs, network_->NumOutputs());
850  LossType loss_type = OutputLossType();
851  if (loss_type == LT_SOFTMAX) {
852  if (!ComputeTextTargets(*fwd_outputs, truth_labels, targets)) {
853  tprintf("Compute simple targets failed!\n");
854  return UNENCODABLE;
855  }
856  } else if (loss_type == LT_CTC) {
857  if (!ComputeCTCTargets(truth_labels, fwd_outputs, targets)) {
858  tprintf("Compute CTC targets failed!\n");
859  return UNENCODABLE;
860  }
861  } else {
862  tprintf("Logistic outputs not implemented yet!\n");
863  return UNENCODABLE;
864  }
865  GenericVector<int> ocr_labels;
866  GenericVector<int> xcoords;
867  LabelsFromOutputs(*fwd_outputs, &ocr_labels, &xcoords);
868  // CTC does not produce correct target labels to begin with.
869  if (loss_type != LT_CTC) {
870  LabelsFromOutputs(*targets, &truth_labels, &xcoords);
871  }
872  if (!DebugLSTMTraining(inputs, *trainingdata, *fwd_outputs, truth_labels,
873  *targets)) {
874  tprintf("Input width was %d\n", inputs.Width());
875  return UNENCODABLE;
876  }
877  STRING ocr_text = DecodeLabels(ocr_labels);
878  STRING truth_text = DecodeLabels(truth_labels);
879  targets->SubtractAllFromFloat(*fwd_outputs);
880  if (debug_interval_ != 0) {
881  tprintf("Iteration %d: BEST OCR TEXT : %s\n", training_iteration(),
882  ocr_text.string());
883  }
884  double char_error = ComputeCharError(truth_labels, ocr_labels);
885  double word_error = ComputeWordError(&truth_text, &ocr_text);
886  double delta_error = ComputeErrorRates(*targets, char_error, word_error);
887  if (debug_interval_ != 0) {
888  tprintf("File %s page %d %s:\n", trainingdata->imagefilename().string(),
889  trainingdata->page_number(), delta_error == 0.0 ? "(Perfect)" : "");
890  }
891  if (delta_error == 0.0) return PERFECT;
892  if (targets->AnySuspiciousTruth(kHighConfidence)) return HI_PRECISION_ERR;
893  return TRAINABLE;
894 }
int NumOutputs() const
Definition: network.h:123
int size() const
Definition: genericvector.h:71
const double kHighConfidence
Definition: lstmtrainer.cpp:64
const char * string() const
Definition: strngs.cpp:196
STRING DecodeLabels(const GenericVector< int > &labels)
bool DebugLSTMTraining(const NetworkIO &inputs, const ImageData &trainingdata, const NetworkIO &fwd_outputs, const GenericVector< int > &truth_labels, const NetworkIO &outputs)
double ComputeWordError(STRING *truth_str, STRING *ocr_str)
bool ComputeCTCTargets(const GenericVector< int > &truth_labels, NetworkIO *outputs, NetworkIO *targets)
void RecognizeLine(const ImageData &image_data, bool invert, bool debug, double worst_dict_cert, const TBOX &line_box, PointerVector< WERD_RES > *words, int lstm_choice_mode=0)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
double ComputeErrorRates(const NetworkIO &deltas, double char_error, double word_error)
bool EncodeString(const STRING &str, GenericVector< int > *labels) const
Definition: lstmtrainer.h:246
LossType OutputLossType() const
Definition: strngs.h:45
bool ComputeTextTargets(const NetworkIO &outputs, const GenericVector< int > &truth_labels, NetworkIO *targets)
double SignedRand(double range)
Definition: helpers.h:61
double ComputeCharError(const GenericVector< int > &truth_str, const GenericVector< int > &ocr_str)
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)

◆ PrepareLogMsg()

void tesseract::LSTMTrainer::PrepareLogMsg ( STRING log_msg) const

Definition at line 400 of file lstmtrainer.cpp.

400  {
401  LogIterations("At", log_msg);
402  log_msg->add_str_double(", Mean rms=", error_rates_[ET_RMS]);
403  log_msg->add_str_double("%, delta=", error_rates_[ET_DELTA]);
404  log_msg->add_str_double("%, char train=", error_rates_[ET_CHAR_ERROR]);
405  log_msg->add_str_double("%, word train=", error_rates_[ET_WORD_RECERR]);
406  log_msg->add_str_double("%, skip ratio=", error_rates_[ET_SKIP_RATIO]);
407  *log_msg += "%, ";
408 }
void add_str_double(const char *str, double number)
Definition: strngs.cpp:389
void LogIterations(const char *intro_str, STRING *log_msg) const
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ ReadLocalTrainingDump()

bool tesseract::LSTMTrainer::ReadLocalTrainingDump ( const TessdataManager mgr,
const char *  data,
int  size 
)

Definition at line 909 of file lstmtrainer.cpp.

910  {
911  if (size == 0) {
912  tprintf("Warning: data size is 0 in LSTMTrainer::ReadLocalTrainingDump\n");
913  return false;
914  }
915  TFile fp;
916  fp.Open(data, size);
917  return DeSerialize(mgr, &fp);
918 }
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
bool DeSerialize(const TessdataManager *mgr, TFile *fp)

◆ ReadSizedTrainingDump()

bool tesseract::LSTMTrainer::ReadSizedTrainingDump ( const char *  data,
int  size,
LSTMTrainer trainer 
) const
inline

Definition at line 296 of file lstmtrainer.h.

297  {
298  return trainer->ReadLocalTrainingDump(&mgr_, data, size);
299  }
TessdataManager mgr_
Definition: lstmtrainer.h:483

◆ ReadTrainingDump()

bool tesseract::LSTMTrainer::ReadTrainingDump ( const GenericVector< char > &  data,
LSTMTrainer trainer 
) const
inline

Definition at line 291 of file lstmtrainer.h.

292  {
293  if (data.empty()) return false;
294  return ReadSizedTrainingDump(&data[0], data.size(), trainer);
295  }
int size() const
Definition: genericvector.h:71
bool ReadSizedTrainingDump(const char *data, int size, LSTMTrainer *trainer) const
Definition: lstmtrainer.h:296
bool empty() const
Definition: genericvector.h:90

◆ ReduceLayerLearningRates()

int tesseract::LSTMTrainer::ReduceLayerLearningRates ( double  factor,
int  num_samples,
LSTMTrainer samples_trainer 
)

Definition at line 609 of file lstmtrainer.cpp.

610  {
611  enum WhichWay {
612  LR_DOWN, // Learning rate will go down by factor.
613  LR_SAME, // Learning rate will stay the same.
614  LR_COUNT // Size of arrays.
615  };
617  int num_layers = layers.size();
618  GenericVector<int> num_weights;
619  num_weights.init_to_size(num_layers, 0);
620  GenericVector<double> bad_sums[LR_COUNT];
621  GenericVector<double> ok_sums[LR_COUNT];
622  for (int i = 0; i < LR_COUNT; ++i) {
623  bad_sums[i].init_to_size(num_layers, 0.0);
624  ok_sums[i].init_to_size(num_layers, 0.0);
625  }
626  double momentum_factor = 1.0 / (1.0 - momentum_);
627  GenericVector<char> orig_trainer;
628  samples_trainer->SaveTrainingDump(LIGHT, this, &orig_trainer);
629  for (int i = 0; i < num_layers; ++i) {
630  Network* layer = GetLayer(layers[i]);
631  num_weights[i] = layer->IsTraining() ? layer->num_weights() : 0;
632  }
633  int iteration = sample_iteration();
634  for (int s = 0; s < num_samples; ++s) {
635  // Which way will we modify the learning rate?
636  for (int ww = 0; ww < LR_COUNT; ++ww) {
637  // Transfer momentum to learning rate and adjust by the ww factor.
638  float ww_factor = momentum_factor;
639  if (ww == LR_DOWN) ww_factor *= factor;
640  // Make a copy of *this, so we can mess about without damaging anything.
641  LSTMTrainer copy_trainer;
642  samples_trainer->ReadTrainingDump(orig_trainer, &copy_trainer);
643  // Clear the updates, doing nothing else.
644  copy_trainer.network_->Update(0.0, 0.0, 0.0, 0);
645  // Adjust the learning rate in each layer.
646  for (int i = 0; i < num_layers; ++i) {
647  if (num_weights[i] == 0) continue;
648  copy_trainer.ScaleLayerLearningRate(layers[i], ww_factor);
649  }
650  copy_trainer.SetIteration(iteration);
651  // Train on the sample, but keep the update in updates_ instead of
652  // applying to the weights.
653  const ImageData* trainingdata =
654  copy_trainer.TrainOnLine(samples_trainer, true);
655  if (trainingdata == nullptr) continue;
656  // We'll now use this trainer again for each layer.
657  GenericVector<char> updated_trainer;
658  samples_trainer->SaveTrainingDump(LIGHT, &copy_trainer, &updated_trainer);
659  for (int i = 0; i < num_layers; ++i) {
660  if (num_weights[i] == 0) continue;
661  LSTMTrainer layer_trainer;
662  samples_trainer->ReadTrainingDump(updated_trainer, &layer_trainer);
663  Network* layer = layer_trainer.GetLayer(layers[i]);
664  // Update the weights in just the layer, using Adam if enabled.
665  layer->Update(0.0, momentum_, adam_beta_,
666  layer_trainer.training_iteration_ + 1);
667  // Zero the updates matrix again.
668  layer->Update(0.0, 0.0, 0.0, 0);
669  // Train again on the same sample, again holding back the updates.
670  layer_trainer.TrainOnLine(trainingdata, true);
671  // Count the sign changes in the updates in layer vs in copy_trainer.
672  float before_bad = bad_sums[ww][i];
673  float before_ok = ok_sums[ww][i];
674  layer->CountAlternators(*copy_trainer.GetLayer(layers[i]),
675  &ok_sums[ww][i], &bad_sums[ww][i]);
676  float bad_frac =
677  bad_sums[ww][i] + ok_sums[ww][i] - before_bad - before_ok;
678  if (bad_frac > 0.0f)
679  bad_frac = (bad_sums[ww][i] - before_bad) / bad_frac;
680  }
681  }
682  ++iteration;
683  }
684  int num_lowered = 0;
685  for (int i = 0; i < num_layers; ++i) {
686  if (num_weights[i] == 0) continue;
687  Network* layer = GetLayer(layers[i]);
688  float lr = GetLayerLearningRate(layers[i]);
689  double total_down = bad_sums[LR_DOWN][i] + ok_sums[LR_DOWN][i];
690  double total_same = bad_sums[LR_SAME][i] + ok_sums[LR_SAME][i];
691  double frac_down = bad_sums[LR_DOWN][i] / total_down;
692  double frac_same = bad_sums[LR_SAME][i] / total_same;
693  tprintf("Layer %d=%s: lr %g->%g%%, lr %g->%g%%", i, layer->name().string(),
694  lr * factor, 100.0 * frac_down, lr, 100.0 * frac_same);
695  if (frac_down < frac_same * kImprovementFraction) {
696  tprintf(" REDUCED\n");
697  ScaleLayerLearningRate(layers[i], factor);
698  ++num_lowered;
699  } else {
700  tprintf(" SAME\n");
701  }
702  }
703  if (num_lowered == 0) {
704  // Just lower everything to make sure.
705  for (int i = 0; i < num_layers; ++i) {
706  if (num_weights[i] > 0) {
707  ScaleLayerLearningRate(layers[i], factor);
708  ++num_lowered;
709  }
710  }
711  }
712  return num_lowered;
713 }
Network * GetLayer(const STRING &id) const
int size() const
Definition: genericvector.h:71
void ScaleLayerLearningRate(const STRING &id, double factor)
float GetLayerLearningRate(const STRING &id) const
const double kImprovementFraction
Definition: lstmtrainer.cpp:66
void init_to_size(int size, const T &t)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
GenericVector< STRING > EnumerateLayers() const

◆ ReduceLearningRates()

void tesseract::LSTMTrainer::ReduceLearningRates ( LSTMTrainer samples_trainer,
STRING log_msg 
)

Definition at line 590 of file lstmtrainer.cpp.

591  {
593  int num_reduced = ReduceLayerLearningRates(
594  kLearningRateDecay, kNumAdjustmentIterations, samples_trainer);
595  log_msg->add_str_int("\nReduced learning rate on layers: ", num_reduced);
596  } else {
598  log_msg->add_str_double("\nReduced learning rate to :", learning_rate_);
599  }
600  *log_msg += "\n";
601 }
int ReduceLayerLearningRates(double factor, int num_samples, LSTMTrainer *samples_trainer)
void ScaleLearningRate(double factor)
void add_str_double(const char *str, double number)
Definition: strngs.cpp:389
const double kLearningRateDecay
Definition: lstmtrainer.cpp:52
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
bool TestFlag(NetworkFlags flag) const
Definition: network.h:144
const int kNumAdjustmentIterations
Definition: lstmtrainer.cpp:54

◆ RollErrorBuffers()

void tesseract::LSTMTrainer::RollErrorBuffers ( )
protected

Definition at line 1261 of file lstmtrainer.cpp.

1261  {
1263  if (NewSingleError(ET_DELTA) > 0.0)
1265  else
1268  if (debug_interval_ != 0) {
1269  tprintf("Mean rms=%g%%, delta=%g%%, train=%g%%(%g%%), skip ratio=%g%%\n",
1273  }
1274 }
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
double NewSingleError(ErrorTypes type) const
Definition: lstmtrainer.h:154
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ SaveRecognitionDump()

void tesseract::LSTMTrainer::SaveRecognitionDump ( GenericVector< char > *  data) const

Definition at line 930 of file lstmtrainer.cpp.

930  {
931  TFile fp;
932  fp.OpenWrite(data);
936 }
TessdataManager mgr_
Definition: lstmtrainer.h:483
virtual void SetEnableTraining(TrainingState state)
Definition: network.cpp:110
bool Serialize(const TessdataManager *mgr, TFile *fp) const
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ SaveTraineddata()

bool tesseract::LSTMTrainer::SaveTraineddata ( const STRING filename)

Definition at line 921 of file lstmtrainer.cpp.

921  {
922  GenericVector<char> recognizer_data;
923  SaveRecognitionDump(&recognizer_data);
924  mgr_.OverwriteEntry(TESSDATA_LSTM, &recognizer_data[0],
925  recognizer_data.size());
926  return mgr_.SaveFile(filename, file_writer_);
927 }
int size() const
Definition: genericvector.h:71
void OverwriteEntry(TessdataType type, const char *data, int size)
TessdataManager mgr_
Definition: lstmtrainer.h:483
void SaveRecognitionDump(GenericVector< char > *data) const
bool SaveFile(const STRING &filename, FileWriter writer) const

◆ SaveTrainingDump()

bool tesseract::LSTMTrainer::SaveTrainingDump ( SerializeAmount  serialize_amount,
const LSTMTrainer trainer,
GenericVector< char > *  data 
) const

Definition at line 900 of file lstmtrainer.cpp.

902  {
903  TFile fp;
904  fp.OpenWrite(data);
905  return trainer->Serialize(serialize_amount, &mgr_, &fp);
906 }
TessdataManager mgr_
Definition: lstmtrainer.h:483

◆ Serialize()

bool tesseract::LSTMTrainer::Serialize ( SerializeAmount  serialize_amount,
const TessdataManager mgr,
TFile fp 
) const

Definition at line 431 of file lstmtrainer.cpp.

432  {
433  if (!LSTMRecognizer::Serialize(mgr, fp)) return false;
434  if (!fp->Serialize(&learning_iteration_)) return false;
435  if (!fp->Serialize(&prev_sample_iteration_)) return false;
436  if (!fp->Serialize(&perfect_delay_)) return false;
437  if (!fp->Serialize(&last_perfect_training_iteration_)) return false;
438  for (int i = 0; i < ET_COUNT; ++i) {
439  if (!error_buffers_[i].Serialize(fp)) return false;
440  }
441  if (!fp->Serialize(&error_rates_[0], countof(error_rates_))) return false;
442  if (!fp->Serialize(&training_stage_)) return false;
443  uint8_t amount = serialize_amount;
444  if (!fp->Serialize(&amount)) return false;
445  if (serialize_amount == LIGHT) return true; // We are done.
446  if (!fp->Serialize(&best_error_rate_)) return false;
447  if (!fp->Serialize(&best_error_rates_[0], countof(best_error_rates_))) return false;
448  if (!fp->Serialize(&best_iteration_)) return false;
449  if (!fp->Serialize(&worst_error_rate_)) return false;
450  if (!fp->Serialize(&worst_error_rates_[0], countof(worst_error_rates_))) return false;
451  if (!fp->Serialize(&worst_iteration_)) return false;
452  if (!fp->Serialize(&stall_iteration_)) return false;
453  if (!best_model_data_.Serialize(fp)) return false;
454  if (!worst_model_data_.Serialize(fp)) return false;
455  if (serialize_amount != NO_BEST_TRAINER && !best_trainer_.Serialize(fp))
456  return false;
457  GenericVector<char> sub_data;
458  if (sub_trainer_ != nullptr && !SaveTrainingDump(LIGHT, sub_trainer_, &sub_data))
459  return false;
460  if (!sub_data.Serialize(fp)) return false;
461  if (!best_error_history_.Serialize(fp)) return false;
462  if (!best_error_iterations_.Serialize(fp)) return false;
463  return fp->Serialize(&improvement_steps_);
464 }
bool SaveTrainingDump(SerializeAmount serialize_amount, const LSTMTrainer *trainer, GenericVector< char > *data) const
double worst_error_rates_[ET_COUNT]
Definition: lstmtrainer.h:438
bool Serialize(SerializeAmount serialize_amount, const TessdataManager *mgr, TFile *fp) const
LSTMTrainer * sub_trainer_
Definition: lstmtrainer.h:450
double best_error_rates_[ET_COUNT]
Definition: lstmtrainer.h:432
bool Serialize(FILE *fp) const
constexpr size_t countof(T const (&)[N]) noexcept
Definition: serialis.h:43
GenericVector< char > best_trainer_
Definition: lstmtrainer.h:447
GenericVector< double > best_error_history_
Definition: lstmtrainer.h:457
GenericVector< int > best_error_iterations_
Definition: lstmtrainer.h:458
GenericVector< char > best_model_data_
Definition: lstmtrainer.h:444
GenericVector< char > worst_model_data_
Definition: lstmtrainer.h:445
GenericVector< double > error_buffers_[ET_COUNT]
Definition: lstmtrainer.h:479
bool Serialize(const TessdataManager *mgr, TFile *fp) const
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ set_perfect_delay()

void tesseract::LSTMTrainer::set_perfect_delay ( int  delay)
inline

Definition at line 151 of file lstmtrainer.h.

151 { perfect_delay_ = delay; }

◆ SetNullChar()

void tesseract::LSTMTrainer::SetNullChar ( )
protected

Definition at line 1005 of file lstmtrainer.cpp.

1005  {
1007  : GetUnicharset().size();
1008  RecodedCharID code;
1010  null_char_ = code(0);
1011 }
const UNICHARSET & GetUnicharset() const
int EncodeUnichar(int unichar_id, RecodedCharID *code) const
int size() const
Definition: unicharset.h:336
bool has_special_codes() const
Definition: unicharset.h:717

◆ SetupCheckpointInfo()

void tesseract::LSTMTrainer::SetupCheckpointInfo ( )

◆ StartSubtrainer()

void tesseract::LSTMTrainer::StartSubtrainer ( STRING log_msg)

Definition at line 517 of file lstmtrainer.cpp.

517  {
518  delete sub_trainer_;
519  sub_trainer_ = new LSTMTrainer();
521  *log_msg += " Failed to revert to previous best for trial!";
522  delete sub_trainer_;
523  sub_trainer_ = nullptr;
524  } else {
525  log_msg->add_str_int(" Trial sub_trainer_ from iteration ",
527  // Reduce learning rate so it doesn't diverge this time.
528  sub_trainer_->ReduceLearningRates(this, log_msg);
529  // If it fails again, we will wait twice as long before reverting again.
530  int stall_offset =
532  stall_iteration_ = learning_iteration() + 2 * stall_offset;
534  // Re-save the best trainer with the new learning rates and stall iteration.
536  }
537 }
int learning_iteration() const
Definition: lstmtrainer.h:149
LSTMTrainer * sub_trainer_
Definition: lstmtrainer.h:450
void ReduceLearningRates(LSTMTrainer *samples_trainer, STRING *log_msg)
GenericVector< char > best_trainer_
Definition: lstmtrainer.h:447
virtual R Run(A1, A2)=0
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
CheckPointReader checkpoint_reader_
Definition: lstmtrainer.h:424
virtual R Run(A1, A2, A3)=0
CheckPointWriter checkpoint_writer_
Definition: lstmtrainer.h:425

◆ training_data()

const DocumentCache& tesseract::LSTMTrainer::training_data ( ) const
inline

Definition at line 165 of file lstmtrainer.h.

165  {
166  return training_data_;
167  }
DocumentCache training_data_
Definition: lstmtrainer.h:414

◆ TrainOnLine() [1/2]

const ImageData* tesseract::LSTMTrainer::TrainOnLine ( LSTMTrainer samples_trainer,
bool  batch 
)
inline

Definition at line 259 of file lstmtrainer.h.

259  {
260  int sample_index = sample_iteration();
261  const ImageData* image =
262  samples_trainer->training_data_.GetPageBySerial(sample_index);
263  if (image != nullptr) {
264  Trainability trainable = TrainOnLine(image, batch);
265  if (trainable == UNENCODABLE || trainable == NOT_BOXED) {
266  return nullptr; // Sample was unusable.
267  }
268  } else {
270  }
271  return image;
272  }
const ImageData * TrainOnLine(LSTMTrainer *samples_trainer, bool batch)
Definition: lstmtrainer.h:259

◆ TrainOnLine() [2/2]

Trainability tesseract::LSTMTrainer::TrainOnLine ( const ImageData trainingdata,
bool  batch 
)

Definition at line 765 of file lstmtrainer.cpp.

766  {
767  NetworkIO fwd_outputs, targets;
768  Trainability trainable =
769  PrepareForBackward(trainingdata, &fwd_outputs, &targets);
771  if (trainable == UNENCODABLE || trainable == NOT_BOXED) {
772  return trainable; // Sample was unusable.
773  }
774  bool debug = debug_interval_ > 0 &&
776  // Run backprop on the output.
777  NetworkIO bp_deltas;
778  if (network_->IsTraining() &&
779  (trainable != PERFECT ||
782  network_->Backward(debug, targets, &scratch_space_, &bp_deltas);
784  training_iteration_ + 1);
785  }
786 #ifndef GRAPHICS_DISABLED
787  if (debug_interval_ == 1 && debug_win_ != nullptr) {
789  }
790 #endif // GRAPHICS_DISABLED
791  // Roll the memory of past means.
793  return trainable;
794 }
NetworkScratch scratch_space_
Trainability PrepareForBackward(const ImageData *trainingdata, NetworkIO *fwd_outputs, NetworkIO *targets)
SVEvent * AwaitEvent(SVEventType type)
Definition: scrollview.cpp:445
bool IsTraining() const
Definition: network.h:115
virtual void Update(float learning_rate, float momentum, float adam_beta, int num_samples)
Definition: network.h:231
virtual bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)
Definition: network.h:273

◆ TransitionTrainingStage()

bool tesseract::LSTMTrainer::TransitionTrainingStage ( float  error_threshold)

Definition at line 421 of file lstmtrainer.cpp.

421  {
422  if (best_error_rate_ < error_threshold &&
424  ++training_stage_;
425  return true;
426  }
427  return false;
428 }

◆ TryLoadingCheckpoint()

bool tesseract::LSTMTrainer::TryLoadingCheckpoint ( const char *  filename,
const char *  old_traineddata 
)

Definition at line 128 of file lstmtrainer.cpp.

129  {
130  GenericVector<char> data;
131  if (!(*file_reader_)(filename, &data)) return false;
132  tprintf("Loaded file %s, unpacking...\n", filename);
133  if (!checkpoint_reader_->Run(data, this)) return false;
134  StaticShape shape = network_->OutputShape(network_->InputShape());
135  if (((old_traineddata == nullptr || *old_traineddata == '\0') &&
137  filename == old_traineddata) {
138  return true; // Normal checkpoint load complete.
139  }
140  tprintf("Code range changed from %d to %d!\n", network_->NumOutputs(),
141  recoder_.code_range());
142  if (old_traineddata == nullptr || *old_traineddata == '\0') {
143  tprintf("Must supply the old traineddata for code conversion!\n");
144  return false;
145  }
146  TessdataManager old_mgr;
147  ASSERT_HOST(old_mgr.Init(old_traineddata));
148  TFile fp;
149  if (!old_mgr.GetComponent(TESSDATA_LSTM_UNICHARSET, &fp)) return false;
150  UNICHARSET old_chset;
151  if (!old_chset.load_from_file(&fp, false)) return false;
152  if (!old_mgr.GetComponent(TESSDATA_LSTM_RECODER, &fp)) return false;
153  UnicharCompress old_recoder;
154  if (!old_recoder.DeSerialize(&fp)) return false;
155  std::vector<int> code_map = MapRecoder(old_chset, old_recoder);
156  // Set the null_char_ to the new value.
157  int old_null_char = null_char_;
158  SetNullChar();
159  // Map the softmax(s) in the network.
160  network_->RemapOutputs(old_recoder.code_range(), code_map);
161  tprintf("Previous null char=%d mapped to %d\n", old_null_char, null_char_);
162  return true;
163 }
int NumOutputs() const
Definition: network.h:123
virtual StaticShape InputShape() const
Definition: network.h:127
virtual R Run(A1, A2)=0
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
virtual StaticShape OutputShape(const StaticShape &input_shape) const
Definition: network.h:133
std::vector< int > MapRecoder(const UNICHARSET &old_chset, const UnicharCompress &old_recoder) const
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:383
CheckPointReader checkpoint_reader_
Definition: lstmtrainer.h:424
virtual int RemapOutputs(int old_no, const std::vector< int > &code_map)
Definition: network.h:186
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ UpdateErrorBuffer()

void tesseract::LSTMTrainer::UpdateErrorBuffer ( double  new_error,
ErrorTypes  type 
)
protected

Definition at line 1248 of file lstmtrainer.cpp.

1248  {
1250  error_buffers_[type][index] = new_error;
1251  // Compute the mean error.
1252  int mean_count = std::min(training_iteration_ + 1, error_buffers_[type].size());
1253  double buffer_sum = 0.0;
1254  for (int i = 0; i < mean_count; ++i) buffer_sum += error_buffers_[type][i];
1255  double mean = buffer_sum / mean_count;
1256  // Trim precision to 1/1000 of 1%.
1257  error_rates_[type] = IntCastRounded(100000.0 * mean) / 1000.0;
1258 }
static const int kRollingBufferSize_
Definition: lstmtrainer.h:478
int IntCastRounded(double x)
Definition: helpers.h:168
GenericVector< double > error_buffers_[ET_COUNT]
Definition: lstmtrainer.h:479
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ UpdateErrorGraph()

STRING tesseract::LSTMTrainer::UpdateErrorGraph ( int  iteration,
double  error_rate,
const GenericVector< char > &  model_data,
TestCallback  tester 
)
protected

Definition at line 1280 of file lstmtrainer.cpp.

1282  {
1283  if (error_rate > best_error_rate_
1284  && iteration < best_iteration_ + kErrorGraphInterval) {
1285  // Too soon to record a new point.
1286  if (tester != nullptr && !worst_model_data_.empty()) {
1289  return tester->Run(worst_iteration_, nullptr, mgr_, CurrentTrainingStage());
1290  } else {
1291  return "";
1292  }
1293  }
1294  STRING result;
1295  // NOTE: there are 2 asymmetries here:
1296  // 1. We are computing the global minimum, but the local maximum in between.
1297  // 2. If the tester returns an empty string, indicating that it is busy,
1298  // call it repeatedly on new local maxima to test the previous min, but
1299  // not the other way around, as there is little point testing the maxima
1300  // between very frequent minima.
1301  if (error_rate < best_error_rate_) {
1302  // This is a new (global) minimum.
1303  if (tester != nullptr && !worst_model_data_.empty()) {
1306  result = tester->Run(worst_iteration_, worst_error_rates_, mgr_,
1309  best_model_data_ = model_data;
1310  }
1311  best_error_rate_ = error_rate;
1312  memcpy(best_error_rates_, error_rates_, sizeof(error_rates_));
1313  best_iteration_ = iteration;
1314  best_error_history_.push_back(error_rate);
1315  best_error_iterations_.push_back(iteration);
1316  // Compute 2% decay time.
1317  double two_percent_more = error_rate + 2.0;
1318  int i;
1319  for (i = best_error_history_.size() - 1;
1320  i >= 0 && best_error_history_[i] < two_percent_more; --i) {
1321  }
1322  int old_iteration = i >= 0 ? best_error_iterations_[i] : 0;
1323  improvement_steps_ = iteration - old_iteration;
1324  tprintf("2 Percent improvement time=%d, best error was %g @ %d\n",
1325  improvement_steps_, i >= 0 ? best_error_history_[i] : 100.0,
1326  old_iteration);
1327  } else if (error_rate > best_error_rate_) {
1328  // This is a new (local) maximum.
1329  if (tester != nullptr) {
1330  if (!best_model_data_.empty()) {
1333  result = tester->Run(best_iteration_, best_error_rates_, mgr_,
1335  } else if (!worst_model_data_.empty()) {
1336  // Allow for multiple data points with "worst" error rate.
1339  result = tester->Run(worst_iteration_, worst_error_rates_, mgr_,
1341  }
1342  if (result.length() > 0)
1344  worst_model_data_ = model_data;
1345  }
1346  }
1347  worst_error_rate_ = error_rate;
1348  memcpy(worst_error_rates_, error_rates_, sizeof(error_rates_));
1349  worst_iteration_ = iteration;
1350  return result;
1351 }
int size() const
Definition: genericvector.h:71
double worst_error_rates_[ET_COUNT]
Definition: lstmtrainer.h:438
void OverwriteEntry(TessdataType type, const char *data, int size)
double best_error_rates_[ET_COUNT]
Definition: lstmtrainer.h:432
int CurrentTrainingStage() const
Definition: lstmtrainer.h:211
TessdataManager mgr_
Definition: lstmtrainer.h:483
GenericVector< double > best_error_history_
Definition: lstmtrainer.h:457
GenericVector< int > best_error_iterations_
Definition: lstmtrainer.h:458
bool empty() const
Definition: genericvector.h:90
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
GenericVector< char > best_model_data_
Definition: lstmtrainer.h:444
int push_back(T object)
Definition: strngs.h:45
void truncate(int size)
GenericVector< char > worst_model_data_
Definition: lstmtrainer.h:445
int32_t length() const
Definition: strngs.cpp:191
const int kErrorGraphInterval
Definition: lstmtrainer.cpp:56
double error_rates_[ET_COUNT]
Definition: lstmtrainer.h:481

◆ UpdateSubtrainer()

SubTrainerResult tesseract::LSTMTrainer::UpdateSubtrainer ( STRING log_msg)

Definition at line 547 of file lstmtrainer.cpp.

547  {
548  double training_error = CharError();
549  double sub_error = sub_trainer_->CharError();
550  double sub_margin = (training_error - sub_error) / sub_error;
551  if (sub_margin >= kSubTrainerMarginFraction) {
552  log_msg->add_str_double(" sub_trainer=", sub_error);
553  log_msg->add_str_double(" margin=", 100.0 * sub_margin);
554  *log_msg += "\n";
555  // Catch up to current iteration.
556  int end_iteration = training_iteration();
557  while (sub_trainer_->training_iteration() < end_iteration &&
558  sub_margin >= kSubTrainerMarginFraction) {
559  int target_iteration =
561  while (sub_trainer_->training_iteration() < target_iteration) {
562  sub_trainer_->TrainOnLine(this, false);
563  }
564  STRING batch_log = "Sub:";
565  sub_trainer_->PrepareLogMsg(&batch_log);
566  batch_log += "\n";
567  tprintf("UpdateSubtrainer:%s", batch_log.string());
568  *log_msg += batch_log;
569  sub_error = sub_trainer_->CharError();
570  sub_margin = (training_error - sub_error) / sub_error;
571  }
572  if (sub_error < best_error_rate_ &&
573  sub_margin >= kSubTrainerMarginFraction) {
574  // The sub_trainer_ has won the race to a new best. Switch to it.
575  GenericVector<char> updated_trainer;
576  SaveTrainingDump(LIGHT, sub_trainer_, &updated_trainer);
577  ReadTrainingDump(updated_trainer, this);
578  log_msg->add_str_int(" Sub trainer wins at iteration ",
580  *log_msg += "\n";
581  return STR_REPLACED;
582  }
583  return STR_UPDATED;
584  }
585  return STR_NONE;
586 }
bool SaveTrainingDump(SerializeAmount serialize_amount, const LSTMTrainer *trainer, GenericVector< char > *data) const
const ImageData * TrainOnLine(LSTMTrainer *samples_trainer, bool batch)
Definition: lstmtrainer.h:259
const char * string() const
Definition: strngs.cpp:196
LSTMTrainer * sub_trainer_
Definition: lstmtrainer.h:450
void PrepareLogMsg(STRING *log_msg) const
bool ReadTrainingDump(const GenericVector< char > &data, LSTMTrainer *trainer) const
Definition: lstmtrainer.h:291
void add_str_double(const char *str, double number)
Definition: strngs.cpp:389
double CharError() const
Definition: lstmtrainer.h:139
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
Definition: strngs.h:45
const double kSubTrainerMarginFraction
Definition: lstmtrainer.cpp:50
const int kNumPagesPerBatch
Definition: lstmtrainer.cpp:58

Member Data Documentation

◆ align_win_

ScrollView* tesseract::LSTMTrainer::align_win_
protected

Definition at line 397 of file lstmtrainer.h.

◆ best_error_history_

GenericVector<double> tesseract::LSTMTrainer::best_error_history_
protected

Definition at line 457 of file lstmtrainer.h.

◆ best_error_iterations_

GenericVector<int> tesseract::LSTMTrainer::best_error_iterations_
protected

Definition at line 458 of file lstmtrainer.h.

◆ best_error_rate_

double tesseract::LSTMTrainer::best_error_rate_
protected

Definition at line 430 of file lstmtrainer.h.

◆ best_error_rates_

double tesseract::LSTMTrainer::best_error_rates_[ET_COUNT]
protected

Definition at line 432 of file lstmtrainer.h.

◆ best_iteration_

int tesseract::LSTMTrainer::best_iteration_
protected

Definition at line 434 of file lstmtrainer.h.

◆ best_model_data_

GenericVector<char> tesseract::LSTMTrainer::best_model_data_
protected

Definition at line 444 of file lstmtrainer.h.

◆ best_model_name_

STRING tesseract::LSTMTrainer::best_model_name_
protected

Definition at line 416 of file lstmtrainer.h.

◆ best_trainer_

GenericVector<char> tesseract::LSTMTrainer::best_trainer_
protected

Definition at line 447 of file lstmtrainer.h.

◆ checkpoint_iteration_

int tesseract::LSTMTrainer::checkpoint_iteration_
protected

Definition at line 407 of file lstmtrainer.h.

◆ checkpoint_name_

STRING tesseract::LSTMTrainer::checkpoint_name_
protected

Definition at line 411 of file lstmtrainer.h.

◆ checkpoint_reader_

CheckPointReader tesseract::LSTMTrainer::checkpoint_reader_
protected

Definition at line 424 of file lstmtrainer.h.

◆ checkpoint_writer_

CheckPointWriter tesseract::LSTMTrainer::checkpoint_writer_
protected

Definition at line 425 of file lstmtrainer.h.

◆ ctc_win_

ScrollView* tesseract::LSTMTrainer::ctc_win_
protected

Definition at line 401 of file lstmtrainer.h.

◆ debug_interval_

int tesseract::LSTMTrainer::debug_interval_
protected

Definition at line 405 of file lstmtrainer.h.

◆ error_buffers_

GenericVector<double> tesseract::LSTMTrainer::error_buffers_[ET_COUNT]
protected

Definition at line 479 of file lstmtrainer.h.

◆ error_rate_of_last_saved_best_

float tesseract::LSTMTrainer::error_rate_of_last_saved_best_
protected

Definition at line 452 of file lstmtrainer.h.

◆ error_rates_

double tesseract::LSTMTrainer::error_rates_[ET_COUNT]
protected

Definition at line 481 of file lstmtrainer.h.

◆ file_reader_

FileReader tesseract::LSTMTrainer::file_reader_
protected

Definition at line 420 of file lstmtrainer.h.

◆ file_writer_

FileWriter tesseract::LSTMTrainer::file_writer_
protected

Definition at line 421 of file lstmtrainer.h.

◆ improvement_steps_

int32_t tesseract::LSTMTrainer::improvement_steps_
protected

Definition at line 460 of file lstmtrainer.h.

◆ kRollingBufferSize_

const int tesseract::LSTMTrainer::kRollingBufferSize_ = 1000
staticprotected

Definition at line 478 of file lstmtrainer.h.

◆ last_perfect_training_iteration_

int tesseract::LSTMTrainer::last_perfect_training_iteration_
protected

Definition at line 475 of file lstmtrainer.h.

◆ learning_iteration_

int tesseract::LSTMTrainer::learning_iteration_
protected

Definition at line 464 of file lstmtrainer.h.

◆ mgr_

TessdataManager tesseract::LSTMTrainer::mgr_
protected

Definition at line 483 of file lstmtrainer.h.

◆ model_base_

STRING tesseract::LSTMTrainer::model_base_
protected

Definition at line 409 of file lstmtrainer.h.

◆ num_training_stages_

int tesseract::LSTMTrainer::num_training_stages_
protected

Definition at line 418 of file lstmtrainer.h.

◆ perfect_delay_

int tesseract::LSTMTrainer::perfect_delay_
protected

Definition at line 472 of file lstmtrainer.h.

◆ prev_sample_iteration_

int tesseract::LSTMTrainer::prev_sample_iteration_
protected

Definition at line 466 of file lstmtrainer.h.

◆ randomly_rotate_

bool tesseract::LSTMTrainer::randomly_rotate_
protected

Definition at line 413 of file lstmtrainer.h.

◆ recon_win_

ScrollView* tesseract::LSTMTrainer::recon_win_
protected

Definition at line 403 of file lstmtrainer.h.

◆ stall_iteration_

int tesseract::LSTMTrainer::stall_iteration_
protected

Definition at line 442 of file lstmtrainer.h.

◆ sub_trainer_

LSTMTrainer* tesseract::LSTMTrainer::sub_trainer_
protected

Definition at line 450 of file lstmtrainer.h.

◆ target_win_

ScrollView* tesseract::LSTMTrainer::target_win_
protected

Definition at line 399 of file lstmtrainer.h.

◆ training_data_

DocumentCache tesseract::LSTMTrainer::training_data_
protected

Definition at line 414 of file lstmtrainer.h.

◆ training_stage_

int tesseract::LSTMTrainer::training_stage_
protected

Definition at line 454 of file lstmtrainer.h.

◆ worst_error_rate_

double tesseract::LSTMTrainer::worst_error_rate_
protected

Definition at line 436 of file lstmtrainer.h.

◆ worst_error_rates_

double tesseract::LSTMTrainer::worst_error_rates_[ET_COUNT]
protected

Definition at line 438 of file lstmtrainer.h.

◆ worst_iteration_

int tesseract::LSTMTrainer::worst_iteration_
protected

Definition at line 440 of file lstmtrainer.h.

◆ worst_model_data_

GenericVector<char> tesseract::LSTMTrainer::worst_model_data_
protected

Definition at line 445 of file lstmtrainer.h.


The documentation for this class was generated from the following files: