tesseract/cube/conv_net_classifier.cpp
Jim O'Regan 524a61452d Doxygen
Squashed commit from https://github.com/tesseract-ocr/tesseract/tree/more-doxygen
closes #14

Commits:
6317305  doxygen
9f42f69  doxygen
0fc4d52  doxygen
37b4b55  fix typo
bded8f1  some more doxy
020eb00  slight tweak
524666d  doxygenify
2a36a3e  doxygenify
229d218  doxygenify
7fd28ae  doxygenify
a8c64bc  doxygenify
f5d21b6  fix
5d8ede8  doxygenify
a58a4e0  language_model.cpp
fa85709  lm_pain_points.cpp lm_state.cpp
6418da3  merge
06190ba  Merge branch 'old_doxygen_merge' into more-doxygen
84acf08  Merge branch 'master' into more-doxygen
50fe1ff  pagewalk.cpp cube_reco_context.cpp
2982583  change to relative
192a24a  applybox.cpp, take one
8eeb053  delete docs for obsolete params
52e4c77  modernise classify/ocrfeatures.cpp
2a1cba6  modernise cutil/emalloc.cpp
773e006  silence doxygen warning
aeb1731  silence doxygen warning
f18387f  silence doxygen; new params are unused?
15ad6bd  doxygenify cutil/efio.cpp
c8b5dad  doxygenify cutil/danerror.cpp
784450f  the globals and exceptions parts are obsolete; remove
8bca324  doxygen classify/normfeat.cpp
9bcbe16  doxygen classify/normmatch.cpp
aa9a971  doxygen ccmain/cube_control.cpp
c083ff2  doxygen ccmain/cube_reco_context.cpp
f842850  params changed
5c94f12  doxygen ccmain/cubeclassifier.cpp
15ba750  case sensitive
f5c71d4  case sensitive
f85655b  doxygen classify/intproto.cpp
4bbc7aa  partial doxygen classify/mfx.cpp
dbb6041  partial doxygen classify/intproto.cpp
2aa72db  finish doxygen classify/intproto.cpp
0b8de99  doxygen training/mftraining.cpp
0b5b35c  partial doxygen ccstruct/coutln.cpp
b81c766  partial doxygen ccstruct/coutln.cpp
40fc415  finished? doxygen ccstruct/coutln.cpp
6e4165c  doxygen classify/clusttool.cpp
0267dec  doxygen classify/cutoffs.cpp
7f0c70c  doxygen classify/fpoint.cpp
512f3bd  ignore ~ files
5668a52  doxygen classify/intmatcher.cpp
84788d4  doxygen classify/kdtree.cpp
29f36ca  doxygen classify/mfoutline.cpp
40b94b1  silence doxygen warnings
6c511b9  doxygen classify/mfx.cpp
f9b4080  doxygen classify/outfeat.cpp
aa1df05  doxygen classify/picofeat.cpp
cc5f466  doxygen training/cntraining.cpp
cce044f  doxygen training/commontraining.cpp
167e216  missing param
9498383  renamed params
37eeac2  renamed param
d87b5dd  case
c8ee174  renamed params
b858db8  typo
4c2a838  h2 context?
81a2c0c  fix some param names; add some missing params, no docs
bcf8a4c  add some missing params, no docs
af77f86  add some missing params, no docs; fix some param names
01df24e  fix some params
6161056  fix some params
68508b6  fix some params
285aeb6  doxygen complains here no matter what
529bcfa  rm some missing params, typos
cd21226  rm some missing params, add some new ones
48a4bc2  fix params
c844628  missing param
312ce37  missing param; rename one
ec2fdec  missing param
05e15e0  missing params
d515858  change "<" to &lt; to make doxygen happy
b476a28  wrong place
2015-07-20 18:48:00 +01:00

391 lines
12 KiB
C++

/**********************************************************************
* File: charclassifier.cpp
* Description: Implementation of Convolutional-NeuralNet Character Classifier
* Author: Ahmad Abdulkader
* Created: 2007
*
* (C) Copyright 2008, Google Inc.
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
** http://www.apache.org/licenses/LICENSE-2.0
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*
**********************************************************************/
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <wctype.h>
#include "char_set.h"
#include "classifier_base.h"
#include "const.h"
#include "conv_net_classifier.h"
#include "cube_utils.h"
#include "feature_base.h"
#include "feature_bmp.h"
#include "tess_lang_model.h"
namespace tesseract {
ConvNetCharClassifier::ConvNetCharClassifier(CharSet *char_set,
TuningParams *params,
FeatureBase *feat_extract)
: CharClassifier(char_set, params, feat_extract) {
char_net_ = NULL;
net_input_ = NULL;
net_output_ = NULL;
}
ConvNetCharClassifier::~ConvNetCharClassifier() {
if (char_net_ != NULL) {
delete char_net_;
char_net_ = NULL;
}
if (net_input_ != NULL) {
delete []net_input_;
net_input_ = NULL;
}
if (net_output_ != NULL) {
delete []net_output_;
net_output_ = NULL;
}
}
/**
* The main training function. Given a sample and a class ID the classifier
* updates its parameters according to its learning algorithm. This function
* is currently not implemented. TODO(ahmadab): implement end-2-end training
*/
bool ConvNetCharClassifier::Train(CharSamp *char_samp, int ClassID) {
return false;
}
/**
* A secondary function needed for training. Allows the trainer to set the
* value of any train-time paramter. This function is currently not
* implemented. TODO(ahmadab): implement end-2-end training
*/
bool ConvNetCharClassifier::SetLearnParam(char *var_name, float val) {
// TODO(ahmadab): implementation of parameter initializing.
return false;
}
/**
* Folds the output of the NeuralNet using the loaded folding sets
*/
void ConvNetCharClassifier::Fold() {
// in case insensitive mode
if (case_sensitive_ == false) {
int class_cnt = char_set_->ClassCount();
// fold case
for (int class_id = 0; class_id < class_cnt; class_id++) {
// get class string
const char_32 *str32 = char_set_->ClassString(class_id);
// get the upper case form of the string
string_32 upper_form32 = str32;
for (int ch = 0; ch < upper_form32.length(); ch++) {
if (iswalpha(static_cast<int>(upper_form32[ch])) != 0) {
upper_form32[ch] = towupper(upper_form32[ch]);
}
}
// find out the upperform class-id if any
int upper_class_id =
char_set_->ClassID(reinterpret_cast<const char_32 *>(
upper_form32.c_str()));
if (upper_class_id != -1 && class_id != upper_class_id) {
float max_out = MAX(net_output_[class_id], net_output_[upper_class_id]);
net_output_[class_id] = max_out;
net_output_[upper_class_id] = max_out;
}
}
}
// The folding sets specify how groups of classes should be folded
// Folding involved assigning a min-activation to all the members
// of the folding set. The min-activation is a fraction of the max-activation
// of the members of the folding set
for (int fold_set = 0; fold_set < fold_set_cnt_; fold_set++) {
if (fold_set_len_[fold_set] == 0)
continue;
float max_prob = net_output_[fold_sets_[fold_set][0]];
for (int ch = 1; ch < fold_set_len_[fold_set]; ch++) {
if (net_output_[fold_sets_[fold_set][ch]] > max_prob) {
max_prob = net_output_[fold_sets_[fold_set][ch]];
}
}
for (int ch = 0; ch < fold_set_len_[fold_set]; ch++) {
net_output_[fold_sets_[fold_set][ch]] = MAX(max_prob * kFoldingRatio,
net_output_[fold_sets_[fold_set][ch]]);
}
}
}
/**
* Compute the features of specified charsamp and feedforward the
* specified nets
*/
bool ConvNetCharClassifier::RunNets(CharSamp *char_samp) {
if (char_net_ == NULL) {
fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::RunNets): "
"NeuralNet is NULL\n");
return false;
}
int feat_cnt = char_net_->in_cnt();
int class_cnt = char_set_->ClassCount();
// allocate i/p and o/p buffers if needed
if (net_input_ == NULL) {
net_input_ = new float[feat_cnt];
if (net_input_ == NULL) {
fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::RunNets): "
"unable to allocate memory for input nodes\n");
return false;
}
net_output_ = new float[class_cnt];
if (net_output_ == NULL) {
fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::RunNets): "
"unable to allocate memory for output nodes\n");
return false;
}
}
// compute input features
if (feat_extract_->ComputeFeatures(char_samp, net_input_) == false) {
fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::RunNets): "
"unable to compute features\n");
return false;
}
if (char_net_ != NULL) {
if (char_net_->FeedForward(net_input_, net_output_) == false) {
fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::RunNets): "
"unable to run feed-forward\n");
return false;
}
} else {
return false;
}
Fold();
return true;
}
/**
* return the cost of being a char
*/
int ConvNetCharClassifier::CharCost(CharSamp *char_samp) {
if (RunNets(char_samp) == false) {
return 0;
}
return CubeUtils::Prob2Cost(1.0f - net_output_[0]);
}
/**
* classifies a charsamp and returns an alternate list
* of chars sorted by char costs
*/
CharAltList *ConvNetCharClassifier::Classify(CharSamp *char_samp) {
// run the needed nets
if (RunNets(char_samp) == false) {
return NULL;
}
int class_cnt = char_set_->ClassCount();
// create an altlist
CharAltList *alt_list = new CharAltList(char_set_, class_cnt);
if (alt_list == NULL) {
fprintf(stderr, "Cube WARNING (ConvNetCharClassifier::Classify): "
"returning emtpy CharAltList\n");
return NULL;
}
for (int out = 1; out < class_cnt; out++) {
int cost = CubeUtils::Prob2Cost(net_output_[out]);
alt_list->Insert(out, cost);
}
return alt_list;
}
/**
* Set an external net (for training purposes)
*/
void ConvNetCharClassifier::SetNet(tesseract::NeuralNet *char_net) {
if (char_net_ != NULL) {
delete char_net_;
char_net_ = NULL;
}
char_net_ = char_net;
}
/**
* This function will return true if the file does not exist.
* But will fail if the it did not pass the sanity checks
*/
bool ConvNetCharClassifier::LoadFoldingSets(const string &data_file_path,
const string &lang,
LangModel *lang_mod) {
fold_set_cnt_ = 0;
string fold_file_name;
fold_file_name = data_file_path + lang;
fold_file_name += ".cube.fold";
// folding sets are optional
FILE *fp = fopen(fold_file_name.c_str(), "rb");
if (fp == NULL) {
return true;
}
fclose(fp);
string fold_sets_str;
if (!CubeUtils::ReadFileToString(fold_file_name,
&fold_sets_str)) {
return false;
}
// split into lines
vector<string> str_vec;
CubeUtils::SplitStringUsing(fold_sets_str, "\r\n", &str_vec);
fold_set_cnt_ = str_vec.size();
fold_sets_ = new int *[fold_set_cnt_];
if (fold_sets_ == NULL) {
return false;
}
fold_set_len_ = new int[fold_set_cnt_];
if (fold_set_len_ == NULL) {
fold_set_cnt_ = 0;
return false;
}
for (int fold_set = 0; fold_set < fold_set_cnt_; fold_set++) {
reinterpret_cast<TessLangModel *>(lang_mod)->RemoveInvalidCharacters(
&str_vec[fold_set]);
// if all or all but one character are invalid, invalidate this set
if (str_vec[fold_set].length() <= 1) {
fprintf(stderr, "Cube WARNING (ConvNetCharClassifier::LoadFoldingSets): "
"invalidating folding set %d\n", fold_set);
fold_set_len_[fold_set] = 0;
fold_sets_[fold_set] = NULL;
continue;
}
string_32 str32;
CubeUtils::UTF8ToUTF32(str_vec[fold_set].c_str(), &str32);
fold_set_len_[fold_set] = str32.length();
fold_sets_[fold_set] = new int[fold_set_len_[fold_set]];
if (fold_sets_[fold_set] == NULL) {
fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::LoadFoldingSets): "
"could not allocate folding set\n");
fold_set_cnt_ = fold_set;
return false;
}
for (int ch = 0; ch < fold_set_len_[fold_set]; ch++) {
fold_sets_[fold_set][ch] = char_set_->ClassID(str32[ch]);
}
}
return true;
}
/**
* Init the classifier provided a data-path and a language string
*/
bool ConvNetCharClassifier::Init(const string &data_file_path,
const string &lang,
LangModel *lang_mod) {
if (init_) {
return true;
}
// load the nets if any. This function will return true if the net file
// does not exist. But will fail if the net did not pass the sanity checks
if (!LoadNets(data_file_path, lang)) {
return false;
}
// load the folding sets if any. This function will return true if the
// file does not exist. But will fail if the it did not pass the sanity checks
if (!LoadFoldingSets(data_file_path, lang, lang_mod)) {
return false;
}
init_ = true;
return true;
}
/**
* Load the classifier's Neural Nets
* This function will return true if the net file does not exist.
* But will fail if the net did not pass the sanity checks
*/
bool ConvNetCharClassifier::LoadNets(const string &data_file_path,
const string &lang) {
string char_net_file;
// add the lang identifier
char_net_file = data_file_path + lang;
char_net_file += ".cube.nn";
// neural network is optional
FILE *fp = fopen(char_net_file.c_str(), "rb");
if (fp == NULL) {
return true;
}
fclose(fp);
// load main net
char_net_ = tesseract::NeuralNet::FromFile(char_net_file);
if (char_net_ == NULL) {
fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::LoadNets): "
"could not load %s\n", char_net_file.c_str());
return false;
}
// validate net
if (char_net_->in_cnt()!= feat_extract_->FeatureCnt()) {
fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::LoadNets): "
"could not validate net %s\n", char_net_file.c_str());
return false;
}
// alloc net i/o buffers
int feat_cnt = char_net_->in_cnt();
int class_cnt = char_set_->ClassCount();
if (char_net_->out_cnt() != class_cnt) {
fprintf(stderr, "Cube ERROR (ConvNetCharClassifier::LoadNets): "
"output count (%d) and class count (%d) are not equal\n",
char_net_->out_cnt(), class_cnt);
return false;
}
// allocate i/p and o/p buffers if needed
if (net_input_ == NULL) {
net_input_ = new float[feat_cnt];
if (net_input_ == NULL) {
return false;
}
net_output_ = new float[class_cnt];
if (net_output_ == NULL) {
return false;
}
}
return true;
}
} // tesseract