tesseract  4.00.00dev
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 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.

◆ ParamsModel() [2/2]

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

Definition at line 41 of file params_model.h.

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

Member Function Documentation

◆ Clear()

void tesseract::ParamsModel::Clear ( )
inline

Definition at line 49 of file params_model.h.

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

◆ ComputeCost()

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

Definition at line 78 of file params_model.cpp.

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

◆ 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 87 of file params_model.cpp.

87  {
88  float epsilon = 0.0001;
89  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
90  if (weights_vec_[p].size() != that.weights_vec_[p].size()) return false;
91  for (int i = 0; i < weights_vec_[p].size(); i++) {
92  if (weights_vec_[p][i] != that.weights_vec_[p][i] &&
93  fabs(weights_vec_[p][i] - that.weights_vec_[p][i]) > epsilon)
94  return false;
95  }
96  }
97  return true;
98 }
voidpf void uLong size
Definition: ioapi.h:39
int size() const
Definition: genericvector.h:72

◆ Initialized()

bool tesseract::ParamsModel::Initialized ( )
inline

Definition at line 43 of file params_model.h.

43  {
44  return weights_vec_[pass_].size() == PTRAIN_NUM_FEATURE_TYPES;
45  }
int size() const
Definition: genericvector.h:72

◆ LoadFromFile()

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

Definition at line 100 of file params_model.cpp.

102  {
103  TFile fp;
104  if (!fp.Open(full_path, nullptr)) {
105  tprintf("Error opening file %s\n", full_path);
106  return false;
107  }
108  return LoadFromFp(lang, &fp);
109 }
bool LoadFromFp(const char *lang, TFile *fp)
#define tprintf(...)
Definition: tprintf.h:31

◆ LoadFromFp()

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

Definition at line 111 of file params_model.cpp.

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

◆ 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 }
#define tprintf(...)
Definition: tprintf.h:31
const char * string() const
Definition: strngs.cpp:198
int size() const
Definition: genericvector.h:72

◆ SaveToFile()

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

Definition at line 149 of file params_model.cpp.

149  {
150  const GenericVector<float> &weights = weights_vec_[pass_];
151  if (weights.size() != PTRAIN_NUM_FEATURE_TYPES) {
152  tprintf("Refusing to save ParamsModel that has not been initialized.\n");
153  return false;
154  }
155  FILE *fp = fopen(full_path, "wb");
156  if (!fp) {
157  tprintf("Could not open %s for writing.\n", full_path);
158  return false;
159  }
160  bool all_good = true;
161  for (int i = 0; i < weights.size(); i++) {
162  if (fprintf(fp, "%s %f\n", kParamsTrainingFeatureTypeName[i], weights[i])
163  < 0) {
164  all_good = false;
165  }
166  }
167  fclose(fp);
168  return all_good;
169 }
#define tprintf(...)
Definition: tprintf.h:31
int size() const
Definition: genericvector.h:72
const GenericVector< float > & weights() const
Definition: params_model.h:66

◆ 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: