tesseract  5.0.0-alpha-619-ge9db
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 <tesseract/genericvector.h> // for GenericVector
23 #include "params_training_featdef.h" // for PTRAIN_NUM_FEATURE_TYPES
24 #include <tesseract/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_
tesseract::ParamsModel::Equivalent
bool Equivalent(const ParamsModel &that) const
Definition: params_model.cpp:89
strngs.h
tesseract::ParamsModel::Copy
void Copy(const ParamsModel &other_model)
Definition: params_model.cpp:47
tesseract::ParamsModel::Clear
void Clear()
Definition: params_model.h:50
params_training_featdef.h
STRING
Definition: strngs.h:45
tesseract::ParamsModel::weights_for_pass
const GenericVector< float > & weights_for_pass(PassEnum pass) const
Definition: params_model.h:69
tesseract::PTRAIN_NUM_FEATURE_TYPES
Definition: params_training_featdef.h:70
tesseract::ParamsModel::SaveToFile
bool SaveToFile(const char *full_path) const
Definition: params_model.cpp:140
genericvector.h
tesseract::ParamsModel::SetPass
void SetPass(PassEnum pass)
Definition: params_model.h:72
tesseract::ParamsModel::PTRAIN_PASS1
Definition: params_model.h:35
tesseract::TFile
Definition: serialis.h:75
tesseract::ParamsModel::Print
void Print()
Definition: params_model.cpp:37
tesseract::ParamsModel
Definition: params_model.h:31
tesseract::ParamsModel::weights
const GenericVector< float > & weights() const
Definition: params_model.h:66
tesseract
Definition: baseapi.h:65
tesseract::ParamsModel::PTRAIN_PASS2
Definition: params_model.h:36
GenericVector< float >
tesseract::ParamsModel::PTRAIN_NUM_PASSES
Definition: params_model.h:38
tesseract::ParamsModel::ComputeCost
float ComputeCost(const float features[]) const
Definition: params_model.cpp:80
tesseract::ParamsModel::Initialized
bool Initialized()
Definition: params_model.h:44
tesseract::ParamsModel::ParamsModel
ParamsModel()
Definition: params_model.h:41
tesseract::ParamsModel::ParamsModel
ParamsModel(const char *lang, const GenericVector< float > &weights)
Definition: params_model.h:42
tesseract::ParamsModel::PassEnum
PassEnum
Definition: params_model.h:34
tesseract::ParamsModel::LoadFromFp
bool LoadFromFp(const char *lang, TFile *fp)
Definition: params_model.cpp:102
GenericVector::size
int size() const
Definition: genericvector.h:71