tesseract 4.1.1
Loading...
Searching...
No Matches
tesseract::LSTMRecognizer Class Reference

#include <lstmrecognizer.h>

Inheritance diagram for tesseract::LSTMRecognizer:
tesseract::LSTMTrainer

Public Member Functions

 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< STRINGEnumerateLayers () const
 
NetworkGetLayer (const STRING &id) const
 
float GetLayerLearningRate (const STRING &id) const
 
void ScaleLearningRate (double factor)
 
void ScaleLayerLearningRate (const STRING &id, double factor)
 
void ConvertToInt ()
 
const UNICHARSETGetUnicharset () const
 
const UnicharCompressGetRecoder () const
 
const DictGetDict () const
 
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)
 
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)
 

Protected Member Functions

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)
 

Protected Attributes

Networknetwork_
 
CCUtil ccutil_
 
UnicharCompress recoder_
 
STRING network_str_
 
int32_t training_flags_
 
int32_t training_iteration_
 
int32_t sample_iteration_
 
int32_t null_char_
 
float learning_rate_
 
float momentum_
 
float adam_beta_
 
TRand randomizer_
 
NetworkScratch scratch_space_
 
Dictdict_
 
RecodeBeamSearchsearch_
 
ScrollViewdebug_win_
 

Detailed Description

Definition at line 54 of file lstmrecognizer.h.

Constructor & Destructor Documentation

◆ LSTMRecognizer() [1/2]

tesseract::LSTMRecognizer::LSTMRecognizer ( )

◆ LSTMRecognizer() [2/2]

tesseract::LSTMRecognizer::LSTMRecognizer ( const STRING  language_data_path_prefix)

Definition at line 49 of file lstmrecognizer.cpp.

◆ ~LSTMRecognizer()

tesseract::LSTMRecognizer::~LSTMRecognizer ( )

Definition at line 67 of file lstmrecognizer.cpp.

67 {
68 delete network_;
69 delete dict_;
70 delete search_;
71}

Member Function Documentation

◆ ConvertToInt()

void tesseract::LSTMRecognizer::ConvertToInt ( )
inline

Definition at line 125 of file lstmrecognizer.h.

125 {
126 if ((training_flags_ & TF_INT_MODE) == 0) {
129 }
130 }
virtual void ConvertToInt()
Definition: network.h:191

◆ DebugActivationPath()

void tesseract::LSTMRecognizer::DebugActivationPath ( const NetworkIO outputs,
const GenericVector< int > &  labels,
const GenericVector< int > &  xcoords 
)
protected

Definition at line 360 of file lstmrecognizer.cpp.

362 {
363 if (xcoords[0] > 0)
364 DebugActivationRange(outputs, "<null>", null_char_, 0, xcoords[0]);
365 int end = 1;
366 for (int start = 0; start < labels.size(); start = end) {
367 if (labels[start] == null_char_) {
368 end = start + 1;
369 DebugActivationRange(outputs, "<null>", null_char_, xcoords[start],
370 xcoords[end]);
371 continue;
372 } else {
373 int decoded;
374 const char* label = DecodeLabel(labels, start, &end, &decoded);
375 DebugActivationRange(outputs, label, labels[start], xcoords[start],
376 xcoords[start + 1]);
377 for (int i = start + 1; i < end; ++i) {
378 DebugActivationRange(outputs, DecodeSingleLabel(labels[i]), labels[i],
379 xcoords[i], xcoords[i + 1]);
380 }
381 }
382 }
383}
int size() const
Definition: genericvector.h:72
const char * DecodeSingleLabel(int label)
const char * DecodeLabel(const GenericVector< int > &labels, int start, int *end, int *decoded)
void DebugActivationRange(const NetworkIO &outputs, const char *label, int best_choice, int x_start, int x_end)

◆ DebugActivationRange()

void tesseract::LSTMRecognizer::DebugActivationRange ( const NetworkIO outputs,
const char *  label,
int  best_choice,
int  x_start,
int  x_end 
)
protected

Definition at line 387 of file lstmrecognizer.cpp.

389 {
390 tprintf("%s=%d On [%d, %d), scores=", label, best_choice, x_start, x_end);
391 double max_score = 0.0;
392 double mean_score = 0.0;
393 const int width = x_end - x_start;
394 for (int x = x_start; x < x_end; ++x) {
395 const float* line = outputs.f(x);
396 const double score = line[best_choice] * 100.0;
397 if (score > max_score) max_score = score;
398 mean_score += score / width;
399 int best_c = 0;
400 double best_score = 0.0;
401 for (int c = 0; c < outputs.NumFeatures(); ++c) {
402 if (c != best_choice && line[c] > best_score) {
403 best_c = c;
404 best_score = line[c];
405 }
406 }
407 tprintf(" %.3g(%s=%d=%.3g)", score, DecodeSingleLabel(best_c), best_c,
408 best_score * 100.0);
409 }
410 tprintf(", Mean=%g, max=%g\n", mean_score, max_score);
411}
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:35

◆ DecodeLabel()

const char * tesseract::LSTMRecognizer::DecodeLabel ( const GenericVector< int > &  labels,
int  start,
int *  end,
int *  decoded 
)
protected

Definition at line 475 of file lstmrecognizer.cpp.

476 {
477 *end = start + 1;
478 if (IsRecoding()) {
479 // Decode labels via recoder_.
480 RecodedCharID code;
481 if (labels[start] == null_char_) {
482 if (decoded != nullptr) {
483 code.Set(0, null_char_);
484 *decoded = recoder_.DecodeUnichar(code);
485 }
486 return "<null>";
487 }
488 int index = start;
489 while (index < labels.size() &&
490 code.length() < RecodedCharID::kMaxCodeLen) {
491 code.Set(code.length(), labels[index++]);
492 while (index < labels.size() && labels[index] == null_char_) ++index;
493 int uni_id = recoder_.DecodeUnichar(code);
494 // If the next label isn't a valid first code, then we need to continue
495 // extending even if we have a valid uni_id from this prefix.
496 if (uni_id != INVALID_UNICHAR_ID &&
497 (index == labels.size() ||
498 code.length() == RecodedCharID::kMaxCodeLen ||
499 recoder_.IsValidFirstCode(labels[index]))) {
500 *end = index;
501 if (decoded != nullptr) *decoded = uni_id;
502 if (uni_id == UNICHAR_SPACE) return " ";
503 return GetUnicharset().get_normed_unichar(uni_id);
504 }
505 }
506 return "<Undecodable>";
507 } else {
508 if (decoded != nullptr) *decoded = labels[start];
509 if (labels[start] == null_char_) return "<null>";
510 if (labels[start] == UNICHAR_SPACE) return " ";
511 return GetUnicharset().get_normed_unichar(labels[start]);
512 }
513}
@ UNICHAR_SPACE
Definition: unicharset.h:34
static const int kMaxCodeLen
bool IsValidFirstCode(int code) const
int DecodeUnichar(const RecodedCharID &code) const
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
Definition: unicharset.h:828
const UNICHARSET & GetUnicharset() const

◆ DecodeLabels()

STRING tesseract::LSTMRecognizer::DecodeLabels ( const GenericVector< int > &  labels)

Definition at line 302 of file lstmrecognizer.cpp.

302 {
303 STRING result;
304 int end = 1;
305 for (int start = 0; start < labels.size(); start = end) {
306 if (labels[start] == null_char_) {
307 end = start + 1;
308 } else {
309 result += DecodeLabel(labels, start, &end, nullptr);
310 }
311 }
312 return result;
313}
Definition: strngs.h:45

◆ DecodeSingleLabel()

const char * tesseract::LSTMRecognizer::DecodeSingleLabel ( int  label)
protected

Definition at line 517 of file lstmrecognizer.cpp.

517 {
518 if (label == null_char_) return "<null>";
519 if (IsRecoding()) {
520 // Decode label via recoder_.
521 RecodedCharID code;
522 code.Set(0, label);
523 label = recoder_.DecodeUnichar(code);
524 if (label == INVALID_UNICHAR_ID) return ".."; // Part of a bigger code.
525 }
526 if (label == UNICHAR_SPACE) return " ";
527 return GetUnicharset().get_normed_unichar(label);
528}

◆ DeSerialize()

bool tesseract::LSTMRecognizer::DeSerialize ( const TessdataManager mgr,
TFile fp 
)

Definition at line 105 of file lstmrecognizer.cpp.

105 {
106 delete network_;
108 if (network_ == nullptr) return false;
109 bool include_charsets = mgr == nullptr ||
110 !mgr->IsComponentAvailable(TESSDATA_LSTM_RECODER) ||
111 !mgr->IsComponentAvailable(TESSDATA_LSTM_UNICHARSET);
112 if (include_charsets && !ccutil_.unicharset.load_from_file(fp, false))
113 return false;
114 if (!network_str_.DeSerialize(fp)) return false;
115 if (!fp->DeSerialize(&training_flags_)) return false;
116 if (!fp->DeSerialize(&training_iteration_)) return false;
117 if (!fp->DeSerialize(&sample_iteration_)) return false;
118 if (!fp->DeSerialize(&null_char_)) return false;
119 if (!fp->DeSerialize(&adam_beta_)) return false;
120 if (!fp->DeSerialize(&learning_rate_)) return false;
121 if (!fp->DeSerialize(&momentum_)) return false;
122 if (include_charsets && !LoadRecoder(fp)) return false;
123 if (!include_charsets && !LoadCharsets(mgr)) return false;
126 return true;
127}
@ TESSDATA_LSTM_UNICHARSET
@ TESSDATA_LSTM_RECODER
UNICHARSET unicharset
Definition: ccutil.h:73
bool DeSerialize(bool swap, FILE *fp)
Definition: strngs.cpp:159
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:388
bool LoadCharsets(const TessdataManager *mgr)
virtual int XScaleFactor() const
Definition: network.h:209
static Network * CreateFromFile(TFile *fp)
Definition: network.cpp:187
virtual void CacheXScaleFactor(int factor)
Definition: network.h:215
virtual void SetRandomizer(TRand *randomizer)
Definition: network.cpp:138

◆ DisplayForward()

void tesseract::LSTMRecognizer::DisplayForward ( const NetworkIO inputs,
const GenericVector< int > &  labels,
const GenericVector< int > &  label_coords,
const char *  window_name,
ScrollView **  window 
)

Definition at line 317 of file lstmrecognizer.cpp.

321 {
322#ifndef GRAPHICS_DISABLED // do nothing if there's no graphics
323 Pix* input_pix = inputs.ToPix();
324 Network::ClearWindow(false, window_name, pixGetWidth(input_pix),
325 pixGetHeight(input_pix), window);
326 int line_height = Network::DisplayImage(input_pix, *window);
327 DisplayLSTMOutput(labels, label_coords, line_height, *window);
328#endif // GRAPHICS_DISABLED
329}
void DisplayLSTMOutput(const GenericVector< int > &labels, const GenericVector< int > &xcoords, int height, ScrollView *window)
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
Definition: network.cpp:312
static int DisplayImage(Pix *pix, ScrollView *window)
Definition: network.cpp:335

◆ DisplayLSTMOutput()

void tesseract::LSTMRecognizer::DisplayLSTMOutput ( const GenericVector< int > &  labels,
const GenericVector< int > &  xcoords,
int  height,
ScrollView window 
)
protected

Definition at line 333 of file lstmrecognizer.cpp.

335 {
336#ifndef GRAPHICS_DISABLED // do nothing if there's no graphics
337 int x_scale = network_->XScaleFactor();
338 window->TextAttributes("Arial", height / 4, false, false, false);
339 int end = 1;
340 for (int start = 0; start < labels.size(); start = end) {
341 int xpos = xcoords[start] * x_scale;
342 if (labels[start] == null_char_) {
343 end = start + 1;
344 window->Pen(ScrollView::RED);
345 } else {
346 window->Pen(ScrollView::GREEN);
347 const char* str = DecodeLabel(labels, start, &end, nullptr);
348 if (*str == '\\') str = "\\\\";
349 xpos = xcoords[(start + end) / 2] * x_scale;
350 window->Text(xpos, height, str);
351 }
352 window->Line(xpos, 0, xpos, height * 3 / 2);
353 }
354 window->Update();
355#endif // GRAPHICS_DISABLED
356}
static void Update()
Definition: scrollview.cpp:709
void Line(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:532
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
Definition: scrollview.cpp:635
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:652
void Pen(Color color)
Definition: scrollview.cpp:719

◆ EnumerateLayers()

GenericVector< STRING > tesseract::LSTMRecognizer::EnumerateLayers ( ) const
inline

Definition at line 80 of file lstmrecognizer.h.

80 {
81 ASSERT_HOST(network_ != nullptr && network_->type() == NT_SERIES);
82 auto* series = static_cast<Series*>(network_);
84 series->EnumerateLayers(nullptr, &layers);
85 return layers;
86 }
#define ASSERT_HOST(x)
Definition: errcode.h:88
@ NT_SERIES
Definition: network.h:54
NetworkType type() const
Definition: network.h:112

◆ GetDict()

const Dict * tesseract::LSTMRecognizer::GetDict ( ) const
inline

Definition at line 137 of file lstmrecognizer.h.

137{ return dict_; }

◆ GetLayer()

Network * tesseract::LSTMRecognizer::GetLayer ( const STRING id) const
inline

Definition at line 88 of file lstmrecognizer.h.

88 {
89 ASSERT_HOST(network_ != nullptr && network_->type() == NT_SERIES);
90 ASSERT_HOST(id.length() > 1 && id[0] == ':');
91 auto* series = static_cast<Series*>(network_);
92 return series->GetLayer(&id[1]);
93 }

◆ GetLayerLearningRate()

float tesseract::LSTMRecognizer::GetLayerLearningRate ( const STRING id) const
inline

Definition at line 95 of file lstmrecognizer.h.

95 {
96 ASSERT_HOST(network_ != nullptr && network_->type() == NT_SERIES);
98 ASSERT_HOST(id.length() > 1 && id[0] == ':');
99 auto* series = static_cast<Series*>(network_);
100 return series->LayerLearningRate(&id[1]);
101 } else {
102 return learning_rate_;
103 }
104 }
@ NF_LAYER_SPECIFIC_LR
Definition: network.h:87
bool TestFlag(NetworkFlags flag) const
Definition: network.h:144

◆ GetRecoder()

const UnicharCompress & tesseract::LSTMRecognizer::GetRecoder ( ) const
inline

Definition at line 135 of file lstmrecognizer.h.

135{ return recoder_; }

◆ GetUnicharset()

const UNICHARSET & tesseract::LSTMRecognizer::GetUnicharset ( ) const
inline

Definition at line 133 of file lstmrecognizer.h.

133{ return ccutil_.unicharset; }

◆ IsIntMode()

bool tesseract::LSTMRecognizer::IsIntMode ( ) const
inline

Definition at line 71 of file lstmrecognizer.h.

71{ return (training_flags_ & TF_INT_MODE) != 0; }

◆ IsRecoding()

bool tesseract::LSTMRecognizer::IsRecoding ( ) const
inline

Definition at line 73 of file lstmrecognizer.h.

73 {
75 }
@ TF_COMPRESS_UNICHARSET

◆ IsTensorFlow()

bool tesseract::LSTMRecognizer::IsTensorFlow ( ) const
inline

Definition at line 77 of file lstmrecognizer.h.

77{ return network_->type() == NT_TENSORFLOW; }
@ NT_TENSORFLOW
Definition: network.h:78

◆ LabelsFromOutputs()

void tesseract::LSTMRecognizer::LabelsFromOutputs ( const NetworkIO outputs,
GenericVector< int > *  labels,
GenericVector< int > *  xcoords 
)

Definition at line 430 of file lstmrecognizer.cpp.

432 {
433 if (SimpleTextOutput()) {
434 LabelsViaSimpleText(outputs, labels, xcoords);
435 } else {
436 LabelsViaReEncode(outputs, labels, xcoords);
437 }
438}
void LabelsViaReEncode(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
void LabelsViaSimpleText(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)

◆ LabelsViaReEncode()

void tesseract::LSTMRecognizer::LabelsViaReEncode ( const NetworkIO output,
GenericVector< int > *  labels,
GenericVector< int > *  xcoords 
)
protected

Definition at line 442 of file lstmrecognizer.cpp.

444 {
445 if (search_ == nullptr) {
446 search_ =
447 new RecodeBeamSearch(recoder_, null_char_, SimpleTextOutput(), dict_);
448 }
449 search_->Decode(output, 1.0, 0.0, RecodeBeamSearch::kMinCertainty, nullptr);
450 search_->ExtractBestPathAsLabels(labels, xcoords);
451}
void Decode(const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset, int lstm_choice_mode=0)
Definition: recodebeam.cpp:76
void ExtractBestPathAsLabels(GenericVector< int > *labels, GenericVector< int > *xcoords) const
Definition: recodebeam.cpp:133
static constexpr float kMinCertainty
Definition: recodebeam.h:222

◆ LabelsViaSimpleText()

void tesseract::LSTMRecognizer::LabelsViaSimpleText ( const NetworkIO output,
GenericVector< int > *  labels,
GenericVector< int > *  xcoords 
)
protected

Definition at line 456 of file lstmrecognizer.cpp.

458 {
459 labels->truncate(0);
460 xcoords->truncate(0);
461 const int width = output.Width();
462 for (int t = 0; t < width; ++t) {
463 float score = 0.0f;
464 const int label = output.BestLabel(t, &score);
465 if (label != null_char_) {
466 labels->push_back(label);
467 xcoords->push_back(t);
468 }
469 }
470 xcoords->push_back(width);
471}
int push_back(T object)
void truncate(int size)

◆ learning_rate()

double tesseract::LSTMRecognizer::learning_rate ( ) const
inline

Definition at line 63 of file lstmrecognizer.h.

63{ return learning_rate_; }

◆ Load()

bool tesseract::LSTMRecognizer::Load ( const ParamsVectors params,
const char *  lang,
TessdataManager mgr 
)

Definition at line 74 of file lstmrecognizer.cpp.

75 {
76 TFile fp;
77 if (!mgr->GetComponent(TESSDATA_LSTM, &fp)) return false;
78 if (!DeSerialize(mgr, &fp)) return false;
79 if (lang == nullptr) return true;
80 // Allow it to run without a dictionary.
81 LoadDictionary(params, lang, mgr);
82 return true;
83}
bool LoadDictionary(const ParamsVectors *params, const char *lang, TessdataManager *mgr)
bool DeSerialize(const TessdataManager *mgr, TFile *fp)

◆ LoadCharsets()

bool tesseract::LSTMRecognizer::LoadCharsets ( const TessdataManager mgr)

Definition at line 130 of file lstmrecognizer.cpp.

130 {
131 TFile fp;
132 if (!mgr->GetComponent(TESSDATA_LSTM_UNICHARSET, &fp)) return false;
133 if (!ccutil_.unicharset.load_from_file(&fp, false)) return false;
134 if (!mgr->GetComponent(TESSDATA_LSTM_RECODER, &fp)) return false;
135 if (!LoadRecoder(&fp)) return false;
136 return true;
137}

◆ LoadDictionary()

bool tesseract::LSTMRecognizer::LoadDictionary ( const ParamsVectors params,
const char *  lang,
TessdataManager mgr 
)

Definition at line 164 of file lstmrecognizer.cpp.

165 {
166 delete dict_;
167 dict_ = new Dict(&ccutil_);
168 dict_->user_words_file.ResetFrom(params);
169 dict_->user_words_suffix.ResetFrom(params);
170 dict_->user_patterns_file.ResetFrom(params);
171 dict_->user_patterns_suffix.ResetFrom(params);
173 dict_->LoadLSTM(lang, mgr);
174 if (dict_->FinishLoad()) return true; // Success.
175 tprintf("Failed to load any lstm-specific dictionaries for lang %s!!\n",
176 lang);
177 delete dict_;
178 dict_ = nullptr;
179 return false;
180}
static TESS_API DawgCache * GlobalDawgCache()
Definition: dict.cpp:184
char * user_patterns_suffix
Definition: dict.h:584
void SetupForLoad(DawgCache *dawg_cache)
Definition: dict.cpp:192
bool FinishLoad()
Definition: dict.cpp:351
char * user_words_file
Definition: dict.h:578
char * user_words_suffix
Definition: dict.h:580
void LoadLSTM(const STRING &lang, TessdataManager *data_file)
Definition: dict.cpp:291
char * user_patterns_file
Definition: dict.h:582

◆ LoadRecoder()

bool tesseract::LSTMRecognizer::LoadRecoder ( TFile fp)

Definition at line 140 of file lstmrecognizer.cpp.

140 {
141 if (IsRecoding()) {
142 if (!recoder_.DeSerialize(fp)) return false;
143 RecodedCharID code;
145 if (code(0) != UNICHAR_SPACE) {
146 tprintf("Space was garbled in recoding!!\n");
147 return false;
148 }
149 } else {
152 }
153 return true;
154}
int EncodeUnichar(int unichar_id, RecodedCharID *code) const
void SetupPassThrough(const UNICHARSET &unicharset)

◆ null_char()

int tesseract::LSTMRecognizer::null_char ( ) const
inline

Definition at line 144 of file lstmrecognizer.h.

144{ return null_char_; }

◆ NumInputs()

int tesseract::LSTMRecognizer::NumInputs ( ) const
inline

Definition at line 143 of file lstmrecognizer.h.

143{ return network_->NumInputs(); }
int NumInputs() const
Definition: network.h:120

◆ NumOutputs()

int tesseract::LSTMRecognizer::NumOutputs ( ) const
inline

Definition at line 60 of file lstmrecognizer.h.

60{ return network_->NumOutputs(); }
int NumOutputs() const
Definition: network.h:123

◆ OutputLossType()

LossType tesseract::LSTMRecognizer::OutputLossType ( ) const
inline

Definition at line 64 of file lstmrecognizer.h.

64 {
65 if (network_ == nullptr) return LT_NONE;
66 StaticShape shape;
67 shape = network_->OutputShape(shape);
68 return shape.loss_type();
69 }
virtual StaticShape OutputShape(const StaticShape &input_shape) const
Definition: network.h:133
LossType loss_type() const
Definition: static_shape.h:50

◆ OutputStats()

void tesseract::LSTMRecognizer::OutputStats ( const NetworkIO outputs,
float *  min_output,
float *  mean_output,
float *  sd 
)

Definition at line 206 of file lstmrecognizer.cpp.

207 {
208 const int kOutputScale = INT8_MAX;
209 STATS stats(0, kOutputScale + 1);
210 for (int t = 0; t < outputs.Width(); ++t) {
211 int best_label = outputs.BestLabel(t, nullptr);
212 if (best_label != null_char_) {
213 float best_output = outputs.f(t)[best_label];
214 stats.add(static_cast<int>(kOutputScale * best_output), 1);
215 }
216 }
217 // If the output is all nulls it could be that the photometric interpretation
218 // is wrong, so make it look bad, so the other way can win, even if not great.
219 if (stats.get_total() == 0) {
220 *min_output = 0.0f;
221 *mean_output = 0.0f;
222 *sd = 1.0f;
223 } else {
224 *min_output = static_cast<float>(stats.min_bucket()) / kOutputScale;
225 *mean_output = stats.mean() / kOutputScale;
226 *sd = stats.sd() / kOutputScale;
227 }
228}
Definition: statistc.h:31

◆ RecognizeLine() [1/2]

bool tesseract::LSTMRecognizer::RecognizeLine ( const ImageData image_data,
bool  invert,
bool  debug,
bool  re_invert,
bool  upside_down,
float *  scale_factor,
NetworkIO inputs,
NetworkIO outputs 
)

Definition at line 232 of file lstmrecognizer.cpp.

235 {
236 // Maximum width of image to train on.
237 const int kMaxImageWidth = 2560;
238 // This ensures consistent recognition results.
240 int min_width = network_->XScaleFactor();
241 Pix* pix = Input::PrepareLSTMInputs(image_data, network_, min_width,
242 &randomizer_, scale_factor);
243 if (pix == nullptr) {
244 tprintf("Line cannot be recognized!!\n");
245 return false;
246 }
247 if (network_->IsTraining() && pixGetWidth(pix) > kMaxImageWidth) {
248 tprintf("Image too large to learn!! Size = %dx%d\n", pixGetWidth(pix),
249 pixGetHeight(pix));
250 pixDestroy(&pix);
251 return false;
252 }
253 if (upside_down) pixRotate180(pix, pix);
254 // Reduction factor from image to coords.
255 *scale_factor = min_width / *scale_factor;
256 inputs->set_int_mode(IsIntMode());
259 network_->Forward(debug, *inputs, nullptr, &scratch_space_, outputs);
260 // Check for auto inversion.
261 float pos_min, pos_mean, pos_sd;
262 OutputStats(*outputs, &pos_min, &pos_mean, &pos_sd);
263 if (invert && pos_min < 0.5) {
264 // Run again inverted and see if it is any better.
265 NetworkIO inv_inputs, inv_outputs;
266 inv_inputs.set_int_mode(IsIntMode());
268 pixInvert(pix, pix);
270 &inv_inputs);
271 network_->Forward(debug, inv_inputs, nullptr, &scratch_space_,
272 &inv_outputs);
273 float inv_min, inv_mean, inv_sd;
274 OutputStats(inv_outputs, &inv_min, &inv_mean, &inv_sd);
275 if (inv_min > pos_min && inv_mean > pos_mean && inv_sd < pos_sd) {
276 // Inverted did better. Use inverted data.
277 if (debug) {
278 tprintf("Inverting image: old min=%g, mean=%g, sd=%g, inv %g,%g,%g\n",
279 pos_min, pos_mean, pos_sd, inv_min, inv_mean, inv_sd);
280 }
281 *outputs = inv_outputs;
282 *inputs = inv_inputs;
283 } else if (re_invert) {
284 // Inverting was not an improvement, so undo and run again, so the
285 // outputs match the best forward result.
287 network_->Forward(debug, *inputs, nullptr, &scratch_space_, outputs);
288 }
289 }
290 pixDestroy(&pix);
291 if (debug) {
292 GenericVector<int> labels, coords;
293 LabelsFromOutputs(*outputs, &labels, &coords);
294 DisplayForward(*inputs, labels, coords, "LSTMForward", &debug_win_);
295 DebugActivationPath(*outputs, labels, coords);
296 }
297 return true;
298}
static void PreparePixInput(const StaticShape &shape, const Pix *pix, TRand *randomizer, NetworkIO *input)
Definition: input.cpp:111
static Pix * PrepareLSTMInputs(const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
Definition: input.cpp:83
NetworkScratch scratch_space_
void OutputStats(const NetworkIO &outputs, float *min_output, float *mean_output, float *sd)
void DebugActivationPath(const NetworkIO &outputs, const GenericVector< int > &labels, const GenericVector< int > &xcoords)
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)
void DisplayForward(const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window)
virtual void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)=0
bool IsTraining() const
Definition: network.h:115
virtual StaticShape InputShape() const
Definition: network.h:127

◆ RecognizeLine() [2/2]

void tesseract::LSTMRecognizer::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 
)

Definition at line 184 of file lstmrecognizer.cpp.

188 {
189 NetworkIO outputs;
190 float scale_factor;
191 NetworkIO inputs;
192 if (!RecognizeLine(image_data, invert, debug, false, false, &scale_factor,
193 &inputs, &outputs))
194 return;
195 if (search_ == nullptr) {
196 search_ =
197 new RecodeBeamSearch(recoder_, null_char_, SimpleTextOutput(), dict_);
198 }
199 search_->Decode(outputs, kDictRatio, kCertOffset, worst_dict_cert,
200 &GetUnicharset(), lstm_choice_mode);
201 search_->ExtractBestPathAsWords(line_box, scale_factor, debug,
202 &GetUnicharset(), words, lstm_choice_mode);
203}
const double kCertOffset
const double kDictRatio
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)
void ExtractBestPathAsWords(const TBOX &line_box, float scale_factor, bool debug, const UNICHARSET *unicharset, PointerVector< WERD_RES > *words, int lstm_choice_mode=0)
Definition: recodebeam.cpp:171

◆ sample_iteration()

int tesseract::LSTMRecognizer::sample_iteration ( ) const
inline

Definition at line 62 of file lstmrecognizer.h.

62{ return sample_iteration_; }

◆ ScaleLayerLearningRate()

void tesseract::LSTMRecognizer::ScaleLayerLearningRate ( const STRING id,
double  factor 
)
inline

Definition at line 117 of file lstmrecognizer.h.

117 {
118 ASSERT_HOST(network_ != nullptr && network_->type() == NT_SERIES);
119 ASSERT_HOST(id.length() > 1 && id[0] == ':');
120 auto* series = static_cast<Series*>(network_);
121 series->ScaleLayerLearningRate(&id[1], factor);
122 }

◆ ScaleLearningRate()

void tesseract::LSTMRecognizer::ScaleLearningRate ( double  factor)
inline

Definition at line 106 of file lstmrecognizer.h.

106 {
107 ASSERT_HOST(network_ != nullptr && network_->type() == NT_SERIES);
108 learning_rate_ *= factor;
111 for (int i = 0; i < layers.size(); ++i) {
112 ScaleLayerLearningRate(layers[i], factor);
113 }
114 }
115 }
void ScaleLayerLearningRate(const STRING &id, double factor)
GenericVector< STRING > EnumerateLayers() const

◆ Serialize()

bool tesseract::LSTMRecognizer::Serialize ( const TessdataManager mgr,
TFile fp 
) const

Definition at line 86 of file lstmrecognizer.cpp.

86 {
87 bool include_charsets = mgr == nullptr ||
88 !mgr->IsComponentAvailable(TESSDATA_LSTM_RECODER) ||
89 !mgr->IsComponentAvailable(TESSDATA_LSTM_UNICHARSET);
90 if (!network_->Serialize(fp)) return false;
91 if (include_charsets && !GetUnicharset().save_to_file(fp)) return false;
92 if (!network_str_.Serialize(fp)) return false;
93 if (!fp->Serialize(&training_flags_)) return false;
94 if (!fp->Serialize(&training_iteration_)) return false;
95 if (!fp->Serialize(&sample_iteration_)) return false;
96 if (!fp->Serialize(&null_char_)) return false;
97 if (!fp->Serialize(&adam_beta_)) return false;
98 if (!fp->Serialize(&learning_rate_)) return false;
99 if (!fp->Serialize(&momentum_)) return false;
100 if (include_charsets && IsRecoding() && !recoder_.Serialize(fp)) return false;
101 return true;
102}
bool Serialize(FILE *fp) const
Definition: strngs.cpp:146
bool Serialize(TFile *fp) const
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151

◆ SetIteration()

void tesseract::LSTMRecognizer::SetIteration ( int  iteration)
inline

Definition at line 141 of file lstmrecognizer.h.

141{ sample_iteration_ = iteration; }

◆ SetRandomSeed()

void tesseract::LSTMRecognizer::SetRandomSeed ( )
inlineprotected

Definition at line 215 of file lstmrecognizer.h.

215 {
216 int64_t seed = static_cast<int64_t>(sample_iteration_) * 0x10000001;
217 randomizer_.set_seed(seed);
219 }
int32_t IntRand()
Definition: helpers.h:50
void set_seed(uint64_t seed)
Definition: helpers.h:40

◆ SimpleTextOutput()

bool tesseract::LSTMRecognizer::SimpleTextOutput ( ) const
inline

Definition at line 70 of file lstmrecognizer.h.

70{ return OutputLossType() == LT_SOFTMAX; }
LossType OutputLossType() const

◆ training_iteration()

int tesseract::LSTMRecognizer::training_iteration ( ) const
inline

Definition at line 61 of file lstmrecognizer.h.

61{ return training_iteration_; }

Member Data Documentation

◆ adam_beta_

float tesseract::LSTMRecognizer::adam_beta_
protected

Definition at line 284 of file lstmrecognizer.h.

◆ ccutil_

CCUtil tesseract::LSTMRecognizer::ccutil_
protected

Definition at line 262 of file lstmrecognizer.h.

◆ debug_win_

ScrollView* tesseract::LSTMRecognizer::debug_win_
protected

Definition at line 296 of file lstmrecognizer.h.

◆ dict_

Dict* tesseract::LSTMRecognizer::dict_
protected

Definition at line 290 of file lstmrecognizer.h.

◆ learning_rate_

float tesseract::LSTMRecognizer::learning_rate_
protected

Definition at line 281 of file lstmrecognizer.h.

◆ momentum_

float tesseract::LSTMRecognizer::momentum_
protected

Definition at line 282 of file lstmrecognizer.h.

◆ network_

Network* tesseract::LSTMRecognizer::network_
protected

Definition at line 259 of file lstmrecognizer.h.

◆ network_str_

STRING tesseract::LSTMRecognizer::network_str_
protected

Definition at line 269 of file lstmrecognizer.h.

◆ null_char_

int32_t tesseract::LSTMRecognizer::null_char_
protected

Definition at line 279 of file lstmrecognizer.h.

◆ randomizer_

TRand tesseract::LSTMRecognizer::randomizer_
protected

Definition at line 287 of file lstmrecognizer.h.

◆ recoder_

UnicharCompress tesseract::LSTMRecognizer::recoder_
protected

Definition at line 266 of file lstmrecognizer.h.

◆ sample_iteration_

int32_t tesseract::LSTMRecognizer::sample_iteration_
protected

Definition at line 276 of file lstmrecognizer.h.

◆ scratch_space_

NetworkScratch tesseract::LSTMRecognizer::scratch_space_
protected

Definition at line 288 of file lstmrecognizer.h.

◆ search_

RecodeBeamSearch* tesseract::LSTMRecognizer::search_
protected

Definition at line 292 of file lstmrecognizer.h.

◆ training_flags_

int32_t tesseract::LSTMRecognizer::training_flags_
protected

Definition at line 272 of file lstmrecognizer.h.

◆ training_iteration_

int32_t tesseract::LSTMRecognizer::training_iteration_
protected

Definition at line 274 of file lstmrecognizer.h.


The documentation for this class was generated from the following files: