tesseract  5.0.0
tesseract::ParamsModel Class Reference

#include <params_model.h>

Public Types

enum  PassEnum { PTRAIN_PASS1 , PTRAIN_PASS2 , PTRAIN_NUM_PASSES }
 

Public Member Functions

 ParamsModel ()
 
 ParamsModel (const char *lang, const std::vector< float > &weights)
 
bool Initialized ()
 
void Print ()
 
void Clear ()
 
void Copy (const ParamsModel &other_model)
 
float ComputeCost (const float features[]) const
 
bool Equivalent (const ParamsModel &that) const
 
bool SaveToFile (const char *full_path) const
 
bool LoadFromFp (const char *lang, TFile *fp)
 
const std::vector< float > & weights () const
 
const std::vector< float > & weights_for_pass (PassEnum pass) const
 
void SetPass (PassEnum pass)
 

Detailed Description

Definition at line 30 of file params_model.h.

Member Enumeration Documentation

◆ PassEnum

Enumerator
PTRAIN_PASS1 
PTRAIN_PASS2 
PTRAIN_NUM_PASSES 

Definition at line 33 of file params_model.h.

Constructor & Destructor Documentation

◆ ParamsModel() [1/2]

tesseract::ParamsModel::ParamsModel ( )
inline

Definition at line 40 of file params_model.h.

40 : pass_(PTRAIN_PASS1) {}

◆ ParamsModel() [2/2]

tesseract::ParamsModel::ParamsModel ( const char *  lang,
const std::vector< float > &  weights 
)
inline

Definition at line 41 of file params_model.h.

42  : lang_(lang), pass_(PTRAIN_PASS1) {
43  weights_vec_[pass_] = weights;
44  }
const std::vector< float > & weights() const
Definition: params_model.h:69

Member Function Documentation

◆ Clear()

void tesseract::ParamsModel::Clear ( )
inline

Definition at line 51 of file params_model.h.

51  {
52  for (auto &p : weights_vec_) {
53  p.clear();
54  }
55  }

◆ ComputeCost()

float tesseract::ParamsModel::ComputeCost ( const float  features[]) const

Definition at line 81 of file params_model.cpp.

81  {
82  float unnorm_score = 0.0;
83  for (int f = 0; f < PTRAIN_NUM_FEATURE_TYPES; ++f) {
84  unnorm_score += weights_vec_[pass_][f] * features[f];
85  }
86  return ClipToRange(-unnorm_score / kScoreScaleFactor, kMinFinalCost, kMaxFinalCost);
87 }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:110

◆ Copy()

void tesseract::ParamsModel::Copy ( const ParamsModel other_model)

Definition at line 48 of file params_model.cpp.

48  {
49  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
50  weights_vec_[p] = other_model.weights_for_pass(static_cast<PassEnum>(p));
51  }
52 }

◆ Equivalent()

bool tesseract::ParamsModel::Equivalent ( const ParamsModel that) const

Definition at line 89 of file params_model.cpp.

89  {
90  float epsilon = 0.0001f;
91  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
92  if (weights_vec_[p].size() != that.weights_vec_[p].size()) {
93  return false;
94  }
95  for (unsigned i = 0; i < weights_vec_[p].size(); i++) {
96  if (weights_vec_[p][i] != that.weights_vec_[p][i] &&
97  std::fabs(weights_vec_[p][i] - that.weights_vec_[p][i]) > epsilon) {
98  return false;
99  }
100  }
101  }
102  return true;
103 }

◆ Initialized()

bool tesseract::ParamsModel::Initialized ( )
inline

Definition at line 45 of file params_model.h.

45  {
46  return weights_vec_[pass_].size() == PTRAIN_NUM_FEATURE_TYPES;
47  }

◆ LoadFromFp()

bool tesseract::ParamsModel::LoadFromFp ( const char *  lang,
TFile fp 
)

Definition at line 105 of file params_model.cpp.

105  {
106  const int kMaxLineSize = 100;
107  char line[kMaxLineSize];
108  BitVector present;
109  present.Init(PTRAIN_NUM_FEATURE_TYPES);
110  lang_ = lang;
111  // Load weights for passes with adaption on.
112  std::vector<float> &weights = weights_vec_[pass_];
113  weights.clear();
114  weights.resize(PTRAIN_NUM_FEATURE_TYPES, 0.0f);
115 
116  while (fp->FGets(line, kMaxLineSize) != nullptr) {
117  char *key = nullptr;
118  float value;
119  if (!ParseLine(line, &key, &value)) {
120  continue;
121  }
122  int idx = ParamsTrainingFeatureByName(key);
123  if (idx < 0) {
124  tprintf("ParamsModel::Unknown parameter %s\n", key);
125  continue;
126  }
127  if (!present[idx]) {
128  present.SetValue(idx, true);
129  }
130  weights[idx] = value;
131  }
132  bool complete = (present.NumSetBits() == PTRAIN_NUM_FEATURE_TYPES);
133  if (!complete) {
134  for (int i = 0; i < PTRAIN_NUM_FEATURE_TYPES; i++) {
135  if (!present[i]) {
136  tprintf("Missing field %s.\n", kParamsTrainingFeatureTypeName[i]);
137  }
138  }
139  lang_ = "";
140  weights.clear();
141  }
142  return complete;
143 }
int ParamsTrainingFeatureByName(const char *name)
void tprintf(const char *format,...)
Definition: tprintf.cpp:41

◆ Print()

void tesseract::ParamsModel::Print ( )

Definition at line 39 of file params_model.cpp.

39  {
40  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
41  tprintf("ParamsModel for pass %d lang %s\n", p, lang_.c_str());
42  for (unsigned i = 0; i < weights_vec_[p].size(); ++i) {
43  tprintf("%s = %g\n", kParamsTrainingFeatureTypeName[i], weights_vec_[p][i]);
44  }
45  }
46 }

◆ SaveToFile()

bool tesseract::ParamsModel::SaveToFile ( const char *  full_path) const

Definition at line 145 of file params_model.cpp.

145  {
146  const std::vector<float> &weights = weights_vec_[pass_];
147  if (weights.size() != PTRAIN_NUM_FEATURE_TYPES) {
148  tprintf("Refusing to save ParamsModel that has not been initialized.\n");
149  return false;
150  }
151  FILE *fp = fopen(full_path, "wb");
152  if (!fp) {
153  tprintf("Could not open %s for writing.\n", full_path);
154  return false;
155  }
156  bool all_good = true;
157  for (unsigned i = 0; i < weights.size(); i++) {
158  if (fprintf(fp, "%s %f\n", kParamsTrainingFeatureTypeName[i], weights[i]) < 0) {
159  all_good = false;
160  }
161  }
162  fclose(fp);
163  return all_good;
164 }

◆ SetPass()

void tesseract::ParamsModel::SetPass ( PassEnum  pass)
inline

Definition at line 75 of file params_model.h.

75  {
76  pass_ = pass;
77  }

◆ weights()

const std::vector<float>& tesseract::ParamsModel::weights ( ) const
inline

Definition at line 69 of file params_model.h.

69  {
70  return weights_vec_[pass_];
71  }

◆ weights_for_pass()

const std::vector<float>& tesseract::ParamsModel::weights_for_pass ( PassEnum  pass) const
inline

Definition at line 72 of file params_model.h.

72  {
73  return weights_vec_[pass];
74  }

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