tesseract  5.0.0-alpha-619-ge9db
lstmtester.cpp
Go to the documentation of this file.
1 // File: lstmtester.cpp
3 // Description: Top-level line evaluation class for LSTM-based networks.
4 // Author: Ray Smith
5 //
6 // (C) Copyright 2016, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 
18 #include <thread> // for std::thread
19 #include "fileio.h" // for LoadFileLinesToStrings
20 #include "lstmtester.h"
22 
23 namespace tesseract {
24 
25 LSTMTester::LSTMTester(int64_t max_memory)
26  : test_data_(max_memory) {}
27 
28 // Loads a set of lstmf files that were created using the lstm.train config to
29 // tesseract into memory ready for testing. Returns false if nothing was
30 // loaded. The arg is a filename of a file that lists the filenames.
31 bool LSTMTester::LoadAllEvalData(const STRING& filenames_file) {
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 }
40 
41 // Loads a set of lstmf files that were created using the lstm.train config to
42 // tesseract into memory ready for testing. Returns false if nothing was
43 // loaded.
45  test_data_.Clear();
46  bool result = test_data_.LoadDocuments(filenames, CS_SEQUENTIAL, nullptr);
47  total_pages_ = test_data_.TotalPages();
48  return result;
49 }
50 
51 // Runs an evaluation asynchronously on the stored data and returns a string
52 // describing the results of the previous test.
53 STRING LSTMTester::RunEvalAsync(int iteration, const double* training_errors,
54  const TessdataManager& model_mgr,
55  int training_stage) {
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 }
81 
82 // Runs an evaluation synchronously on the stored data and returns a string
83 // describing the results.
84 STRING LSTMTester::RunEvalSync(int iteration, const double* training_errors,
85  const TessdataManager& model_mgr,
86  int training_stage, int verbosity) {
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 }
127 
128 // Helper thread function for RunEvalAsync.
129 // LockIfNotRunning must have returned true before calling ThreadFunc, and
130 // it will call UnlockRunning to release the lock after RunEvalSync completes.
131 void LSTMTester::ThreadFunc() {
132  test_result_ = RunEvalSync(
133  test_iteration_, test_training_errors_,
134  test_model_mgr_, test_training_stage_,
135  /*verbosity*/ 0);
136  UnlockRunning();
137 }
138 
139 // Returns true if there is currently nothing running, and takes the lock
140 // if there is nothing running.
141 bool LSTMTester::LockIfNotRunning() {
142  std::lock_guard<std::mutex> lock(running_mutex_);
143  if (async_running_) return false;
144  async_running_ = true;
145  return true;
146 }
147 
148 // Releases the running lock.
149 void LSTMTester::UnlockRunning() {
150  std::lock_guard<std::mutex> lock(running_mutex_);
151  async_running_ = false;
152 }
153 
154 } // namespace tesseract
tesseract::LSTMTrainer::PrepareForBackward
Trainability PrepareForBackward(const ImageData *trainingdata, NetworkIO *fwd_outputs, NetworkIO *targets)
Definition: lstmtrainer.cpp:770
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
tesseract::TessdataManager
Definition: tessdatamanager.h:126
tesseract::DocumentCache::LoadDocuments
bool LoadDocuments(const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, FileReader reader)
Definition: imagedata.cpp:566
STRING
Definition: strngs.h:45
tesseract::LSTMRecognizer::LabelsFromOutputs
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)
Definition: lstmrecognizer.cpp:462
lstmtester.h
tesseract::CS_SEQUENTIAL
Definition: imagedata.h:48
tesseract::ImageData
Definition: imagedata.h:104
fileio.h
tesseract::ET_WORD_RECERR
Definition: lstmtrainer.h:40
tesseract::PERFECT
Definition: lstmtrainer.h:49
genericvector.h
tesseract::LSTMRecognizer::SetIteration
void SetIteration(int iteration)
Definition: lstmrecognizer.h:142
tesseract::LSTMTrainer::NewSingleError
double NewSingleError(ErrorTypes type) const
Definition: lstmtrainer.h:140
tesseract::DocumentCache::GetPageBySerial
const ImageData * GetPageBySerial(int serial)
Definition: imagedata.h:343
tesseract::ImageData::transcription
const STRING & transcription() const
Definition: imagedata.h:146
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
tesseract::DocumentCache::TotalPages
int TotalPages()
Definition: imagedata.cpp:607
tesseract::TessdataManager::GetComponent
bool GetComponent(TessdataType type, TFile *fp)
Definition: tessdatamanager.cpp:216
tesseract::LoadFileLinesToStrings
bool LoadFileLinesToStrings(const char *filename, GenericVector< STRING > *lines)
Definition: fileio.h:43
tesseract::TFile
Definition: serialis.h:75
tesseract::NetworkIO
Definition: networkio.h:39
tesseract::Trainability
Trainability
Definition: lstmtrainer.h:47
tesseract
Definition: baseapi.h:65
tesseract::ET_CHAR_ERROR
Definition: lstmtrainer.h:41
tesseract::LSTMTester::RunEvalAsync
STRING RunEvalAsync(int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage)
Definition: lstmtester.cpp:53
GenericVector< STRING >
tesseract::LSTMTrainer::DeSerialize
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
Definition: lstmtrainer.cpp:440
tesseract::LSTMTrainer::InitCharSet
void InitCharSet(const std::string &traineddata_path)
Definition: lstmtrainer.h:95
tesseract::LSTMTrainer
Definition: lstmtrainer.h:79
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::LSTMTester::LSTMTester
LSTMTester(int64_t max_memory)
Definition: lstmtester.cpp:25
tesseract::LSTMTester::RunEvalSync
STRING RunEvalSync(int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage, int verbosity)
Definition: lstmtester.cpp:84
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
tesseract::LSTMRecognizer::DecodeLabels
STRING DecodeLabels(const GenericVector< int > &labels)
Definition: lstmrecognizer.cpp:334