Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new option -l for combine_tessdata to list LSTM information #3237

Merged
merged 4 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/combine_tessdata.1.asc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ OPTIONS
*-e* '.traineddata' 'FILE'...:
Extracts the specified components from the .traineddata file

*-l* '.traineddata' 'FILE'...:
List the network information.

*-o* '.traineddata' 'FILE'...:
Overwrites the specified components of the .traineddata file
with those provided on the command line.
Expand Down
2 changes: 1 addition & 1 deletion src/ccmain/tessedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ bool Tesseract::init_tesseract_lang_data(
tessedit_ocr_engine_mode == OEM_TESSERACT_LSTM_COMBINED) {
#endif // ndef DISABLED_LEGACY_ENGINE
if (mgr->IsComponentAvailable(TESSDATA_LSTM)) {
lstm_recognizer_ = new LSTMRecognizer(language_data_path_prefix);
lstm_recognizer_ = new LSTMRecognizer(language_data_path_prefix.c_str());
ASSERT_HOST(lstm_recognizer_->Load(
this->params(), lstm_use_matrix ? language : nullptr, mgr));
} else {
Expand Down
18 changes: 18 additions & 0 deletions src/ccutil/serialis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ template TESS_API bool TFile::DeSerialize(std::vector<int32_t>& data);
template TESS_API bool TFile::Serialize(const std::vector<double>& data);
template TESS_API bool TFile::Serialize(const std::vector<int32_t>& data);

bool TFile::DeSerialize(std::string& data) {
uint32_t size;
if (!DeSerialize(&size)) {
return false;
} else if (size > 0) {
// TODO: optimize.
data.resize(size);
return DeSerialize(&data[0], size);
}
data.clear();
return true;
}

bool TFile::Serialize(const std::string& data) {
uint32_t size = data.size();
return Serialize(&size) && Serialize(data.c_str(), size);
}

bool TFile::DeSerialize(std::vector<char>& data) {
uint32_t size;
if (!DeSerialize(&size)) {
Expand Down
2 changes: 2 additions & 0 deletions src/ccutil/serialis.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class TESS_API TFile {
}

// Deserialize data.
bool DeSerialize(std::string& data);
bool DeSerialize(std::vector<char>& data);
template <typename T> bool DeSerialize(std::vector<T>& data);
template <typename T>
Expand All @@ -93,6 +94,7 @@ class TESS_API TFile {
}

// Serialize data.
bool Serialize(const std::string& data);
bool Serialize(const std::vector<char>& data);
template <typename T> bool Serialize(const std::vector<T>& data);
template <typename T>
Expand Down
6 changes: 3 additions & 3 deletions src/lstm/lstmrecognizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const double kDictRatio = 2.25;
// Default certainty offset to give the dictionary a chance.
const double kCertOffset = -0.085;

LSTMRecognizer::LSTMRecognizer(const STRING language_data_path_prefix)
LSTMRecognizer::LSTMRecognizer(const char* language_data_path_prefix)
: LSTMRecognizer::LSTMRecognizer() {
ccutil_.language_data_path_prefix = language_data_path_prefix;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ bool LSTMRecognizer::Serialize(const TessdataManager* mgr, TFile* fp) const {
!mgr->IsComponentAvailable(TESSDATA_LSTM_UNICHARSET);
if (!network_->Serialize(fp)) return false;
if (include_charsets && !GetUnicharset().save_to_file(fp)) return false;
if (!network_str_.Serialize(fp)) return false;
if (!fp->Serialize(network_str_)) return false;
if (!fp->Serialize(&training_flags_)) return false;
if (!fp->Serialize(&training_iteration_)) return false;
if (!fp->Serialize(&sample_iteration_)) return false;
Expand All @@ -112,7 +112,7 @@ bool LSTMRecognizer::DeSerialize(const TessdataManager* mgr, TFile* fp) {
!mgr->IsComponentAvailable(TESSDATA_LSTM_UNICHARSET);
if (include_charsets && !ccutil_.unicharset.load_from_file(fp, false))
return false;
if (!network_str_.DeSerialize(fp)) return false;
if (!fp->DeSerialize(network_str_)) return false;
if (!fp->DeSerialize(&training_flags_)) return false;
if (!fp->DeSerialize(&training_iteration_)) return false;
if (!fp->DeSerialize(&sample_iteration_)) return false;
Expand Down
31 changes: 28 additions & 3 deletions src/lstm/lstmrecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ enum TrainingFlags {
class TESS_API LSTMRecognizer {
public:
LSTMRecognizer();
LSTMRecognizer(const STRING language_data_path_prefix);
LSTMRecognizer(const char* language_data_path_prefix);
~LSTMRecognizer();

int NumOutputs() const { return network_->NumOutputs(); }

// Return the training iterations.
int training_iteration() const { return training_iteration_; }

// Return the sample iterations.
int sample_iteration() const { return sample_iteration_; }
double learning_rate() const { return learning_rate_; }

// Return the learning rate.
float learning_rate() const { return learning_rate_; }

LossType OutputLossType() const {
if (network_ == nullptr) return LT_NONE;
StaticShape shape;
Expand Down Expand Up @@ -101,6 +108,22 @@ class TESS_API LSTMRecognizer {
return learning_rate_;
}
}

// Return the network string.
const char* GetNetwork() const {
return network_str_.c_str();
}

// Return the adam beta.
float GetAdamBeta() const {
return adam_beta_;
}

// Return the momentum.
float GetMomentum() const {
return momentum_;
}

// Multiplies the all the learning rate(s) by the given factor.
void ScaleLearningRate(double factor) {
ASSERT_HOST(network_ != nullptr && network_->type() == NT_SERIES);
Expand Down Expand Up @@ -142,6 +165,8 @@ class TESS_API LSTMRecognizer {
void SetIteration(int iteration) { sample_iteration_ = iteration; }
// Accessors for textline image normalization.
int NumInputs() const { return network_->NumInputs(); }

// Return the null char index.
int null_char() const { return null_char_; }

// Loads a model from mgr, including the dictionary only if lang is not null.
Expand Down Expand Up @@ -268,7 +293,7 @@ class TESS_API LSTMRecognizer {
UnicharCompress recoder_;

// ==Training parameters that are serialized to provide a record of them.==
STRING network_str_;
std::string network_str_;
// Flags used to determine the training method of the network.
// See enum TrainingFlags above.
int32_t training_flags_;
Expand Down
32 changes: 30 additions & 2 deletions src/training/combine_tessdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "tessdatamanager.h"

#include <cerrno>
#include <iostream> // std::cout

using namespace tesseract;

Expand Down Expand Up @@ -174,6 +175,30 @@ int main(int argc, char **argv) {
} else if (argc == 3 && strcmp(argv[1], "-d") == 0) {
// Initialize TessdataManager with the data in the given traineddata file.
tm.Init(argv[2]);
} else if (argc == 3 && strcmp(argv[1], "-l") == 0) {
if (!tm.Init(argv[2])) {
tprintf("Failed to read %s\n", argv[2]);
return EXIT_FAILURE;
}
tesseract::TFile fp;
if (tm.GetComponent(tesseract::TESSDATA_LSTM, &fp)) {
tesseract::LSTMRecognizer recognizer;
if (!recognizer.DeSerialize(&tm, &fp)) {
tprintf("Failed to deserialize LSTM in %s!\n", argv[2]);
return EXIT_FAILURE;
}
std::cout << "LSTM: network=" << recognizer.GetNetwork()
<< ", int_mode=" << recognizer.IsIntMode()
<< ", recoding=" << recognizer.IsRecoding()
<< ", iteration=" << recognizer.training_iteration()
<< ", sample_iteration=" << recognizer.sample_iteration()
<< ", null_char=" << recognizer.null_char()
<< ", learning_rate=" << recognizer.learning_rate()
<< ", momentum=" << recognizer.GetMomentum()
<< ", adam_beta=" << recognizer.GetAdamBeta()
<< '\n';
}
return EXIT_SUCCESS;
} else {
printf("Usage for combining tessdata components:\n"
" %s language_data_path_prefix\n"
Expand All @@ -188,10 +213,13 @@ int main(int argc, char **argv) {
argv[0], argv[0]);
printf("Usage for unpacking all tessdata components:\n"
" %s -u traineddata_file output_path_prefix\n"
" (e.g. %s -u eng.traineddata tmp/eng.)\n", argv[0], argv[0]);
" (e.g. %s -u eng.traineddata tmp/eng.)\n\n", argv[0], argv[0]);
printf("Usage for listing the network information\n"
" %s -l traineddata_file\n"
" (e.g. %s -l eng.traineddata)\n\n", argv[0], argv[0]);
printf(
"Usage for listing directory of components:\n"
" %s -d traineddata_file\n",
" %s -d traineddata_file\n\n",
argv[0]);
printf(
"Usage for compacting LSTM component to int:\n"
Expand Down