tesseract  5.0.0-alpha-619-ge9db
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 GenericVector< 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 GenericVector< float > & weights () const
 
const GenericVector< float > & weights_for_pass (PassEnum pass) const
 
void SetPass (PassEnum pass)
 

Detailed Description

Definition at line 31 of file params_model.h.

Member Enumeration Documentation

◆ PassEnum

Enumerator
PTRAIN_PASS1 
PTRAIN_PASS2 
PTRAIN_NUM_PASSES 

Definition at line 34 of file params_model.h.

34  {
37 
39  };

Constructor & Destructor Documentation

◆ ParamsModel() [1/2]

tesseract::ParamsModel::ParamsModel ( )
inline

Definition at line 41 of file params_model.h.

41 : pass_(PTRAIN_PASS1) {}

◆ ParamsModel() [2/2]

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

Definition at line 42 of file params_model.h.

42  :
43  lang_(lang), pass_(PTRAIN_PASS1) { weights_vec_[pass_] = weights; }

Member Function Documentation

◆ Clear()

void tesseract::ParamsModel::Clear ( )
inline

Definition at line 50 of file params_model.h.

50  {
51  for (auto & p : weights_vec_) p.clear();
52  }

◆ ComputeCost()

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

Definition at line 80 of file params_model.cpp.

80  {
81  float unnorm_score = 0.0;
82  for (int f = 0; f < PTRAIN_NUM_FEATURE_TYPES; ++f) {
83  unnorm_score += weights_vec_[pass_][f] * features[f];
84  }
85  return ClipToRange(-unnorm_score / kScoreScaleFactor,
86  kMinFinalCost, kMaxFinalCost);
87 }

◆ Copy()

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

Definition at line 47 of file params_model.cpp.

47  {
48  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
49  weights_vec_[p] = other_model.weights_for_pass(
50  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.0001;
91  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
92  if (weights_vec_[p].size() != that.weights_vec_[p].size()) return false;
93  for (int i = 0; i < weights_vec_[p].size(); i++) {
94  if (weights_vec_[p][i] != that.weights_vec_[p][i] &&
95  fabs(weights_vec_[p][i] - that.weights_vec_[p][i]) > epsilon)
96  return false;
97  }
98  }
99  return true;
100 }

◆ Initialized()

bool tesseract::ParamsModel::Initialized ( )
inline

Definition at line 44 of file params_model.h.

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

◆ LoadFromFp()

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

Definition at line 102 of file params_model.cpp.

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

◆ Print()

void tesseract::ParamsModel::Print ( )

Definition at line 37 of file params_model.cpp.

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

◆ SaveToFile()

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

Definition at line 140 of file params_model.cpp.

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

◆ SetPass()

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

Definition at line 72 of file params_model.h.

72 { pass_ = pass; }

◆ weights()

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

Definition at line 66 of file params_model.h.

66  {
67  return weights_vec_[pass_];
68  }

◆ weights_for_pass()

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

Definition at line 69 of file params_model.h.

69  {
70  return weights_vec_[pass];
71  }

The documentation for this class was generated from the following files:
ClipToRange
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:106
tesseract::ParamsTrainingFeatureByName
int ParamsTrainingFeatureByName(const char *name)
Definition: params_training_featdef.cpp:26
tesseract::PTRAIN_NUM_FEATURE_TYPES
Definition: params_training_featdef.h:70
tesseract::ParamsModel::PTRAIN_PASS1
Definition: params_model.h:35
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
tesseract::ParamsModel::weights
const GenericVector< float > & weights() const
Definition: params_model.h:66
tesseract::ParamsModel::PTRAIN_PASS2
Definition: params_model.h:36
GenericVector< float >
tesseract::ParamsModel::PTRAIN_NUM_PASSES
Definition: params_model.h:38
GenericVector::truncate
void truncate(int size)
Definition: genericvector.h:132
GenericVector::init_to_size
void init_to_size(int size, const T &t)
Definition: genericvector.h:706
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
GenericVector::size
int size() const
Definition: genericvector.h:71