tesseract  5.0.0-alpha-619-ge9db
tesseract::LSTMTester Class Reference

#include <lstmtester.h>

Public Member Functions

 LSTMTester (int64_t max_memory)
 
bool LoadAllEvalData (const STRING &filenames_file)
 
bool LoadAllEvalData (const GenericVector< STRING > &filenames)
 
STRING RunEvalAsync (int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage)
 
STRING RunEvalSync (int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage, int verbosity)
 

Detailed Description

Definition at line 28 of file lstmtester.h.

Constructor & Destructor Documentation

◆ LSTMTester()

tesseract::LSTMTester::LSTMTester ( int64_t  max_memory)

Definition at line 25 of file lstmtester.cpp.

26  : test_data_(max_memory) {}

Member Function Documentation

◆ LoadAllEvalData() [1/2]

bool tesseract::LSTMTester::LoadAllEvalData ( const GenericVector< STRING > &  filenames)

Definition at line 44 of file lstmtester.cpp.

44  {
45  test_data_.Clear();
46  bool result = test_data_.LoadDocuments(filenames, CS_SEQUENTIAL, nullptr);
47  total_pages_ = test_data_.TotalPages();
48  return result;
49 }

◆ LoadAllEvalData() [2/2]

bool tesseract::LSTMTester::LoadAllEvalData ( const STRING filenames_file)

Definition at line 31 of file lstmtester.cpp.

31  {
32  GenericVector<STRING> filenames;
33  if (!LoadFileLinesToStrings(filenames_file.c_str(), &filenames)) {
34  tprintf("Failed to load list of eval filenames from %s\n",
35  filenames_file.c_str());
36  return false;
37  }
38  return LoadAllEvalData(filenames);
39 }

◆ RunEvalAsync()

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

Definition at line 53 of file lstmtester.cpp.

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

◆ RunEvalSync()

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

Definition at line 84 of file lstmtester.cpp.

86  {
87  LSTMTrainer trainer;
88  trainer.InitCharSet(model_mgr);
89  TFile fp;
90  if (!model_mgr.GetComponent(TESSDATA_LSTM, &fp) ||
91  !trainer.DeSerialize(&model_mgr, &fp)) {
92  return "Deserialize failed";
93  }
94  int eval_iteration = 0;
95  double char_error = 0.0;
96  double word_error = 0.0;
97  int error_count = 0;
98  while (error_count < total_pages_) {
99  const ImageData* trainingdata = test_data_.GetPageBySerial(eval_iteration);
100  trainer.SetIteration(++eval_iteration);
101  NetworkIO fwd_outputs, targets;
102  Trainability result =
103  trainer.PrepareForBackward(trainingdata, &fwd_outputs, &targets);
104  if (result != UNENCODABLE) {
105  char_error += trainer.NewSingleError(tesseract::ET_CHAR_ERROR);
106  word_error += trainer.NewSingleError(tesseract::ET_WORD_RECERR);
107  ++error_count;
108  if (verbosity > 1 || (verbosity > 0 && result != PERFECT)) {
109  tprintf("Truth:%s\n", trainingdata->transcription().c_str());
110  GenericVector<int> ocr_labels;
111  GenericVector<int> xcoords;
112  trainer.LabelsFromOutputs(fwd_outputs, &ocr_labels, &xcoords);
113  STRING ocr_text = trainer.DecodeLabels(ocr_labels);
114  tprintf("OCR :%s\n", ocr_text.c_str());
115  }
116  }
117  }
118  char_error *= 100.0 / total_pages_;
119  word_error *= 100.0 / total_pages_;
120  STRING result;
121  result.add_str_int("At iteration ", iteration);
122  result.add_str_int(", stage ", training_stage);
123  result.add_str_double(", Eval Char error rate=", char_error);
124  result.add_str_double(", Word error rate=", word_error);
125  return result;
126 }

The documentation for this class was generated from the following files:
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
tesseract::DocumentCache::LoadDocuments
bool LoadDocuments(const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, FileReader reader)
Definition: imagedata.cpp:566
STRING
Definition: strngs.h:45
tesseract::CS_SEQUENTIAL
Definition: imagedata.h:48
tesseract::ET_WORD_RECERR
Definition: lstmtrainer.h:40
tesseract::PERFECT
Definition: lstmtrainer.h:49
tesseract::DocumentCache::GetPageBySerial
const ImageData * GetPageBySerial(int serial)
Definition: imagedata.h:343
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
tesseract::DocumentCache::TotalPages
int TotalPages()
Definition: imagedata.cpp:607
tesseract::LoadFileLinesToStrings
bool LoadFileLinesToStrings(const char *filename, GenericVector< STRING > *lines)
Definition: fileio.h:43
tesseract::Trainability
Trainability
Definition: lstmtrainer.h:47
tesseract::ET_CHAR_ERROR
Definition: lstmtrainer.h:41
GenericVector< STRING >
STRING::add_str_double
void add_str_double(const char *str, double number)
Definition: strngs.cpp:380
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::DocumentCache::Clear
void Clear()
Definition: imagedata.h:326
tesseract::UNENCODABLE
Definition: lstmtrainer.h:50
tesseract::TESSDATA_LSTM
Definition: tessdatamanager.h:74
tesseract::LSTMTester::LoadAllEvalData
bool LoadAllEvalData(const STRING &filenames_file)
Definition: lstmtester.cpp:31