tesseract  4.0.0-1-g2a2b
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 // Created: Wed Nov 23 12:20:06 PST 2016
6 //
7 // (C) Copyright 2016, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 
19 #ifdef GOOGLE_TESSERACT
20 #include "base/commandlineflags.h"
21 #endif
22 #include "commontraining.h"
23 #include "genericvector.h"
24 #include "lstmtester.h"
25 #include "strngs.h"
26 #include "tprintf.h"
27 
28 STRING_PARAM_FLAG(model, "", "Name of model file (training or recognition)");
29 STRING_PARAM_FLAG(traineddata, "",
30  "If model is a training checkpoint, then traineddata must "
31  "be the traineddata file that was given to the trainer");
32 STRING_PARAM_FLAG(eval_listfile, "",
33  "File listing sample files in lstmf training format.");
34 INT_PARAM_FLAG(max_image_MB, 2000, "Max memory to use for images.");
35 INT_PARAM_FLAG(verbosity, 1,
36  "Amount of diagnosting information to output (0-2).");
37 
38 int main(int argc, char **argv) {
39  tesseract::CheckSharedLibraryVersion();
40  ParseArguments(&argc, &argv);
41  if (FLAGS_model.empty()) {
42  tprintf("Must provide a --model!\n");
43  return 1;
44  }
45  if (FLAGS_eval_listfile.empty()) {
46  tprintf("Must provide a --eval_listfile!\n");
47  return 1;
48  }
50  if (!mgr.Init(FLAGS_model.c_str())) {
51  if (FLAGS_traineddata.empty()) {
52  tprintf("Must supply --traineddata to eval a training checkpoint!\n");
53  return 1;
54  }
55  tprintf("%s is not a recognition model, trying training checkpoint...\n",
56  FLAGS_model.c_str());
57  if (!mgr.Init(FLAGS_traineddata.c_str())) {
58  tprintf("Failed to load language model from %s!\n",
59  FLAGS_traineddata.c_str());
60  return 1;
61  }
62  GenericVector<char> model_data;
63  if (!tesseract::LoadDataFromFile(FLAGS_model.c_str(), &model_data)) {
64  tprintf("Failed to load model from: %s\n", FLAGS_model.c_str());
65  return 1;
66  }
67  mgr.OverwriteEntry(tesseract::TESSDATA_LSTM, &model_data[0],
68  model_data.size());
69  }
70  tesseract::LSTMTester tester(static_cast<int64_t>(FLAGS_max_image_MB) *
71  1048576);
72  if (!tester.LoadAllEvalData(FLAGS_eval_listfile.c_str())) {
73  tprintf("Failed to load eval data from: %s\n", FLAGS_eval_listfile.c_str());
74  return 1;
75  }
76  double errs = 0.0;
77  STRING result =
78  tester.RunEvalSync(0, &errs, mgr,
79  /*training_stage (irrelevant)*/ 0, FLAGS_verbosity);
80  tprintf("%s\n", result.string());
81  return 0;
82 } /* main */
int size() const
Definition: genericvector.h:71
STRING_PARAM_FLAG(model, "", "Name of model file (training or recognition)")
void OverwriteEntry(TessdataType type, const char *data, int size)
const char * string() const
Definition: strngs.cpp:196
void ParseArguments(int *argc, char ***argv)
bool LoadAllEvalData(const STRING &filenames_file)
Definition: lstmtester.cpp:30
bool Init(const char *data_file_name)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
STRING RunEvalSync(int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage, int verbosity)
Definition: lstmtester.cpp:82
INT_PARAM_FLAG(max_image_MB, 2000, "Max memory to use for images.")
Definition: strngs.h:45
int main(int argc, char **argv)
Definition: lstmeval.cpp:38
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)