tesseract  5.0.0-alpha-619-ge9db
tesseract::ClassPruner Class Reference

Public Member Functions

 ClassPruner (int max_classes)
 
 ~ClassPruner ()
 
void ComputeScores (const INT_TEMPLATES_STRUCT *int_templates, int num_features, const INT_FEATURE_STRUCT *features)
 
void AdjustForExpectedNumFeatures (const uint16_t *expected_num_features, int cutoff_strength)
 
void DisableDisabledClasses (const UNICHARSET &unicharset)
 
void DisableFragments (const UNICHARSET &unicharset)
 
void NormalizeForXheight (int norm_multiplier, const uint8_t *normalization_factors)
 
void NoNormalization ()
 
void PruneAndSort (int pruning_factor, int keep_this, bool max_of_non_fragments, const UNICHARSET &unicharset)
 
void DebugMatch (const Classify &classify, const INT_TEMPLATES_STRUCT *int_templates, const INT_FEATURE_STRUCT *features) const
 
void SummarizeResult (const Classify &classify, const INT_TEMPLATES_STRUCT *int_templates, const uint16_t *expected_num_features, int norm_multiplier, const uint8_t *normalization_factors) const
 
int SetupResults (GenericVector< CP_RESULT_STRUCT > *results) const
 

Detailed Description

Definition at line 146 of file intmatcher.cpp.

Constructor & Destructor Documentation

◆ ClassPruner()

tesseract::ClassPruner::ClassPruner ( int  max_classes)
inline

Definition at line 148 of file intmatcher.cpp.

148  :
149  ClassPruner(int max_classes) {
150  // The unrolled loop in ComputeScores means that the array sizes need to
151  // be rounded up so that the array is big enough to accommodate the extra
152  // entries accessed by the unrolling. Each pruner word is of sized
153  // BITS_PER_WERD and each entry is NUM_BITS_PER_CLASS, so there are
154  // BITS_PER_WERD / NUM_BITS_PER_CLASS entries.
155  // See ComputeScores.
156  max_classes_ = max_classes;
157  rounded_classes_ = RoundUp(
159  class_count_ = new int[rounded_classes_];
160  norm_count_ = new int[rounded_classes_];
161  sort_key_ = new int[rounded_classes_ + 1];
162  sort_index_ = new int[rounded_classes_ + 1];
163  for (int i = 0; i < rounded_classes_; i++) {
164  class_count_[i] = 0;
165  }
166  pruning_threshold_ = 0;
167  num_features_ = 0;
168  num_classes_ = 0;

◆ ~ClassPruner()

tesseract::ClassPruner::~ClassPruner ( )
inline

Definition at line 170 of file intmatcher.cpp.

171  {
172  delete []class_count_;
173  delete []norm_count_;
174  delete []sort_key_;
175  delete []sort_index_;

Member Function Documentation

◆ AdjustForExpectedNumFeatures()

void tesseract::ClassPruner::AdjustForExpectedNumFeatures ( const uint16_t *  expected_num_features,
int  cutoff_strength 
)
inline

Adjusts the scores according to the number of expected features. Used in lieu of a constant bias, this penalizes classes that expect more features than there are present. Thus an actual c will score higher for c than e, even though almost all the features match e as well as c, because e expects more features to be present.

Definition at line 250 of file intmatcher.cpp.

252  {
253  for (int class_id = 0; class_id < max_classes_; ++class_id) {
254  if (num_features_ < expected_num_features[class_id]) {
255  int deficit = expected_num_features[class_id] - num_features_;
256  class_count_[class_id] -= class_count_[class_id] * deficit /
257  (num_features_ * cutoff_strength + deficit);
258  }
259  }

◆ ComputeScores()

void tesseract::ClassPruner::ComputeScores ( const INT_TEMPLATES_STRUCT int_templates,
int  num_features,
const INT_FEATURE_STRUCT features 
)
inline

Computes the scores for every class in the character set, by summing the weights for each feature and stores the sums internally in class_count_.

Definition at line 179 of file intmatcher.cpp.

181  {
182  num_features_ = num_features;
183  int num_pruners = int_templates->NumClassPruners;
184  for (int f = 0; f < num_features; ++f) {
185  const INT_FEATURE_STRUCT* feature = &features[f];
186  // Quantize the feature to NUM_CP_BUCKETS*NUM_CP_BUCKETS*NUM_CP_BUCKETS.
187  int x = feature->X * NUM_CP_BUCKETS >> 8;
188  int y = feature->Y * NUM_CP_BUCKETS >> 8;
189  int theta = feature->Theta * NUM_CP_BUCKETS >> 8;
190  int class_id = 0;
191  // Each CLASS_PRUNER_STRUCT only covers CLASSES_PER_CP(32) classes, so
192  // we need a collection of them, indexed by pruner_set.
193  for (int pruner_set = 0; pruner_set < num_pruners; ++pruner_set) {
194  // Look up quantized feature in a 3-D array, an array of weights for
195  // each class.
196  const uint32_t* pruner_word_ptr =
197  int_templates->ClassPruners[pruner_set]->p[x][y][theta];
198  for (int word = 0; word < WERDS_PER_CP_VECTOR; ++word) {
199  uint32_t pruner_word = *pruner_word_ptr++;
200  // This inner loop is unrolled to speed up the ClassPruner.
201  // Currently gcc would not unroll it unless it is set to O3
202  // level of optimization or -funroll-loops is specified.
203  /*
204  uint32_t class_mask = (1 << NUM_BITS_PER_CLASS) - 1;
205  for (int bit = 0; bit < BITS_PER_WERD/NUM_BITS_PER_CLASS; bit++) {
206  class_count_[class_id++] += pruner_word & class_mask;
207  pruner_word >>= NUM_BITS_PER_CLASS;
208  }
209  */
210  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
211  pruner_word >>= NUM_BITS_PER_CLASS;
212  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
213  pruner_word >>= NUM_BITS_PER_CLASS;
214  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
215  pruner_word >>= NUM_BITS_PER_CLASS;
216  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
217  pruner_word >>= NUM_BITS_PER_CLASS;
218  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
219  pruner_word >>= NUM_BITS_PER_CLASS;
220  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
221  pruner_word >>= NUM_BITS_PER_CLASS;
222  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
223  pruner_word >>= NUM_BITS_PER_CLASS;
224  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
225  pruner_word >>= NUM_BITS_PER_CLASS;
226  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
227  pruner_word >>= NUM_BITS_PER_CLASS;
228  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
229  pruner_word >>= NUM_BITS_PER_CLASS;
230  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
231  pruner_word >>= NUM_BITS_PER_CLASS;
232  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
233  pruner_word >>= NUM_BITS_PER_CLASS;
234  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
235  pruner_word >>= NUM_BITS_PER_CLASS;
236  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
237  pruner_word >>= NUM_BITS_PER_CLASS;
238  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
239  pruner_word >>= NUM_BITS_PER_CLASS;
240  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
241  }
242  }
243  }

◆ DebugMatch()

void tesseract::ClassPruner::DebugMatch ( const Classify classify,
const INT_TEMPLATES_STRUCT int_templates,
const INT_FEATURE_STRUCT features 
) const
inline

Prints debug info on the class pruner matches for the pruned classes only.

Definition at line 339 of file intmatcher.cpp.

342  {
343  int num_pruners = int_templates->NumClassPruners;
344  int max_num_classes = int_templates->NumClasses;
345  for (int f = 0; f < num_features_; ++f) {
346  const INT_FEATURE_STRUCT* feature = &features[f];
347  tprintf("F=%3d(%d,%d,%d),", f, feature->X, feature->Y, feature->Theta);
348  // Quantize the feature to NUM_CP_BUCKETS*NUM_CP_BUCKETS*NUM_CP_BUCKETS.
349  int x = feature->X * NUM_CP_BUCKETS >> 8;
350  int y = feature->Y * NUM_CP_BUCKETS >> 8;
351  int theta = feature->Theta * NUM_CP_BUCKETS >> 8;
352  int class_id = 0;
353  for (int pruner_set = 0; pruner_set < num_pruners; ++pruner_set) {
354  // Look up quantized feature in a 3-D array, an array of weights for
355  // each class.
356  const uint32_t* pruner_word_ptr =
357  int_templates->ClassPruners[pruner_set]->p[x][y][theta];
358  for (int word = 0; word < WERDS_PER_CP_VECTOR; ++word) {
359  uint32_t pruner_word = *pruner_word_ptr++;
360  for (int word_class = 0; word_class < 16 &&
361  class_id < max_num_classes; ++word_class, ++class_id) {
362  if (norm_count_[class_id] >= pruning_threshold_) {
363  tprintf(" %s=%d,",
364  classify.ClassIDToDebugStr(int_templates,
365  class_id, 0).c_str(),
366  pruner_word & CLASS_PRUNER_CLASS_MASK);
367  }
368  pruner_word >>= NUM_BITS_PER_CLASS;
369  }
370  }
371  tprintf("\n");
372  }
373  }

◆ DisableDisabledClasses()

void tesseract::ClassPruner::DisableDisabledClasses ( const UNICHARSET unicharset)
inline

Zeros the scores for classes disabled in the unicharset. Implements the black-list to recognize a subset of the character set.

Definition at line 263 of file intmatcher.cpp.

264  {
265  for (int class_id = 0; class_id < max_classes_; ++class_id) {
266  if (!unicharset.get_enabled(class_id))
267  class_count_[class_id] = 0; // This char is disabled!
268  }

◆ DisableFragments()

void tesseract::ClassPruner::DisableFragments ( const UNICHARSET unicharset)
inline

Zeros the scores of fragments.

Definition at line 271 of file intmatcher.cpp.

272  {
273  for (int class_id = 0; class_id < max_classes_; ++class_id) {
274  // Do not include character fragments in the class pruner
275  // results if disable_character_fragments is true.
276  if (unicharset.get_fragment(class_id)) {
277  class_count_[class_id] = 0;
278  }
279  }

◆ NoNormalization()

void tesseract::ClassPruner::NoNormalization ( )
inline

The nop normalization copies the class_count_ array to norm_count_.

Definition at line 294 of file intmatcher.cpp.

295  {
296  for (int class_id = 0; class_id < max_classes_; class_id++) {
297  norm_count_[class_id] = class_count_[class_id];
298  }

◆ NormalizeForXheight()

void tesseract::ClassPruner::NormalizeForXheight ( int  norm_multiplier,
const uint8_t *  normalization_factors 
)
inline

Normalizes the counts for xheight, putting the normalized result in norm_count_. Applies a simple subtractive penalty for incorrect vertical position provided by the normalization_factors array, indexed by character class, and scaled by the norm_multiplier.

Definition at line 285 of file intmatcher.cpp.

287  {
288  for (int class_id = 0; class_id < max_classes_; class_id++) {
289  norm_count_[class_id] = class_count_[class_id] -
290  ((norm_multiplier * normalization_factors[class_id]) >> 8);
291  }

◆ PruneAndSort()

void tesseract::ClassPruner::PruneAndSort ( int  pruning_factor,
int  keep_this,
bool  max_of_non_fragments,
const UNICHARSET unicharset 
)
inline

Prunes the classes using <the maximum count> * pruning_factor/256 as a threshold for keeping classes. If max_of_non_fragments, then ignore fragments in computing the maximum count.

Definition at line 303 of file intmatcher.cpp.

305  {
306  int max_count = 0;
307  for (int c = 0; c < max_classes_; ++c) {
308  if (norm_count_[c] > max_count &&
309  // This additional check is added in order to ensure that
310  // the classifier will return at least one non-fragmented
311  // character match.
312  // TODO(daria): verify that this helps accuracy and does not
313  // hurt performance.
314  (!max_of_non_fragments || !unicharset.get_fragment(c))) {
315  max_count = norm_count_[c];
316  }
317  }
318  // Prune Classes.
319  pruning_threshold_ = (max_count * pruning_factor) >> 8;
320  // Select Classes.
321  if (pruning_threshold_ < 1)
322  pruning_threshold_ = 1;
323  num_classes_ = 0;
324  for (int class_id = 0; class_id < max_classes_; class_id++) {
325  if (norm_count_[class_id] >= pruning_threshold_ ||
326  class_id == keep_this) {
327  ++num_classes_;
328  sort_index_[num_classes_] = class_id;
329  sort_key_[num_classes_] = norm_count_[class_id];
330  }
331  }
332 
333  // Sort Classes using Heapsort Algorithm.
334  if (num_classes_ > 1)
335  HeapSort(num_classes_, sort_key_, sort_index_);

◆ SetupResults()

int tesseract::ClassPruner::SetupResults ( GenericVector< CP_RESULT_STRUCT > *  results) const
inline

Copies the pruned, sorted classes into the output results and returns the number of classes.

Definition at line 399 of file intmatcher.cpp.

400  {
401  CP_RESULT_STRUCT empty;
402  results->init_to_size(num_classes_, empty);
403  for (int c = 0; c < num_classes_; ++c) {
404  (*results)[c].Class = sort_index_[num_classes_ - c];
405  (*results)[c].Rating = 1.0f - sort_key_[num_classes_ - c] /
406  (static_cast<float>(CLASS_PRUNER_CLASS_MASK) * num_features_);
407  }
408  return num_classes_;

◆ SummarizeResult()

void tesseract::ClassPruner::SummarizeResult ( const Classify classify,
const INT_TEMPLATES_STRUCT int_templates,
const uint16_t *  expected_num_features,
int  norm_multiplier,
const uint8_t *  normalization_factors 
) const
inline

Prints a summary of the pruner result.

Definition at line 376 of file intmatcher.cpp.

381  {
382  tprintf("CP:%d classes, %d features:\n", num_classes_, num_features_);
383  for (int i = 0; i < num_classes_; ++i) {
384  int class_id = sort_index_[num_classes_ - i];
385  STRING class_string = classify.ClassIDToDebugStr(int_templates,
386  class_id, 0);
387  tprintf("%s:Initial=%d, E=%d, Xht-adj=%d, N=%d, Rat=%.2f\n",
388  class_string.c_str(),
389  class_count_[class_id],
390  expected_num_features[class_id],
391  (norm_multiplier * normalization_factors[class_id]) >> 8,
392  sort_key_[num_classes_ - i],
393  100.0 - 100.0 * sort_key_[num_classes_ - i] /
394  (CLASS_PRUNER_CLASS_MASK * num_features_));
395  }

The documentation for this class was generated from the following file:
INT_FEATURE_STRUCT::Theta
uint8_t Theta
Definition: intproto.h:141
STRING
Definition: strngs.h:45
RoundUp
int RoundUp(int n, int block_size)
Definition: helpers.h:100
INT_TEMPLATES_STRUCT::NumClassPruners
int NumClassPruners
Definition: intproto.h:119
CP_RESULT_STRUCT
Definition: intmatcher.h:42
BITS_PER_WERD
#define BITS_PER_WERD
Definition: intproto.h:44
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
UNICHARSET::get_enabled
bool get_enabled(UNICHAR_ID unichar_id) const
Definition: unicharset.h:868
INT_TEMPLATES_STRUCT::NumClasses
int NumClasses
Definition: intproto.h:118
INT_FEATURE_STRUCT::Y
uint8_t Y
Definition: intproto.h:140
NUM_CP_BUCKETS
#define NUM_CP_BUCKETS
Definition: intproto.h:52
tesseract::ClassPruner::ClassPruner
ClassPruner(int max_classes)
Definition: intmatcher.cpp:148
INT_FEATURE_STRUCT
Definition: intproto.h:131
CLASS_PRUNER_CLASS_MASK
#define CLASS_PRUNER_CLASS_MASK
Definition: intproto.h:55
GenericVector::init_to_size
void init_to_size(int size, const T &t)
Definition: genericvector.h:706
INT_TEMPLATES_STRUCT::ClassPruners
CLASS_PRUNER_STRUCT * ClassPruners[MAX_NUM_CLASS_PRUNERS]
Definition: intproto.h:121
WERDS_PER_CP_VECTOR
#define WERDS_PER_CP_VECTOR
Definition: intproto.h:61
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
UNICHARSET::get_fragment
const CHAR_FRAGMENT * get_fragment(UNICHAR_ID unichar_id) const
Definition: unicharset.h:724
CLASS_PRUNER_STRUCT::p
uint32_t p[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR]
Definition: intproto.h:77
NUM_BITS_PER_CLASS
#define NUM_BITS_PER_CLASS
Definition: intproto.h:54
INT_FEATURE_STRUCT::X
uint8_t X
Definition: intproto.h:139