21# include "config_auto.h"
26#include "allheaders.h"
57 training_iteration_(0),
65 debug_win_(nullptr) {}
79 if (lang ==
nullptr)
return true;
87 bool include_charsets = mgr ==
nullptr ||
91 if (include_charsets && !
GetUnicharset().save_to_file(fp))
return false;
108 if (
network_ ==
nullptr)
return false;
109 bool include_charsets = mgr ==
nullptr ||
122 if (include_charsets && !
LoadRecoder(fp))
return false;
123 if (!include_charsets && !
LoadCharsets(mgr))
return false;
146 tprintf(
"Space was garbled in recoding!!\n");
175 tprintf(
"Failed to load any lstm-specific dictionaries for lang %s!!\n",
185 bool debug,
double worst_dict_cert,
186 const TBOX& line_box,
188 int lstm_choice_mode) {
192 if (!
RecognizeLine(image_data, invert, debug,
false,
false, &scale_factor,
207 float* mean_output,
float* sd) {
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);
213 float best_output = outputs.
f(t)[best_label];
214 stats.
add(
static_cast<int>(kOutputScale * best_output), 1);
224 *min_output =
static_cast<float>(stats.
min_bucket()) / kOutputScale;
225 *mean_output = stats.
mean() / kOutputScale;
226 *sd = stats.
sd() / kOutputScale;
233 bool debug,
bool re_invert,
bool upside_down,
237 const int kMaxImageWidth = 2560;
243 if (pix ==
nullptr) {
244 tprintf(
"Line cannot be recognized!!\n");
248 tprintf(
"Image too large to learn!! Size = %dx%d\n", pixGetWidth(pix),
253 if (upside_down) pixRotate180(pix, pix);
255 *scale_factor = min_width / *scale_factor;
261 float pos_min, pos_mean, pos_sd;
262 OutputStats(*outputs, &pos_min, &pos_mean, &pos_sd);
263 if (invert && pos_min < 0.5) {
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) {
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);
281 *outputs = inv_outputs;
282 *inputs = inv_inputs;
283 }
else if (re_invert) {
305 for (
int start = 0; start < labels.
size(); start = end) {
309 result +=
DecodeLabel(labels, start, &end,
nullptr);
320 const char* window_name,
322#ifndef GRAPHICS_DISABLED
323 Pix* input_pix = inputs.
ToPix();
325 pixGetHeight(input_pix), window);
336#ifndef GRAPHICS_DISABLED
340 for (
int start = 0; start < labels.
size(); start = end) {
341 int xpos = xcoords[start] * x_scale;
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);
352 window->
Line(xpos, 0, xpos, height * 3 / 2);
366 for (
int start = 0; start < labels.
size(); start = end) {
374 const char* label =
DecodeLabel(labels, start, &end, &decoded);
377 for (
int i = start + 1; i < end; ++i) {
379 xcoords[i], xcoords[i + 1]);
388 const char* label,
int best_choice,
389 int x_start,
int x_end) {
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;
400 double best_score = 0.0;
402 if (c != best_choice && line[c] > best_score) {
404 best_score = line[c];
410 tprintf(
", Mean=%g, max=%g\n", mean_score, max_score);
417static bool NullIsBest(
const NetworkIO& output,
float null_thr,
418 int null_char,
int t) {
419 if (output.
f(t)[null_char] >= null_thr)
return true;
461 const int width = output.
Width();
462 for (
int t = 0; t < width; ++t) {
464 const int label = output.
BestLabel(t, &score);
476 int start,
int* end,
int* decoded) {
482 if (decoded !=
nullptr) {
489 while (index < labels.
size() &&
491 code.
Set(code.
length(), labels[index++]);
492 while (index < labels.
size() && labels[index] ==
null_char_) ++index;
496 if (uni_id != INVALID_UNICHAR_ID &&
497 (index == labels.
size() ||
501 if (decoded !=
nullptr) *decoded = uni_id;
506 return "<Undecodable>";
508 if (decoded !=
nullptr) *decoded = labels[start];
509 if (labels[start] ==
null_char_)
return "<null>";
524 if (label == INVALID_UNICHAR_ID)
return "..";
DLLSYM 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
STRING language_data_path_prefix
bool Serialize(const char *data, size_t count=1)
bool DeSerialize(char *data, size_t count=1)
bool Serialize(FILE *fp) const
bool DeSerialize(bool swap, FILE *fp)
bool GetComponent(TessdataType type, TFile *fp)
bool IsComponentAvailable(TessdataType type) const
void Set(int index, int value)
static const int kMaxCodeLen
int EncodeUnichar(int 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 TESS_API DawgCache * GlobalDawgCache()
char * user_patterns_suffix
void SetupForLoad(DawgCache *dawg_cache)
void LoadLSTM(const STRING &lang, TessdataManager *data_file)
char * user_patterns_file
static void PreparePixInput(const StaticShape &shape, const Pix *pix, TRand *randomizer, NetworkIO *input)
static Pix * PrepareLSTMInputs(const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
bool LoadRecoder(TFile *fp)
bool SimpleTextOutput() const
bool Load(const ParamsVectors *params, const char *lang, TessdataManager *mgr)
NetworkScratch scratch_space_
const char * DecodeSingleLabel(int label)
void DisplayLSTMOutput(const GenericVector< int > &labels, const GenericVector< int > &xcoords, int height, ScrollView *window)
bool LoadCharsets(const TessdataManager *mgr)
const char * DecodeLabel(const GenericVector< int > &labels, int start, int *end, int *decoded)
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)
STRING DecodeLabels(const GenericVector< int > &labels)
void LabelsViaReEncode(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
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)
RecodeBeamSearch * search_
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)
int32_t training_iteration_
bool LoadDictionary(const ParamsVectors *params, const char *lang, TessdataManager *mgr)
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)
const UNICHARSET & GetUnicharset() const
int32_t sample_iteration_
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
void DisplayForward(const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window)
void LabelsViaSimpleText(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
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)
static int DisplayImage(Pix *pix, ScrollView *window)
virtual bool Serialize(TFile *fp) const
virtual void CacheXScaleFactor(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)
void ExtractBestPathAsLabels(GenericVector< int > *labels, GenericVector< 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 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)