21 #include "config_auto.h" 26 #include "allheaders.h" 55 training_iteration_(0),
76 if (lang ==
nullptr)
return true;
84 bool include_charsets = mgr ==
nullptr ||
109 bool include_charsets = mgr ==
nullptr ||
127 if (include_charsets && !
LoadRecoder(fp))
return false;
128 if (!include_charsets && !
LoadCharsets(mgr))
return false;
151 tprintf(
"Space was garbled in recoding!!\n");
174 tprintf(
"Failed to load any lstm-specific dictionaries for lang %s!!\n",
184 bool debug,
double worst_dict_cert,
185 const TBOX& line_box,
190 if (!
RecognizeLine(image_data, invert, debug,
false,
false, &scale_factor,
197 search_->
Decode(outputs, kDictRatio, kCertOffset, worst_dict_cert, NULL);
204 float* mean_output,
float* sd) {
206 STATS stats(0, kOutputScale + 1);
207 for (
int t = 0; t < outputs.
Width(); ++t) {
208 int best_label = outputs.
BestLabel(t, NULL);
210 float best_output = outputs.
f(t)[best_label];
211 stats.
add(static_cast<int>(kOutputScale * best_output), 1);
221 *min_output =
static_cast<float>(stats.
min_bucket()) / kOutputScale;
222 *mean_output = stats.
mean() / kOutputScale;
223 *sd = stats.
sd() / kOutputScale;
230 bool debug,
bool re_invert,
bool upside_down,
234 const int kMaxImageWidth = 2560;
241 tprintf(
"Line cannot be recognized!!\n");
245 tprintf(
"Image too large to learn!! Size = %dx%d\n", pixGetWidth(pix),
250 if (upside_down) pixRotate180(pix, pix);
252 *scale_factor = min_width / *scale_factor;
258 float pos_min, pos_mean, pos_sd;
259 OutputStats(*outputs, &pos_min, &pos_mean, &pos_sd);
260 if (invert && pos_min < 0.5) {
269 float inv_min, inv_mean, inv_sd;
270 OutputStats(inv_outputs, &inv_min, &inv_mean, &inv_sd);
271 if (inv_min > pos_min && inv_mean > pos_mean && inv_sd < pos_sd) {
274 tprintf(
"Inverting image: old min=%g, mean=%g, sd=%g, inv %g,%g,%g\n",
275 pos_min, pos_mean, pos_sd, inv_min, inv_mean, inv_sd);
277 *outputs = inv_outputs;
278 *inputs = inv_inputs;
279 }
else if (re_invert) {
301 for (
int start = 0; start < labels.
size(); start = end) {
316 const char* window_name,
318 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics 319 Pix* input_pix = inputs.
ToPix();
321 pixGetHeight(input_pix), window);
324 #endif // GRAPHICS_DISABLED 332 #ifndef GRAPHICS_DISABLED // do nothing if there's no graphics 336 for (
int start = 0; start < labels.
size(); start = end) {
337 int xpos = xcoords[start] * x_scale;
343 const char* str =
DecodeLabel(labels, start, &end, NULL);
344 if (*str ==
'\\') str =
"\\\\";
345 xpos = xcoords[(start + end) / 2] * x_scale;
346 window->
Text(xpos, height, str);
348 window->
Line(xpos, 0, xpos, height * 3 / 2);
351 #endif // GRAPHICS_DISABLED 362 for (
int start = 0; start < labels.
size(); start = end) {
370 const char* label =
DecodeLabel(labels, start, &end, &decoded);
373 for (
int i = start + 1; i < end; ++i) {
375 xcoords[i], xcoords[i + 1]);
384 const char* label,
int best_choice,
385 int x_start,
int x_end) {
386 tprintf(
"%s=%d On [%d, %d), scores=", label, best_choice, x_start, x_end);
387 double max_score = 0.0;
388 double mean_score = 0.0;
389 int width = x_end - x_start;
390 for (
int x = x_start; x < x_end; ++x) {
391 const float* line = outputs.
f(x);
392 double score = line[best_choice] * 100.0;
393 if (score > max_score) max_score = score;
394 mean_score += score / width;
396 double best_score = 0.0;
398 if (c != best_choice && line[c] > best_score) {
400 best_score = line[c];
406 tprintf(
", Mean=%g, max=%g\n", mean_score, max_score);
412 static bool NullIsBest(
const NetworkIO& output,
float null_thr,
414 if (output.
f(t)[
null_char] >= null_thr)
return true;
455 int width = output.
Width();
456 for (
int t = 0; t < width; ++t) {
470 int start,
int* end,
int* decoded) {
476 if (decoded != NULL) {
483 while (index < labels.
size() &&
485 code.
Set(code.
length(), labels[index++]);
486 while (index < labels.
size() && labels[index] ==
null_char_) ++index;
490 if (uni_id != INVALID_UNICHAR_ID &&
491 (index == labels.
size() ||
495 if (decoded != NULL) *decoded = uni_id;
500 return "<Undecodable>";
502 if (decoded != NULL) *decoded = labels[start];
503 if (labels[start] ==
null_char_)
return "<null>";
518 if (label == INVALID_UNICHAR_ID)
return "..";
void DisplayForward(const NetworkIO &inputs, const GenericVector< int > &labels, const GenericVector< int > &label_coords, const char *window_name, ScrollView **window)
void SetupForLoad(DawgCache *dawg_cache)
virtual bool Serialize(TFile *fp) const
int FWrite(const void *buffer, int size, int count)
void add(inT32 value, inT32 count)
void DebugActivationPath(const NetworkIO &outputs, const GenericVector< int > &labels, const GenericVector< int > &xcoords)
bool LoadRecoder(TFile *fp)
virtual void SetRandomizer(TRand *randomizer)
void LabelsFromOutputs(const NetworkIO &outputs, GenericVector< int > *labels, GenericVector< int > *xcoords)
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)
void OutputStats(const NetworkIO &outputs, float *min_output, float *mean_output, float *sd)
static void PreparePixInput(const StaticShape &shape, const Pix *pix, TRand *randomizer, NetworkIO *input)
static const int kMaxCodeLen
bool Serialize(const TessdataManager *mgr, TFile *fp) const
void Line(int x1, int y1, int x2, int y2)
virtual int XScaleFactor() const
inT32 training_iteration_
static void ClearWindow(bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
RecodeBeamSearch * search_
bool IsComponentAvailable(TessdataType type) const
bool GetComponent(TessdataType type, TFile *fp)
bool save_to_file(const char *const filename) const
virtual void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)
virtual StaticShape InputShape() const
bool SimpleTextOutput() const
const char * DecodeLabel(const GenericVector< int > &labels, int start, int *end, int *decoded)
void ExtractBestPathAsLabels(GenericVector< int > *labels, GenericVector< int > *xcoords) const
int EncodeUnichar(int unichar_id, RecodedCharID *code) const
void set_int_mode(bool is_quantized)
virtual void CacheXScaleFactor(int factor)
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
void Text(int x, int y, const char *mystring)
void RecognizeLine(const ImageData &image_data, bool invert, bool debug, double worst_dict_cert, const TBOX &line_box, PointerVector< WERD_RES > *words)
static DawgCache * GlobalDawgCache()
STRING DecodeLabels(const GenericVector< int > &labels)
bool DeSerialize(const TessdataManager *mgr, TFile *fp)
void Set(int index, int value)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
bool Serialize(TFile *fp) const
bool load_from_file(const char *const filename, bool skip_fragments)
static Network * CreateFromFile(TFile *fp)
static int DisplayImage(Pix *pix, ScrollView *window)
int FReadEndian(void *buffer, int size, int count)
bool DeSerialize(bool swap, FILE *fp)
static const float kMinCertainty
void SetupPassThrough(const UNICHARSET &unicharset)
bool LoadCharsets(const TessdataManager *mgr)
int BestLabel(int t, float *score) const
void DisplayLSTMOutput(const GenericVector< int > &labels, const GenericVector< int > &xcoords, int height, ScrollView *window)
bool LoadDictionary(const char *lang, TessdataManager *mgr)
static Pix * PrepareLSTMInputs(const ImageData &image_data, const Network *network, int min_width, TRand *randomizer, float *image_scale)
void ExtractBestPathAsWords(const TBOX &line_box, float scale_factor, bool debug, const UNICHARSET *unicharset, PointerVector< WERD_RES > *words)
const char * DecodeSingleLabel(int label)
void LabelsViaReEncode(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
const UNICHARSET & GetUnicharset() const
bool Serialize(FILE *fp) const
bool Load(const char *lang, TessdataManager *mgr)
bool DeSerialize(TFile *fp)
bool IsValidFirstCode(int code) const
int DecodeUnichar(const RecodedCharID &code) const
NetworkScratch scratch_space_
void LabelsViaSimpleText(const NetworkIO &output, GenericVector< int > *labels, GenericVector< int > *xcoords)
void LoadLSTM(const STRING &lang, TessdataManager *data_file)