tesseract  4.0.0-1-g2a2b
recodebeam.cpp
Go to the documentation of this file.
1 // File: recodebeam.cpp
3 // Description: Beam search to decode from the re-encoded CJK as a sequence of
4 // smaller numbers in place of a single large code.
5 // Author: Ray Smith
6 // Created: Fri Mar 13 09:39:01 PDT 2015
7 //
8 // (C) Copyright 2015, Google Inc.
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
20 
21 #include "recodebeam.h"
22 #include "networkio.h"
23 #include "pageres.h"
24 #include "unicharcompress.h"
25 #include <deque>
26 #include <map>
27 #include <set>
28 #include <vector>
29 
30 #include <algorithm>
31 
32 namespace tesseract {
33 
34 // Clipping value for certainty inside Tesseract. Reflects the minimum value
35 // of certainty that will be returned by ExtractBestPathAsUnicharIds.
36 // Supposedly on a uniform scale that can be compared across languages and
37 // engines.
38 const float RecodeBeamSearch::kMinCertainty = -20.0f;
39 
40 // The beam width at each code position.
41 const int RecodeBeamSearch::kBeamWidths[RecodedCharID::kMaxCodeLen + 1] = {
42  5, 10, 16, 16, 16, 16, 16, 16, 16, 16,
43 };
44 
45 const char* kNodeContNames[] = {"Anything", "OnlyDup", "NoDup"};
46 
47 // Prints debug details of the node.
48 void RecodeNode::Print(int null_char, const UNICHARSET& unicharset,
49  int depth) const {
50  if (code == null_char) {
51  tprintf("null_char");
52  } else {
53  tprintf("label=%d, uid=%d=%s", code, unichar_id,
54  unicharset.debug_str(unichar_id).string());
55  }
56  tprintf(" score=%g, c=%g,%s%s%s perm=%d, hash=%lx", score, certainty,
57  start_of_dawg ? " DawgStart" : "", start_of_word ? " Start" : "",
58  end_of_word ? " End" : "", permuter, code_hash);
59  if (depth > 0 && prev != nullptr) {
60  tprintf(" prev:");
61  prev->Print(null_char, unicharset, depth - 1);
62  } else {
63  tprintf("\n");
64  }
65 }
66 
67 // Borrows the pointer, which is expected to survive until *this is deleted.
69  int null_char, bool simple_text, Dict* dict)
70  : recoder_(recoder),
71  beam_size_(0),
72  top_code_(-1),
73  second_code_(-1),
74  dict_(dict),
75  space_delimited_(true),
76  is_simple_text_(simple_text),
77  null_char_(null_char) {
78  if (dict_ != nullptr && !dict_->IsSpaceDelimitedLang()) space_delimited_ = false;
79 }
80 
81 // Decodes the set of network outputs, storing the lattice internally.
82 void RecodeBeamSearch::Decode(const NetworkIO& output, double dict_ratio,
83  double cert_offset, double worst_dict_cert,
84  const UNICHARSET* charset, int lstm_choice_mode) {
85  beam_size_ = 0;
86  int width = output.Width();
87  if (lstm_choice_mode)
88  timesteps.clear();
89  for (int t = 0; t < width; ++t) {
90  ComputeTopN(output.f(t), output.NumFeatures(), kBeamWidths[0]);
91  DecodeStep(output.f(t), t, dict_ratio, cert_offset, worst_dict_cert,
92  charset);
93  if (lstm_choice_mode) {
94  SaveMostCertainChoices(output.f(t), output.NumFeatures(), charset, t);
95  }
96  }
97 }
99  double dict_ratio, double cert_offset,
100  double worst_dict_cert,
101  const UNICHARSET* charset) {
102  beam_size_ = 0;
103  int width = output.dim1();
104  for (int t = 0; t < width; ++t) {
105  ComputeTopN(output[t], output.dim2(), kBeamWidths[0]);
106  DecodeStep(output[t], t, dict_ratio, cert_offset, worst_dict_cert, charset);
107  }
108 }
109 
110 void RecodeBeamSearch::SaveMostCertainChoices(const float* outputs,
111  int num_outputs,
112  const UNICHARSET* charset,
113  int xCoord) {
114  std::vector<std::pair<const char*, float>> choices;
115  int pos = 0;
116  for (int i = 0; i < num_outputs; ++i) {
117  if (outputs[i] >= 0.01f) {
118  const char* character;
119  if (i + 2 >= num_outputs) {
120  character = "";
121  } else if (i > 0) {
122  character = charset->id_to_unichar_ext(i + 2);
123  } else {
124  character = charset->id_to_unichar_ext(i);
125  }
126  pos = 0;
127  //order the possible choices within one timestep
128  //beginning with the most likely
129  while (choices.size() > pos && choices[pos].second > outputs[i]) {
130  pos++;
131  }
132  choices.insert(choices.begin() + pos,
133  std::pair<const char*, float>(character, outputs[i]));
134  }
135  }
136  timesteps.push_back(choices);
137 }
138 
139 // Returns the best path as labels/scores/xcoords similar to simple CTC.
141  GenericVector<int>* labels, GenericVector<int>* xcoords) const {
142  labels->truncate(0);
143  xcoords->truncate(0);
145  ExtractBestPaths(&best_nodes, nullptr);
146  // Now just run CTC on the best nodes.
147  int t = 0;
148  int width = best_nodes.size();
149  while (t < width) {
150  int label = best_nodes[t]->code;
151  if (label != null_char_) {
152  labels->push_back(label);
153  xcoords->push_back(t);
154  }
155  while (++t < width && !is_simple_text_ && best_nodes[t]->code == label) {
156  }
157  }
158  xcoords->push_back(width);
159 }
160 
161 // Returns the best path as unichar-ids/certs/ratings/xcoords skipping
162 // duplicates, nulls and intermediate parts.
164  bool debug, const UNICHARSET* unicharset, GenericVector<int>* unichar_ids,
166  GenericVector<int>* xcoords) const {
168  ExtractBestPaths(&best_nodes, nullptr);
169  ExtractPathAsUnicharIds(best_nodes, unichar_ids, certs, ratings, xcoords);
170  if (debug) {
171  DebugPath(unicharset, best_nodes);
172  DebugUnicharPath(unicharset, best_nodes, *unichar_ids, *certs, *ratings,
173  *xcoords);
174  }
175 }
176 
177 // Returns the best path as a set of WERD_RES.
179  float scale_factor, bool debug,
180  const UNICHARSET* unicharset,
182  int lstm_choice_mode) {
183  words->truncate(0);
184  GenericVector<int> unichar_ids;
185  GenericVector<float> certs;
186  GenericVector<float> ratings;
187  GenericVector<int> xcoords;
190  std::deque<std::pair<int,int>> best_choices;
191  ExtractBestPaths(&best_nodes, &second_nodes);
192  if (debug) {
193  DebugPath(unicharset, best_nodes);
194  ExtractPathAsUnicharIds(second_nodes, &unichar_ids, &certs, &ratings,
195  &xcoords);
196  tprintf("\nSecond choice path:\n");
197  DebugUnicharPath(unicharset, second_nodes, unichar_ids, certs, ratings,
198  xcoords);
199  }
200  int current_char;
201  int timestepEnd = 0;
202  //if lstm choice mode is required in granularity level 2 it stores the x
203  //Coordinates of every chosen character to match the alternative choices to it
204  if (lstm_choice_mode == 2) {
205  ExtractPathAsUnicharIds(best_nodes, &unichar_ids, &certs, &ratings,
206  &xcoords, &best_choices);
207  if (best_choices.size() > 0) {
208  current_char = best_choices.front().first;
209  timestepEnd = best_choices.front().second;
210  best_choices.pop_front();
211  }
212  } else {
213  ExtractPathAsUnicharIds(best_nodes, &unichar_ids, &certs, &ratings,
214  &xcoords);
215  }
216  int num_ids = unichar_ids.size();
217  if (debug) {
218  DebugUnicharPath(unicharset, best_nodes, unichar_ids, certs, ratings,
219  xcoords);
220  }
221  // Convert labels to unichar-ids.
222  int word_end = 0;
223  float prev_space_cert = 0.0f;
224  for (int word_start = 0; word_start < num_ids; word_start = word_end) {
225  for (word_end = word_start + 1; word_end < num_ids; ++word_end) {
226  // A word is terminated when a space character or start_of_word flag is
227  // hit. We also want to force a separate word for every non
228  // space-delimited character when not in a dictionary context.
229  if (unichar_ids[word_end] == UNICHAR_SPACE) break;
230  int index = xcoords[word_end];
231  if (best_nodes[index]->start_of_word) break;
232  if (best_nodes[index]->permuter == TOP_CHOICE_PERM &&
233  (!unicharset->IsSpaceDelimited(unichar_ids[word_end]) ||
234  !unicharset->IsSpaceDelimited(unichar_ids[word_end - 1])))
235  break;
236  }
237  float space_cert = 0.0f;
238  if (word_end < num_ids && unichar_ids[word_end] == UNICHAR_SPACE)
239  space_cert = certs[word_end];
240  bool leading_space =
241  word_start > 0 && unichar_ids[word_start - 1] == UNICHAR_SPACE;
242  // Create a WERD_RES for the output word.
243  WERD_RES* word_res = InitializeWord(
244  leading_space, line_box, word_start, word_end,
245  std::min(space_cert, prev_space_cert), unicharset, xcoords, scale_factor);
246  if (lstm_choice_mode == 1) {
247  for (size_t i = timestepEnd; i < xcoords[word_end]; i++) {
248  word_res->timesteps.push_back(timesteps[i]);
249  }
250  timestepEnd = xcoords[word_end];
251  } else if (lstm_choice_mode == 2) {
252  float sum = 0;
253  std::vector<std::pair<const char*, float>> choice_pairs;
254  for (size_t i = timestepEnd; i < xcoords[word_end]; i++) {
255  for (std::pair<const char*, float> choice : timesteps[i]) {
256  if (std::strcmp(choice.first, "") != 0) {
257  sum += choice.second;
258  choice_pairs.push_back(choice);
259  }
260  }
261  if ((best_choices.size() > 0 && i == best_choices.front().second - 1)
262  || i == xcoords[word_end]-1) {
263  std::map<const char*, float> summed_propabilities;
264  for (auto it = choice_pairs.begin(); it != choice_pairs.end(); ++it) {
265  summed_propabilities[it->first] += it->second;
266  }
267  std::vector<std::pair<const char*, float>> accumulated_timestep;
268  accumulated_timestep.push_back(std::pair<const char*,float>
269  (unicharset->id_to_unichar_ext
270  (current_char), 2.0));
271  int pos;
272  for (auto it = summed_propabilities.begin();
273  it != summed_propabilities.end(); ++it) {
274  if(sum == 0) break;
275  it->second/=sum;
276  pos = 0;
277  while (accumulated_timestep.size() > pos
278  && accumulated_timestep[pos].second > it->second) {
279  pos++;
280  }
281  accumulated_timestep.insert(accumulated_timestep.begin() + pos,
282  std::pair<const char*,float>(it->first,
283  it->second));
284  }
285  if (best_choices.size() > 0) {
286  current_char = best_choices.front().first;
287  best_choices.pop_front();
288  }
289  choice_pairs.clear();
290  word_res->timesteps.push_back(accumulated_timestep);
291  sum = 0;
292  }
293  }
294  timestepEnd = xcoords[word_end];
295  }
296  for (int i = word_start; i < word_end; ++i) {
297  BLOB_CHOICE_LIST* choices = new BLOB_CHOICE_LIST;
298  BLOB_CHOICE_IT bc_it(choices);
299  BLOB_CHOICE* choice = new BLOB_CHOICE(
300  unichar_ids[i], ratings[i], certs[i], -1, 1.0f,
301  static_cast<float>(INT16_MAX), 0.0f, BCC_STATIC_CLASSIFIER);
302  int col = i - word_start;
303  choice->set_matrix_cell(col, col);
304  bc_it.add_after_then_move(choice);
305  word_res->ratings->put(col, col, choices);
306  }
307  int index = xcoords[word_end - 1];
308  word_res->FakeWordFromRatings(best_nodes[index]->permuter);
309  words->push_back(word_res);
310  prev_space_cert = space_cert;
311  if (word_end < num_ids && unichar_ids[word_end] == UNICHAR_SPACE)
312  ++word_end;
313  }
314 }
315 
316 // Generates debug output of the content of the beams after a Decode.
317 void RecodeBeamSearch::DebugBeams(const UNICHARSET& unicharset) const {
318  for (int p = 0; p < beam_size_; ++p) {
319  for (int d = 0; d < 2; ++d) {
320  for (int c = 0; c < NC_COUNT; ++c) {
321  NodeContinuation cont = static_cast<NodeContinuation>(c);
322  int index = BeamIndex(d, cont, 0);
323  if (beam_[p]->beams_[index].empty()) continue;
324  // Print all the best scoring nodes for each unichar found.
325  tprintf("Position %d: %s+%s beam\n", p, d ? "Dict" : "Non-Dict",
326  kNodeContNames[c]);
327  DebugBeamPos(unicharset, beam_[p]->beams_[index]);
328  }
329  }
330  }
331 }
332 
333 // Generates debug output of the content of a single beam position.
334 void RecodeBeamSearch::DebugBeamPos(const UNICHARSET& unicharset,
335  const RecodeHeap& heap) const {
336  GenericVector<const RecodeNode*> unichar_bests;
337  unichar_bests.init_to_size(unicharset.size(), nullptr);
338  const RecodeNode* null_best = nullptr;
339  int heap_size = heap.size();
340  for (int i = 0; i < heap_size; ++i) {
341  const RecodeNode* node = &heap.get(i).data;
342  if (node->unichar_id == INVALID_UNICHAR_ID) {
343  if (null_best == nullptr || null_best->score < node->score) null_best = node;
344  } else {
345  if (unichar_bests[node->unichar_id] == nullptr ||
346  unichar_bests[node->unichar_id]->score < node->score) {
347  unichar_bests[node->unichar_id] = node;
348  }
349  }
350  }
351  for (int u = 0; u < unichar_bests.size(); ++u) {
352  if (unichar_bests[u] != nullptr) {
353  const RecodeNode& node = *unichar_bests[u];
354  node.Print(null_char_, unicharset, 1);
355  }
356  }
357  if (null_best != nullptr) {
358  null_best->Print(null_char_, unicharset, 1);
359  }
360 }
361 
362 // Returns the given best_nodes as unichar-ids/certs/ratings/xcoords skipping
363 // duplicates, nulls and intermediate parts.
364 /* static */
365 void RecodeBeamSearch::ExtractPathAsUnicharIds(
366  const GenericVector<const RecodeNode*>& best_nodes,
367  GenericVector<int>* unichar_ids, GenericVector<float>* certs,
368  GenericVector<float>* ratings, GenericVector<int>* xcoords,
369  std::deque<std::pair<int, int>>* best_choices) {
370  unichar_ids->truncate(0);
371  certs->truncate(0);
372  ratings->truncate(0);
373  xcoords->truncate(0);
374  // Backtrack extracting only valid, non-duplicate unichar-ids.
375  int t = 0;
376  int width = best_nodes.size();
377  while (t < width) {
378  double certainty = 0.0;
379  double rating = 0.0;
380  while (t < width && best_nodes[t]->unichar_id == INVALID_UNICHAR_ID) {
381  double cert = best_nodes[t++]->certainty;
382  if (cert < certainty) certainty = cert;
383  rating -= cert;
384  }
385  if (t < width) {
386  int unichar_id = best_nodes[t]->unichar_id;
387  if (unichar_id == UNICHAR_SPACE && !certs->empty() &&
388  best_nodes[t]->permuter != NO_PERM) {
389  // All the rating and certainty go on the previous character except
390  // for the space itself.
391  if (certainty < certs->back()) certs->back() = certainty;
392  ratings->back() += rating;
393  certainty = 0.0;
394  rating = 0.0;
395  }
396  unichar_ids->push_back(unichar_id);
397  xcoords->push_back(t);
398  if (best_choices != nullptr) {
399  best_choices->push_back(std::pair<int, int>(unichar_id, t));
400  }
401  do {
402  double cert = best_nodes[t++]->certainty;
403  // Special-case NO-PERM space to forget the certainty of the previous
404  // nulls. See long comment in ContinueContext.
405  if (cert < certainty || (unichar_id == UNICHAR_SPACE &&
406  best_nodes[t - 1]->permuter == NO_PERM)) {
407  certainty = cert;
408  }
409  rating -= cert;
410  } while (t < width && best_nodes[t]->duplicate);
411  certs->push_back(certainty);
412  ratings->push_back(rating);
413  } else if (!certs->empty()) {
414  if (certainty < certs->back()) certs->back() = certainty;
415  ratings->back() += rating;
416  }
417  }
418  xcoords->push_back(width);
419 }
420 
421 // Sets up a word with the ratings matrix and fake blobs with boxes in the
422 // right places.
423 WERD_RES* RecodeBeamSearch::InitializeWord(bool leading_space,
424  const TBOX& line_box, int word_start,
425  int word_end, float space_certainty,
426  const UNICHARSET* unicharset,
427  const GenericVector<int>& xcoords,
428  float scale_factor) {
429  // Make a fake blob for each non-zero label.
430  C_BLOB_LIST blobs;
431  C_BLOB_IT b_it(&blobs);
432  for (int i = word_start; i < word_end; ++i) {
433  int min_half_width = xcoords[i + 1] - xcoords[i];
434  if (i > 0 && xcoords[i] - xcoords[i - 1] < min_half_width)
435  min_half_width = xcoords[i] - xcoords[i - 1];
436  if (min_half_width < 1) min_half_width = 1;
437  // Make a fake blob.
438  TBOX box(xcoords[i] - min_half_width, 0, xcoords[i] + min_half_width,
439  line_box.height());
440  box.scale(scale_factor);
441  box.move(ICOORD(line_box.left(), line_box.bottom()));
442  box.set_top(line_box.top());
443  b_it.add_after_then_move(C_BLOB::FakeBlob(box));
444  }
445  // Make a fake word from the blobs.
446  WERD* word = new WERD(&blobs, leading_space, nullptr);
447  // Make a WERD_RES from the word.
448  WERD_RES* word_res = new WERD_RES(word);
449  word_res->uch_set = unicharset;
450  word_res->combination = true; // Give it ownership of the word.
451  word_res->space_certainty = space_certainty;
452  word_res->ratings = new MATRIX(word_end - word_start, 1);
453  return word_res;
454 }
455 
456 // Fills top_n_flags_ with bools that are true iff the corresponding output
457 // is one of the top_n.
458 void RecodeBeamSearch::ComputeTopN(const float* outputs, int num_outputs,
459  int top_n) {
460  top_n_flags_.init_to_size(num_outputs, TN_ALSO_RAN);
461  top_code_ = -1;
462  second_code_ = -1;
463  top_heap_.clear();
464  for (int i = 0; i < num_outputs; ++i) {
465  if (top_heap_.size() < top_n || outputs[i] > top_heap_.PeekTop().key) {
466  TopPair entry(outputs[i], i);
467  top_heap_.Push(&entry);
468  if (top_heap_.size() > top_n) top_heap_.Pop(&entry);
469  }
470  }
471  while (!top_heap_.empty()) {
472  TopPair entry;
473  top_heap_.Pop(&entry);
474  if (top_heap_.size() > 1) {
475  top_n_flags_[entry.data] = TN_TOPN;
476  } else {
477  top_n_flags_[entry.data] = TN_TOP2;
478  if (top_heap_.empty())
479  top_code_ = entry.data;
480  else
481  second_code_ = entry.data;
482  }
483  }
484  top_n_flags_[null_char_] = TN_TOP2;
485 }
486 
487 // Adds the computation for the current time-step to the beam. Call at each
488 // time-step in sequence from left to right. outputs is the activation vector
489 // for the current timestep.
490 void RecodeBeamSearch::DecodeStep(const float* outputs, int t,
491  double dict_ratio, double cert_offset,
492  double worst_dict_cert,
493  const UNICHARSET* charset, bool debug) {
494  if (t == beam_.size()) beam_.push_back(new RecodeBeam);
495  RecodeBeam* step = beam_[t];
496  beam_size_ = t + 1;
497  step->Clear();
498  if (t == 0) {
499  // The first step can only use singles and initials.
500  ContinueContext(nullptr, BeamIndex(false, NC_ANYTHING, 0), outputs, TN_TOP2,
501  dict_ratio, cert_offset, worst_dict_cert, step);
502  if (dict_ != nullptr) {
503  ContinueContext(nullptr, BeamIndex(true, NC_ANYTHING, 0), outputs,
504  TN_TOP2, dict_ratio, cert_offset, worst_dict_cert, step);
505  }
506  } else {
507  RecodeBeam* prev = beam_[t - 1];
508  if (debug) {
509  int beam_index = BeamIndex(true, NC_ANYTHING, 0);
510  for (int i = prev->beams_[beam_index].size() - 1; i >= 0; --i) {
512  ExtractPath(&prev->beams_[beam_index].get(i).data, &path);
513  tprintf("Step %d: Dawg beam %d:\n", t, i);
514  DebugPath(charset, path);
515  }
516  beam_index = BeamIndex(false, NC_ANYTHING, 0);
517  for (int i = prev->beams_[beam_index].size() - 1; i >= 0; --i) {
519  ExtractPath(&prev->beams_[beam_index].get(i).data, &path);
520  tprintf("Step %d: Non-Dawg beam %d:\n", t, i);
521  DebugPath(charset, path);
522  }
523  }
524  int total_beam = 0;
525  // Work through the scores by group (top-2, top-n, the rest) while the beam
526  // is empty. This enables extending the context using only the top-n results
527  // first, which may have an empty intersection with the valid codes, so we
528  // fall back to the rest if the beam is empty.
529  for (int tn = 0; tn < TN_COUNT && total_beam == 0; ++tn) {
530  TopNState top_n = static_cast<TopNState>(tn);
531  for (int index = 0; index < kNumBeams; ++index) {
532  // Working backwards through the heaps doesn't guarantee that we see the
533  // best first, but it comes before a lot of the worst, so it is slightly
534  // more efficient than going forwards.
535  for (int i = prev->beams_[index].size() - 1; i >= 0; --i) {
536  ContinueContext(&prev->beams_[index].get(i).data, index, outputs,
537  top_n, dict_ratio, cert_offset, worst_dict_cert,
538  step);
539  }
540  }
541  for (int index = 0; index < kNumBeams; ++index) {
543  total_beam += step->beams_[index].size();
544  }
545  }
546  // Special case for the best initial dawg. Push it on the heap if good
547  // enough, but there is only one, so it doesn't blow up the beam.
548  for (int c = 0; c < NC_COUNT; ++c) {
549  if (step->best_initial_dawgs_[c].code >= 0) {
550  int index = BeamIndex(true, static_cast<NodeContinuation>(c), 0);
551  RecodeHeap* dawg_heap = &step->beams_[index];
552  PushHeapIfBetter(kBeamWidths[0], &step->best_initial_dawgs_[c],
553  dawg_heap);
554  }
555  }
556  }
557 }
558 
559 // Adds to the appropriate beams the legal (according to recoder)
560 // continuations of context prev, which is of the given length, using the
561 // given network outputs to provide scores to the choices. Uses only those
562 // choices for which top_n_flags[index] == top_n_flag.
563 void RecodeBeamSearch::ContinueContext(const RecodeNode* prev, int index,
564  const float* outputs,
565  TopNState top_n_flag, double dict_ratio,
566  double cert_offset,
567  double worst_dict_cert,
568  RecodeBeam* step) {
569  RecodedCharID prefix;
570  RecodedCharID full_code;
571  const RecodeNode* previous = prev;
572  int length = LengthFromBeamsIndex(index);
573  bool use_dawgs = IsDawgFromBeamsIndex(index);
574  NodeContinuation prev_cont = ContinuationFromBeamsIndex(index);
575  for (int p = length - 1; p >= 0; --p, previous = previous->prev) {
576  while (previous != nullptr &&
577  (previous->duplicate || previous->code == null_char_)) {
578  previous = previous->prev;
579  }
580  if (previous != nullptr) {
581  prefix.Set(p, previous->code);
582  full_code.Set(p, previous->code);
583  }
584  }
585  if (prev != nullptr && !is_simple_text_) {
586  if (top_n_flags_[prev->code] == top_n_flag) {
587  if (prev_cont != NC_NO_DUP) {
588  float cert =
589  NetworkIO::ProbToCertainty(outputs[prev->code]) + cert_offset;
590  PushDupOrNoDawgIfBetter(length, true, prev->code, prev->unichar_id,
591  cert, worst_dict_cert, dict_ratio, use_dawgs,
592  NC_ANYTHING, prev, step);
593  }
594  if (prev_cont == NC_ANYTHING && top_n_flag == TN_TOP2 &&
595  prev->code != null_char_) {
596  float cert = NetworkIO::ProbToCertainty(outputs[prev->code] +
597  outputs[null_char_]) +
598  cert_offset;
599  PushDupOrNoDawgIfBetter(length, true, prev->code, prev->unichar_id,
600  cert, worst_dict_cert, dict_ratio, use_dawgs,
601  NC_NO_DUP, prev, step);
602  }
603  }
604  if (prev_cont == NC_ONLY_DUP) return;
605  if (prev->code != null_char_ && length > 0 &&
606  top_n_flags_[null_char_] == top_n_flag) {
607  // Allow nulls within multi code sequences, as the nulls within are not
608  // explicitly included in the code sequence.
609  float cert =
610  NetworkIO::ProbToCertainty(outputs[null_char_]) + cert_offset;
611  PushDupOrNoDawgIfBetter(length, false, null_char_, INVALID_UNICHAR_ID,
612  cert, worst_dict_cert, dict_ratio, use_dawgs,
613  NC_ANYTHING, prev, step);
614  }
615  }
616  const GenericVector<int>* final_codes = recoder_.GetFinalCodes(prefix);
617  if (final_codes != nullptr) {
618  for (int i = 0; i < final_codes->size(); ++i) {
619  int code = (*final_codes)[i];
620  if (top_n_flags_[code] != top_n_flag) continue;
621  if (prev != nullptr && prev->code == code && !is_simple_text_) continue;
622  float cert = NetworkIO::ProbToCertainty(outputs[code]) + cert_offset;
623  if (cert < kMinCertainty && code != null_char_) continue;
624  full_code.Set(length, code);
625  int unichar_id = recoder_.DecodeUnichar(full_code);
626  // Map the null char to INVALID.
627  if (length == 0 && code == null_char_) unichar_id = INVALID_UNICHAR_ID;
628  ContinueUnichar(code, unichar_id, cert, worst_dict_cert, dict_ratio,
629  use_dawgs, NC_ANYTHING, prev, step);
630  if (top_n_flag == TN_TOP2 && code != null_char_) {
631  float prob = outputs[code] + outputs[null_char_];
632  if (prev != nullptr && prev_cont == NC_ANYTHING &&
633  prev->code != null_char_ &&
634  ((prev->code == top_code_ && code == second_code_) ||
635  (code == top_code_ && prev->code == second_code_))) {
636  prob += outputs[prev->code];
637  }
638  float cert = NetworkIO::ProbToCertainty(prob) + cert_offset;
639  ContinueUnichar(code, unichar_id, cert, worst_dict_cert, dict_ratio,
640  use_dawgs, NC_ONLY_DUP, prev, step);
641  }
642  }
643  }
644  const GenericVector<int>* next_codes = recoder_.GetNextCodes(prefix);
645  if (next_codes != nullptr) {
646  for (int i = 0; i < next_codes->size(); ++i) {
647  int code = (*next_codes)[i];
648  if (top_n_flags_[code] != top_n_flag) continue;
649  if (prev != nullptr && prev->code == code && !is_simple_text_) continue;
650  float cert = NetworkIO::ProbToCertainty(outputs[code]) + cert_offset;
651  PushDupOrNoDawgIfBetter(length + 1, false, code, INVALID_UNICHAR_ID, cert,
652  worst_dict_cert, dict_ratio, use_dawgs,
653  NC_ANYTHING, prev, step);
654  if (top_n_flag == TN_TOP2 && code != null_char_) {
655  float prob = outputs[code] + outputs[null_char_];
656  if (prev != nullptr && prev_cont == NC_ANYTHING &&
657  prev->code != null_char_ &&
658  ((prev->code == top_code_ && code == second_code_) ||
659  (code == top_code_ && prev->code == second_code_))) {
660  prob += outputs[prev->code];
661  }
662  float cert = NetworkIO::ProbToCertainty(prob) + cert_offset;
663  PushDupOrNoDawgIfBetter(length + 1, false, code, INVALID_UNICHAR_ID,
664  cert, worst_dict_cert, dict_ratio, use_dawgs,
665  NC_ONLY_DUP, prev, step);
666  }
667  }
668  }
669 }
670 
671 // Continues for a new unichar, using dawg or non-dawg as per flag.
672 void RecodeBeamSearch::ContinueUnichar(int code, int unichar_id, float cert,
673  float worst_dict_cert, float dict_ratio,
674  bool use_dawgs, NodeContinuation cont,
675  const RecodeNode* prev,
676  RecodeBeam* step) {
677  if (use_dawgs) {
678  if (cert > worst_dict_cert) {
679  ContinueDawg(code, unichar_id, cert, cont, prev, step);
680  }
681  } else {
682  RecodeHeap* nodawg_heap = &step->beams_[BeamIndex(false, cont, 0)];
683  PushHeapIfBetter(kBeamWidths[0], code, unichar_id, TOP_CHOICE_PERM, false,
684  false, false, false, cert * dict_ratio, prev, nullptr,
685  nodawg_heap);
686  if (dict_ != nullptr &&
687  ((unichar_id == UNICHAR_SPACE && cert > worst_dict_cert) ||
688  !dict_->getUnicharset().IsSpaceDelimited(unichar_id))) {
689  // Any top choice position that can start a new word, ie a space or
690  // any non-space-delimited character, should also be considered
691  // by the dawg search, so push initial dawg to the dawg heap.
692  float dawg_cert = cert;
693  PermuterType permuter = TOP_CHOICE_PERM;
694  // Since we use the space either side of a dictionary word in the
695  // certainty of the word, (to properly handle weak spaces) and the
696  // space is coming from a non-dict word, we need special conditions
697  // to avoid degrading the certainty of the dict word that follows.
698  // With a space we don't multiply the certainty by dict_ratio, and we
699  // flag the space with NO_PERM to indicate that we should not use the
700  // predecessor nulls to generate the confidence for the space, as they
701  // have already been multiplied by dict_ratio, and we can't go back to
702  // insert more entries in any previous heaps.
703  if (unichar_id == UNICHAR_SPACE)
704  permuter = NO_PERM;
705  else
706  dawg_cert *= dict_ratio;
707  PushInitialDawgIfBetter(code, unichar_id, permuter, false, false,
708  dawg_cert, cont, prev, step);
709  }
710  }
711 }
712 
713 // Adds a RecodeNode composed of the tuple (code, unichar_id, cert, prev,
714 // appropriate-dawg-args, cert) to the given heap (dawg_beam_) if unichar_id
715 // is a valid continuation of whatever is in prev.
716 void RecodeBeamSearch::ContinueDawg(int code, int unichar_id, float cert,
717  NodeContinuation cont,
718  const RecodeNode* prev, RecodeBeam* step) {
719  RecodeHeap* dawg_heap = &step->beams_[BeamIndex(true, cont, 0)];
720  RecodeHeap* nodawg_heap = &step->beams_[BeamIndex(false, cont, 0)];
721  if (unichar_id == INVALID_UNICHAR_ID) {
722  PushHeapIfBetter(kBeamWidths[0], code, unichar_id, NO_PERM, false, false,
723  false, false, cert, prev, nullptr, dawg_heap);
724  return;
725  }
726  // Avoid dictionary probe if score a total loss.
727  float score = cert;
728  if (prev != nullptr) score += prev->score;
729  if (dawg_heap->size() >= kBeamWidths[0] &&
730  score <= dawg_heap->PeekTop().data.score &&
731  nodawg_heap->size() >= kBeamWidths[0] &&
732  score <= nodawg_heap->PeekTop().data.score) {
733  return;
734  }
735  const RecodeNode* uni_prev = prev;
736  // Prev may be a partial code, null_char, or duplicate, so scan back to the
737  // last valid unichar_id.
738  while (uni_prev != nullptr &&
739  (uni_prev->unichar_id == INVALID_UNICHAR_ID || uni_prev->duplicate))
740  uni_prev = uni_prev->prev;
741  if (unichar_id == UNICHAR_SPACE) {
742  if (uni_prev != nullptr && uni_prev->end_of_word) {
743  // Space is good. Push initial state, to the dawg beam and a regular
744  // space to the top choice beam.
745  PushInitialDawgIfBetter(code, unichar_id, uni_prev->permuter, false,
746  false, cert, cont, prev, step);
747  PushHeapIfBetter(kBeamWidths[0], code, unichar_id, uni_prev->permuter,
748  false, false, false, false, cert, prev, nullptr,
749  nodawg_heap);
750  }
751  return;
752  } else if (uni_prev != nullptr && uni_prev->start_of_dawg &&
753  uni_prev->unichar_id != UNICHAR_SPACE &&
754  dict_->getUnicharset().IsSpaceDelimited(uni_prev->unichar_id) &&
755  dict_->getUnicharset().IsSpaceDelimited(unichar_id)) {
756  return; // Can't break words between space delimited chars.
757  }
758  DawgPositionVector initial_dawgs;
759  DawgPositionVector* updated_dawgs = new DawgPositionVector;
760  DawgArgs dawg_args(&initial_dawgs, updated_dawgs, NO_PERM);
761  bool word_start = false;
762  if (uni_prev == nullptr) {
763  // Starting from beginning of line.
764  dict_->default_dawgs(&initial_dawgs, false);
765  word_start = true;
766  } else if (uni_prev->dawgs != nullptr) {
767  // Continuing a previous dict word.
768  dawg_args.active_dawgs = uni_prev->dawgs;
769  word_start = uni_prev->start_of_dawg;
770  } else {
771  return; // Can't continue if not a dict word.
772  }
773  PermuterType permuter = static_cast<PermuterType>(
774  dict_->def_letter_is_okay(&dawg_args,
775  dict_->getUnicharset(), unichar_id, false));
776  if (permuter != NO_PERM) {
777  PushHeapIfBetter(kBeamWidths[0], code, unichar_id, permuter, false,
778  word_start, dawg_args.valid_end, false, cert, prev,
779  dawg_args.updated_dawgs, dawg_heap);
780  if (dawg_args.valid_end && !space_delimited_) {
781  // We can start another word right away, so push initial state as well,
782  // to the dawg beam, and the regular character to the top choice beam,
783  // since non-dict words can start here too.
784  PushInitialDawgIfBetter(code, unichar_id, permuter, word_start, true,
785  cert, cont, prev, step);
786  PushHeapIfBetter(kBeamWidths[0], code, unichar_id, permuter, false,
787  word_start, true, false, cert, prev, nullptr, nodawg_heap);
788  }
789  } else {
790  delete updated_dawgs;
791  }
792 }
793 
794 // Adds a RecodeNode composed of the tuple (code, unichar_id,
795 // initial-dawg-state, prev, cert) to the given heap if/ there is room or if
796 // better than the current worst element if already full.
797 void RecodeBeamSearch::PushInitialDawgIfBetter(int code, int unichar_id,
798  PermuterType permuter,
799  bool start, bool end, float cert,
800  NodeContinuation cont,
801  const RecodeNode* prev,
802  RecodeBeam* step) {
803  RecodeNode* best_initial_dawg = &step->best_initial_dawgs_[cont];
804  float score = cert;
805  if (prev != nullptr) score += prev->score;
806  if (best_initial_dawg->code < 0 || score > best_initial_dawg->score) {
807  DawgPositionVector* initial_dawgs = new DawgPositionVector;
808  dict_->default_dawgs(initial_dawgs, false);
809  RecodeNode node(code, unichar_id, permuter, true, start, end, false, cert,
810  score, prev, initial_dawgs,
811  ComputeCodeHash(code, false, prev));
812  *best_initial_dawg = node;
813  }
814 }
815 
816 // Adds a RecodeNode composed of the tuple (code, unichar_id, permuter,
817 // false, false, false, false, cert, prev, nullptr) to heap if there is room
818 // or if better than the current worst element if already full.
819 /* static */
820 void RecodeBeamSearch::PushDupOrNoDawgIfBetter(
821  int length, bool dup, int code, int unichar_id, float cert,
822  float worst_dict_cert, float dict_ratio, bool use_dawgs,
823  NodeContinuation cont, const RecodeNode* prev, RecodeBeam* step) {
824  int index = BeamIndex(use_dawgs, cont, length);
825  if (use_dawgs) {
826  if (cert > worst_dict_cert) {
827  PushHeapIfBetter(kBeamWidths[length], code, unichar_id,
828  prev ? prev->permuter : NO_PERM, false, false, false,
829  dup, cert, prev, nullptr, &step->beams_[index]);
830  }
831  } else {
832  cert *= dict_ratio;
833  if (cert >= kMinCertainty || code == null_char_) {
834  PushHeapIfBetter(kBeamWidths[length], code, unichar_id,
835  prev ? prev->permuter : TOP_CHOICE_PERM, false, false,
836  false, dup, cert, prev, nullptr, &step->beams_[index]);
837  }
838  }
839 }
840 
841 // Adds a RecodeNode composed of the tuple (code, unichar_id, permuter,
842 // dawg_start, word_start, end, dup, cert, prev, d) to heap if there is room
843 // or if better than the current worst element if already full.
844 void RecodeBeamSearch::PushHeapIfBetter(int max_size, int code, int unichar_id,
845  PermuterType permuter, bool dawg_start,
846  bool word_start, bool end, bool dup,
847  float cert, const RecodeNode* prev,
848  DawgPositionVector* d,
849  RecodeHeap* heap) {
850  float score = cert;
851  if (prev != nullptr) score += prev->score;
852  if (heap->size() < max_size || score > heap->PeekTop().data.score) {
853  uint64_t hash = ComputeCodeHash(code, dup, prev);
854  RecodeNode node(code, unichar_id, permuter, dawg_start, word_start, end,
855  dup, cert, score, prev, d, hash);
856  if (UpdateHeapIfMatched(&node, heap)) return;
857  RecodePair entry(score, node);
858  heap->Push(&entry);
859  ASSERT_HOST(entry.data.dawgs == nullptr);
860  if (heap->size() > max_size) heap->Pop(&entry);
861  } else {
862  delete d;
863  }
864 }
865 
866 // Adds a RecodeNode to heap if there is room
867 // or if better than the current worst element if already full.
868 void RecodeBeamSearch::PushHeapIfBetter(int max_size, RecodeNode* node,
869  RecodeHeap* heap) {
870  if (heap->size() < max_size || node->score > heap->PeekTop().data.score) {
871  if (UpdateHeapIfMatched(node, heap)) {
872  return;
873  }
874  RecodePair entry(node->score, *node);
875  heap->Push(&entry);
876  ASSERT_HOST(entry.data.dawgs == nullptr);
877  if (heap->size() > max_size) heap->Pop(&entry);
878  }
879 }
880 
881 // Searches the heap for a matching entry, and updates the score with
882 // reshuffle if needed. Returns true if there was a match.
883 bool RecodeBeamSearch::UpdateHeapIfMatched(RecodeNode* new_node,
884  RecodeHeap* heap) {
885  // TODO(rays) consider hash map instead of linear search.
886  // It might not be faster because the hash map would have to be updated
887  // every time a heap reshuffle happens, and that would be a lot of overhead.
888  GenericVector<RecodePair>* nodes = heap->heap();
889  for (int i = 0; i < nodes->size(); ++i) {
890  RecodeNode& node = (*nodes)[i].data;
891  if (node.code == new_node->code && node.code_hash == new_node->code_hash &&
892  node.permuter == new_node->permuter &&
893  node.start_of_dawg == new_node->start_of_dawg) {
894  if (new_node->score > node.score) {
895  // The new one is better. Update the entire node in the heap and
896  // reshuffle.
897  node = *new_node;
898  (*nodes)[i].key = node.score;
899  heap->Reshuffle(&(*nodes)[i]);
900  }
901  return true;
902  }
903  }
904  return false;
905 }
906 
907 // Computes and returns the code-hash for the given code and prev.
908 uint64_t RecodeBeamSearch::ComputeCodeHash(int code, bool dup,
909  const RecodeNode* prev) const {
910  uint64_t hash = prev == nullptr ? 0 : prev->code_hash;
911  if (!dup && code != null_char_) {
912  int num_classes = recoder_.code_range();
913  uint64_t carry = (((hash >> 32) * num_classes) >> 32);
914  hash *= num_classes;
915  hash += carry;
916  hash += code;
917  }
918  return hash;
919 }
920 
921 // Backtracks to extract the best path through the lattice that was built
922 // during Decode. On return the best_nodes vector essentially contains the set
923 // of code, score pairs that make the optimal path with the constraint that
924 // the recoder can decode the code sequence back to a sequence of unichar-ids.
925 void RecodeBeamSearch::ExtractBestPaths(
927  GenericVector<const RecodeNode*>* second_nodes) const {
928  // Scan both beams to extract the best and second best paths.
929  const RecodeNode* best_node = nullptr;
930  const RecodeNode* second_best_node = nullptr;
931  const RecodeBeam* last_beam = beam_[beam_size_ - 1];
932  for (int c = 0; c < NC_COUNT; ++c) {
933  if (c == NC_ONLY_DUP) continue;
934  NodeContinuation cont = static_cast<NodeContinuation>(c);
935  for (int is_dawg = 0; is_dawg < 2; ++is_dawg) {
936  int beam_index = BeamIndex(is_dawg, cont, 0);
937  int heap_size = last_beam->beams_[beam_index].size();
938  for (int h = 0; h < heap_size; ++h) {
939  const RecodeNode* node = &last_beam->beams_[beam_index].get(h).data;
940  if (is_dawg) {
941  // dawg_node may be a null_char, or duplicate, so scan back to the
942  // last valid unichar_id.
943  const RecodeNode* dawg_node = node;
944  while (dawg_node != nullptr &&
945  (dawg_node->unichar_id == INVALID_UNICHAR_ID ||
946  dawg_node->duplicate))
947  dawg_node = dawg_node->prev;
948  if (dawg_node == nullptr || (!dawg_node->end_of_word &&
949  dawg_node->unichar_id != UNICHAR_SPACE)) {
950  // Dawg node is not valid.
951  continue;
952  }
953  }
954  if (best_node == nullptr || node->score > best_node->score) {
955  second_best_node = best_node;
956  best_node = node;
957  } else if (second_best_node == nullptr ||
958  node->score > second_best_node->score) {
959  second_best_node = node;
960  }
961  }
962  }
963  }
964  if (second_nodes != nullptr) ExtractPath(second_best_node, second_nodes);
965  ExtractPath(best_node, best_nodes);
966 }
967 
968 // Helper backtracks through the lattice from the given node, storing the
969 // path and reversing it.
970 void RecodeBeamSearch::ExtractPath(
971  const RecodeNode* node, GenericVector<const RecodeNode*>* path) const {
972  path->truncate(0);
973  while (node != nullptr) {
974  path->push_back(node);
975  node = node->prev;
976  }
977  path->reverse();
978 }
979 
980 // Helper prints debug information on the given lattice path.
981 void RecodeBeamSearch::DebugPath(
982  const UNICHARSET* unicharset,
983  const GenericVector<const RecodeNode*>& path) const {
984  for (int c = 0; c < path.size(); ++c) {
985  const RecodeNode& node = *path[c];
986  tprintf("%d ", c);
987  node.Print(null_char_, *unicharset, 1);
988  }
989 }
990 
991 // Helper prints debug information on the given unichar path.
992 void RecodeBeamSearch::DebugUnicharPath(
993  const UNICHARSET* unicharset, const GenericVector<const RecodeNode*>& path,
994  const GenericVector<int>& unichar_ids, const GenericVector<float>& certs,
995  const GenericVector<float>& ratings,
996  const GenericVector<int>& xcoords) const {
997  int num_ids = unichar_ids.size();
998  double total_rating = 0.0;
999  for (int c = 0; c < num_ids; ++c) {
1000  int coord = xcoords[c];
1001  tprintf("%d %d=%s r=%g, c=%g, s=%d, e=%d, perm=%d\n", coord, unichar_ids[c],
1002  unicharset->debug_str(unichar_ids[c]).string(), ratings[c],
1003  certs[c], path[coord]->start_of_word, path[coord]->end_of_word,
1004  path[coord]->permuter);
1005  total_rating += ratings[c];
1006  }
1007  tprintf("Path total rating = %g\n", total_rating);
1008 }
1009 
1010 } // namespace tesseract.
void ExtractBestPathAsLabels(GenericVector< int > *labels, GenericVector< int > *xcoords) const
Definition: recodebeam.cpp:140
float space_certainty
Definition: pageres.h:316
static const int kMaxCodeLen
static int BeamIndex(bool is_dawg, NodeContinuation cont, int length)
Definition: recodebeam.h:237
int size() const
Definition: genericvector.h:71
const Pair & get(int index) const
Definition: genericheap.h:87
int DecodeUnichar(const RecodedCharID &code) const
void FakeWordFromRatings(PermuterType permuter)
Definition: pageres.cpp:904
bool IsSpaceDelimited(UNICHAR_ID unichar_id) const
Definition: unicharset.h:647
void Decode(const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset, int lstm_choice_mode=0)
Definition: recodebeam.cpp:82
GenericHeap< RecodePair > RecodeHeap
Definition: recodebeam.h:176
static int LengthFromBeamsIndex(int index)
Definition: recodebeam.h:229
const char * string() const
Definition: strngs.cpp:196
void set_matrix_cell(int col, int row)
Definition: ratngs.h:157
Definition: rect.h:34
static NodeContinuation ContinuationFromBeamsIndex(int index)
Definition: recodebeam.h:230
void scale(const float f)
Definition: rect.h:175
const GenericVector< int > * GetFinalCodes(const RecodedCharID &code) const
PermuterType permuter
Definition: recodebeam.h:147
void Print(int null_char, const UNICHARSET &unicharset, int depth) const
Definition: recodebeam.cpp:48
T & back() const
std::vector< std::vector< std::pair< const char *, float > > > timesteps
Definition: recodebeam.h:216
int size() const
Definition: unicharset.h:336
void DebugBeams(const UNICHARSET &unicharset) const
Definition: recodebeam.cpp:317
int16_t left() const
Definition: rect.h:72
static bool IsDawgFromBeamsIndex(int index)
Definition: recodebeam.h:233
int16_t top() const
Definition: rect.h:58
NodeContinuation
Definition: recodebeam.h:72
const GenericVector< int > * GetNextCodes(const RecodedCharID &code) const
integer coordinate
Definition: points.h:32
void init_to_size(int size, const T &t)
STRING debug_str(UNICHAR_ID id) const
Definition: unicharset.cpp:342
bool IsSpaceDelimitedLang() const
Returns true if the language is space-delimited (not CJ, or T).
Definition: dict.cpp:857
KDPairInc< double, RecodeNode > RecodePair
Definition: recodebeam.h:175
Definition: werd.h:59
bool empty() const
Definition: genericvector.h:90
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
static float ProbToCertainty(float prob)
Definition: networkio.cpp:573
void put(ICOORD pos, const T &thing)
Definition: matrix.h:220
int def_letter_is_okay(void *void_dawg_args, const UNICHARSET &unicharset, UNICHAR_ID unichar_id, bool word_end) const
Definition: dict.cpp:367
const char * id_to_unichar_ext(UNICHAR_ID id) const
Definition: unicharset.cpp:298
int push_back(T object)
int dim1() const
Definition: matrix.h:206
RecodeBeamSearch(const UnicharCompress &recoder, int null_char, bool simple_text, Dict *dict)
Definition: recodebeam.cpp:68
float * f(int t)
Definition: networkio.h:115
const RecodeNode * prev
Definition: recodebeam.h:167
bool combination
Definition: pageres.h:334
static const float kMinCertainty
Definition: recodebeam.h:222
MATRIX * ratings
Definition: pageres.h:231
const UNICHARSET * uch_set
Definition: pageres.h:206
void ExtractBestPathAsUnicharIds(bool debug, const UNICHARSET *unicharset, GenericVector< int > *unichar_ids, GenericVector< float > *certs, GenericVector< float > *ratings, GenericVector< int > *xcoords) const
Definition: recodebeam.cpp:163
const UNICHARSET & getUnicharset() const
Definition: dict.h:98
int dim2() const
Definition: matrix.h:207
std::vector< std::vector< std::pair< const char *, float > > > timesteps
Definition: pageres.h:224
static const int kNumBeams
Definition: recodebeam.h:227
void truncate(int size)
void default_dawgs(DawgPositionVector *anylength_dawgs, bool suppress_patterns) const
Definition: dict.cpp:586
void ExtractBestPathAsWords(const TBOX &line_box, float scale_factor, bool debug, const UNICHARSET *unicharset, PointerVector< WERD_RES > *words, int lstm_choice_mode=0)
Definition: recodebeam.cpp:178
Definition: matrix.h:575
const char * kNodeContNames[]
Definition: recodebeam.cpp:45
static C_BLOB * FakeBlob(const TBOX &box)
Definition: stepblob.cpp:243
int16_t bottom() const
Definition: rect.h:65
int Width() const
Definition: networkio.h:107
int16_t height() const
Definition: rect.h:108
PermuterType
Definition: ratngs.h:242
int NumFeatures() const
Definition: networkio.h:111
#define ASSERT_HOST(x)
Definition: errcode.h:84