tesseract  4.00.00dev
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 // Created: Mon Jun 11 11:26:42 PDT 2012
6 //
7 // (C) Copyright 2011, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 #ifndef TESSERACT_WORDREC_PARAMS_MODEL_H_
21 #define TESSERACT_WORDREC_PARAMS_MODEL_H_
22 
24 #include "ratngs.h"
25 #include "strngs.h"
26 
27 namespace tesseract {
28 
29 // Represents the learned weights for a given language.
30 class ParamsModel {
31  public:
32  // Enum for expressing OCR pass.
33  enum PassEnum {
36 
38  };
39 
40  ParamsModel() : pass_(PTRAIN_PASS1) {}
42  lang_(lang), pass_(PTRAIN_PASS1) { weights_vec_[pass_] = weights; }
43  inline bool Initialized() {
44  return weights_vec_[pass_].size() == PTRAIN_NUM_FEATURE_TYPES;
45  }
46  // Prints out feature weights.
47  void Print();
48  // Clears weights for all passes.
49  void Clear() {
50  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) weights_vec_[p].clear();
51  }
52  // Copies the weights of the given params model.
53  void Copy(const ParamsModel &other_model);
54  // Applies params model weights to the given features.
55  // Assumes that features is an array of size PTRAIN_NUM_FEATURE_TYPES.
56  float ComputeCost(const float features[]) const;
57  bool Equivalent(const ParamsModel &that) const;
58 
59  // Returns true on success.
60  bool SaveToFile(const char *full_path) const;
61 
62  // Returns true on success.
63  bool LoadFromFile(const char *lang, const char *full_path);
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_
89 
bool Equivalent(const ParamsModel &that) const
bool LoadFromFp(const char *lang, TFile *fp)
float ComputeCost(const float features[]) const
int size() const
Definition: genericvector.h:72
void Copy(const ParamsModel &other_model)
bool SaveToFile(const char *full_path) const
Definition: strngs.h:45
const GenericVector< float > & weights() const
Definition: params_model.h:66
bool LoadFromFile(const char *lang, const char *full_path)
const char features[]
Definition: feature_tests.c:2
void SetPass(PassEnum pass)
Definition: params_model.h:72
ParamsModel(const char *lang, const GenericVector< float > &weights)
Definition: params_model.h:41
const GenericVector< float > & weights_for_pass(PassEnum pass) const
Definition: params_model.h:69