mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2024-12-12 15:39:04 +08:00
524a61452d
Squashed commit from https://github.com/tesseract-ocr/tesseract/tree/more-doxygen closes #14 Commits:6317305
doxygen9f42f69
doxygen0fc4d52
doxygen37b4b55
fix typobded8f1
some more doxy020eb00
slight tweak524666d
doxygenify2a36a3e
doxygenify229d218
doxygenify7fd28ae
doxygenifya8c64bc
doxygenifyf5d21b6
fix5d8ede8
doxygenifya58a4e0
language_model.cppfa85709
lm_pain_points.cpp lm_state.cpp6418da3
merge06190ba
Merge branch 'old_doxygen_merge' into more-doxygen84acf08
Merge branch 'master' into more-doxygen50fe1ff
pagewalk.cpp cube_reco_context.cpp2982583
change to relative192a24a
applybox.cpp, take one8eeb053
delete docs for obsolete params52e4c77
modernise classify/ocrfeatures.cpp2a1cba6
modernise cutil/emalloc.cpp773e006
silence doxygen warningaeb1731
silence doxygen warningf18387f
silence doxygen; new params are unused?15ad6bd
doxygenify cutil/efio.cppc8b5dad
doxygenify cutil/danerror.cpp784450f
the globals and exceptions parts are obsolete; remove8bca324
doxygen classify/normfeat.cpp9bcbe16
doxygen classify/normmatch.cppaa9a971
doxygen ccmain/cube_control.cppc083ff2
doxygen ccmain/cube_reco_context.cppf842850
params changed5c94f12
doxygen ccmain/cubeclassifier.cpp15ba750
case sensitivef5c71d4
case sensitivef85655b
doxygen classify/intproto.cpp4bbc7aa
partial doxygen classify/mfx.cppdbb6041
partial doxygen classify/intproto.cpp2aa72db
finish doxygen classify/intproto.cpp0b8de99
doxygen training/mftraining.cpp0b5b35c
partial doxygen ccstruct/coutln.cppb81c766
partial doxygen ccstruct/coutln.cpp40fc415
finished? doxygen ccstruct/coutln.cpp6e4165c
doxygen classify/clusttool.cpp0267dec
doxygen classify/cutoffs.cpp7f0c70c
doxygen classify/fpoint.cpp512f3bd
ignore ~ files5668a52
doxygen classify/intmatcher.cpp84788d4
doxygen classify/kdtree.cpp29f36ca
doxygen classify/mfoutline.cpp40b94b1
silence doxygen warnings6c511b9
doxygen classify/mfx.cppf9b4080
doxygen classify/outfeat.cppaa1df05
doxygen classify/picofeat.cppcc5f466
doxygen training/cntraining.cppcce044f
doxygen training/commontraining.cpp167e216
missing param9498383
renamed params37eeac2
renamed paramd87b5dd
casec8ee174
renamed paramsb858db8
typo4c2a838
h2 context?81a2c0c
fix some param names; add some missing params, no docsbcf8a4c
add some missing params, no docsaf77f86
add some missing params, no docs; fix some param names01df24e
fix some params6161056
fix some params68508b6
fix some params285aeb6
doxygen complains here no matter what529bcfa
rm some missing params, typoscd21226
rm some missing params, add some new ones48a4bc2
fix paramsc844628
missing param312ce37
missing param; rename oneec2fdec
missing param05e15e0
missing paramsd515858
change "<" to < to make doxygen happyb476a28
wrong place
391 lines
12 KiB
C++
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
|