tesseract  4.1.1
params_model.h
Go to the documentation of this file.
1 // File: params_model.h
3 // Description: Trained feature serialization for language parameter training.
4 // Author: David Eger
5 //
6 // (C) Copyright 2011, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
18 
19 #ifndef TESSERACT_WORDREC_PARAMS_MODEL_H_
20 #define TESSERACT_WORDREC_PARAMS_MODEL_H_
21 
22 #include "genericvector.h" // for GenericVector
23 #include "params_training_featdef.h" // for PTRAIN_NUM_FEATURE_TYPES
24 #include "strngs.h" // for STRING
25 
26 namespace tesseract {
27 
28 class TFile;
29 
30 // Represents the learned weights for a given language.
31 class ParamsModel {
32  public:
33  // Enum for expressing OCR pass.
34  enum PassEnum {
37 
39  };
40 
41  ParamsModel() : pass_(PTRAIN_PASS1) {}
42  ParamsModel(const char *lang, const GenericVector<float> &weights) :
43  lang_(lang), pass_(PTRAIN_PASS1) { weights_vec_[pass_] = weights; }
44  inline bool Initialized() {
45  return weights_vec_[pass_].size() == PTRAIN_NUM_FEATURE_TYPES;
46  }
47  // Prints out feature weights.
48  void Print();
49  // Clears weights for all passes.
50  void Clear() {
51  for (auto & p : weights_vec_) p.clear();
52  }
53  // Copies the weights of the given params model.
54  void Copy(const ParamsModel &other_model);
55  // Applies params model weights to the given features.
56  // Assumes that features is an array of size PTRAIN_NUM_FEATURE_TYPES.
57  float ComputeCost(const float features[]) const;
58  bool Equivalent(const ParamsModel &that) const;
59 
60  // Returns true on success.
61  bool SaveToFile(const char *full_path) const;
62 
63  // Returns true on success.
64  bool LoadFromFp(const char *lang, TFile *fp);
65 
66  const GenericVector<float>& weights() const {
67  return weights_vec_[pass_];
68  }
70  return weights_vec_[pass];
71  }
72  void SetPass(PassEnum pass) { pass_ = pass; }
73 
74  private:
75  bool ParseLine(char *line, char **key, float *val);
76 
77  STRING lang_;
78  // Set to the current pass type and used to determine which set of weights
79  // should be used for ComputeCost() and other functions.
80  PassEnum pass_;
81  // Several sets of weights for various OCR passes (e.g. pass1 with adaption,
82  // pass2 without adaption, etc).
84 };
85 
86 } // namespace tesseract
87 
88 #endif // TESSERACT_WORDREC_PARAMS_MODEL_H_
int size() const
Definition: genericvector.h:72
Definition: strngs.h:45
bool SaveToFile(const char *full_path) const
void SetPass(PassEnum pass)
Definition: params_model.h:72
float ComputeCost(const float features[]) const
bool LoadFromFp(const char *lang, TFile *fp)
ParamsModel(const char *lang, const GenericVector< float > &weights)
Definition: params_model.h:42
const GenericVector< float > & weights() const
Definition: params_model.h:66
bool Equivalent(const ParamsModel &that) const
const GenericVector< float > & weights_for_pass(PassEnum pass) const
Definition: params_model.h:69
void Copy(const ParamsModel &other_model)