tesseract
5.0.0-alpha-619-ge9db
|
#include <lstmrecognizer.h>
|
| 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 | 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 53 of file lstmrecognizer.h.
◆ LSTMRecognizer() [1/2]
tesseract::LSTMRecognizer::LSTMRecognizer |
( |
| ) |
|
◆ LSTMRecognizer() [2/2]
tesseract::LSTMRecognizer::LSTMRecognizer |
( |
const STRING |
language_data_path_prefix | ) |
|
◆ ~LSTMRecognizer()
tesseract::LSTMRecognizer::~LSTMRecognizer |
( |
| ) |
|
◆ ConvertToInt()
void tesseract::LSTMRecognizer::ConvertToInt |
( |
| ) |
|
|
inline |
◆ DebugActivationPath()
Definition at line 392 of file lstmrecognizer.cpp.
398 for (
int start = 0; start < labels.
size(); start = end) {
406 const char* label =
DecodeLabel(labels, start, &end, &decoded);
409 for (
int i = start + 1; i < end; ++i) {
411 xcoords[i], xcoords[i + 1]);
◆ DebugActivationRange()
void tesseract::LSTMRecognizer::DebugActivationRange |
( |
const NetworkIO & |
outputs, |
|
|
const char * |
label, |
|
|
int |
best_choice, |
|
|
int |
x_start, |
|
|
int |
x_end |
|
) |
| |
|
protected |
Definition at line 419 of file lstmrecognizer.cpp.
422 tprintf(
"%s=%d On [%d, %d), scores=", label, best_choice, x_start, x_end);
423 double max_score = 0.0;
424 double mean_score = 0.0;
425 const int width = x_end - x_start;
426 for (
int x = x_start; x < x_end; ++x) {
427 const float* line = outputs.f(x);
428 const double score = line[best_choice] * 100.0;
429 if (score > max_score) max_score = score;
430 mean_score += score / width;
432 double best_score = 0.0;
433 for (
int c = 0; c < outputs.NumFeatures(); ++c) {
434 if (c != best_choice && line[c] > best_score) {
436 best_score = line[c];
442 tprintf(
", Mean=%g, max=%g\n", mean_score, max_score);
◆ DecodeLabel()
const char * tesseract::LSTMRecognizer::DecodeLabel |
( |
const GenericVector< int > & |
labels, |
|
|
int |
start, |
|
|
int * |
end, |
|
|
int * |
decoded |
|
) |
| |
|
protected |
Definition at line 507 of file lstmrecognizer.cpp.
514 if (decoded !=
nullptr) {
521 while (index < labels.
size() &&
523 code.Set(code.length(), labels[index++]);
524 while (index < labels.
size() && labels[index] ==
null_char_) ++index;
528 if (uni_id != INVALID_UNICHAR_ID &&
529 (index == labels.
size() ||
533 if (decoded !=
nullptr) *decoded = uni_id;
538 return "<Undecodable>";
540 if (decoded !=
nullptr) *decoded = labels[start];
541 if (labels[start] ==
null_char_)
return "<null>";
◆ DecodeLabels()
◆ DecodeSingleLabel()
const char * tesseract::LSTMRecognizer::DecodeSingleLabel |
( |
int |
label | ) |
|
|
protected |
◆ DeSerialize()
Definition at line 108 of file lstmrecognizer.cpp.
111 if (
network_ ==
nullptr)
return false;
112 bool include_charsets = mgr ==
nullptr ||
121 if (!fp->DeSerialize(&
null_char_))
return false;
122 if (!fp->DeSerialize(&
adam_beta_))
return false;
124 if (!fp->DeSerialize(&
momentum_))
return false;
125 if (include_charsets && !
LoadRecoder(fp))
return false;
126 if (!include_charsets && !
LoadCharsets(mgr))
return false;
◆ DisplayForward()
Definition at line 349 of file lstmrecognizer.cpp.
354 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics
355 Pix* input_pix = inputs.ToPix();
357 pixGetHeight(input_pix), window);
360 #endif // GRAPHICS_DISABLED
◆ DisplayLSTMOutput()
Definition at line 365 of file lstmrecognizer.cpp.
368 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics
372 for (
int start = 0; start < labels.
size(); start = end) {
373 int xpos = xcoords[start] * x_scale;
379 const char* str =
DecodeLabel(labels, start, &end,
nullptr);
380 if (*str ==
'\\') str =
"\\\\";
381 xpos = xcoords[(start + end) / 2] * x_scale;
382 window->
Text(xpos, height, str);
384 window->
Line(xpos, 0, xpos, height * 3 / 2);
387 #endif // GRAPHICS_DISABLED
◆ EnumerateLayers()
Definition at line 79 of file lstmrecognizer.h.
81 auto* series = static_cast<Series*>(
network_);
83 series->EnumerateLayers(
nullptr, &layers);
◆ GetDict() [1/2]
Dict* tesseract::LSTMRecognizer::GetDict |
( |
| ) |
|
|
inline |
◆ GetDict() [2/2]
const Dict* tesseract::LSTMRecognizer::GetDict |
( |
| ) |
const |
|
inline |
◆ GetLayer()
Network* tesseract::LSTMRecognizer::GetLayer |
( |
const STRING & |
id | ) |
const |
|
inline |
Definition at line 87 of file lstmrecognizer.h.
90 auto* series = static_cast<Series*>(
network_);
91 return series->GetLayer(&
id[1]);
◆ GetLayerLearningRate()
float tesseract::LSTMRecognizer::GetLayerLearningRate |
( |
const STRING & |
id | ) |
const |
|
inline |
Definition at line 94 of file lstmrecognizer.h.
98 auto* series = static_cast<Series*>(
network_);
99 return series->LayerLearningRate(&
id[1]);
◆ GetRecoder()
◆ GetUnicharset() [1/2]
UNICHARSET& tesseract::LSTMRecognizer::GetUnicharset |
( |
| ) |
|
|
inline |
◆ GetUnicharset() [2/2]
const UNICHARSET& tesseract::LSTMRecognizer::GetUnicharset |
( |
| ) |
const |
|
inline |
◆ IsIntMode()
bool tesseract::LSTMRecognizer::IsIntMode |
( |
| ) |
const |
|
inline |
◆ IsRecoding()
bool tesseract::LSTMRecognizer::IsRecoding |
( |
| ) |
const |
|
inline |
◆ IsTensorFlow()
bool tesseract::LSTMRecognizer::IsTensorFlow |
( |
| ) |
const |
|
inline |
◆ LabelsFromOutputs()
◆ LabelsViaReEncode()
◆ LabelsViaSimpleText()
Definition at line 488 of file lstmrecognizer.cpp.
493 const int width = output.Width();
494 for (
int t = 0; t < width; ++t) {
496 const int label = output.BestLabel(t, &score);
◆ learning_rate()
double tesseract::LSTMRecognizer::learning_rate |
( |
| ) |
const |
|
inline |
◆ Load()
◆ LoadCharsets()
bool tesseract::LSTMRecognizer::LoadCharsets |
( |
const TessdataManager * |
mgr | ) |
|
◆ LoadDictionary()
◆ LoadRecoder()
bool tesseract::LSTMRecognizer::LoadRecoder |
( |
TFile * |
fp | ) |
|
◆ null_char()
int tesseract::LSTMRecognizer::null_char |
( |
| ) |
const |
|
inline |
◆ NumInputs()
int tesseract::LSTMRecognizer::NumInputs |
( |
| ) |
const |
|
inline |
◆ NumOutputs()
int tesseract::LSTMRecognizer::NumOutputs |
( |
| ) |
const |
|
inline |
◆ OutputLossType()
LossType tesseract::LSTMRecognizer::OutputLossType |
( |
| ) |
const |
|
inline |
◆ OutputStats()
void tesseract::LSTMRecognizer::OutputStats |
( |
const NetworkIO & |
outputs, |
|
|
float * |
min_output, |
|
|
float * |
mean_output, |
|
|
float * |
sd |
|
) |
| |
Definition at line 238 of file lstmrecognizer.cpp.
240 const int kOutputScale = INT8_MAX;
241 STATS stats(0, kOutputScale + 1);
242 for (
int t = 0; t < outputs.Width(); ++t) {
243 int best_label = outputs.BestLabel(t,
nullptr);
245 float best_output = outputs.f(t)[best_label];
246 stats.add(static_cast<int>(kOutputScale * best_output), 1);
251 if (stats.get_total() == 0) {
256 *min_output = static_cast<float>(stats.min_bucket()) / kOutputScale;
257 *mean_output = stats.mean() / kOutputScale;
258 *sd = stats.sd() / kOutputScale;
◆ 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 264 of file lstmrecognizer.cpp.
269 const int kMaxImageWidth = 2560;
275 if (pix ==
nullptr) {
276 tprintf(
"Line cannot be recognized!!\n");
280 tprintf(
"Image too large to learn!! Size = %dx%d\n", pixGetWidth(pix),
285 if (upside_down) pixRotate180(pix, pix);
287 *scale_factor = min_width / *scale_factor;
293 float pos_min, pos_mean, pos_sd;
294 OutputStats(*outputs, &pos_min, &pos_mean, &pos_sd);
295 if (invert && pos_min < 0.5) {
297 NetworkIO inv_inputs, inv_outputs;
305 float inv_min, inv_mean, inv_sd;
306 OutputStats(inv_outputs, &inv_min, &inv_mean, &inv_sd);
307 if (inv_min > pos_min && inv_mean > pos_mean && inv_sd < pos_sd) {
310 tprintf(
"Inverting image: old min=%g, mean=%g, sd=%g, inv %g,%g,%g\n",
311 pos_min, pos_mean, pos_sd, inv_min, inv_mean, inv_sd);
313 *outputs = inv_outputs;
314 *inputs = inv_inputs;
315 }
else if (re_invert) {
◆ 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 , |
|
|
int |
lstm_choice_amount = 5 |
|
) |
| |
Definition at line 187 of file lstmrecognizer.cpp.
196 if (!
RecognizeLine(image_data, invert, debug,
false,
false, &scale_factor,
208 if (lstm_choice_mode){
210 for (
int i = 0; i < lstm_choice_amount; ++i) {
218 for (
int i = 0; i < words->
size(); ++i) {
219 for (
int j = 0; j < words->
get(i)->end; ++j) {
220 if (char_it < search_->ctc_choices.size())
221 words->
get(i)->CTC_symbol_choices.push_back(
223 if (char_it < search_->segmentedTimesteps.size())
224 words->
get(i)->segmented_timesteps.push_back(
229 &words->
get(i)->segmented_timesteps);
◆ sample_iteration()
int tesseract::LSTMRecognizer::sample_iteration |
( |
| ) |
const |
|
inline |
◆ ScaleLayerLearningRate()
void tesseract::LSTMRecognizer::ScaleLayerLearningRate |
( |
const STRING & |
id, |
|
|
double |
factor |
|
) |
| |
|
inline |
Definition at line 116 of file lstmrecognizer.h.
119 auto* series = static_cast<Series*>(
network_);
120 series->ScaleLayerLearningRate(&
id[1], factor);
◆ ScaleLearningRate()
void tesseract::LSTMRecognizer::ScaleLearningRate |
( |
double |
factor | ) |
|
|
inline |
◆ Serialize()
Definition at line 89 of file lstmrecognizer.cpp.
90 bool include_charsets = mgr ==
nullptr ||
100 if (!fp->Serialize(&
adam_beta_))
return false;
102 if (!fp->Serialize(&
momentum_))
return false;
◆ SetIteration()
void tesseract::LSTMRecognizer::SetIteration |
( |
int |
iteration | ) |
|
|
inline |
◆ SetRandomSeed()
void tesseract::LSTMRecognizer::SetRandomSeed |
( |
| ) |
|
|
inlineprotected |
◆ SimpleTextOutput()
bool tesseract::LSTMRecognizer::SimpleTextOutput |
( |
| ) |
const |
|
inline |
◆ training_iteration()
int tesseract::LSTMRecognizer::training_iteration |
( |
| ) |
const |
|
inline |
◆ adam_beta_
float tesseract::LSTMRecognizer::adam_beta_ |
|
protected |
◆ ccutil_
CCUtil tesseract::LSTMRecognizer::ccutil_ |
|
protected |
◆ debug_win_
◆ dict_
Dict* tesseract::LSTMRecognizer::dict_ |
|
protected |
◆ learning_rate_
float tesseract::LSTMRecognizer::learning_rate_ |
|
protected |
◆ momentum_
float tesseract::LSTMRecognizer::momentum_ |
|
protected |
◆ network_
Network* tesseract::LSTMRecognizer::network_ |
|
protected |
◆ network_str_
STRING tesseract::LSTMRecognizer::network_str_ |
|
protected |
◆ null_char_
int32_t tesseract::LSTMRecognizer::null_char_ |
|
protected |
◆ randomizer_
TRand tesseract::LSTMRecognizer::randomizer_ |
|
protected |
◆ recoder_
◆ sample_iteration_
int32_t tesseract::LSTMRecognizer::sample_iteration_ |
|
protected |
◆ scratch_space_
◆ search_
◆ training_flags_
int32_t tesseract::LSTMRecognizer::training_flags_ |
|
protected |
◆ training_iteration_
int32_t tesseract::LSTMRecognizer::training_iteration_ |
|
protected |
The documentation for this class was generated from the following files:
bool load_from_file(const char *const filename, bool skip_fragments)
static constexpr float kMinCertainty
std::vector< std::unordered_set< int > > excludedUnichars
LossType loss_type() const
virtual void SetRandomizer(TRand *randomizer)
void DebugActivationPath(const NetworkIO &outputs, const GenericVector< int > &labels, const GenericVector< int > &xcoords)
std::vector< std::vector< std::pair< const char *, float > > > ctc_choices
void LabelsViaSimpleText(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
void ScaleLayerLearningRate(const STRING &id, double factor)
bool DeSerialize(TFile *fp)
RecodeBeamSearch * search_
virtual int XScaleFactor() const
bool LoadDictionary(const ParamsVectors *params, const char *lang, TessdataManager *mgr)
int32_t sample_iteration_
void Decode(const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset, int lstm_choice_mode=0)
GenericVector< STRING > EnumerateLayers() const
char * user_patterns_file
void DebugActivationRange(const NetworkIO &outputs, const char *label, int best_choice, int x_start, int x_end)
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)
static TESS_API DawgCache * GlobalDawgCache()
void DisplayForward(const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window)
STRING language_data_path_prefix
static void PreparePixInput(const StaticShape &shape, const Pix *pix, TRand *randomizer, NetworkIO *input)
const UNICHARSET & GetUnicharset() const
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
static Pix * PrepareLSTMInputs(const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
bool DeSerialize(bool swap, FILE *fp)
bool IsValidFirstCode(int code) const
bool Serialize(FILE *fp) const
bool save_to_file(const char *const filename) const
bool LoadRecoder(TFile *fp)
bool TestFlag(NetworkFlags flag) const
virtual void CacheXScaleFactor(int factor)
bool LoadCharsets(const TessdataManager *mgr)
virtual StaticShape OutputShape(const StaticShape &input_shape) 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_
static Network * CreateFromFile(TFile *fp)
void extractSymbolChoices(const UNICHARSET *unicharset)
int DecodeUnichar(const RecodedCharID &code) const
virtual void ConvertToInt()
LossType OutputLossType() const
virtual StaticShape InputShape() const
void SetupForLoad(DawgCache *dawg_cache)
int32_t training_iteration_
void OutputStats(const NetworkIO &outputs, float *min_output, float *mean_output, float *sd)
bool SimpleTextOutput() const
void DecodeSecondaryBeams(const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset, int lstm_choice_mode=0)
void segmentTimestepsByCharacters()
void LoadLSTM(const STRING &lang, TessdataManager *data_file)
void set_seed(uint64_t seed)
void LabelsViaReEncode(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
static const int kMaxCodeLen
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
const char * DecodeSingleLabel(int label)
std::vector< std::vector< std::pair< const char *, float > > > combineSegmentedTimesteps(std::vector< std::vector< std::vector< std::pair< const char *, float >>>> *segmentedTimesteps)
void SetupPassThrough(const UNICHARSET &unicharset)
void Line(int x1, int y1, int x2, int y2)
static int DisplayImage(Pix *pix, ScrollView *window)
DLLSYM void tprintf(const char *format,...)
void ExtractBestPathAsWords(const TBOX &line_box, float scale_factor, bool debug, const UNICHARSET *unicharset, PointerVector< WERD_RES > *words, int lstm_choice_mode=0)
void DisplayLSTMOutput(const GenericVector< int > &labels, const GenericVector< int > &xcoords, int height, ScrollView *window)
const char * DecodeLabel(const GenericVector< int > &labels, int start, int *end, int *decoded)
virtual bool Serialize(TFile *fp) const
int EncodeUnichar(int unichar_id, RecodedCharID *code) const
void Text(int x, int y, const char *mystring)
char * user_patterns_suffix
void ExtractBestPathAsLabels(GenericVector< int > *labels, GenericVector< int > *xcoords) const
std::vector< std::vector< std::vector< std::pair< const char *, float > > > > segmentedTimesteps
virtual void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)=0
bool Serialize(TFile *fp) const