tesseract  4.0.0-1-g2a2b
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 LoadFromFile (const char *lang, const char *full_path)
 
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 32 of file params_model.h.

Member Enumeration Documentation

◆ PassEnum

Enumerator
PTRAIN_PASS1 
PTRAIN_PASS2 
PTRAIN_NUM_PASSES 

Definition at line 35 of file params_model.h.

Constructor & Destructor Documentation

◆ ParamsModel() [1/2]

tesseract::ParamsModel::ParamsModel ( )
inline

Definition at line 42 of file params_model.h.

◆ ParamsModel() [2/2]

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

Definition at line 43 of file params_model.h.

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

Member Function Documentation

◆ Clear()

void tesseract::ParamsModel::Clear ( )
inline

Definition at line 51 of file params_model.h.

51  {
52  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) weights_vec_[p].clear();
53  }

◆ 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,
87  kMinFinalCost, kMaxFinalCost);
88 }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111

◆ 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(
51  static_cast<PassEnum>(p));
52  }
53 }

◆ Equivalent()

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

Definition at line 90 of file params_model.cpp.

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

◆ 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  }
int size() const
Definition: genericvector.h:71

◆ LoadFromFile()

bool tesseract::ParamsModel::LoadFromFile ( const char *  lang,
const char *  full_path 
)

Definition at line 103 of file params_model.cpp.

105  {
106  TFile fp;
107  if (!fp.Open(full_path, nullptr)) {
108  tprintf("Error opening file %s\n", full_path);
109  return false;
110  }
111  return LoadFromFp(lang, &fp);
112 }
bool LoadFromFp(const char *lang, TFile *fp)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ LoadFromFp()

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

Definition at line 114 of file params_model.cpp.

114  {
115  const int kMaxLineSize = 100;
116  char line[kMaxLineSize];
117  BitVector present;
118  present.Init(PTRAIN_NUM_FEATURE_TYPES);
119  lang_ = lang;
120  // Load weights for passes with adaption on.
121  GenericVector<float> &weights = weights_vec_[pass_];
123 
124  while (fp->FGets(line, kMaxLineSize) != nullptr) {
125  char *key = nullptr;
126  float value;
127  if (!ParseLine(line, &key, &value))
128  continue;
129  int idx = ParamsTrainingFeatureByName(key);
130  if (idx < 0) {
131  tprintf("ParamsModel::Unknown parameter %s\n", key);
132  continue;
133  }
134  if (!present[idx]) {
135  present.SetValue(idx, true);
136  }
137  weights[idx] = value;
138  }
139  bool complete = (present.NumSetBits() == PTRAIN_NUM_FEATURE_TYPES);
140  if (!complete) {
141  for (int i = 0; i < PTRAIN_NUM_FEATURE_TYPES; i++) {
142  if (!present[i]) {
143  tprintf("Missing field %s.\n", kParamsTrainingFeatureTypeName[i]);
144  }
145  }
146  lang_ = "";
147  weights.truncate(0);
148  }
149  return complete;
150 }
int ParamsTrainingFeatureByName(const char *name)
void init_to_size(int size, const T &t)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
const GenericVector< float > & weights() const
Definition: params_model.h:68
void truncate(int size)

◆ Print()

void tesseract::ParamsModel::Print ( )

Definition at line 38 of file params_model.cpp.

38  {
39  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
40  tprintf("ParamsModel for pass %d lang %s\n", p, lang_.string());
41  for (int i = 0; i < weights_vec_[p].size(); ++i) {
42  tprintf("%s = %g\n", kParamsTrainingFeatureTypeName[i],
43  weights_vec_[p][i]);
44  }
45  }
46 }
int size() const
Definition: genericvector.h:71
const char * string() const
Definition: strngs.cpp:196
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ SaveToFile()

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

Definition at line 152 of file params_model.cpp.

152  {
153  const GenericVector<float> &weights = weights_vec_[pass_];
155  tprintf("Refusing to save ParamsModel that has not been initialized.\n");
156  return false;
157  }
158  FILE *fp = fopen(full_path, "wb");
159  if (!fp) {
160  tprintf("Could not open %s for writing.\n", full_path);
161  return false;
162  }
163  bool all_good = true;
164  for (int i = 0; i < weights.size(); i++) {
165  if (fprintf(fp, "%s %f\n", kParamsTrainingFeatureTypeName[i], weights[i])
166  < 0) {
167  all_good = false;
168  }
169  }
170  fclose(fp);
171  return all_good;
172 }
int size() const
Definition: genericvector.h:71
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
const GenericVector< float > & weights() const
Definition: params_model.h:68

◆ SetPass()

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

Definition at line 74 of file params_model.h.

74 { pass_ = pass; }

◆ weights()

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

Definition at line 68 of file params_model.h.

68  {
69  return weights_vec_[pass_];
70  }

◆ weights_for_pass()

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

Definition at line 71 of file params_model.h.

71  {
72  return weights_vec_[pass];
73  }

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