tesseract  4.0.0-1-g2a2b
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 29 of file lstmtester.h.

Constructor & Destructor Documentation

◆ LSTMTester()

tesseract::LSTMTester::LSTMTester ( int64_t  max_memory)

Definition at line 24 of file lstmtester.cpp.

25  : test_data_(max_memory), total_pages_(0), async_running_(false) {}

Member Function Documentation

◆ LoadAllEvalData() [1/2]

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

Definition at line 30 of file lstmtester.cpp.

30  {
31  GenericVector<STRING> filenames;
32  if (!LoadFileLinesToStrings(filenames_file, &filenames)) {
33  tprintf("Failed to load list of eval filenames from %s\n",
34  filenames_file.string());
35  return false;
36  }
37  return LoadAllEvalData(filenames);
38 }
const char * string() const
Definition: strngs.cpp:196
bool LoadAllEvalData(const STRING &filenames_file)
Definition: lstmtester.cpp:30
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
bool LoadFileLinesToStrings(const STRING &filename, GenericVector< STRING > *lines)

◆ LoadAllEvalData() [2/2]

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

Definition at line 43 of file lstmtester.cpp.

43  {
44  test_data_.Clear();
45  bool result = test_data_.LoadDocuments(filenames, CS_SEQUENTIAL, nullptr);
46  total_pages_ = test_data_.TotalPages();
47  return result;
48 }
bool LoadDocuments(const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, FileReader reader)
Definition: imagedata.cpp:572

◆ RunEvalAsync()

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

Definition at line 52 of file lstmtester.cpp.

54  {
55  STRING result;
56  if (total_pages_ == 0) {
57  result.add_str_int("No test data at iteration", iteration);
58  return result;
59  }
60  if (!LockIfNotRunning()) {
61  result.add_str_int("Previous test incomplete, skipping test at iteration",
62  iteration);
63  return result;
64  }
65  // Save the args.
66  STRING prev_result = test_result_;
67  test_result_ = "";
68  if (training_errors != nullptr) {
69  test_iteration_ = iteration;
70  test_training_errors_ = training_errors;
71  test_model_mgr_ = model_mgr;
72  test_training_stage_ = training_stage;
73  SVSync::StartThread(&LSTMTester::ThreadFunc, this);
74  } else {
75  UnlockRunning();
76  }
77  return prev_result;
78 }
static void StartThread(void *(*func)(void *), void *arg)
Create new thread.
Definition: svutil.cpp:87
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
Definition: strngs.h:45

◆ RunEvalSync()

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

Definition at line 82 of file lstmtester.cpp.

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

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