tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
params_model.h
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 
23 #include "genericvector.h" // for GenericVector
24 #include "params_training_featdef.h" // for PTRAIN_NUM_FEATURE_TYPES
25 #include "strngs.h" // for STRING
26 
27 namespace tesseract {
28 
29 class TFile;
30 
31 // Represents the learned weights for a given language.
32 class ParamsModel {
33  public:
34  // Enum for expressing OCR pass.
35  enum PassEnum {
38 
40  };
41 
43  ParamsModel(const char *lang, const GenericVector<float> &weights) :
45  inline bool Initialized() {
47  }
48  // Prints out feature weights.
49  void Print();
50  // Clears weights for all passes.
51  void Clear() {
52  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) weights_vec_[p].clear();
53  }
54  // Copies the weights of the given params model.
55  void Copy(const ParamsModel &other_model);
56  // Applies params model weights to the given features.
57  // Assumes that features is an array of size PTRAIN_NUM_FEATURE_TYPES.
58  float ComputeCost(const float features[]) const;
59  bool Equivalent(const ParamsModel &that) const;
60 
61  // Returns true on success.
62  bool SaveToFile(const char *full_path) const;
63 
64  // Returns true on success.
65  bool LoadFromFile(const char *lang, const char *full_path);
66  bool LoadFromFp(const char *lang, TFile *fp);
67 
68  const GenericVector<float>& weights() const {
69  return weights_vec_[pass_];
70  }
72  return weights_vec_[pass];
73  }
74  void SetPass(PassEnum pass) { pass_ = pass; }
75 
76  private:
77  bool ParseLine(char *line, char **key, float *val);
78 
80  // Set to the current pass type and used to determine which set of weights
81  // should be used for ComputeCost() and other functions.
83  // Several sets of weights for various OCR passes (e.g. pass1 with adaption,
84  // pass2 without adaption, etc).
86 };
87 
88 } // namespace tesseract
89 
90 #endif // TESSERACT_WORDREC_PARAMS_MODEL_H_
void Clear()
Definition: params_model.h:51
bool SaveToFile(const char *full_path) const
Definition: params_model.cpp:152
ParamsModel(const char *lang, const GenericVector< float > &weights)
Definition: params_model.h:43
ParamsModel()
Definition: params_model.h:42
Definition: params_model.h:39
bool Equivalent(const ParamsModel &that) const
Definition: params_model.cpp:90
GenericVector< float > weights_vec_[PTRAIN_NUM_PASSES]
Definition: params_model.h:85
void Copy(const ParamsModel &other_model)
Definition: params_model.cpp:48
void SetPass(PassEnum pass)
Definition: params_model.h:74
STRING lang_
Definition: params_model.h:79
Definition: serialis.h:77
Definition: params_model.h:37
Definition: params_model.h:36
Definition: baseapi.cpp:94
const GenericVector< float > & weights_for_pass(PassEnum pass) const
Definition: params_model.h:71
bool Initialized()
Definition: params_model.h:45
PassEnum pass_
Definition: params_model.h:82
Definition: strngs.h:45
bool ParseLine(char *line, char **key, float *val)
Definition: params_model.cpp:57
Definition: params_training_featdef.h:70
Definition: params_model.h:32
int size() const
Definition: genericvector.h:71
float ComputeCost(const float features[]) const
Definition: params_model.cpp:81
bool LoadFromFp(const char *lang, TFile *fp)
Definition: params_model.cpp:114
PassEnum
Definition: params_model.h:35
bool LoadFromFile(const char *lang, const char *full_path)
Definition: params_model.cpp:103
const GenericVector< float > & weights() const
Definition: params_model.h:68
void Print()
Definition: params_model.cpp:38