tesseract v5.3.3.20231005
lstmeval.cpp
Go to the documentation of this file.
1
2// 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
22using namespace tesseract;
23
24static STRING_PARAM_FLAG(model, "", "Name of model file (training or recognition)");
25static 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");
28static STRING_PARAM_FLAG(eval_listfile, "", "File listing sample files in lstmf training format.");
29static INT_PARAM_FLAG(max_image_MB, 2000, "Max memory to use for images.");
30static INT_PARAM_FLAG(verbosity, 1, "Amount of diagnosting information to output (0-2).");
31
32int 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 EXIT_FAILURE;
38 }
39 if (FLAGS_eval_listfile.empty()) {
40 tprintf("Must provide a --eval_listfile!\n");
41 return EXIT_FAILURE;
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 EXIT_FAILURE;
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 EXIT_FAILURE;
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 EXIT_FAILURE;
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 EXIT_FAILURE;
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 EXIT_SUCCESS;
71} /* main */
#define INT_PARAM_FLAG(name, val, comment)
#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)
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
int verbosity
Definition: upload.py:74
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:80
bool LoadAllEvalData(const char *filenames_file)
Definition: lstmtester.cpp:30