tesseract  3.03
tesseract::ParamsModel Class Reference

#include <params_model.h>

List of all members.

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 LoadFromFile (const char *lang, const char *full_path)
bool LoadFromFp (const char *lang, FILE *fp, inT64 end_offset)
const GenericVector< float > & weights () const
const GenericVector< float > & weights_for_pass (PassEnum pass) const
void SetPass (PassEnum pass)

Detailed Description

Definition at line 30 of file params_model.h.


Member Enumeration Documentation

Enumerator:
PTRAIN_PASS1 
PTRAIN_PASS2 
PTRAIN_NUM_PASSES 

Definition at line 33 of file params_model.h.


Constructor & Destructor Documentation

Definition at line 40 of file params_model.h.

: pass_(PTRAIN_PASS1) {}
tesseract::ParamsModel::ParamsModel ( const char *  lang,
const GenericVector< float > &  weights 
) [inline]

Definition at line 41 of file params_model.h.

                                                                     :
    lang_(lang), pass_(PTRAIN_PASS1) { weights_vec_[pass_] = weights; }

Member Function Documentation

void tesseract::ParamsModel::Clear ( ) [inline]

Definition at line 49 of file params_model.h.

               {
    for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) weights_vec_[p].clear();
  }
float tesseract::ParamsModel::ComputeCost ( const float  features[]) const

Definition at line 78 of file params_model.cpp.

                                                           {
  float unnorm_score = 0.0;
  for (int f = 0; f < PTRAIN_NUM_FEATURE_TYPES; ++f) {
    unnorm_score += weights_vec_[pass_][f] * features[f];
  }
  return ClipToRange(-unnorm_score / kScoreScaleFactor,
                     kMinFinalCost, kMaxFinalCost);
}
void tesseract::ParamsModel::Copy ( const ParamsModel other_model)

Definition at line 48 of file params_model.cpp.

                                                     {
  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
    weights_vec_[p] = other_model.weights_for_pass(
        static_cast<PassEnum>(p));
  }
}
bool tesseract::ParamsModel::Equivalent ( const ParamsModel that) const

Definition at line 87 of file params_model.cpp.

                                                          {
  float epsilon = 0.0001;
  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
    if (weights_vec_[p].size() != that.weights_vec_[p].size()) return false;
    for (int i = 0; i < weights_vec_[p].size(); i++) {
      if (weights_vec_[p][i] != that.weights_vec_[p][i] &&
          fabs(weights_vec_[p][i] - that.weights_vec_[p][i]) > epsilon)
        return false;
    }
  }
  return true;
}

Definition at line 43 of file params_model.h.

                            {
    return weights_vec_[pass_].size() == PTRAIN_NUM_FEATURE_TYPES;
  }
bool tesseract::ParamsModel::LoadFromFile ( const char *  lang,
const char *  full_path 
)

Definition at line 100 of file params_model.cpp.

                           {
  FILE *fp = fopen(full_path, "rb");
  if (!fp) {
    tprintf("Error opening file %s\n", full_path);
    return false;
  }
  bool result = LoadFromFp(lang, fp, -1);
  fclose(fp);
  return result;
}
bool tesseract::ParamsModel::LoadFromFp ( const char *  lang,
FILE *  fp,
inT64  end_offset 
)

Definition at line 113 of file params_model.cpp.

                                                                         {
  const int kMaxLineSize = 100;
  char line[kMaxLineSize];
  BitVector present;
  present.Init(PTRAIN_NUM_FEATURE_TYPES);
  lang_ = lang;
  // Load weights for passes with adaption on.
  GenericVector<float> &weights = weights_vec_[pass_];
  weights.init_to_size(PTRAIN_NUM_FEATURE_TYPES, 0.0);

  while ((end_offset < 0 || ftell(fp) < end_offset) &&
      fgets(line, kMaxLineSize, fp)) {
    char *key = NULL;
    float value;
    if (!ParseLine(line, &key, &value))
      continue;
    int idx = ParamsTrainingFeatureByName(key);
    if (idx < 0) {
      tprintf("ParamsModel::Unknown parameter %s\n", key);
      continue;
    }
    if (!present[idx]) {
      present.SetValue(idx, true);
    }
    weights[idx] = value;
  }
  bool complete = (present.NumSetBits() == PTRAIN_NUM_FEATURE_TYPES);
  if (!complete) {
    for (int i = 0; i < PTRAIN_NUM_FEATURE_TYPES; i++) {
      if (!present[i]) {
        tprintf("Missing field %s.\n", kParamsTrainingFeatureTypeName[i]);
      }
    }
    lang_ = "";
    weights.truncate(0);
  }
  return complete;
}

Definition at line 38 of file params_model.cpp.

                        {
  for (int p = 0; p < PTRAIN_NUM_PASSES; ++p) {
    tprintf("ParamsModel for pass %d lang %s\n", p, lang_.string());
    for (int i = 0; i < weights_vec_[p].size(); ++i) {
      tprintf("%s = %g\n", kParamsTrainingFeatureTypeName[i],
              weights_vec_[p][i]);
    }
  }
}
bool tesseract::ParamsModel::SaveToFile ( const char *  full_path) const

Definition at line 152 of file params_model.cpp.

                                                        {
  const GenericVector<float> &weights = weights_vec_[pass_];
  if (weights.size() != PTRAIN_NUM_FEATURE_TYPES) {
    tprintf("Refusing to save ParamsModel that has not been initialized.\n");
    return false;
  }
  FILE *fp = fopen(full_path, "wb");
  if (!fp) {
    tprintf("Could not open %s for writing.\n", full_path);
    return false;
  }
  bool all_good = true;
  for (int i = 0; i < weights.size(); i++) {
    if (fprintf(fp, "%s %f\n", kParamsTrainingFeatureTypeName[i], weights[i])
        < 0) {
      all_good = false;
    }
  }
  fclose(fp);
  return all_good;
}
void tesseract::ParamsModel::SetPass ( PassEnum  pass) [inline]

Definition at line 72 of file params_model.h.

{ pass_ = pass; }
const GenericVector<float>& tesseract::ParamsModel::weights ( ) const [inline]

Definition at line 66 of file params_model.h.

                                              {
    return weights_vec_[pass_];
  }
const GenericVector<float>& tesseract::ParamsModel::weights_for_pass ( PassEnum  pass) const [inline]

Definition at line 69 of file params_model.h.

                                                                    {
    return weights_vec_[pass];
  }

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines