19 #ifdef GOOGLE_TESSERACT 20 #include "base/commandlineflags.h" 30 INT_PARAM_FLAG(debug_interval, 0,
"How often to display the alignment.");
34 "How many imperfect samples between perfect ones.");
40 INT_PARAM_FLAG(max_image_MB, 6000,
"Max memory to use for images.");
44 "File listing training files in lstmf training format.");
46 "File listing eval files in lstmf training format.");
48 "Just convert the training model to a runtime model.");
50 "Convert the recognition model to an integer model.");
52 "Use the training files sequentially instead of round-robin.");
53 INT_PARAM_FLAG(append_index, -1,
"Index in continue_from Network at which to" 54 " attach the new network defined by net_spec");
56 "Get info on distribution of weight values");
57 INT_PARAM_FLAG(max_iterations, 0,
"If set, exit after this many iterations");
59 "Combined Dawgs/Unicharset/Recoder for language model");
61 "When changing the character set, this specifies the old" 62 " character set that is to be replaced");
64 "Train OSD and randomly turn training samples upside-down");
73 int main(
int argc,
char **argv) {
76 if (FLAGS_model_output.empty()) {
77 tprintf(
"Must provide a --model_output!\n");
80 if (FLAGS_traineddata.empty()) {
81 tprintf(
"Must provide a --traineddata see training wiki\n");
85 for (
int i = 0; i < model_output.
length(); ++i) {
86 if (model_output[i] ==
'[' || model_output[i] ==
']')
87 model_output[i] =
'-';
88 if (model_output[i] ==
'(' || model_output[i] ==
')')
89 model_output[i] =
'_';
92 STRING checkpoint_file = FLAGS_model_output.
c_str();
93 checkpoint_file +=
"_checkpoint";
94 STRING checkpoint_bak = checkpoint_file +
".bak";
96 nullptr,
nullptr,
nullptr,
nullptr, FLAGS_model_output.c_str(),
97 checkpoint_file.
c_str(), FLAGS_debug_interval,
98 static_cast<inT64>(FLAGS_max_image_MB) * 1048576);
103 if (FLAGS_stop_training || FLAGS_debug_network) {
105 tprintf(
"Failed to read continue from: %s\n",
106 FLAGS_continue_from.c_str());
109 if (FLAGS_debug_network) {
114 tprintf(
"Failed to write recognition model : %s\n",
115 FLAGS_model_output.c_str());
122 if (FLAGS_train_listfile.empty()) {
123 tprintf(
"Must supply a list of training filenames! --train_listfile\n");
129 tprintf(
"Failed to load list of training filenames from %s\n",
130 FLAGS_train_listfile.c_str());
137 tprintf(
"Successfully restored trainer from %s\n",
138 checkpoint_file.
string());
140 if (!FLAGS_continue_from.empty()) {
143 FLAGS_append_index >= 0
144 ? FLAGS_continue_from.c_str()
145 : FLAGS_old_traineddata.c_str())) {
146 tprintf(
"Failed to continue from: %s\n", FLAGS_continue_from.c_str());
149 tprintf(
"Continuing from %s\n", FLAGS_continue_from.c_str());
152 if (FLAGS_continue_from.empty() || FLAGS_append_index >= 0) {
153 if (FLAGS_append_index >= 0) {
154 tprintf(
"Appending a new network to an old one!!");
155 if (FLAGS_continue_from.empty()) {
156 tprintf(
"Must set --continue_from for appending!\n");
161 if (!trainer.
InitNetwork(FLAGS_net_spec.c_str(), FLAGS_append_index,
162 FLAGS_net_mode, FLAGS_weight_range,
163 FLAGS_learning_rate, FLAGS_momentum,
165 tprintf(
"Failed to create network from spec: %s\n",
166 FLAGS_net_spec.c_str());
173 FLAGS_sequential_training
176 FLAGS_randomly_rotate)) {
177 tprintf(
"Load of images failed!!\n");
184 if (!FLAGS_eval_listfile.empty()) {
186 tprintf(
"Failed to load eval data from: %s\n",
187 FLAGS_eval_listfile.c_str());
197 iteration < target_iteration;
206 FLAGS_max_iterations == 0));
207 delete tester_callback;
int main(int argc, char **argv)
bool TryLoadingCheckpoint(const char *filename, const char *old_traineddata)
const int kNumPagesPerBatch
void set_perfect_delay(int delay)
INT_PARAM_FLAG(debug_interval, 0, "How often to display the alignment.")
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
DOUBLE_PARAM_FLAG(target_error_rate, 0.01, "Final error rate in percent.")
bool InitNetwork(const STRING &network_spec, int append_index, int net_flags, float weight_range, float learning_rate, float momentum, float adam_beta)
bool SaveTraineddata(const STRING &filename)
int training_iteration() const
BOOL_PARAM_FLAG(stop_training, false, "Just convert the training model to a runtime model.")
bool MaintainCheckpoints(TestCallback tester, STRING *log_msg)
const char * string() const
const ImageData * TrainOnLine(LSTMTrainer *samples_trainer, bool batch)
bool LoadAllTrainingData(const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, bool randomly_rotate)
void InitCharSet(const string &traineddata_path)
double best_error_rate() const
bool LoadAllEvalData(const STRING &filenames_file)
STRING_PARAM_FLAG(net_spec, "", "Network specification")
void ParseArguments(int *argc, char ***argv)
const char * c_str() const
bool LoadFileLinesToStrings(const STRING &filename, GenericVector< STRING > *lines)
STRING RunEvalAsync(int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage)