Skip to content

Commit bbd23bb

Browse files
Shreeshriistweil
authored andcommitted
Fix and enable lstm related unittests (#2180)
* Fix and build lstm related unittests * Use ./tmp instead of ./ for files created by unittests
1 parent 12c1abc commit bbd23bb

File tree

7 files changed

+129
-53
lines changed

7 files changed

+129
-53
lines changed

unittest/Makefile.am

+13-1
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,12 @@ check_PROGRAMS = \
133133

134134
if ENABLE_TRAINING
135135
check_PROGRAMS += commandlineflags_test
136+
check_PROGRAMS += lstm_recode_test
137+
check_PROGRAMS += lstm_squashed_test
138+
check_PROGRAMS += lstm_test
136139
check_PROGRAMS += unichar_test
137-
check_PROGRAMS += unicharset_test
138140
check_PROGRAMS += unicharcompress_test
141+
check_PROGRAMS += unicharset_test
139142
check_PROGRAMS += validate_grapheme_test
140143
check_PROGRAMS += validate_indic_test
141144
check_PROGRAMS += validate_khmer_test
@@ -215,6 +218,15 @@ linlsq_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
215218
loadlang_test_SOURCES = loadlang_test.cc
216219
loadlang_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)
217220

221+
lstm_recode_test_SOURCES = lstm_recode_test.cc
222+
lstm_recode_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS) $(TRAINING_LIBS)
223+
224+
lstm_squashed_test_SOURCES = lstm_squashed_test.cc
225+
lstm_squashed_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS) $(TRAINING_LIBS)
226+
227+
lstm_test_SOURCES = lstm_test.cc
228+
lstm_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS) $(TRAINING_LIBS)
229+
218230
mastertrainer_test_SOURCES = mastertrainer_test.cc
219231
mastertrainer_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TRAINING_LIBS) $(TESS_LIBS)
220232

unittest/include_gunit.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "fileio.h" // for tesseract::File
1818
#include "gtest/gtest.h"
1919

20-
const char* FLAGS_test_tmpdir = ".";
20+
const char* FLAGS_test_tmpdir = "./tmp";
2121

2222
class file : public tesseract::File {
2323
public:

unittest/lstm_recode_test.cc

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
1+
// (C) Copyright 2017, Google Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
// Unless required by applicable law or agreed to in writing, software
7+
// distributed under the License is distributed on an "AS IS" BASIS,
8+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
// See the License for the specific language governing permissions and
10+
// limitations under the License.
111

2-
#include "tesseract/unittest/lstm_test.h"
12+
#include "lstm_test.h"
313

414
namespace tesseract {
515

616
// Tests that training with unicharset recoding learns faster than without,
717
// for Korean. This test is split in two, so it can be run sharded.
18+
819
TEST_F(LSTMTrainerTest, RecodeTestKorBase) {
920
// A basic single-layer, bi-di 1d LSTM on Korean.
10-
SetupTrainer("[1,1,0,32 Lbx96 O1c1]", "kor-full", "kor.unicharset",
11-
"arialuni.kor.lstmf", false, true, 5e-4, false);
12-
double kor_full_err = TrainIterations(kTrainerIterations);
21+
SetupTrainer("[1,1,0,32 Lbx96 O1c1]", "kor-full", "kor/kor.unicharset",
22+
"kor.Arial_Unicode_MS.exp0.lstmf", false, true, 5e-4, false);
23+
double kor_full_err = TrainIterations(kTrainerIterations * 2);
1324
EXPECT_LT(kor_full_err, 88);
14-
EXPECT_GT(kor_full_err, 85);
25+
// EXPECT_GT(kor_full_err, 85);
1526
}
1627

1728
TEST_F(LSTMTrainerTest, RecodeTestKor) {
1829
// A basic single-layer, bi-di 1d LSTM on Korean.
19-
SetupTrainer("[1,1,0,32 Lbx96 O1c1]", "kor-recode", "kor.unicharset",
20-
"arialuni.kor.lstmf", true, true, 5e-4, false);
30+
SetupTrainer("[1,1,0,32 Lbx96 O1c1]", "kor-recode", "kor/kor.unicharset",
31+
"kor.Arial_Unicode_MS.exp0.lstmf", true, true, 5e-4, false);
2132
double kor_recode_err = TrainIterations(kTrainerIterations);
2233
EXPECT_LT(kor_recode_err, 60);
2334
}
2435

36+
// Tests that the given string encodes and decodes back to the same
37+
// with both recode on and off for Korean.
38+
39+
TEST_F(LSTMTrainerTest, EncodeDecodeBothTestKor) {
40+
TestEncodeDecodeBoth("kor", "한국어 위키백과에 오신 것을 환영합니다!");
41+
}
42+
43+
2544
} // namespace tesseract.

unittest/lstm_squashed_test.cc

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
// (C) Copyright 2017, Google Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
// Unless required by applicable law or agreed to in writing, software
7+
// distributed under the License is distributed on an "AS IS" BASIS,
8+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
// See the License for the specific language governing permissions and
10+
// limitations under the License.
111

2-
#include "tesseract/unittest/lstm_test.h"
12+
#include "lstm_test.h"
313

414
namespace tesseract {
515

@@ -14,6 +24,7 @@ TEST_F(LSTMTrainerTest, TestSquashed) {
1424
"SQU-2-layer-lstm", /*recode*/ true, /*adam*/ true);
1525
double lstm_2d_err = TrainIterations(kTrainerIterations * 2);
1626
EXPECT_LT(lstm_2d_err, 80);
27+
LOG(INFO) << "********** < 80 ************" ;
1728
TestIntMode(kTrainerIterations);
1829
}
1930

unittest/lstm_test.cc

+30-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1+
// (C) Copyright 2017, Google Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
// Unless required by applicable law or agreed to in writing, software
7+
// distributed under the License is distributed on an "AS IS" BASIS,
8+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
// See the License for the specific language governing permissions and
10+
// limitations under the License.
111

212
// Generating the training data:
313
// If the format of the lstmf (ImageData) file changes, the training data will
4-
// have to be regenerated as follows:
5-
// ./tesseract/text2image --xsize=800 --font=Arial \
6-
// --text=tesseract/testdata/lstm_training.txt --leading=32 \
7-
// --outputbase=tesseract/testdata/lstm_training.arial
8-
// ./tesseract tesseract/testdata/lstm_training.arial.tif \
9-
// tesseract/testdata/lstm_training.arial lstm.train \
10-
// --pageseg_mode=6
14+
// have to be regenerated as follows:
15+
//
16+
// Use --xsize 800 for text2image to be similar to original training data.
17+
//
18+
// src/training/tesstrain.sh --fonts_dir /usr/share/fonts --lang eng --linedata_only --noextract_font_properties --langdata_dir ../langdata_lstm --tessdata_dir ../tessdata --output_dir ~/tesseract/test/testdata --fontlist "Arial" --maxpages 10
19+
//
1120

12-
#include "tesseract/unittest/lstm_test.h"
21+
#include "lstm_test.h"
1322

1423
namespace tesseract {
1524

@@ -19,15 +28,17 @@ TEST_F(LSTMTrainerTest, BasicTest) {
1928
SetupTrainer(
2029
"[1,32,0,1 Ct5,5,16 Mp4,4 Ct1,1,16 Ct3,3,128 Mp4,1 Ct1,1,64 S2,1 "
2130
"Ct1,1,64O1c1]",
22-
"no-lstm", "eng.unicharset", "lstm_training.arial.lstmf", false, false,
31+
"no-lstm", "eng/eng.unicharset", "eng.Arial.exp0.lstmf", false, false,
2332
2e-4, false);
24-
double non_lstm_err = TrainIterations(kTrainerIterations * 3 / 2);
33+
double non_lstm_err = TrainIterations(kTrainerIterations * 3);
2534
EXPECT_LT(non_lstm_err, 98);
35+
LOG(INFO) << "********** Expected < 98 ************" ;
2636

2737
// A basic single-layer, single direction LSTM.
2838
SetupTrainerEng("[1,1,0,32 Lfx100 O1c1]", "1D-lstm", false, false);
2939
double lstm_uni_err = TrainIterations(kTrainerIterations * 2);
3040
EXPECT_LT(lstm_uni_err, 86);
41+
LOG(INFO) << "********** Expected < 86 ************" ;
3142
// Beats the convolver. (Although it does have a lot more weights, it still
3243
// iterates faster.)
3344
EXPECT_LT(lstm_uni_err, non_lstm_err);
@@ -41,14 +52,15 @@ TEST_F(LSTMTrainerTest, ColorTest) {
4152
double lstm_uni_err = TrainIterations(kTrainerIterations);
4253
EXPECT_LT(lstm_uni_err, 85);
4354
EXPECT_GT(lstm_uni_err, 66);
55+
LOG(INFO) << "********** Expected > 66 ** < 85 ************" ;
4456
}
4557

4658
TEST_F(LSTMTrainerTest, BidiTest) {
4759
// A basic single-layer, bi-di 1d LSTM.
4860
SetupTrainerEng("[1,1,0,32 Lbx100 O1c1]", "bidi-lstm", false, false);
4961
double lstm_bi_err = TrainIterations(kTrainerIterations);
5062
EXPECT_LT(lstm_bi_err, 75);
51-
63+
LOG(INFO) << "********** Expected < 75 ************" ;
5264
// Int mode training is dead, so convert the trained network to int and check
5365
// that its error rate is close to the float version.
5466
TestIntMode(kTrainerIterations);
@@ -63,6 +75,7 @@ TEST_F(LSTMTrainerTest, Test2D) {
6375
double lstm_2d_err = TrainIterations(kTrainerIterations);
6476
EXPECT_LT(lstm_2d_err, 98);
6577
EXPECT_GT(lstm_2d_err, 90);
78+
LOG(INFO) << "********** Expected > 90 ** < 98 ************" ;
6679
// Int mode training is dead, so convert the trained network to int and check
6780
// that its error rate is close to the float version.
6881
TestIntMode(kTrainerIterations);
@@ -76,6 +89,7 @@ TEST_F(LSTMTrainerTest, TestAdam) {
7689
"2-D-2-layer-lstm", false, true);
7790
double lstm_2d_err = TrainIterations(kTrainerIterations);
7891
EXPECT_LT(lstm_2d_err, 70);
92+
LOG(INFO) << "********** Expected < 70 ************" ;
7993
TestIntMode(kTrainerIterations);
8094
}
8195

@@ -86,6 +100,7 @@ TEST_F(LSTMTrainerTest, SpeedTest) {
86100
"O1c1]",
87101
"2-D-2-layer-lstm", false, true);
88102
TrainIterations(kTrainerIterations);
103+
LOG(INFO) << "********** *** ************" ;
89104
}
90105

91106
// Tests that two identical networks trained the same get the same results.
@@ -121,6 +136,7 @@ TEST_F(LSTMTrainerTest, DeterminismTest) {
121136
EXPECT_FLOAT_EQ(lstm_2d_err_a, lstm_2d_err_b);
122137
EXPECT_FLOAT_EQ(act_error_a, act_error_b);
123138
EXPECT_FLOAT_EQ(char_error_a, char_error_b);
139+
LOG(INFO) << "********** *** ************" ;
124140
}
125141

126142
// The baseline network against which to test the built-in softmax.
@@ -130,6 +146,7 @@ TEST_F(LSTMTrainerTest, SoftmaxBaselineTest) {
130146
double lstm_uni_err = TrainIterations(kTrainerIterations * 2);
131147
EXPECT_LT(lstm_uni_err, 60);
132148
EXPECT_GT(lstm_uni_err, 48);
149+
LOG(INFO) << "********** Expected > 48 ** < 60 ************" ;
133150
// Check that it works in int mode too.
134151
TestIntMode(kTrainerIterations);
135152
// If we run TestIntMode again, it tests that int_mode networks can
@@ -148,6 +165,7 @@ TEST_F(LSTMTrainerTest, SoftmaxTest) {
148165
SetupTrainerEng("[1,1,0,32 LS96]", "Lstm-+-softmax", false, true);
149166
double lstm_sm_err = TrainIterations(kTrainerIterations * 2);
150167
EXPECT_LT(lstm_sm_err, 49.0);
168+
LOG(INFO) << "********** Expected < 49 ************" ;
151169
// Check that it works in int mode too.
152170
TestIntMode(kTrainerIterations);
153171
}
@@ -159,6 +177,7 @@ TEST_F(LSTMTrainerTest, EncodedSoftmaxTest) {
159177
SetupTrainerEng("[1,1,0,32 LE96]", "Lstm-+-softmax", false, true);
160178
double lstm_sm_err = TrainIterations(kTrainerIterations * 2);
161179
EXPECT_LT(lstm_sm_err, 62.0);
180+
LOG(INFO) << "********** Expected < 62 ************" ;
162181
// Check that it works in int mode too.
163182
TestIntMode(kTrainerIterations);
164183
}

unittest/lstm_test.h

+44-32
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1+
// (C) Copyright 2017, Google Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
// Unless required by applicable law or agreed to in writing, software
7+
// distributed under the License is distributed on an "AS IS" BASIS,
8+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
// See the License for the specific language governing permissions and
10+
// limitations under the License.
11+
112
#ifndef TESSERACT_UNITTEST_LSTM_TEST_H_
213
#define TESSERACT_UNITTEST_LSTM_TEST_H_
314

415
#include <memory>
516
#include <string>
617
#include <utility>
718

8-
#include "base/logging.h"
9-
#include "base/stringprintf.h"
10-
#include "file/base/file.h"
11-
#include "file/base/helpers.h"
12-
#include "file/base/path.h"
13-
#include "testing/base/public/googletest.h"
14-
#include "testing/base/public/gunit.h"
19+
#include "include_gunit.h"
20+
1521
#include "absl/strings/str_cat.h"
16-
#include "tesseract/ccutil/unicharset.h"
17-
#include "tesseract/lstm/functions.h"
18-
#include "tesseract/lstm/lstmtrainer.h"
19-
#include "tesseract/training/lang_model_helpers.h"
22+
#include "tprintf.h"
23+
#include "helpers.h"
24+
25+
#include "functions.h"
26+
#include "lang_model_helpers.h"
27+
#include "log.h" // for LOG
28+
#include "lstmtrainer.h"
29+
#include "unicharset.h"
2030

2131
namespace tesseract {
2232

@@ -36,32 +46,32 @@ const int kBatchIterations = 1;
3646
class LSTMTrainerTest : public testing::Test {
3747
protected:
3848
LSTMTrainerTest() {}
39-
string TestDataNameToPath(const string& name) {
40-
return file::JoinPath(FLAGS_test_srcdir,
41-
"tesseract/testdata/" + name);
49+
std::string TestDataNameToPath(const std::string& name) {
50+
return file::JoinPath(TESTDATA_DIR,
51+
"" + name);
4252
}
4353

44-
void SetupTrainerEng(const string& network_spec, const string& model_name,
54+
void SetupTrainerEng(const std::string& network_spec, const std::string& model_name,
4555
bool recode, bool adam) {
46-
SetupTrainer(network_spec, model_name, "eng.unicharset",
47-
"lstm_training.arial.lstmf", recode, adam, 5e-4, false);
56+
SetupTrainer(network_spec, model_name, "eng/eng.unicharset",
57+
"eng.Arial.exp0.lstmf", recode, adam, 5e-4, false);
4858
}
49-
void SetupTrainer(const string& network_spec, const string& model_name,
50-
const string& unicharset_file, const string& lstmf_file,
59+
void SetupTrainer(const std::string& network_spec, const std::string& model_name,
60+
const std::string& unicharset_file, const std::string& lstmf_file,
5161
bool recode, bool adam, double learning_rate,
5262
bool layer_specific) {
5363
constexpr char kLang[] = "eng"; // Exact value doesn't matter.
54-
string unicharset_name = TestDataNameToPath(unicharset_file);
64+
std::string unicharset_name = TestDataNameToPath(unicharset_file);
5565
UNICHARSET unicharset;
5666
ASSERT_TRUE(unicharset.load_from_file(unicharset_name.c_str(), false));
57-
string script_dir = file::JoinPath(
58-
FLAGS_test_srcdir, "tesseract/training/langdata");
67+
std::string script_dir = file::JoinPath(
68+
LANGDATA_DIR, "");
5969
GenericVector<STRING> words;
6070
EXPECT_EQ(0, CombineLangModel(unicharset, script_dir, "", FLAGS_test_tmpdir,
6171
kLang, !recode, words, words, words, false,
6272
nullptr, nullptr));
63-
string model_path = file::JoinPath(FLAGS_test_tmpdir, model_name);
64-
string checkpoint_path = model_path + "_checkpoint";
73+
std::string model_path = file::JoinPath(FLAGS_test_tmpdir, model_name);
74+
std::string checkpoint_path = model_path + "_checkpoint";
6575
trainer_.reset(new LSTMTrainer(nullptr, nullptr, nullptr, nullptr,
6676
model_path.c_str(), checkpoint_path.c_str(),
6777
0, 0));
@@ -98,10 +108,11 @@ class LSTMTrainerTest : public testing::Test {
98108
iteration = trainer_->training_iteration();
99109
mean_error *= 100.0 / kBatchIterations;
100110
LOG(INFO) << log_str.string();
101-
LOG(INFO) << "Batch error = " << mean_error;
111+
LOG(INFO) << "Best error = " << best_error;
112+
LOG(INFO) << "Mean error = " << mean_error;
102113
if (mean_error < best_error) best_error = mean_error;
103114
} while (iteration < iteration_limit);
104-
LOG(INFO) << "Trainer error rate = " << best_error;
115+
LOG(INFO) << "Trainer error rate = " << best_error << "\n";
105116
return best_error;
106117
}
107118
// Tests for a given number of iterations and returns the char error rate.
@@ -122,7 +133,7 @@ class LSTMTrainerTest : public testing::Test {
122133
trainer_->SetIteration(++iteration);
123134
}
124135
mean_error *= 100.0 / max_iterations;
125-
LOG(INFO) << "Tester error rate = " << mean_error;
136+
LOG(INFO) << "Tester error rate = " << mean_error << "\n" ;
126137
return mean_error;
127138
}
128139
// Tests that the current trainer_ can be converted to int mode and still gets
@@ -144,18 +155,19 @@ class LSTMTrainerTest : public testing::Test {
144155
// Sets up a trainer with the given language and given recode+ctc condition.
145156
// It then verifies that the given str encodes and decodes back to the same
146157
// string.
147-
void TestEncodeDecode(const string& lang, const string& str, bool recode) {
148-
string unicharset_name = lang + ".unicharset";
158+
void TestEncodeDecode(const std::string& lang, const std::string& str, bool recode) {
159+
std::string unicharset_name = lang + "/" + lang + ".unicharset";
160+
std::string lstmf_name = lang + ".Arial_Unicode_MS.exp0.lstmf";
149161
SetupTrainer("[1,1,0,32 Lbx100 O1c1]", "bidi-lstm", unicharset_name,
150-
"arialuni.kor.lstmf", recode, true, 5e-4, true);
162+
lstmf_name, recode, true, 5e-4, true);
151163
GenericVector<int> labels;
152164
EXPECT_TRUE(trainer_->EncodeString(str.c_str(), &labels));
153165
STRING decoded = trainer_->DecodeLabels(labels);
154-
string decoded_str(&decoded[0], decoded.length());
166+
std::string decoded_str(&decoded[0], decoded.length());
155167
EXPECT_EQ(str, decoded_str);
156168
}
157169
// Calls TestEncodeDeode with both recode on and off.
158-
void TestEncodeDecodeBoth(const string& lang, const string& str) {
170+
void TestEncodeDecodeBoth(const std::string& lang, const std::string& str) {
159171
TestEncodeDecode(lang, str, false);
160172
TestEncodeDecode(lang, str, true);
161173
}

unittest/tmp/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Directory for holding temporary files created during unittests
2+
3+
Clear it before running `make check`.

0 commit comments

Comments
 (0)