25 #include <allheaders.h>
40 #include <unordered_set>
58 , training_iteration_(0)
59 , sample_iteration_(0)
61 , learning_rate_(0.0f)
66 , debug_win_(nullptr) {}
206 tprintf(
"Space was garbled in recoding!!\n");
228 dict_->user_words_file.ResetFrom(params);
229 dict_->user_words_suffix.ResetFrom(params);
230 dict_->user_patterns_file.ResetFrom(params);
231 dict_->user_patterns_suffix.ResetFrom(params);
238 tprintf(
"Failed to load any lstm-specific dictionaries for lang %s!!\n", lang.c_str());
248 double worst_dict_cert,
const TBOX &line_box,
250 int lstm_choice_amount) {
254 if (!
RecognizeLine(image_data, invert, debug,
false,
false, &scale_factor, &inputs, &outputs)) {
265 if (lstm_choice_mode) {
267 for (
int i = 0; i < lstm_choice_amount; ++i) {
273 unsigned char_it = 0;
274 for (
size_t i = 0; i < words->
size(); ++i) {
275 for (
int j = 0; j < words->
at(i)->end; ++j) {
276 if (char_it < search_->ctc_choices.size()) {
279 if (char_it < search_->segmentedTimesteps.size()) {
284 words->
at(i)->timesteps =
296 const int kOutputScale = INT8_MAX;
297 STATS stats(0, kOutputScale + 1);
298 for (
int t = 0; t < outputs.
Width(); ++t) {
299 int best_label = outputs.
BestLabel(t,
nullptr);
301 float best_output = outputs.
f(t)[best_label];
302 stats.
add(
static_cast<int>(kOutputScale * best_output), 1);
312 *min_output =
static_cast<float>(stats.
min_bucket()) / kOutputScale;
313 *mean_output = stats.
mean() / kOutputScale;
314 *sd = stats.
sd() / kOutputScale;
321 bool re_invert,
bool upside_down,
float *scale_factor,
327 if (pix ==
nullptr) {
328 tprintf(
"Line cannot be recognized!!\n");
332 const int kMaxImageWidth = 128 * pixGetHeight(pix);
334 tprintf(
"Image too large to learn!! Size = %dx%d\n", pixGetWidth(pix), pixGetHeight(pix));
339 pixRotate180(pix, pix);
342 *scale_factor = min_width / *scale_factor;
349 float pos_min, pos_mean, pos_sd;
350 OutputStats(*outputs, &pos_min, &pos_mean, &pos_sd);
351 if (pos_mean < 0.5f) {
359 float inv_min, inv_mean, inv_sd;
360 OutputStats(inv_outputs, &inv_min, &inv_mean, &inv_sd);
361 if (inv_mean > pos_mean) {
364 tprintf(
"Inverting image: old min=%g, mean=%g, sd=%g, inv %g,%g,%g\n", pos_min, pos_mean,
365 pos_sd, inv_min, inv_mean, inv_sd);
367 *outputs = inv_outputs;
368 *inputs = inv_inputs;
369 }
else if (re_invert) {
380 std::vector<int> labels, coords;
382 #ifndef GRAPHICS_DISABLED
395 for (
unsigned start = 0; start < labels.size(); start = end) {
399 result +=
DecodeLabel(labels, start, &end,
nullptr);
405 #ifndef GRAPHICS_DISABLED
410 const std::vector<int> &label_coords,
const char *window_name,
413 Network::ClearWindow(
false, window_name, pixGetWidth(input_pix), pixGetHeight(input_pix), window);
421 const std::vector<int> &xcoords,
int height,
426 for (
unsigned start = 0; start < labels.size(); start = end) {
427 int xpos = xcoords[start] * x_scale;
433 const char *str =
DecodeLabel(labels, start, &end,
nullptr);
437 xpos = xcoords[(start + end) / 2] * x_scale;
438 window->
Text(xpos, height, str);
440 window->
Line(xpos, 0, xpos, height * 3 / 2);
450 const std::vector<int> &xcoords) {
451 if (xcoords[0] > 0) {
455 for (
unsigned start = 0; start < labels.size(); start = end) {
462 const char *label =
DecodeLabel(labels, start, &end, &decoded);
464 for (
unsigned i = start + 1; i < end; ++i) {
475 int best_choice,
int x_start,
int x_end) {
476 tprintf(
"%s=%d On [%d, %d), scores=", label, best_choice, x_start, x_end);
477 double max_score = 0.0;
478 double mean_score = 0.0;
479 const int width = x_end - x_start;
480 for (
int x = x_start; x < x_end; ++x) {
481 const float *line = outputs.
f(x);
482 const double score = line[best_choice] * 100.0;
483 if (score > max_score) {
486 mean_score += score / width;
488 double best_score = 0.0;
490 if (c != best_choice && line[c] > best_score) {
492 best_score = line[c];
497 tprintf(
", Mean=%g, max=%g\n", mean_score, max_score);
504 static bool NullIsBest(
const NetworkIO& output,
float null_thr,
505 int null_char,
int t) {
506 if (output.
f(t)[null_char] >= null_thr)
return true;
518 std::vector<int> *xcoords) {
529 std::vector<int> *xcoords) {
541 std::vector<int> *xcoords) {
544 const int width = output.
Width();
545 for (
int t = 0; t < width; ++t) {
547 const int label = output.
BestLabel(t, &score);
549 labels->push_back(label);
550 xcoords->push_back(t);
553 xcoords->push_back(width);
565 if (decoded !=
nullptr) {
571 unsigned index = start;
573 code.
Set(code.
length(), labels[index++]);
574 while (index < labels.size() && labels[index] ==
null_char_) {
580 if (uni_id != INVALID_UNICHAR_ID &&
584 if (decoded !=
nullptr) {
593 return "<Undecodable>";
595 if (decoded !=
nullptr) {
596 *decoded = labels[start];
619 if (label == INVALID_UNICHAR_ID) {
void tprintf(const char *format,...)
@ TESSDATA_LSTM_UNICHARSET
void add(int32_t value, int32_t count)
int32_t get_total() const
int32_t min_bucket() const
std::string language_data_path_prefix
bool DeSerialize(std::string &data)
bool Serialize(const std::string &data)
bool GetComponent(TessdataType type, TFile *fp)
bool IsComponentAvailable(TessdataType type) const
void Set(int index, int value)
static const int kMaxCodeLen
int EncodeUnichar(unsigned unichar_id, RecodedCharID *code) const
bool DeSerialize(TFile *fp)
bool IsValidFirstCode(int code) const
void SetupPassThrough(const UNICHARSET &unicharset)
int DecodeUnichar(const RecodedCharID &code) const
bool Serialize(TFile *fp) const
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
bool load_from_file(const char *const filename, bool skip_fragments)
static DawgCache * GlobalDawgCache()
void LoadLSTM(const std::string &lang, TessdataManager *data_file)
void SetupForLoad(DawgCache *dawg_cache)
static Image PrepareLSTMInputs(const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
static void PreparePixInput(const StaticShape &shape, const Image pix, TRand *randomizer, NetworkIO *input)
void DebugActivationPath(const NetworkIO &outputs, const std::vector< int > &labels, const std::vector< int > &xcoords)
std::string DecodeLabels(const std::vector< int > &labels)
bool LoadRecoder(TFile *fp)
bool SimpleTextOutput() const
NetworkScratch scratch_space_
void LabelsViaReEncode(const NetworkIO &output, std::vector< int > *labels, std::vector< int > *xcoords)
const char * DecodeSingleLabel(int label)
bool LoadCharsets(const TessdataManager *mgr)
const UNICHARSET & GetUnicharset() const
void LabelsFromOutputs(const NetworkIO &outputs, std::vector< int > *labels, std::vector< int > *xcoords)
void DisplayForward(const NetworkIO &inputs, const std::vector< int > &labels, const std::vector< int > &label_coords, const char *window_name, ScrollView **window)
void OutputStats(const NetworkIO &outputs, float *min_output, float *mean_output, float *sd)
const char * DecodeLabel(const std::vector< int > &labels, unsigned start, unsigned *end, int *decoded)
bool Load(const ParamsVectors *params, const std::string &lang, TessdataManager *mgr)
RecodeBeamSearch * search_
void LabelsViaSimpleText(const NetworkIO &output, std::vector< int > *labels, std::vector< int > *xcoords)
int32_t training_iteration_
void DisplayLSTMOutput(const std::vector< int > &labels, const std::vector< int > &xcoords, int height, ScrollView *window)
bool Serialize(const TessdataManager *mgr, TFile *fp) const
void DebugActivationRange(const NetworkIO &outputs, const char *label, int best_choice, int x_start, int x_end)
int32_t sample_iteration_
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
bool LoadDictionary(const ParamsVectors *params, const std::string &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)
virtual int XScaleFactor() const
virtual void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)=0
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
static Network * CreateFromFile(TFile *fp)
virtual bool Serialize(TFile *fp) const
static int DisplayImage(Image pix, ScrollView *window)
virtual void CacheXScaleFactor([[maybe_unused]] int factor)
virtual void SetRandomizer(TRand *randomizer)
virtual StaticShape InputShape() const
void set_int_mode(bool is_quantized)
int BestLabel(int t, float *score) const
void Decode(const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset, int lstm_choice_mode=0)
std::vector< std::vector< std::pair< const char *, float > > > ctc_choices
void DecodeSecondaryBeams(const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset, int lstm_choice_mode=0)
std::vector< std::vector< std::vector< std::pair< const char *, float > > > > segmentedTimesteps
void extractSymbolChoices(const UNICHARSET *unicharset)
std::vector< std::unordered_set< int > > excludedUnichars
std::vector< std::vector< std::pair< const char *, float > > > combineSegmentedTimesteps(std::vector< std::vector< std::vector< std::pair< const char *, float >>>> *segmentedTimesteps)
void ExtractBestPathAsLabels(std::vector< int > *labels, std::vector< int > *xcoords) const
static constexpr float kMinCertainty
void ExtractBestPathAsWords(const TBOX &line_box, float scale_factor, bool debug, const UNICHARSET *unicharset, PointerVector< WERD_RES > *words, int lstm_choice_mode=0)
void segmentTimestepsByCharacters()
void Line(int x1, int y1, int x2, int y2)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
void Text(int x, int y, const char *mystring)