tesseract
5.0.0-alpha-619-ge9db
|
#include <lstmtrainer.h>
|
| LSTMTrainer () |
|
| LSTMTrainer (const char *model_base, const char *checkpoint_name, int debug_interval, int64_t max_memory) |
|
virtual | ~LSTMTrainer () |
|
bool | TryLoadingCheckpoint (const char *filename, const char *old_traineddata) |
|
void | InitCharSet (const std::string &traineddata_path) |
|
void | InitCharSet (const TessdataManager &mgr) |
|
bool | InitNetwork (const STRING &network_spec, int append_index, int net_flags, float weight_range, float learning_rate, float momentum, float adam_beta) |
|
int | InitTensorFlowNetwork (const std::string &tf_proto) |
|
void | InitIterations () |
|
double | ActivationError () const |
|
double | CharError () const |
|
const double * | error_rates () const |
|
double | best_error_rate () const |
|
int | best_iteration () const |
|
int | learning_iteration () const |
|
int32_t | improvement_steps () const |
|
void | set_perfect_delay (int delay) |
|
const GenericVector< char > & | best_trainer () const |
|
double | NewSingleError (ErrorTypes type) const |
|
double | LastSingleError (ErrorTypes type) const |
|
const DocumentCache & | training_data () const |
|
DocumentCache * | mutable_training_data () |
|
Trainability | GridSearchDictParams (const ImageData *trainingdata, int iteration, double min_dict_ratio, double dict_ratio_step, double max_dict_ratio, double min_cert_offset, double cert_offset_step, double max_cert_offset, STRING *results) |
|
void | DebugNetwork () |
|
bool | LoadAllTrainingData (const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, bool randomly_rotate) |
|
bool | MaintainCheckpoints (TestCallback tester, STRING *log_msg) |
|
bool | MaintainCheckpointsSpecific (int iteration, const GenericVector< char > *train_model, const GenericVector< char > *rec_model, TestCallback tester, STRING *log_msg) |
|
void | PrepareLogMsg (STRING *log_msg) const |
|
void | LogIterations (const char *intro_str, STRING *log_msg) const |
|
bool | TransitionTrainingStage (float error_threshold) |
|
int | CurrentTrainingStage () const |
|
bool | Serialize (SerializeAmount serialize_amount, const TessdataManager *mgr, TFile *fp) const |
|
bool | DeSerialize (const TessdataManager *mgr, TFile *fp) |
|
void | StartSubtrainer (STRING *log_msg) |
|
SubTrainerResult | UpdateSubtrainer (STRING *log_msg) |
|
void | ReduceLearningRates (LSTMTrainer *samples_trainer, STRING *log_msg) |
|
int | ReduceLayerLearningRates (double factor, int num_samples, LSTMTrainer *samples_trainer) |
|
bool | EncodeString (const STRING &str, GenericVector< int > *labels) const |
|
const ImageData * | TrainOnLine (LSTMTrainer *samples_trainer, bool batch) |
|
Trainability | TrainOnLine (const ImageData *trainingdata, bool batch) |
|
Trainability | PrepareForBackward (const ImageData *trainingdata, NetworkIO *fwd_outputs, NetworkIO *targets) |
|
bool | SaveTrainingDump (SerializeAmount serialize_amount, const LSTMTrainer *trainer, GenericVector< char > *data) const |
|
bool | ReadTrainingDump (const GenericVector< char > &data, LSTMTrainer *trainer) const |
|
bool | ReadSizedTrainingDump (const char *data, int size, LSTMTrainer *trainer) const |
|
bool | ReadLocalTrainingDump (const TessdataManager *mgr, const char *data, int size) |
|
void | SetupCheckpointInfo () |
|
bool | SaveTraineddata (const STRING &filename) |
|
void | SaveRecognitionDump (GenericVector< char > *data) const |
|
STRING | DumpFilename () const |
|
void | FillErrorBuffer (double new_error, ErrorTypes type) |
|
std::vector< int > | MapRecoder (const UNICHARSET &old_chset, const UnicharCompress &old_recoder) const |
|
| LSTMRecognizer () |
|
| LSTMRecognizer (const STRING language_data_path_prefix) |
|
| ~LSTMRecognizer () |
|
int | NumOutputs () const |
|
int | training_iteration () const |
|
int | sample_iteration () const |
|
double | learning_rate () const |
|
LossType | OutputLossType () const |
|
bool | SimpleTextOutput () const |
|
bool | IsIntMode () const |
|
bool | IsRecoding () const |
|
bool | IsTensorFlow () const |
|
GenericVector< STRING > | EnumerateLayers () const |
|
Network * | GetLayer (const STRING &id) const |
|
float | GetLayerLearningRate (const STRING &id) const |
|
void | ScaleLearningRate (double factor) |
|
void | ScaleLayerLearningRate (const STRING &id, double factor) |
|
void | ConvertToInt () |
|
const UNICHARSET & | GetUnicharset () const |
|
UNICHARSET & | GetUnicharset () |
|
const UnicharCompress & | GetRecoder () const |
|
const Dict * | GetDict () const |
|
Dict * | GetDict () |
|
void | SetIteration (int iteration) |
|
int | NumInputs () const |
|
int | null_char () const |
|
bool | Load (const ParamsVectors *params, const char *lang, TessdataManager *mgr) |
|
bool | Serialize (const TessdataManager *mgr, TFile *fp) const |
|
bool | DeSerialize (const TessdataManager *mgr, TFile *fp) |
|
bool | LoadCharsets (const TessdataManager *mgr) |
|
bool | LoadRecoder (TFile *fp) |
|
bool | LoadDictionary (const ParamsVectors *params, const char *lang, TessdataManager *mgr) |
|
void | RecognizeLine (const ImageData &image_data, bool invert, bool debug, double worst_dict_cert, const TBOX &line_box, PointerVector< WERD_RES > *words, int lstm_choice_mode=0, int lstm_choice_amount=5) |
|
void | OutputStats (const NetworkIO &outputs, float *min_output, float *mean_output, float *sd) |
|
bool | RecognizeLine (const ImageData &image_data, bool invert, bool debug, bool re_invert, bool upside_down, float *scale_factor, NetworkIO *inputs, NetworkIO *outputs) |
|
STRING | DecodeLabels (const GenericVector< int > &labels) |
|
void | DisplayForward (const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window) |
|
void | LabelsFromOutputs (const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords) |
|
|
void | InitCharSet () |
|
void | SetNullChar () |
|
void | EmptyConstructor () |
|
bool | DebugLSTMTraining (const NetworkIO &inputs, const ImageData &trainingdata, const NetworkIO &fwd_outputs, const GenericVector< int > &truth_labels, const NetworkIO &outputs) |
|
void | DisplayTargets (const NetworkIO &targets, const char *window_name, ScrollView **window) |
|
bool | ComputeTextTargets (const NetworkIO &outputs, const GenericVector< int > &truth_labels, NetworkIO *targets) |
|
bool | ComputeCTCTargets (const GenericVector< int > &truth_labels, NetworkIO *outputs, NetworkIO *targets) |
|
double | ComputeErrorRates (const NetworkIO &deltas, double char_error, double word_error) |
|
double | ComputeRMSError (const NetworkIO &deltas) |
|
double | ComputeWinnerError (const NetworkIO &deltas) |
|
double | ComputeCharError (const GenericVector< int > &truth_str, const GenericVector< int > &ocr_str) |
|
double | ComputeWordError (STRING *truth_str, STRING *ocr_str) |
|
void | UpdateErrorBuffer (double new_error, ErrorTypes type) |
|
void | RollErrorBuffers () |
|
STRING | UpdateErrorGraph (int iteration, double error_rate, const GenericVector< char > &model_data, TestCallback tester) |
|
void | SetRandomSeed () |
|
void | DisplayLSTMOutput (const GenericVector< int > &labels, const GenericVector< int > &xcoords, int height, ScrollView *window) |
|
void | DebugActivationPath (const NetworkIO &outputs, const GenericVector< int > &labels, const GenericVector< int > &xcoords) |
|
void | DebugActivationRange (const NetworkIO &outputs, const char *label, int best_choice, int x_start, int x_end) |
|
void | LabelsViaReEncode (const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords) |
|
void | LabelsViaSimpleText (const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords) |
|
const char * | DecodeLabel (const GenericVector< int > &labels, int start, int *end, int *decoded) |
|
const char * | DecodeSingleLabel (int label) |
|
Definition at line 79 of file lstmtrainer.h.
◆ LSTMTrainer() [1/2]
tesseract::LSTMTrainer::LSTMTrainer |
( |
| ) |
|
◆ LSTMTrainer() [2/2]
tesseract::LSTMTrainer::LSTMTrainer |
( |
const char * |
model_base, |
|
|
const char * |
checkpoint_name, |
|
|
int |
debug_interval, |
|
|
int64_t |
max_memory |
|
) |
| |
◆ ~LSTMTrainer()
tesseract::LSTMTrainer::~LSTMTrainer |
( |
| ) |
|
|
virtual |
◆ ActivationError()
double tesseract::LSTMTrainer::ActivationError |
( |
| ) |
const |
|
inline |
◆ best_error_rate()
double tesseract::LSTMTrainer::best_error_rate |
( |
| ) |
const |
|
inline |
◆ best_iteration()
int tesseract::LSTMTrainer::best_iteration |
( |
| ) |
const |
|
inline |
◆ best_trainer()
const GenericVector<char>& tesseract::LSTMTrainer::best_trainer |
( |
| ) |
const |
|
inline |
◆ CharError()
double tesseract::LSTMTrainer::CharError |
( |
| ) |
const |
|
inline |
◆ ComputeCharError()
Definition at line 1167 of file lstmtrainer.cpp.
1172 for (
int i = 0; i < truth_str.
size(); ++i) {
1174 ++label_counts[truth_str[i]];
1178 for (
int i = 0; i < ocr_str.
size(); ++i) {
1180 --label_counts[ocr_str[i]];
1183 int char_errors = 0;
1184 for (
int i = 0; i < label_counts.
size(); ++i) {
1185 char_errors += abs(label_counts[i]);
1187 if (truth_size == 0) {
1188 return (char_errors == 0) ? 0.0 : 1.0;
1190 return static_cast<double>(char_errors) / truth_size;
◆ ComputeCTCTargets()
◆ ComputeErrorRates()
double tesseract::LSTMTrainer::ComputeErrorRates |
( |
const NetworkIO & |
deltas, |
|
|
double |
char_error, |
|
|
double |
word_error |
|
) |
| |
|
protected |
◆ ComputeRMSError()
double tesseract::LSTMTrainer::ComputeRMSError |
( |
const NetworkIO & |
deltas | ) |
|
|
protected |
Definition at line 1130 of file lstmtrainer.cpp.
1131 double total_error = 0.0;
1132 int width = deltas.Width();
1133 int num_classes = deltas.NumFeatures();
1134 for (
int t = 0; t < width; ++t) {
1135 const float* class_errs = deltas.f(t);
1136 for (
int c = 0; c < num_classes; ++c) {
1137 double error = class_errs[c];
1138 total_error += error * error;
1141 return sqrt(total_error / (width * num_classes));
◆ ComputeTextTargets()
Definition at line 1079 of file lstmtrainer.cpp.
1082 if (truth_labels.
size() > targets->Width()) {
1083 tprintf(
"Error: transcription %s too long to fit into target of width %d\n",
1087 for (
int i = 0; i < truth_labels.
size() && i < targets->Width(); ++i) {
1088 targets->SetActivations(i, truth_labels[i], 1.0);
1090 for (
int i = truth_labels.
size(); i < targets->Width(); ++i) {
◆ ComputeWinnerError()
double tesseract::LSTMTrainer::ComputeWinnerError |
( |
const NetworkIO & |
deltas | ) |
|
|
protected |
Definition at line 1149 of file lstmtrainer.cpp.
1151 int width = deltas.Width();
1152 int num_classes = deltas.NumFeatures();
1153 for (
int t = 0; t < width; ++t) {
1154 const float* class_errs = deltas.f(t);
1155 for (
int c = 0; c < num_classes; ++c) {
1156 float abs_delta = fabs(class_errs[c]);
1159 if (0.5 <= abs_delta)
1163 return static_cast<double>(num_errors) / width;
◆ ComputeWordError()
double tesseract::LSTMTrainer::ComputeWordError |
( |
STRING * |
truth_str, |
|
|
STRING * |
ocr_str |
|
) |
| |
|
protected |
Definition at line 1195 of file lstmtrainer.cpp.
1196 using StrMap = std::unordered_map<std::string, int, std::hash<std::string>>;
1198 truth_str->
split(
' ', &truth_words);
1199 if (truth_words.
empty())
return 0.0;
1200 ocr_str->
split(
' ', &ocr_words);
1202 for (
int i = 0; i < truth_words.
size(); ++i) {
1204 auto it = word_counts.find(truth_word);
1205 if (it == word_counts.end())
1206 word_counts.insert(std::make_pair(truth_word, 1));
1210 for (
int i = 0; i < ocr_words.
size(); ++i) {
1212 auto it = word_counts.find(ocr_word);
1213 if (it == word_counts.end())
1214 word_counts.insert(std::make_pair(ocr_word, -1));
1218 int word_recall_errs = 0;
1219 for (StrMap::const_iterator it = word_counts.begin(); it != word_counts.end();
1221 if (it->second > 0) word_recall_errs += it->second;
1223 return static_cast<double>(word_recall_errs) / truth_words.
size();
◆ CurrentTrainingStage()
int tesseract::LSTMTrainer::CurrentTrainingStage |
( |
| ) |
const |
|
inline |
◆ DebugLSTMTraining()
Definition at line 1005 of file lstmtrainer.cpp.
1011 if (truth_text.
c_str() ==
nullptr || truth_text.
length() <= 0) {
1012 tprintf(
"Empty truth string at decode time!\n");
1021 tprintf(
"Iteration %d: GROUND TRUTH : %s\n",
1023 if (truth_text != text) {
1024 tprintf(
"Iteration %d: ALIGNED TRUTH : %s\n",
1028 tprintf(
"TRAINING activation path for truth string %s\n",
1029 truth_text.
c_str());
◆ DebugNetwork()
void tesseract::LSTMTrainer::DebugNetwork |
( |
| ) |
|
◆ DeSerialize()
Definition at line 440 of file lstmtrainer.cpp.
446 tprintf(
"Warning: LSTMTrainer deserialized an LSTMRecognizer!\n");
455 if (!error_buffer.DeSerialize(fp))
return false;
460 if (!fp->DeSerialize(&amount))
return false;
461 if (amount ==
LIGHT)
return true;
475 if (sub_data.
empty()) {
◆ DisplayTargets()
void tesseract::LSTMTrainer::DisplayTargets |
( |
const NetworkIO & |
targets, |
|
|
const char * |
window_name, |
|
|
ScrollView ** |
window |
|
) |
| |
|
protected |
Definition at line 1042 of file lstmtrainer.cpp.
1044 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics.
1045 int width = targets.Width();
1046 int num_features = targets.NumFeatures();
1049 for (
int c = 0; c < num_features; ++c) {
1051 (*window)->Pen(static_cast<ScrollView::Color>(color));
1053 for (
int t = 0; t < width; ++t) {
1054 double target = targets.f(t)[c];
1058 (*window)->SetCursor(t - 1, 0);
1061 (*window)->DrawTo(t, target);
1062 }
else if (start_t >= 0) {
1063 (*window)->DrawTo(t, 0);
1064 (*window)->DrawTo(start_t - 1, 0);
1069 (*window)->DrawTo(width, 0);
1070 (*window)->DrawTo(start_t - 1, 0);
1073 (*window)->Update();
1074 #endif // GRAPHICS_DISABLED
◆ DumpFilename()
STRING tesseract::LSTMTrainer::DumpFilename |
( |
| ) |
const |
◆ EmptyConstructor()
void tesseract::LSTMTrainer::EmptyConstructor |
( |
| ) |
|
|
protected |
◆ EncodeString() [1/2]
Definition at line 690 of file lstmtrainer.cpp.
694 tprintf(
"Empty truth string!\n");
702 if (unicharset.
encode_string(cleaned.c_str(),
true, &internal_labels,
nullptr,
705 for (
int i = 0; i < internal_labels.
size(); ++i) {
706 if (recoder !=
nullptr) {
709 int len = recoder->EncodeUnichar(internal_labels[i], &code);
711 for (
int j = 0; j < len; ++j) {
725 if (success)
return true;
727 tprintf(
"Encoding of string failed! Failure bytes:");
728 while (err_index < cleaned.size()) {
729 tprintf(
" %x", cleaned[err_index++] & 0xff);
◆ EncodeString() [2/2]
bool tesseract::LSTMTrainer::EncodeString |
( |
const STRING & |
str, |
|
|
GenericVector< int > * |
labels |
|
) |
| const |
|
inline |
◆ error_rates()
const double* tesseract::LSTMTrainer::error_rates |
( |
| ) |
const |
|
inline |
◆ FillErrorBuffer()
void tesseract::LSTMTrainer::FillErrorBuffer |
( |
double |
new_error, |
|
|
ErrorTypes |
type |
|
) |
| |
◆ GridSearchDictParams()
Trainability tesseract::LSTMTrainer::GridSearchDictParams |
( |
const ImageData * |
trainingdata, |
|
|
int |
iteration, |
|
|
double |
min_dict_ratio, |
|
|
double |
dict_ratio_step, |
|
|
double |
max_dict_ratio, |
|
|
double |
min_cert_offset, |
|
|
double |
cert_offset_step, |
|
|
double |
max_cert_offset, |
|
|
STRING * |
results |
|
) |
| |
Definition at line 215 of file lstmtrainer.cpp.
220 NetworkIO fwd_outputs, targets;
233 base_search.ExtractBestPathAsLabels(&ocr_labels, &xcoords);
240 for (
double r = min_dict_ratio; r < max_dict_ratio; r += dict_ratio_step) {
241 for (
double c = min_cert_offset; c < max_cert_offset;
242 c += cert_offset_step) {
244 search.ExtractBestPathAsLabels(&ocr_labels, &xcoords);
249 if ((r == min_dict_ratio && c == min_cert_offset) ||
250 !std::isfinite(word_error)) {
253 tprintf(
"r=%g, c=%g, truth=%s, ocr=%s, wderr=%g, truth[0]=%d\n", r, c,
254 t.
c_str(), o.
c_str(), word_error, truth_labels[0]);
◆ improvement_steps()
int32_t tesseract::LSTMTrainer::improvement_steps |
( |
| ) |
const |
|
inline |
◆ InitCharSet() [1/3]
void tesseract::LSTMTrainer::InitCharSet |
( |
| ) |
|
|
protected |
Definition at line 968 of file lstmtrainer.cpp.
974 "Must provide a traineddata containing lstm_unicharset and"
975 " lstm_recoder!\n" !=
nullptr);
◆ InitCharSet() [2/3]
void tesseract::LSTMTrainer::InitCharSet |
( |
const std::string & |
traineddata_path | ) |
|
|
inline |
◆ InitCharSet() [3/3]
◆ InitIterations()
void tesseract::LSTMTrainer::InitIterations |
( |
| ) |
|
◆ InitNetwork()
bool tesseract::LSTMTrainer::InitNetwork |
( |
const STRING & |
network_spec, |
|
|
int |
append_index, |
|
|
int |
net_flags, |
|
|
float |
weight_range, |
|
|
float |
learning_rate, |
|
|
float |
momentum, |
|
|
float |
adam_beta |
|
) |
| |
Definition at line 146 of file lstmtrainer.cpp.
156 append_index, net_flags, weight_range,
161 tprintf(
"Built network:%s from request %s\n",
164 "Training parameters:\n Debug interval = %d,"
165 " weights = %g, learning rate = %g, momentum=%g\n",
◆ InitTensorFlowNetwork()
int tesseract::LSTMTrainer::InitTensorFlowNetwork |
( |
const std::string & |
tf_proto | ) |
|
◆ LastSingleError()
double tesseract::LSTMTrainer::LastSingleError |
( |
ErrorTypes |
type | ) |
const |
|
inline |
◆ learning_iteration()
int tesseract::LSTMTrainer::learning_iteration |
( |
| ) |
const |
|
inline |
◆ LoadAllTrainingData()
◆ LogIterations()
void tesseract::LSTMTrainer::LogIterations |
( |
const char * |
intro_str, |
|
|
STRING * |
log_msg |
|
) |
| const |
◆ MaintainCheckpoints()
bool tesseract::LSTMTrainer::MaintainCheckpoints |
( |
TestCallback |
tester, |
|
|
STRING * |
log_msg |
|
) |
| |
Definition at line 285 of file lstmtrainer.cpp.
311 *log_msg +=
UpdateErrorGraph(iteration, error_rate, rec_model_data, tester);
324 *log_msg +=
" failed to write best model:";
326 *log_msg +=
" wrote best model:";
329 *log_msg += best_model_name;
334 *log_msg +=
UpdateErrorGraph(iteration, error_rate, rec_model_data, tester);
338 *log_msg +=
"\nDivergence! ";
355 result = sub_trainer_result !=
STR_NONE;
362 *log_msg +=
" failed to write checkpoint.";
364 *log_msg +=
" wrote checkpoint.";
◆ MaintainCheckpointsSpecific()
◆ MapRecoder()
std::vector< int > tesseract::LSTMTrainer::MapRecoder |
( |
const UNICHARSET & |
old_chset, |
|
|
const UnicharCompress & |
old_recoder |
|
) |
| const |
Definition at line 933 of file lstmtrainer.cpp.
937 std::vector<int> code_map(num_new_codes, -1);
938 for (
int c = 0; c < num_new_codes; ++c) {
942 for (
int uid = 0; uid <= num_new_unichars; ++uid) {
946 while (code_index < length && codes(code_index) != c) ++code_index;
947 if (code_index == length)
continue;
950 uid < num_new_unichars
952 : old_chset.
size() - 1;
953 if (old_uid == INVALID_UNICHAR_ID)
continue;
955 RecodedCharID old_codes;
956 if (code_index < old_recoder.EncodeUnichar(old_uid, &old_codes)) {
957 old_code = old_codes(code_index);
961 code_map[c] = old_code;
◆ mutable_training_data()
◆ NewSingleError()
double tesseract::LSTMTrainer::NewSingleError |
( |
ErrorTypes |
type | ) |
const |
|
inline |
◆ PrepareForBackward()
Definition at line 770 of file lstmtrainer.cpp.
773 if (trainingdata ==
nullptr) {
774 tprintf(
"Null trainingdata.\n");
781 if (!
EncodeString(trainingdata->transcription(), &truth_labels)) {
782 tprintf(
"Can't encode transcription: '%s' in language '%s'\n",
783 trainingdata->transcription().c_str(),
784 trainingdata->language().c_str());
787 bool upside_down =
false;
797 for (
int c = 0; c < truth_labels.
size(); ++c) {
805 while (w < truth_labels.
size() &&
808 if (w == truth_labels.
size()) {
809 tprintf(
"Blank transcription: %s\n",
810 trainingdata->transcription().c_str());
815 bool invert = trainingdata->boxes().empty();
816 if (!
RecognizeLine(*trainingdata, invert, debug, invert, upside_down,
817 &image_scale, &inputs, fwd_outputs)) {
818 tprintf(
"Image not trainable\n");
825 tprintf(
"Compute simple targets failed!\n");
828 }
else if (loss_type ==
LT_CTC) {
830 tprintf(
"Compute CTC targets failed!\n");
834 tprintf(
"Logistic outputs not implemented yet!\n");
841 if (loss_type !=
LT_CTC) {
846 tprintf(
"Input width was %d\n", inputs.Width());
851 targets->SubtractAllFromFloat(*fwd_outputs);
853 if (truth_text != ocr_text) {
854 tprintf(
"Iteration %d: BEST OCR TEXT : %s\n",
862 tprintf(
"File %s line %d %s:\n", trainingdata->imagefilename().c_str(),
863 trainingdata->page_number(), delta_error == 0.0 ?
"(Perfect)" :
"");
865 if (delta_error == 0.0)
return PERFECT;
◆ PrepareLogMsg()
void tesseract::LSTMTrainer::PrepareLogMsg |
( |
STRING * |
log_msg | ) |
const |
◆ ReadLocalTrainingDump()
bool tesseract::LSTMTrainer::ReadLocalTrainingDump |
( |
const TessdataManager * |
mgr, |
|
|
const char * |
data, |
|
|
int |
size |
|
) |
| |
Definition at line 883 of file lstmtrainer.cpp.
886 tprintf(
"Warning: data size is 0 in LSTMTrainer::ReadLocalTrainingDump\n");
◆ ReadSizedTrainingDump()
bool tesseract::LSTMTrainer::ReadSizedTrainingDump |
( |
const char * |
data, |
|
|
int |
size, |
|
|
LSTMTrainer * |
trainer |
|
) |
| const |
|
inline |
Definition at line 282 of file lstmtrainer.h.
284 return trainer->ReadLocalTrainingDump(&
mgr_, data, size);
◆ ReadTrainingDump()
◆ ReduceLayerLearningRates()
int tesseract::LSTMTrainer::ReduceLayerLearningRates |
( |
double |
factor, |
|
|
int |
num_samples, |
|
|
LSTMTrainer * |
samples_trainer |
|
) |
| |
Definition at line 581 of file lstmtrainer.cpp.
589 int num_layers = layers.
size();
594 for (
int i = 0; i < LR_COUNT; ++i) {
598 double momentum_factor = 1.0 / (1.0 -
momentum_);
600 samples_trainer->SaveTrainingDump(
LIGHT,
this, &orig_trainer);
601 for (
int i = 0; i < num_layers; ++i) {
602 Network* layer =
GetLayer(layers[i]);
603 num_weights[i] = layer->IsTraining() ? layer->num_weights() : 0;
606 for (
int s = 0; s < num_samples; ++s) {
608 for (
int ww = 0; ww < LR_COUNT; ++ww) {
610 float ww_factor = momentum_factor;
611 if (ww == LR_DOWN) ww_factor *= factor;
614 samples_trainer->ReadTrainingDump(orig_trainer, ©_trainer);
616 copy_trainer.network_->Update(0.0, 0.0, 0.0, 0);
618 for (
int i = 0; i < num_layers; ++i) {
619 if (num_weights[i] == 0)
continue;
620 copy_trainer.ScaleLayerLearningRate(layers[i], ww_factor);
622 copy_trainer.SetIteration(iteration);
625 const ImageData* trainingdata =
626 copy_trainer.TrainOnLine(samples_trainer,
true);
627 if (trainingdata ==
nullptr)
continue;
630 samples_trainer->SaveTrainingDump(
LIGHT, ©_trainer, &updated_trainer);
631 for (
int i = 0; i < num_layers; ++i) {
632 if (num_weights[i] == 0)
continue;
634 samples_trainer->ReadTrainingDump(updated_trainer, &layer_trainer);
635 Network* layer = layer_trainer.GetLayer(layers[i]);
638 layer_trainer.training_iteration_ + 1);
640 layer->Update(0.0, 0.0, 0.0, 0);
642 layer_trainer.TrainOnLine(trainingdata,
true);
644 float before_bad = bad_sums[ww][i];
645 float before_ok = ok_sums[ww][i];
646 layer->CountAlternators(*copy_trainer.GetLayer(layers[i]),
647 &ok_sums[ww][i], &bad_sums[ww][i]);
649 bad_sums[ww][i] + ok_sums[ww][i] - before_bad - before_ok;
651 bad_frac = (bad_sums[ww][i] - before_bad) / bad_frac;
657 for (
int i = 0; i < num_layers; ++i) {
658 if (num_weights[i] == 0)
continue;
659 Network* layer =
GetLayer(layers[i]);
661 double total_down = bad_sums[LR_DOWN][i] + ok_sums[LR_DOWN][i];
662 double total_same = bad_sums[LR_SAME][i] + ok_sums[LR_SAME][i];
663 double frac_down = bad_sums[LR_DOWN][i] / total_down;
664 double frac_same = bad_sums[LR_SAME][i] / total_same;
665 tprintf(
"Layer %d=%s: lr %g->%g%%, lr %g->%g%%", i, layer->name().c_str(),
666 lr * factor, 100.0 * frac_down, lr, 100.0 * frac_same);
675 if (num_lowered == 0) {
677 for (
int i = 0; i < num_layers; ++i) {
678 if (num_weights[i] > 0) {
◆ ReduceLearningRates()
void tesseract::LSTMTrainer::ReduceLearningRates |
( |
LSTMTrainer * |
samples_trainer, |
|
|
STRING * |
log_msg |
|
) |
| |
◆ RollErrorBuffers()
void tesseract::LSTMTrainer::RollErrorBuffers |
( |
| ) |
|
|
protected |
◆ SaveRecognitionDump()
void tesseract::LSTMTrainer::SaveRecognitionDump |
( |
GenericVector< char > * |
data | ) |
const |
◆ SaveTraineddata()
bool tesseract::LSTMTrainer::SaveTraineddata |
( |
const STRING & |
filename | ) |
|
◆ SaveTrainingDump()
◆ Serialize()
Definition at line 403 of file lstmtrainer.cpp.
411 if (!error_buffer.Serialize(fp))
return false;
415 uint8_t amount = serialize_amount;
416 if (!fp->Serialize(&amount))
return false;
417 if (serialize_amount ==
LIGHT)
return true;
432 if (!sub_data.
Serialize(fp))
return false;
◆ set_perfect_delay()
void tesseract::LSTMTrainer::set_perfect_delay |
( |
int |
delay | ) |
|
|
inline |
◆ SetNullChar()
void tesseract::LSTMTrainer::SetNullChar |
( |
| ) |
|
|
protected |
◆ SetupCheckpointInfo()
void tesseract::LSTMTrainer::SetupCheckpointInfo |
( |
| ) |
|
◆ StartSubtrainer()
void tesseract::LSTMTrainer::StartSubtrainer |
( |
STRING * |
log_msg | ) |
|
Definition at line 489 of file lstmtrainer.cpp.
493 *log_msg +=
" Failed to revert to previous best for trial!";
497 log_msg->
add_str_int(
" Trial sub_trainer_ from iteration ",
◆ training_data()
const DocumentCache& tesseract::LSTMTrainer::training_data |
( |
| ) |
const |
|
inline |
◆ TrainOnLine() [1/2]
Definition at line 737 of file lstmtrainer.cpp.
739 NetworkIO fwd_outputs, targets;
758 #ifndef GRAPHICS_DISABLED
762 #endif // GRAPHICS_DISABLED
◆ TrainOnLine() [2/2]
Definition at line 245 of file lstmtrainer.h.
247 const ImageData* image =
248 samples_trainer->training_data_.GetPageBySerial(sample_index);
249 if (image !=
nullptr) {
◆ TransitionTrainingStage()
bool tesseract::LSTMTrainer::TransitionTrainingStage |
( |
float |
error_threshold | ) |
|
◆ TryLoadingCheckpoint()
bool tesseract::LSTMTrainer::TryLoadingCheckpoint |
( |
const char * |
filename, |
|
|
const char * |
old_traineddata |
|
) |
| |
Definition at line 103 of file lstmtrainer.cpp.
107 tprintf(
"Loaded file %s, unpacking...\n", filename);
110 if (((old_traineddata ==
nullptr || *old_traineddata ==
'\0') &&
112 filename == old_traineddata) {
117 if (old_traineddata ==
nullptr || *old_traineddata ==
'\0') {
118 tprintf(
"Must supply the old traineddata for code conversion!\n");
121 TessdataManager old_mgr;
128 UnicharCompress old_recoder;
129 if (!old_recoder.DeSerialize(&fp))
return false;
130 std::vector<int> code_map =
MapRecoder(old_chset, old_recoder);
◆ UpdateErrorBuffer()
void tesseract::LSTMTrainer::UpdateErrorBuffer |
( |
double |
new_error, |
|
|
ErrorTypes |
type |
|
) |
| |
|
protected |
Definition at line 1228 of file lstmtrainer.cpp.
1233 double buffer_sum = 0.0;
1235 double mean = buffer_sum / mean_count;
◆ UpdateErrorGraph()
Definition at line 1260 of file lstmtrainer.cpp.
1297 double two_percent_more = error_rate + 2.0;
1304 tprintf(
"2 Percent improvement time=%d, best error was %g @ %d\n",
1309 if (tester !=
nullptr) {
◆ UpdateSubtrainer()
Definition at line 519 of file lstmtrainer.cpp.
522 double sub_margin = (training_error - sub_error) / sub_error;
531 int target_iteration =
536 STRING batch_log =
"Sub:";
540 *log_msg += batch_log;
542 sub_margin = (training_error - sub_error) / sub_error;
550 log_msg->add_str_int(
" Sub trainer wins at iteration ",
◆ align_win_
◆ best_error_history_
GenericVector<double> tesseract::LSTMTrainer::best_error_history_ |
|
protected |
◆ best_error_iterations_
GenericVector<int> tesseract::LSTMTrainer::best_error_iterations_ |
|
protected |
◆ best_error_rate_
double tesseract::LSTMTrainer::best_error_rate_ |
|
protected |
◆ best_error_rates_
double tesseract::LSTMTrainer::best_error_rates_[ET_COUNT] |
|
protected |
◆ best_iteration_
int tesseract::LSTMTrainer::best_iteration_ |
|
protected |
◆ best_model_data_
◆ best_model_name_
STRING tesseract::LSTMTrainer::best_model_name_ |
|
protected |
◆ best_trainer_
◆ checkpoint_iteration_
int tesseract::LSTMTrainer::checkpoint_iteration_ |
|
protected |
◆ checkpoint_name_
STRING tesseract::LSTMTrainer::checkpoint_name_ |
|
protected |
◆ ctc_win_
◆ debug_interval_
int tesseract::LSTMTrainer::debug_interval_ |
|
protected |
◆ error_buffers_
◆ error_rate_of_last_saved_best_
float tesseract::LSTMTrainer::error_rate_of_last_saved_best_ |
|
protected |
◆ error_rates_
double tesseract::LSTMTrainer::error_rates_[ET_COUNT] |
|
protected |
◆ improvement_steps_
int32_t tesseract::LSTMTrainer::improvement_steps_ |
|
protected |
◆ kRollingBufferSize_
const int tesseract::LSTMTrainer::kRollingBufferSize_ = 1000 |
|
staticprotected |
◆ last_perfect_training_iteration_
int tesseract::LSTMTrainer::last_perfect_training_iteration_ |
|
protected |
◆ learning_iteration_
int tesseract::LSTMTrainer::learning_iteration_ |
|
protected |
◆ mgr_
◆ model_base_
STRING tesseract::LSTMTrainer::model_base_ |
|
protected |
◆ num_training_stages_
int tesseract::LSTMTrainer::num_training_stages_ |
|
protected |
◆ perfect_delay_
int tesseract::LSTMTrainer::perfect_delay_ |
|
protected |
◆ prev_sample_iteration_
int tesseract::LSTMTrainer::prev_sample_iteration_ |
|
protected |
◆ randomly_rotate_
bool tesseract::LSTMTrainer::randomly_rotate_ |
|
protected |
◆ recon_win_
◆ stall_iteration_
int tesseract::LSTMTrainer::stall_iteration_ |
|
protected |
◆ sub_trainer_
◆ target_win_
◆ training_data_
◆ training_stage_
int tesseract::LSTMTrainer::training_stage_ |
|
protected |
◆ worst_error_rate_
double tesseract::LSTMTrainer::worst_error_rate_ |
|
protected |
◆ worst_error_rates_
double tesseract::LSTMTrainer::worst_error_rates_[ET_COUNT] |
|
protected |
◆ worst_iteration_
int tesseract::LSTMTrainer::worst_iteration_ |
|
protected |
◆ worst_model_data_
The documentation for this class was generated from the following files:
bool load_from_file(const char *const filename, bool skip_fragments)
bool DebugLSTMTraining(const NetworkIO &inputs, const ImageData &trainingdata, const NetworkIO &fwd_outputs, const GenericVector< int > &truth_labels, const NetworkIO &outputs)
static constexpr float kMinCertainty
const double kSubTrainerMarginFraction
const double kMinDivergenceRate
virtual bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)=0
void DebugActivationPath(const NetworkIO &outputs, const GenericVector< int > &labels, const GenericVector< int > &xcoords)
Trainability PrepareForBackward(const ImageData *trainingdata, NetworkIO *fwd_outputs, NetworkIO *targets)
void add_str_int(const char *str, int number)
SubTrainerResult UpdateSubtrainer(STRING *log_msg)
bool SaveTrainingDump(SerializeAmount serialize_amount, const LSTMTrainer *trainer, GenericVector< char > *data) const
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
void SetVersionString(const std::string &v_str)
virtual void SetEnableTraining(TrainingState state)
void SaveRecognitionDump(GenericVector< char > *data) const
void ScaleLayerLearningRate(const STRING &id, double factor)
bool ReadSizedTrainingDump(const char *data, int size, LSTMTrainer *trainer) const
double best_error_rates_[ET_COUNT]
std::vector< int > MapRecoder(const UNICHARSET &old_chset, const UnicharCompress &old_recoder) const
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
int prev_sample_iteration_
int training_iteration() const
std::string VersionString() const
bool LoadDocuments(const GenericVector< STRING > &filenames, CachingStrategy cache_strategy, FileReader reader)
STRING DumpFilename() const
constexpr size_t countof(T const (&)[N]) noexcept
double learning_rate() const
const int kNumAdjustmentIterations
virtual int RemapOutputs(int old_no, const std::vector< int > &code_map)
GenericVector< double > error_buffers_[ET_COUNT]
bool Serialize(FILE *fp) const
bool SaveDataToFile(const GenericVector< char > &data, const char *filename)
int32_t sample_iteration_
bool EncodeString(const STRING &str, GenericVector< int > *labels) const
int IntCastRounded(double x)
GenericVector< STRING > EnumerateLayers() const
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)
const int kMinStartedErrorRate
void DisplayForward(const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window)
const UNICHARSET & GetUnicharset() const
virtual void DebugWeights()=0
const ImageData * TrainOnLine(LSTMTrainer *samples_trainer, bool batch)
GenericVector< double > best_error_history_
bool TestFlag(NetworkFlags flag) const
float GetLayerLearningRate(const STRING &id) const
GenericVector< char > best_trainer_
void StartSubtrainer(STRING *log_msg)
void OverwriteEntry(TessdataType type, const char *data, int size)
bool LoadCharsets(const TessdataManager *mgr)
virtual StaticShape OutputShape(const StaticShape &input_shape) const
float error_rate_of_last_saved_best_
double NewSingleError(ErrorTypes type) const
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
void RecognizeLine(const ImageData &image_data, bool invert, bool debug, double worst_dict_cert, const TBOX &line_box, PointerVector< WERD_RES > *words, int lstm_choice_mode=0, int lstm_choice_amount=5)
NetworkScratch scratch_space_
const char * c_str() const
bool DeSerialize(bool swap, FILE *fp)
GenericVector< char > best_model_data_
void UpdateErrorBuffer(double new_error, ErrorTypes type)
int learning_iteration() const
double ComputeRMSError(const NetworkIO &deltas)
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
LossType OutputLossType() const
void ScaleLearningRate(double factor)
virtual StaticShape InputShape() const
double ComputeErrorRates(const NetworkIO &deltas, double char_error, double word_error)
const int kNumPagesPerBatch
int32_t improvement_steps_
Network * GetLayer(const STRING &id) const
const double kStageTransitionThreshold
bool Serialize(const TessdataManager *mgr, TFile *fp) const
static std::string CleanupString(const char *utf8_str)
int32_t training_iteration_
double ComputeWordError(STRING *truth_str, STRING *ocr_str)
bool ReadTrainingDump(const GenericVector< char > &data, LSTMTrainer *trainer) const
const double kLearningRateDecay
bool SimpleTextOutput() const
void DisplayTargets(const NetworkIO &targets, const char *window_name, ScrollView **window)
bool has_special_codes() const
double SignedRand(double range)
LSTMTrainer * sub_trainer_
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
const double kImprovementFraction
SVEvent * AwaitEvent(SVEventType type)
const double kBestCheckpointFraction
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
virtual void Update(float learning_rate, float momentum, float adam_beta, int num_samples)
bool ComputeCTCTargets(const GenericVector< int > &truth_labels, NetworkIO *outputs, NetworkIO *targets)
bool Init(const char *data_file_name)
static void NormalizeProbs(NetworkIO *probs)
void add_str_double(const char *str, double number)
void ReduceLearningRates(LSTMTrainer *samples_trainer, STRING *log_msg)
const int kMinStallIterations
void init_to_size(int size, const T &t)
int last_perfect_training_iteration_
static bool InitNetwork(int num_outputs, STRING network_spec, int append_index, int net_flags, float weight_range, TRand *randomizer, Network **network)
int CurrentTrainingStage() const
int ReduceLayerLearningRates(double factor, int num_samples, LSTMTrainer *samples_trainer)
DLLSYM void tprintf(const char *format,...)
GenericVector< int > best_error_iterations_
const int kErrorGraphInterval
STRING UpdateErrorGraph(int iteration, double error_rate, const GenericVector< char > &model_data, TestCallback tester)
virtual STRING spec() const
GenericVector< char > worst_model_data_
double worst_error_rates_[ET_COUNT]
const double kHighConfidence
static const int kRollingBufferSize_
double error_rates_[ET_COUNT]
DocumentCache training_data_
bool TransitionTrainingStage(float error_threshold)
int EncodeUnichar(int unichar_id, RecodedCharID *code) const
void PrepareLogMsg(STRING *log_msg) const
bool ComputeTextTargets(const NetworkIO &outputs, const GenericVector< int > &truth_labels, NetworkIO *targets)
int checkpoint_iteration_
static bool ComputeCTCTargets(const GenericVector< int > &truth_labels, int null_char, const GENERIC_2D_ARRAY< float > &outputs, NetworkIO *targets)
LIST search(LIST list, void *key, int_compare is_equal)
bool SaveFile(const STRING &filename, FileWriter writer) const
void split(char c, GenericVector< STRING > *splited)
double ComputeWinnerError(const NetworkIO &deltas)
void LogIterations(const char *intro_str, STRING *log_msg) const
double ComputeCharError(const GenericVector< int > &truth_str, const GenericVector< int > &ocr_str)
int sample_iteration() const
STRING DecodeLabels(const GenericVector< int > &labels)