tesseract v5.3.3.20231005
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 25 of file lstmtester.cpp.

25: test_data_(max_memory) {}

Member Function Documentation

◆ LoadAllEvalData() [1/2]

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

Definition at line 30 of file lstmtester.cpp.

30 {
31 std::vector<std::string> filenames;
32 if (!LoadFileLinesToStrings(filenames_file, &filenames)) {
33 tprintf("Failed to load list of eval filenames from %s\n", filenames_file);
34 return false;
35 }
36 return LoadAllEvalData(filenames);
37}
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:30

◆ LoadAllEvalData() [2/2]

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

Definition at line 42 of file lstmtester.cpp.

42 {
43 test_data_.Clear();
44 bool result = test_data_.LoadDocuments(filenames, CS_SEQUENTIAL, nullptr);
45 total_pages_ = test_data_.TotalPages();
46 return result;
47}
@ 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 51 of file lstmtester.cpp.

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

◆ RunEvalSync()

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

Definition at line 80 of file lstmtester.cpp.

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

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