tesseract 4.1.1
Loading...
Searching...
No Matches
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.

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; }
const GenericVector< float > & weights() const
Definition: params_model.h:66

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}
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:108

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

◆ 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}
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:35
int ParamsTrainingFeatureByName(const char *name)
void init_to_size(int size, const T &t)
void truncate(int size)

◆ 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_.string());
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}
const char * string() const
Definition: strngs.cpp:194

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