18 #ifdef GOOGLE_TESSERACT 19 #include "base/commandlineflags.h" 30 static INT_PARAM_FLAG(debug_interval, 0,
"How often to display the alignment.");
32 static INT_PARAM_FLAG(net_mode, 192,
"Controls network behavior.");
34 "How many imperfect samples between perfect ones.");
35 static DOUBLE_PARAM_FLAG(target_error_rate, 0.01,
"Final error rate in percent.");
37 static DOUBLE_PARAM_FLAG(learning_rate, 10.0e-4,
"Weight factor for new deltas.");
40 static INT_PARAM_FLAG(max_image_MB, 6000,
"Max memory to use for images.");
42 static STRING_PARAM_FLAG(model_output,
"lstmtrain",
"Basename for output models");
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 static 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 static 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) {
74 tesseract::CheckSharedLibraryVersion();
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");
87 test_file +=
"_wtest";
88 FILE* f = fopen(test_file.
c_str(),
"wb");
91 if (
remove(test_file.
c_str()) != 0) {
92 tprintf(
"Error, failed to remove %s: %s\n",
93 test_file.
c_str(), strerror(errno));
97 tprintf(
"Error, model output cannot be written: %s\n", strerror(errno));
102 STRING checkpoint_file = FLAGS_model_output.
c_str();
103 checkpoint_file +=
"_checkpoint";
104 STRING checkpoint_bak = checkpoint_file +
".bak";
106 nullptr,
nullptr,
nullptr,
nullptr, FLAGS_model_output.c_str(),
107 checkpoint_file.
c_str(), FLAGS_debug_interval,
108 static_cast<int64_t
>(FLAGS_max_image_MB) * 1048576);
113 if (FLAGS_stop_training || FLAGS_debug_network) {
115 tprintf(
"Failed to read continue from: %s\n",
116 FLAGS_continue_from.c_str());
119 if (FLAGS_debug_network) {
124 tprintf(
"Failed to write recognition model : %s\n",
125 FLAGS_model_output.c_str());
132 if (FLAGS_train_listfile.empty()) {
133 tprintf(
"Must supply a list of training filenames! --train_listfile\n");
139 tprintf(
"Failed to load list of training filenames from %s\n",
140 FLAGS_train_listfile.c_str());
147 tprintf(
"Successfully restored trainer from %s\n",
148 checkpoint_file.
string());
150 if (!FLAGS_continue_from.empty()) {
153 FLAGS_append_index >= 0
154 ? FLAGS_continue_from.c_str()
155 : FLAGS_old_traineddata.c_str())) {
156 tprintf(
"Failed to continue from: %s\n", FLAGS_continue_from.c_str());
159 tprintf(
"Continuing from %s\n", FLAGS_continue_from.c_str());
162 if (FLAGS_continue_from.empty() || FLAGS_append_index >= 0) {
163 if (FLAGS_append_index >= 0) {
164 tprintf(
"Appending a new network to an old one!!");
165 if (FLAGS_continue_from.empty()) {
166 tprintf(
"Must set --continue_from for appending!\n");
171 if (!trainer.
InitNetwork(FLAGS_net_spec.c_str(), FLAGS_append_index,
172 FLAGS_net_mode, FLAGS_weight_range,
173 FLAGS_learning_rate, FLAGS_momentum,
175 tprintf(
"Failed to create network from spec: %s\n",
176 FLAGS_net_spec.c_str());
183 FLAGS_sequential_training
186 FLAGS_randomly_rotate)) {
187 tprintf(
"Load of images failed!!\n");
194 if (!FLAGS_eval_listfile.empty()) {
196 tprintf(
"Failed to load eval data from: %s\n",
197 FLAGS_eval_listfile.c_str());
207 iteration < target_iteration &&
208 (iteration < FLAGS_max_iterations || FLAGS_max_iterations == 0);
217 FLAGS_max_iterations == 0));
218 delete tester_callback;
#define STRING_PARAM_FLAG(name, val, comment)
bool InitNetwork(const STRING &network_spec, int append_index, int net_flags, float weight_range, float learning_rate, float momentum, float adam_beta)
bool LoadFileLinesToStrings(const STRING &filename, GenericVector< STRING > *lines)
bool SaveTraineddata(const STRING &filename)
const int kNumPagesPerBatch
#define DOUBLE_PARAM_FLAG(name, val, comment)
void InitCharSet(const std::string &traineddata_path)
bool TryLoadingCheckpoint(const char *filename, const char *old_traineddata)
STRING RunEvalAsync(int iteration, const double *training_errors, const TessdataManager &model_mgr, int training_stage)
#define BOOL_PARAM_FLAG(name, val, comment)
double best_error_rate() const
const char * string() const
DLLSYM void tprintf(const char *format,...)
bool LoadAllTrainingData(const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, bool randomly_rotate)
void ParseArguments(int *argc, char ***argv)
bool LoadAllEvalData(const STRING &filenames_file)
int training_iteration() const
#define INT_PARAM_FLAG(name, val, comment)
_ConstTessMemberResultCallback_5_0< false, R, T1, P1, P2, P3, P4, P5 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)(P1, P2, P3, P4, P5) const, typename Identity< P1 >::type p1, typename Identity< P2 >::type p2, typename Identity< P3 >::type p3, typename Identity< P4 >::type p4, typename Identity< P5 >::type p5)
void set_perfect_delay(int delay)
int main(int argc, char **argv)
bool MaintainCheckpoints(TestCallback tester, STRING *log_msg)
const ImageData * TrainOnLine(LSTMTrainer *samples_trainer, bool batch)
const char * c_str() const