tesseract  5.0.0
tesseract::LSTMTester Class Reference

#include <lstmtester.h>

Public Member Functions

 LSTMTester (int64_t max_memory)
 
bool LoadAllEvalData (const char *filenames_file)
 
bool LoadAllEvalData (const std::vector< std::string > &filenames)
 
std::string RunEvalAsync (int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage)
 
std::string RunEvalSync (int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage, int verbosity)
 

Detailed Description

Definition at line 31 of file lstmtester.h.

Constructor & Destructor Documentation

◆ LSTMTester()

tesseract::LSTMTester::LSTMTester ( int64_t  max_memory)

Definition at line 24 of file lstmtester.cpp.

24 : test_data_(max_memory) {}

Member Function Documentation

◆ LoadAllEvalData() [1/2]

bool tesseract::LSTMTester::LoadAllEvalData ( const char *  filenames_file)

Definition at line 29 of file lstmtester.cpp.

29  {
30  std::vector<std::string> filenames;
31  if (!LoadFileLinesToStrings(filenames_file, &filenames)) {
32  tprintf("Failed to load list of eval filenames from %s\n", filenames_file);
33  return false;
34  }
35  return LoadAllEvalData(filenames);
36 }
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
bool LoadFileLinesToStrings(const char *filename, std::vector< std::string > *lines)
Definition: fileio.h:32
bool LoadAllEvalData(const char *filenames_file)
Definition: lstmtester.cpp:29

◆ LoadAllEvalData() [2/2]

bool tesseract::LSTMTester::LoadAllEvalData ( const std::vector< std::string > &  filenames)

Definition at line 41 of file lstmtester.cpp.

41  {
42  test_data_.Clear();
43  bool result = test_data_.LoadDocuments(filenames, CS_SEQUENTIAL, nullptr);
44  total_pages_ = test_data_.TotalPages();
45  return result;
46 }
@ CS_SEQUENTIAL
Definition: imagedata.h:49
TESS_API bool LoadDocuments(const std::vector< std::string > &filenames, CachingStrategy cache_strategy, FileReader reader)
Definition: imagedata.cpp:614
TESS_API int TotalPages()
Definition: imagedata.cpp:659

◆ RunEvalAsync()

std::string tesseract::LSTMTester::RunEvalAsync ( int  iteration,
const double *  training_errors,
const TessdataManager model_mgr,
int  training_stage 
)

Definition at line 50 of file lstmtester.cpp.

51  {
52  std::string result;
53  if (total_pages_ == 0) {
54  result += "No test data at iteration " + std::to_string(iteration);
55  return result;
56  }
57  if (!LockIfNotRunning()) {
58  result += "Previous test incomplete, skipping test at iteration " + std::to_string(iteration);
59  return result;
60  }
61  // Save the args.
62  std::string prev_result = test_result_;
63  test_result_ = "";
64  if (training_errors != nullptr) {
65  test_iteration_ = iteration;
66  test_training_errors_ = training_errors;
67  test_model_mgr_ = model_mgr;
68  test_training_stage_ = training_stage;
69  std::thread t(&LSTMTester::ThreadFunc, this);
70  t.detach();
71  } else {
72  UnlockRunning();
73  }
74  return prev_result;
75 }

◆ RunEvalSync()

std::string tesseract::LSTMTester::RunEvalSync ( int  iteration,
const double *  training_errors,
const TessdataManager model_mgr,
int  training_stage,
int  verbosity 
)

Definition at line 79 of file lstmtester.cpp.

81  {
82  LSTMTrainer trainer;
83  trainer.InitCharSet(model_mgr);
84  TFile fp;
85  if (!model_mgr.GetComponent(TESSDATA_LSTM, &fp) || !trainer.DeSerialize(&model_mgr, &fp)) {
86  return "Deserialize failed";
87  }
88  int eval_iteration = 0;
89  double char_error = 0.0;
90  double word_error = 0.0;
91  int error_count = 0;
92  while (error_count < total_pages_) {
93  const ImageData *trainingdata = test_data_.GetPageBySerial(eval_iteration);
94  trainer.SetIteration(++eval_iteration);
95  NetworkIO fwd_outputs, targets;
96  Trainability result = trainer.PrepareForBackward(trainingdata, &fwd_outputs, &targets);
97  if (result != UNENCODABLE) {
98  char_error += trainer.NewSingleError(tesseract::ET_CHAR_ERROR);
99  word_error += trainer.NewSingleError(tesseract::ET_WORD_RECERR);
100  ++error_count;
101  if (verbosity > 1 || (verbosity > 0 && result != PERFECT)) {
102  tprintf("Truth:%s\n", trainingdata->transcription().c_str());
103  std::vector<int> ocr_labels;
104  std::vector<int> xcoords;
105  trainer.LabelsFromOutputs(fwd_outputs, &ocr_labels, &xcoords);
106  std::string ocr_text = trainer.DecodeLabels(ocr_labels);
107  tprintf("OCR :%s\n", ocr_text.c_str());
108  if (verbosity > 2 || (verbosity > 1 && result != PERFECT)) {
109  tprintf("Line BCER=%f, BWER=%f\n\n",
110  trainer.NewSingleError(tesseract::ET_CHAR_ERROR),
111  trainer.NewSingleError(tesseract::ET_WORD_RECERR));
112  }
113  }
114  }
115  }
116  char_error *= 100.0 / total_pages_;
117  word_error *= 100.0 / total_pages_;
118  std::string result;
119  if (iteration != 0 || training_stage != 0) {
120  result += "At iteration " + std::to_string(iteration);
121  result += ", stage " + std::to_string(training_stage) + ", ";
122  }
123  result += "BCER eval=" + std::to_string(char_error);
124  result += ", BWER eval=" + std::to_string(word_error);
125  return result;
126 }
@ ET_WORD_RECERR
Definition: lstmtrainer.h:43
@ ET_CHAR_ERROR
Definition: lstmtrainer.h:44
const ImageData * GetPageBySerial(int serial)
Definition: imagedata.h:317

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