tesseract  5.0.0
lstmtester.h
Go to the documentation of this file.
1 // File: lstmtester.h
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 #ifndef TESSERACT_TRAINING_LSTMTESTER_H_
19 #define TESSERACT_TRAINING_LSTMTESTER_H_
20 
21 #include "export.h"
22 
23 #include "lstmtrainer.h"
24 
25 #include <mutex>
26 #include <string>
27 #include <vector>
28 
29 namespace tesseract {
30 
31 class TESS_UNICHARSET_TRAINING_API LSTMTester {
32 public:
33  LSTMTester(int64_t max_memory);
34 
35  // Loads a set of lstmf files that were created using the lstm.train config to
36  // tesseract into memory ready for testing. Returns false if nothing was
37  // loaded. The arg is a filename of a file that lists the filenames, with one
38  // name per line. Conveniently, tesstrain.py generates such a file, along
39  // with the files themselves.
40  bool LoadAllEvalData(const char *filenames_file);
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.
44  bool LoadAllEvalData(const std::vector<std::string> &filenames);
45 
46  // Runs an evaluation asynchronously on the stored eval data and returns a
47  // string describing the results of the previous test. Args match TestCallback
48  // declared in lstmtrainer.h:
49  // iteration: Current learning iteration number.
50  // training_errors: If not null, is an array of size ET_COUNT, indexed by
51  // the ErrorTypes enum and indicates the current errors measured by the
52  // trainer, and this is a serious request to run an evaluation. If null,
53  // then the caller is just polling for the results of the previous eval.
54  // model_data: is the model to evaluate, which should be a serialized
55  // LSTMTrainer.
56  // training_stage: an arbitrary number on the progress of training.
57  std::string RunEvalAsync(int iteration, const double *training_errors,
58  const TessdataManager &model_mgr, int training_stage);
59  // Runs an evaluation synchronously on the stored eval data and returns a
60  // string describing the results. Args as RunEvalAsync, except verbosity,
61  // which outputs errors, if 1, or all results if 2.
62  std::string RunEvalSync(int iteration, const double *training_errors, const TessdataManager &model_mgr,
63  int training_stage, int verbosity);
64 
65 private:
66  // Helper thread function for RunEvalAsync.
67  // LockIfNotRunning must have returned true before calling ThreadFunc, and
68  // it will call UnlockRunning to release the lock after RunEvalSync completes.
69  void ThreadFunc();
70  // Returns true if there is currently nothing running, and takes the lock
71  // if there is nothing running.
72  bool LockIfNotRunning();
73  // Releases the running lock.
74  void UnlockRunning();
75 
76  // The data to test with.
77  DocumentCache test_data_;
78  int total_pages_ = 0;
79  // Flag that indicates an asynchronous test is currently running.
80  // Protected by running_mutex_.
81  bool async_running_ = false;
82  std::mutex running_mutex_;
83  // Stored copies of the args for use while running asynchronously.
84  int test_iteration_ = 0;
85  const double *test_training_errors_ = nullptr;
86  TessdataManager test_model_mgr_;
87  int test_training_stage_ = 0;
88  std::string test_result_;
89 };
90 
91 } // namespace tesseract
92 
93 #endif // TESSERACT_TRAINING_LSTMTESTER_H_