tesseract  5.0.0-alpha-619-ge9db
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 #ifdef GOOGLE_TESSERACT
19 #include "base/commandlineflags.h"
20 #endif
21 #include "commontraining.h"
23 #include "lstmtester.h"
24 #include <tesseract/strngs.h>
25 #include "tprintf.h"
26 
27 static STRING_PARAM_FLAG(model, "", "Name of model file (training or recognition)");
28 static STRING_PARAM_FLAG(traineddata, "",
29  "If model is a training checkpoint, then traineddata must "
30  "be the traineddata file that was given to the trainer");
31 static STRING_PARAM_FLAG(eval_listfile, "",
32  "File listing sample files in lstmf training format.");
33 static INT_PARAM_FLAG(max_image_MB, 2000, "Max memory to use for images.");
34 static INT_PARAM_FLAG(verbosity, 1,
35  "Amount of diagnosting information to output (0-2).");
36 
37 int main(int argc, char **argv) {
38  tesseract::CheckSharedLibraryVersion();
39  ParseArguments(&argc, &argv);
40  if (FLAGS_model.empty()) {
41  tprintf("Must provide a --model!\n");
42  return 1;
43  }
44  if (FLAGS_eval_listfile.empty()) {
45  tprintf("Must provide a --eval_listfile!\n");
46  return 1;
47  }
49  if (!mgr.Init(FLAGS_model.c_str())) {
50  if (FLAGS_traineddata.empty()) {
51  tprintf("Must supply --traineddata to eval a training checkpoint!\n");
52  return 1;
53  }
54  tprintf("%s is not a recognition model, trying training checkpoint...\n",
55  FLAGS_model.c_str());
56  if (!mgr.Init(FLAGS_traineddata.c_str())) {
57  tprintf("Failed to load language model from %s!\n",
58  FLAGS_traineddata.c_str());
59  return 1;
60  }
61  GenericVector<char> model_data;
62  if (!tesseract::LoadDataFromFile(FLAGS_model.c_str(), &model_data)) {
63  tprintf("Failed to load model from: %s\n", FLAGS_model.c_str());
64  return 1;
65  }
66  mgr.OverwriteEntry(tesseract::TESSDATA_LSTM, &model_data[0],
67  model_data.size());
68  }
69  tesseract::LSTMTester tester(static_cast<int64_t>(FLAGS_max_image_MB) *
70  1048576);
71  if (!tester.LoadAllEvalData(FLAGS_eval_listfile.c_str())) {
72  tprintf("Failed to load eval data from: %s\n", FLAGS_eval_listfile.c_str());
73  return 1;
74  }
75  double errs = 0.0;
76  STRING result =
77  tester.RunEvalSync(0, &errs, mgr,
78  /*training_stage (irrelevant)*/ 0, FLAGS_verbosity);
79  tprintf("%s\n", result.c_str());
80  return 0;
81 } /* main */
tesseract::LSTMTester
Definition: lstmtester.h:28
strngs.h
INT_PARAM_FLAG
#define INT_PARAM_FLAG(name, val, comment)
Definition: commandlineflags.h:25
commontraining.h
tesseract::TessdataManager
Definition: tessdatamanager.h:126
STRING_PARAM_FLAG
#define STRING_PARAM_FLAG(name, val, comment)
Definition: commandlineflags.h:37
tesseract::LoadDataFromFile
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
Definition: genericvector.h:341
STRING
Definition: strngs.h:45
lstmtester.h
tesseract::TessdataManager::OverwriteEntry
void OverwriteEntry(TessdataType type, const char *data, int size)
Definition: tessdatamanager.cpp:145
genericvector.h
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
main
int main(int argc, char **argv)
Definition: lstmeval.cpp:37
tprintf.h
GenericVector< char >
ParseArguments
void ParseArguments(int *argc, char ***argv)
Definition: commontraining.cpp:122
tesseract::TessdataManager::Init
bool Init(const char *data_file_name)
Definition: tessdatamanager.cpp:97
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
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::TESSDATA_LSTM
Definition: tessdatamanager.h:74
GenericVector::size
int size() const
Definition: genericvector.h:71
tesseract::LSTMTester::LoadAllEvalData
bool LoadAllEvalData(const STRING &filenames_file)
Definition: lstmtester.cpp:31