tesseract  5.0.0
lstmeval.cpp
Go to the documentation of this file.
1 // File: lstmeval.cpp
3 // Description: Evaluation program 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 "commontraining.h"
19 #include "lstmtester.h"
20 #include "tprintf.h"
21 
22 using namespace tesseract;
23 
24 static STRING_PARAM_FLAG(model, "", "Name of model file (training or recognition)");
25 static STRING_PARAM_FLAG(traineddata, "",
26  "If model is a training checkpoint, then traineddata must "
27  "be the traineddata file that was given to the trainer");
28 static STRING_PARAM_FLAG(eval_listfile, "", "File listing sample files in lstmf training format.");
29 static INT_PARAM_FLAG(max_image_MB, 2000, "Max memory to use for images.");
30 static INT_PARAM_FLAG(verbosity, 1, "Amount of diagnosting information to output (0-2).");
31 
32 int main(int argc, char **argv) {
33  tesseract::CheckSharedLibraryVersion();
34  ParseArguments(&argc, &argv);
35  if (FLAGS_model.empty()) {
36  tprintf("Must provide a --model!\n");
37  return 1;
38  }
39  if (FLAGS_eval_listfile.empty()) {
40  tprintf("Must provide a --eval_listfile!\n");
41  return 1;
42  }
44  if (!mgr.Init(FLAGS_model.c_str())) {
45  if (FLAGS_traineddata.empty()) {
46  tprintf("Must supply --traineddata to eval a training checkpoint!\n");
47  return 1;
48  }
49  tprintf("%s is not a recognition model, trying training checkpoint...\n", FLAGS_model.c_str());
50  if (!mgr.Init(FLAGS_traineddata.c_str())) {
51  tprintf("Failed to load language model from %s!\n", FLAGS_traineddata.c_str());
52  return 1;
53  }
54  std::vector<char> model_data;
55  if (!tesseract::LoadDataFromFile(FLAGS_model.c_str(), &model_data)) {
56  tprintf("Failed to load model from: %s\n", FLAGS_model.c_str());
57  return 1;
58  }
59  mgr.OverwriteEntry(tesseract::TESSDATA_LSTM, &model_data[0], model_data.size());
60  }
61  tesseract::LSTMTester tester(static_cast<int64_t>(FLAGS_max_image_MB) * 1048576);
62  if (!tester.LoadAllEvalData(FLAGS_eval_listfile.c_str())) {
63  tprintf("Failed to load eval data from: %s\n", FLAGS_eval_listfile.c_str());
64  return 1;
65  }
66  double errs = 0.0;
67  std::string result = tester.RunEvalSync(0, &errs, mgr,
68  /*training_stage (irrelevant)*/ 0, FLAGS_verbosity);
69  tprintf("%s\n", result.c_str());
70  return 0;
71 } /* main */
#define STRING_PARAM_FLAG(name, val, comment)
int main(int argc, char **argv)
Definition: lstmeval.cpp:32
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void ParseArguments(int *argc, char ***argv)
INT_PARAM_FLAG(debug_level, 0, "Level of Trainer debugging")
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
void OverwriteEntry(TessdataType type, const char *data, int size)
bool Init(const char *data_file_name)
std::string RunEvalSync(int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage, int verbosity)
Definition: lstmtester.cpp:79
bool LoadAllEvalData(const char *filenames_file)
Definition: lstmtester.cpp:29