21 #include "config_auto.h" 26 #include "allheaders.h" 52 training_iteration_(0),
60 debug_win_(nullptr) {}
73 if (lang ==
nullptr)
return true;
81 bool include_charsets = mgr ==
nullptr ||
102 if (
network_ ==
nullptr)
return false;
103 bool include_charsets = mgr ==
nullptr ||
116 if (include_charsets && !
LoadRecoder(fp))
return false;
117 if (!include_charsets && !
LoadCharsets(mgr))
return false;
140 tprintf(
"Space was garbled in recoding!!\n");
163 tprintf(
"Failed to load any lstm-specific dictionaries for lang %s!!\n",
173 bool debug,
double worst_dict_cert,
174 const TBOX& line_box,
176 int lstm_choice_mode) {
180 if (!
RecognizeLine(image_data, invert, debug,
false,
false, &scale_factor,
195 float* mean_output,
float* sd) {
196 const int kOutputScale = INT8_MAX;
197 STATS stats(0, kOutputScale + 1);
198 for (
int t = 0; t < outputs.
Width(); ++t) {
199 int best_label = outputs.
BestLabel(t,
nullptr);
201 float best_output = outputs.
f(t)[best_label];
202 stats.
add(static_cast<int>(kOutputScale * best_output), 1);
212 *min_output =
static_cast<float>(stats.
min_bucket()) / kOutputScale;
213 *mean_output = stats.
mean() / kOutputScale;
214 *sd = stats.
sd() / kOutputScale;
221 bool debug,
bool re_invert,
bool upside_down,
225 const int kMaxImageWidth = 2560;
231 if (pix ==
nullptr) {
232 tprintf(
"Line cannot be recognized!!\n");
236 tprintf(
"Image too large to learn!! Size = %dx%d\n", pixGetWidth(pix),
241 if (upside_down) pixRotate180(pix, pix);
243 *scale_factor = min_width / *scale_factor;
249 float pos_min, pos_mean, pos_sd;
250 OutputStats(*outputs, &pos_min, &pos_mean, &pos_sd);
251 if (invert && pos_min < 0.5) {
260 float inv_min, inv_mean, inv_sd;
261 OutputStats(inv_outputs, &inv_min, &inv_mean, &inv_sd);
262 if (inv_min > pos_min && inv_mean > pos_mean && inv_sd < pos_sd) {
265 tprintf(
"Inverting image: old min=%g, mean=%g, sd=%g, inv %g,%g,%g\n",
266 pos_min, pos_mean, pos_sd, inv_min, inv_mean, inv_sd);
268 *outputs = inv_outputs;
269 *inputs = inv_inputs;
270 }
else if (re_invert) {
292 for (
int start = 0; start < labels.
size(); start = end) {
296 result +=
DecodeLabel(labels, start, &end,
nullptr);
307 const char* window_name,
309 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics 310 Pix* input_pix = inputs.
ToPix();
312 pixGetHeight(input_pix), window);
315 #endif // GRAPHICS_DISABLED 323 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics 327 for (
int start = 0; start < labels.
size(); start = end) {
328 int xpos = xcoords[start] * x_scale;
334 const char* str =
DecodeLabel(labels, start, &end,
nullptr);
335 if (*str ==
'\\') str =
"\\\\";
336 xpos = xcoords[(start + end) / 2] * x_scale;
337 window->
Text(xpos, height, str);
339 window->
Line(xpos, 0, xpos, height * 3 / 2);
342 #endif // GRAPHICS_DISABLED 353 for (
int start = 0; start < labels.
size(); start = end) {
361 const char* label =
DecodeLabel(labels, start, &end, &decoded);
364 for (
int i = start + 1; i < end; ++i) {
366 xcoords[i], xcoords[i + 1]);
375 const char* label,
int best_choice,
376 int x_start,
int x_end) {
377 tprintf(
"%s=%d On [%d, %d), scores=", label, best_choice, x_start, x_end);
378 double max_score = 0.0;
379 double mean_score = 0.0;
380 const int width = x_end - x_start;
381 for (
int x = x_start; x < x_end; ++x) {
382 const float* line = outputs.
f(x);
383 const double score = line[best_choice] * 100.0;
384 if (score > max_score) max_score = score;
385 mean_score += score / width;
387 double best_score = 0.0;
389 if (c != best_choice && line[c] > best_score) {
391 best_score = line[c];
397 tprintf(
", Mean=%g, max=%g\n", mean_score, max_score);
403 #if 0 // TODO: unused, remove if still unused after 2020. 404 static bool NullIsBest(
const NetworkIO& output,
float null_thr,
405 int null_char,
int t) {
406 if (output.
f(t)[null_char] >= null_thr)
return true;
448 const int width = output.
Width();
449 for (
int t = 0; t < width; ++t) {
451 const int label = output.
BestLabel(t, &score);
463 int start,
int* end,
int* decoded) {
469 if (decoded !=
nullptr) {
476 while (index < labels.
size() &&
478 code.
Set(code.
length(), labels[index++]);
479 while (index < labels.
size() && labels[index] ==
null_char_) ++index;
483 if (uni_id != INVALID_UNICHAR_ID &&
484 (index == labels.
size() ||
488 if (decoded !=
nullptr) *decoded = uni_id;
493 return "<Undecodable>";
495 if (decoded !=
nullptr) *decoded = labels[start];
496 if (labels[start] ==
null_char_)
return "<null>";
511 if (label == INVALID_UNICHAR_ID)
return "..";
bool Serialize(FILE *fp) const
void set_int_mode(bool is_quantized)
const UNICHARSET & GetUnicharset() const
static DawgCache * GlobalDawgCache()
int BestLabel(int t, float *score) const
void ExtractBestPathAsLabels(GenericVector< int > *labels, GenericVector< int > *xcoords) const
bool LoadCharsets(const TessdataManager *mgr)
bool LoadDictionary(const char *lang, TessdataManager *mgr)
virtual void CacheXScaleFactor(int factor)
static const int kMaxCodeLen
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
static Network * CreateFromFile(TFile *fp)
const char * DecodeSingleLabel(int label)
int DecodeUnichar(const RecodedCharID &code) const
bool GetComponent(TessdataType type, TFile *fp)
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
void DebugActivationRange(const NetworkIO &outputs, const char *label, int best_choice, int x_start, int x_end)
void Decode(const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset, int lstm_choice_mode=0)
int32_t min_bucket() const
bool save_to_file(const char *const filename) const
int EncodeUnichar(int unichar_id, RecodedCharID *code) const
virtual void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)
bool DeSerialize(bool swap, FILE *fp)
STRING DecodeLabels(const GenericVector< int > &labels)
bool DeSerialize(char *data, size_t count=1)
const char * DecodeLabel(const GenericVector< int > &labels, int start, int *end, int *decoded)
virtual void SetRandomizer(TRand *randomizer)
bool SimpleTextOutput() const
NetworkScratch scratch_space_
void Set(int index, int value)
virtual int XScaleFactor() const
virtual StaticShape InputShape() const
void DebugActivationPath(const NetworkIO &outputs, const GenericVector< int > &labels, const GenericVector< int > &xcoords)
virtual bool Serialize(TFile *fp) const
void DisplayForward(const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window)
void OutputStats(const NetworkIO &outputs, float *min_output, float *mean_output, float *sd)
void Text(int x, int y, const char *mystring)
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
bool Load(const char *lang, TessdataManager *mgr)
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
void LabelsViaReEncode(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
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)
bool Serialize(const char *data, size_t count=1)
void DisplayLSTMOutput(const GenericVector< int > &labels, const GenericVector< int > &xcoords, int height, ScrollView *window)
DLLSYM void tprintf(const char *format,...)
void LoadLSTM(const STRING &lang, TessdataManager *data_file)
void SetupPassThrough(const UNICHARSET &unicharset)
void SetupForLoad(DawgCache *dawg_cache)
void add(int32_t value, int32_t count)
static int DisplayImage(Pix *pix, ScrollView *window)
static void PreparePixInput(const StaticShape &shape, const Pix *pix, TRand *randomizer, NetworkIO *input)
static const float kMinCertainty
void LabelsViaSimpleText(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
bool IsValidFirstCode(int code) const
void ExtractBestPathAsWords(const TBOX &line_box, float scale_factor, bool debug, const UNICHARSET *unicharset, PointerVector< WERD_RES > *words, int lstm_choice_mode=0)
bool DeSerialize(TFile *fp)
bool LoadRecoder(TFile *fp)
int32_t sample_iteration_
int32_t training_iteration_
bool load_from_file(const char *const filename, bool skip_fragments)
bool Serialize(const TessdataManager *mgr, TFile *fp) const
bool Serialize(TFile *fp) const
int32_t get_total() const
void Line(int x1, int y1, int x2, int y2)
RecodeBeamSearch * search_
static Pix * PrepareLSTMInputs(const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)
bool IsComponentAvailable(TessdataType type) const