tesseract  4.00.00dev
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 *expected_num_features, int cutoff_strength)
 
void DisableDisabledClasses (const UNICHARSET &unicharset)
 
void DisableFragments (const UNICHARSET &unicharset)
 
void NormalizeForXheight (int norm_multiplier, const uinT8 *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 *expected_num_features, int norm_multiplier, const uinT8 *normalization_factors) const
 
int SetupResults (GenericVector< CP_RESULT_STRUCT > *results) const
 

Detailed Description

Definition at line 107 of file intmatcher.cpp.

Constructor & Destructor Documentation

◆ ClassPruner()

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

Definition at line 109 of file intmatcher.cpp.

109  {
110  // The unrolled loop in ComputeScores means that the array sizes need to
111  // be rounded up so that the array is big enough to accommodate the extra
112  // entries accessed by the unrolling. Each pruner word is of sized
113  // BITS_PER_WERD and each entry is NUM_BITS_PER_CLASS, so there are
114  // BITS_PER_WERD / NUM_BITS_PER_CLASS entries.
115  // See ComputeScores.
116  max_classes_ = max_classes;
117  rounded_classes_ = RoundUp(
119  class_count_ = new int[rounded_classes_];
120  norm_count_ = new int[rounded_classes_];
121  sort_key_ = new int[rounded_classes_ + 1];
122  sort_index_ = new int[rounded_classes_ + 1];
123  for (int i = 0; i < rounded_classes_; i++) {
124  class_count_[i] = 0;
125  }
126  pruning_threshold_ = 0;
127  num_features_ = 0;
128  num_classes_ = 0;
129  }
#define NUM_BITS_PER_CLASS
Definition: intproto.h:54
#define WERDS_PER_CP_VECTOR
Definition: intproto.h:61
#define BITS_PER_WERD
Definition: intproto.h:44
int RoundUp(int n, int block_size)
Definition: helpers.h:116

◆ ~ClassPruner()

tesseract::ClassPruner::~ClassPruner ( )
inline

Definition at line 131 of file intmatcher.cpp.

131  {
132  delete []class_count_;
133  delete []norm_count_;
134  delete []sort_key_;
135  delete []sort_index_;
136  }

Member Function Documentation

◆ AdjustForExpectedNumFeatures()

void tesseract::ClassPruner::AdjustForExpectedNumFeatures ( const uinT16 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 211 of file intmatcher.cpp.

212  {
213  for (int class_id = 0; class_id < max_classes_; ++class_id) {
214  if (num_features_ < expected_num_features[class_id]) {
215  int deficit = expected_num_features[class_id] - num_features_;
216  class_count_[class_id] -= class_count_[class_id] * deficit /
217  (num_features_ * cutoff_strength + deficit);
218  }
219  }
220  }

◆ 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 140 of file intmatcher.cpp.

141  {
142  num_features_ = num_features;
143  int num_pruners = int_templates->NumClassPruners;
144  for (int f = 0; f < num_features; ++f) {
145  const INT_FEATURE_STRUCT* feature = &features[f];
146  // Quantize the feature to NUM_CP_BUCKETS*NUM_CP_BUCKETS*NUM_CP_BUCKETS.
147  int x = feature->X * NUM_CP_BUCKETS >> 8;
148  int y = feature->Y * NUM_CP_BUCKETS >> 8;
149  int theta = feature->Theta * NUM_CP_BUCKETS >> 8;
150  int class_id = 0;
151  // Each CLASS_PRUNER_STRUCT only covers CLASSES_PER_CP(32) classes, so
152  // we need a collection of them, indexed by pruner_set.
153  for (int pruner_set = 0; pruner_set < num_pruners; ++pruner_set) {
154  // Look up quantized feature in a 3-D array, an array of weights for
155  // each class.
156  const uinT32* pruner_word_ptr =
157  int_templates->ClassPruners[pruner_set]->p[x][y][theta];
158  for (int word = 0; word < WERDS_PER_CP_VECTOR; ++word) {
159  uinT32 pruner_word = *pruner_word_ptr++;
160  // This inner loop is unrolled to speed up the ClassPruner.
161  // Currently gcc would not unroll it unless it is set to O3
162  // level of optimization or -funroll-loops is specified.
163  /*
164  uinT32 class_mask = (1 << NUM_BITS_PER_CLASS) - 1;
165  for (int bit = 0; bit < BITS_PER_WERD/NUM_BITS_PER_CLASS; bit++) {
166  class_count_[class_id++] += pruner_word & class_mask;
167  pruner_word >>= NUM_BITS_PER_CLASS;
168  }
169  */
170  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
171  pruner_word >>= NUM_BITS_PER_CLASS;
172  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
173  pruner_word >>= NUM_BITS_PER_CLASS;
174  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
175  pruner_word >>= NUM_BITS_PER_CLASS;
176  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
177  pruner_word >>= NUM_BITS_PER_CLASS;
178  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
179  pruner_word >>= NUM_BITS_PER_CLASS;
180  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
181  pruner_word >>= NUM_BITS_PER_CLASS;
182  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
183  pruner_word >>= NUM_BITS_PER_CLASS;
184  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
185  pruner_word >>= NUM_BITS_PER_CLASS;
186  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
187  pruner_word >>= NUM_BITS_PER_CLASS;
188  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
189  pruner_word >>= NUM_BITS_PER_CLASS;
190  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
191  pruner_word >>= NUM_BITS_PER_CLASS;
192  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
193  pruner_word >>= NUM_BITS_PER_CLASS;
194  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
195  pruner_word >>= NUM_BITS_PER_CLASS;
196  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
197  pruner_word >>= NUM_BITS_PER_CLASS;
198  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
199  pruner_word >>= NUM_BITS_PER_CLASS;
200  class_count_[class_id++] += pruner_word & CLASS_PRUNER_CLASS_MASK;
201  }
202  }
203  }
204  }
#define NUM_BITS_PER_CLASS
Definition: intproto.h:54
CLASS_PRUNER_STRUCT * ClassPruners[MAX_NUM_CLASS_PRUNERS]
Definition: intproto.h:125
#define WERDS_PER_CP_VECTOR
Definition: intproto.h:61
uinT32 p[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR]
Definition: intproto.h:77
#define NUM_CP_BUCKETS
Definition: intproto.h:52
uint32_t uinT32
Definition: host.h:39
#define CLASS_PRUNER_CLASS_MASK
Definition: intproto.h:55

◆ 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 300 of file intmatcher.cpp.

302  {
303  int num_pruners = int_templates->NumClassPruners;
304  int max_num_classes = int_templates->NumClasses;
305  for (int f = 0; f < num_features_; ++f) {
306  const INT_FEATURE_STRUCT* feature = &features[f];
307  tprintf("F=%3d(%d,%d,%d),", f, feature->X, feature->Y, feature->Theta);
308  // Quantize the feature to NUM_CP_BUCKETS*NUM_CP_BUCKETS*NUM_CP_BUCKETS.
309  int x = feature->X * NUM_CP_BUCKETS >> 8;
310  int y = feature->Y * NUM_CP_BUCKETS >> 8;
311  int theta = feature->Theta * NUM_CP_BUCKETS >> 8;
312  int class_id = 0;
313  for (int pruner_set = 0; pruner_set < num_pruners; ++pruner_set) {
314  // Look up quantized feature in a 3-D array, an array of weights for
315  // each class.
316  const uinT32* pruner_word_ptr =
317  int_templates->ClassPruners[pruner_set]->p[x][y][theta];
318  for (int word = 0; word < WERDS_PER_CP_VECTOR; ++word) {
319  uinT32 pruner_word = *pruner_word_ptr++;
320  for (int word_class = 0; word_class < 16 &&
321  class_id < max_num_classes; ++word_class, ++class_id) {
322  if (norm_count_[class_id] >= pruning_threshold_) {
323  tprintf(" %s=%d,",
324  classify.ClassIDToDebugStr(int_templates,
325  class_id, 0).string(),
326  pruner_word & CLASS_PRUNER_CLASS_MASK);
327  }
328  pruner_word >>= NUM_BITS_PER_CLASS;
329  }
330  }
331  tprintf("\n");
332  }
333  }
334  }
#define NUM_BITS_PER_CLASS
Definition: intproto.h:54
CLASS_PRUNER_STRUCT * ClassPruners[MAX_NUM_CLASS_PRUNERS]
Definition: intproto.h:125
#define WERDS_PER_CP_VECTOR
Definition: intproto.h:61
#define tprintf(...)
Definition: tprintf.h:31
uinT32 p[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR]
Definition: intproto.h:77
#define NUM_CP_BUCKETS
Definition: intproto.h:52
uint32_t uinT32
Definition: host.h:39
#define CLASS_PRUNER_CLASS_MASK
Definition: intproto.h:55

◆ 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 224 of file intmatcher.cpp.

224  {
225  for (int class_id = 0; class_id < max_classes_; ++class_id) {
226  if (!unicharset.get_enabled(class_id))
227  class_count_[class_id] = 0; // This char is disabled!
228  }
229  }
bool get_enabled(UNICHAR_ID unichar_id) const
Definition: unicharset.h:838

◆ DisableFragments()

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

Zeros the scores of fragments.

Definition at line 232 of file intmatcher.cpp.

232  {
233  for (int class_id = 0; class_id < max_classes_; ++class_id) {
234  // Do not include character fragments in the class pruner
235  // results if disable_character_fragments is true.
236  if (unicharset.get_fragment(class_id)) {
237  class_count_[class_id] = 0;
238  }
239  }
240  }
const CHAR_FRAGMENT * get_fragment(UNICHAR_ID unichar_id) const
Definition: unicharset.h:694

◆ NoNormalization()

void tesseract::ClassPruner::NoNormalization ( )
inline

The nop normalization copies the class_count_ array to norm_count_.

Definition at line 255 of file intmatcher.cpp.

255  {
256  for (int class_id = 0; class_id < max_classes_; class_id++) {
257  norm_count_[class_id] = class_count_[class_id];
258  }
259  }

◆ NormalizeForXheight()

void tesseract::ClassPruner::NormalizeForXheight ( int  norm_multiplier,
const uinT8 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 246 of file intmatcher.cpp.

247  {
248  for (int class_id = 0; class_id < max_classes_; class_id++) {
249  norm_count_[class_id] = class_count_[class_id] -
250  ((norm_multiplier * normalization_factors[class_id]) >> 8);
251  }
252  }

◆ 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 264 of file intmatcher.cpp.

265  {
266  int max_count = 0;
267  for (int c = 0; c < max_classes_; ++c) {
268  if (norm_count_[c] > max_count &&
269  // This additional check is added in order to ensure that
270  // the classifier will return at least one non-fragmented
271  // character match.
272  // TODO(daria): verify that this helps accuracy and does not
273  // hurt performance.
274  (!max_of_non_fragments || !unicharset.get_fragment(c))) {
275  max_count = norm_count_[c];
276  }
277  }
278  // Prune Classes.
279  pruning_threshold_ = (max_count * pruning_factor) >> 8;
280  // Select Classes.
281  if (pruning_threshold_ < 1)
282  pruning_threshold_ = 1;
283  num_classes_ = 0;
284  for (int class_id = 0; class_id < max_classes_; class_id++) {
285  if (norm_count_[class_id] >= pruning_threshold_ ||
286  class_id == keep_this) {
287  ++num_classes_;
288  sort_index_[num_classes_] = class_id;
289  sort_key_[num_classes_] = norm_count_[class_id];
290  }
291  }
292 
293  // Sort Classes using Heapsort Algorithm.
294  if (num_classes_ > 1)
295  HeapSort(num_classes_, sort_key_, sort_index_);
296  }
void HeapSort(int n, register int ra[], register int rb[])
const CHAR_FRAGMENT * get_fragment(UNICHAR_ID unichar_id) const
Definition: unicharset.h:694

◆ 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 360 of file intmatcher.cpp.

360  {
361  CP_RESULT_STRUCT empty;
362  results->init_to_size(num_classes_, empty);
363  for (int c = 0; c < num_classes_; ++c) {
364  (*results)[c].Class = sort_index_[num_classes_ - c];
365  (*results)[c].Rating = 1.0 - sort_key_[num_classes_ - c] /
366  (static_cast<float>(CLASS_PRUNER_CLASS_MASK) * num_features_);
367  }
368  return num_classes_;
369  }
void init_to_size(int size, T t)
#define CLASS_PRUNER_CLASS_MASK
Definition: intproto.h:55

◆ SummarizeResult()

void tesseract::ClassPruner::SummarizeResult ( const Classify classify,
const INT_TEMPLATES_STRUCT int_templates,
const uinT16 expected_num_features,
int  norm_multiplier,
const uinT8 normalization_factors 
) const
inline

Prints a summary of the pruner result.

Definition at line 337 of file intmatcher.cpp.

341  {
342  tprintf("CP:%d classes, %d features:\n", num_classes_, num_features_);
343  for (int i = 0; i < num_classes_; ++i) {
344  int class_id = sort_index_[num_classes_ - i];
345  STRING class_string = classify.ClassIDToDebugStr(int_templates,
346  class_id, 0);
347  tprintf("%s:Initial=%d, E=%d, Xht-adj=%d, N=%d, Rat=%.2f\n",
348  class_string.string(),
349  class_count_[class_id],
350  expected_num_features[class_id],
351  (norm_multiplier * normalization_factors[class_id]) >> 8,
352  sort_key_[num_classes_ - i],
353  100.0 - 100.0 * sort_key_[num_classes_ - i] /
354  (CLASS_PRUNER_CLASS_MASK * num_features_));
355  }
356  }
#define tprintf(...)
Definition: tprintf.h:31
const char * string() const
Definition: strngs.cpp:198
Definition: strngs.h:45
#define CLASS_PRUNER_CLASS_MASK
Definition: intproto.h:55

The documentation for this class was generated from the following file: