32static INT_PARAM_FLAG(debug_interval, 0,
"How often to display the alignment.");
35static INT_PARAM_FLAG(perfect_sample_delay, 0,
"How many imperfect samples between perfect ones.");
40 "Resets all stored learning rates to the value specified by --learning_rate.");
43static INT_PARAM_FLAG(max_image_MB, 6000,
"Max memory to use for images.");
47 "File listing training files in lstmf training format.");
48static STRING_PARAM_FLAG(eval_listfile,
"",
"File listing eval files in lstmf training format.");
50static BOOL_PARAM_FLAG(debug_float,
false,
"Raise error on certain float errors.");
52static BOOL_PARAM_FLAG(stop_training,
false,
"Just convert the training model to a runtime model.");
53static BOOL_PARAM_FLAG(convert_to_int,
false,
"Convert the recognition model to an integer model.");
55 "Use the training files sequentially instead of round-robin.");
57 "Index in continue_from Network at which to"
58 " attach the new network defined by net_spec");
59static BOOL_PARAM_FLAG(debug_network,
false,
"Get info on distribution of weight values");
60static INT_PARAM_FLAG(max_iterations, 0,
"If set, exit after this many iterations");
61static STRING_PARAM_FLAG(traineddata,
"",
"Combined Dawgs/Unicharset/Recoder for language model");
63 "When changing the character set, this specifies the old"
64 " character set that is to be replaced");
66 "Train OSD and randomly turn training samples upside-down");
75int main(
int argc,
char **argv) {
76 tesseract::CheckSharedLibraryVersion();
79 if (FLAGS_debug_float) {
81 feenableexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_INVALID);
84 if (FLAGS_model_output.empty()) {
85 tprintf(
"Must provide a --model_output!\n");
88 if (FLAGS_traineddata.empty()) {
89 tprintf(
"Must provide a --traineddata see training documentation\n");
94 std::string test_file = FLAGS_model_output.c_str();
95 test_file +=
"_wtest";
96 FILE *f = fopen(test_file.c_str(),
"wb");
99 if (remove(test_file.c_str()) != 0) {
100 tprintf(
"Error, failed to remove %s: %s\n", test_file.c_str(), strerror(errno));
104 tprintf(
"Error, model output cannot be written: %s\n", strerror(errno));
109 std::string checkpoint_file = FLAGS_model_output.c_str();
110 checkpoint_file +=
"_checkpoint";
111 std::string checkpoint_bak = checkpoint_file +
".bak";
113 FLAGS_debug_interval,
114 static_cast<int64_t
>(FLAGS_max_image_MB) * 1048576);
115 if (!trainer.
InitCharSet(FLAGS_traineddata.c_str())) {
116 tprintf(
"Error, failed to read %s\n", FLAGS_traineddata.c_str());
122 if (FLAGS_stop_training || FLAGS_debug_network) {
124 tprintf(
"Failed to read continue from: %s\n", FLAGS_continue_from.c_str());
127 if (FLAGS_debug_network) {
130 if (FLAGS_convert_to_int) {
134 tprintf(
"Failed to write recognition model : %s\n", FLAGS_model_output.c_str());
141 if (FLAGS_train_listfile.empty()) {
142 tprintf(
"Must supply a list of training filenames! --train_listfile\n");
145 std::vector<std::string> filenames;
147 tprintf(
"Failed to load list of training filenames from %s\n", FLAGS_train_listfile.c_str());
154 tprintf(
"Successfully restored trainer from %s\n", checkpoint_file.c_str());
156 if (!FLAGS_continue_from.empty()) {
159 FLAGS_append_index >= 0 ? FLAGS_continue_from.c_str()
160 : FLAGS_old_traineddata.c_str())) {
161 tprintf(
"Failed to continue from: %s\n", FLAGS_continue_from.c_str());
164 tprintf(
"Continuing from %s\n", FLAGS_continue_from.c_str());
165 if (FLAGS_reset_learning_rate) {
167 tprintf(
"Set learning rate to %f\n",
static_cast<float>(FLAGS_learning_rate));
171 if (FLAGS_continue_from.empty() || FLAGS_append_index >= 0) {
172 if (FLAGS_append_index >= 0) {
173 tprintf(
"Appending a new network to an old one!!");
174 if (FLAGS_continue_from.empty()) {
175 tprintf(
"Must set --continue_from for appending!\n");
180 if (!trainer.
InitNetwork(FLAGS_net_spec.c_str(), FLAGS_append_index, FLAGS_net_mode,
181 FLAGS_weight_range, FLAGS_learning_rate, FLAGS_momentum,
183 tprintf(
"Failed to create network from spec: %s\n", FLAGS_net_spec.c_str());
192 FLAGS_randomly_rotate)) {
193 tprintf(
"Load of images failed!!\n");
199 if (!FLAGS_eval_listfile.empty()) {
200 using namespace std::placeholders;
202 tprintf(
"Failed to load eval data from: %s\n", FLAGS_eval_listfile.c_str());
208 int max_iterations = FLAGS_max_iterations;
209 if (max_iterations < 0) {
211 max_iterations = filenames.size() * (-max_iterations);
212 }
else if (max_iterations == 0) {
214 max_iterations = INT_MAX;
221 iteration < target_iteration && iteration < max_iterations;
227 tprintf(
"%s\n", log_str.c_str());
230 tprintf(
"Finished! Selected model with minimal training error rate (BCER) = %g\n",
const int kNumPagesPerBatch
#define DOUBLE_PARAM_FLAG(name, val, comment)
#define BOOL_PARAM_FLAG(name, val, comment)
#define INT_PARAM_FLAG(name, val, comment)
#define STRING_PARAM_FLAG(name, val, comment)
void tprintf(const char *format,...)
void ParseArguments(int *argc, char ***argv)
std::function< std::string(int, const double *, const TessdataManager &, int)> TestCallback
bool LoadFileLinesToStrings(const char *filename, std::vector< std::string > *lines)
int training_iteration() const
void SetLearningRate(float learning_rate)
std::string RunEvalAsync(int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage)
bool LoadAllEvalData(const char *filenames_file)
bool LoadAllTrainingData(const std::vector< std::string > &filenames, CachingStrategy cache_strategy, bool randomly_rotate)
bool InitCharSet(const std::string &traineddata_path)
bool MaintainCheckpoints(const TestCallback &tester, std::string &log_msg)
double best_error_rate() const
bool SaveTraineddata(const char *filename)
void set_perfect_delay(int delay)
bool InitNetwork(const char *network_spec, int append_index, int net_flags, float weight_range, float learning_rate, float momentum, float adam_beta)
const ImageData * TrainOnLine(LSTMTrainer *samples_trainer, bool batch)
bool TryLoadingCheckpoint(const char *filename, const char *old_traineddata)