unittest: Fix and enable stridemap_test

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2019-06-30 15:26:28 +02:00
parent 4e576f844c
commit b00e53fabf
3 changed files with 38 additions and 10 deletions

View File

@ -149,8 +149,10 @@ check_PROGRAMS += rect_test
check_PROGRAMS += resultiterator_test
check_PROGRAMS += scanutils_test
check_PROGRAMS += shapetable_test
# check_PROGRAMS += stridemap_test
check_PROGRAMS += stats_test
if TENSORFLOW
check_PROGRAMS += stridemap_test
endif
check_PROGRAMS += stringrenderer_test
check_PROGRAMS += tablefind_test
check_PROGRAMS += tablerecog_test
@ -345,6 +347,11 @@ shapetable_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TESS_LIBS)
stats_test_SOURCES = stats_test.cc
stats_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
if TENSORFLOW
stridemap_test_SOURCES = stridemap_test.cc
stridemap_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
endif
stringrenderer_test_SOURCES = stringrenderer_test.cc
stringrenderer_test_LDADD = $(ABSEIL_LIBS) $(GTEST_LIBS) $(TRAINING_LIBS) $(LEPTONICA_LIBS) $(ICU_I18N_LIBS) $(ICU_UC_LIBS) -lfontconfig -lpangocairo-1.0 -lpangoft2-1.0 $(cairo_LIBS) $(pango_LIBS)

View File

@ -51,11 +51,15 @@ public:
};
#define ARRAYSIZE(arr) (sizeof(arr) / sizeof(arr[0]))
// /usr/include/tensorflow/core/platform/default/logging.h defines the CHECK* macros.
#if !defined(CHECK)
#define CHECK(test) ASSERT_HOST(test)
#define CHECK_EQ(test, value) CHECK((test) == (value))
#define CHECK_GT(test, value) CHECK((test) > (value))
#define CHECK_LT(test, value) CHECK((test) < (value))
#define CHECK_LE(test, value) CHECK((test) <= (value))
#define CHECK_OK(test) CHECK(test)
#endif
#endif // TESSERACT_UNITTEST_INCLUDE_GUNIT_H_

View File

@ -1,4 +1,17 @@
#include "tesseract/lstm/stridemap.h"
// (C) Copyright 2017, 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 <tensorflow/compiler/xla/array2d.h> // for xla::Array2D
#include "include_gunit.h"
#include "stridemap.h"
using tesseract::FD_BATCH;
using tesseract::FD_HEIGHT;
@ -16,8 +29,8 @@ class StridemapTest : public ::testing::Test {
// Sets up an Array2d object of the given size, initialized to increasing
// values starting with start.
std::unique_ptr<Array2D<int>> SetupArray(int ysize, int xsize, int start) {
std::unique_ptr<Array2D<int>> a(new Array2D<int>(ysize, xsize));
std::unique_ptr<xla::Array2D<int>> SetupArray(int ysize, int xsize, int start) {
std::unique_ptr<xla::Array2D<int>> a(new xla::Array2D<int>(ysize, xsize));
int value = start;
for (int y = 0; y < ysize; ++y) {
for (int x = 0; x < xsize; ++x) {
@ -31,13 +44,13 @@ class StridemapTest : public ::testing::Test {
TEST_F(StridemapTest, Indexing) {
// This test verifies that with a batch of arrays of different sizes, the
// iteration index each of them in turn, without going out of bounds.
std::vector<std::unique_ptr<Array2D<int>>> arrays;
std::vector<std::unique_ptr<xla::Array2D<int>>> arrays;
arrays.push_back(SetupArray(3, 4, 0));
arrays.push_back(SetupArray(4, 5, 12));
arrays.push_back(SetupArray(4, 4, 32));
arrays.push_back(SetupArray(3, 5, 48));
std::vector<std::pair<int, int>> h_w_sizes;
for (int i = 0; i < arrays.size(); ++i) {
for (size_t i = 0; i < arrays.size(); ++i) {
h_w_sizes.emplace_back(arrays[i].get()->height(), arrays[i].get()->width());
}
StrideMap stride_map;
@ -72,13 +85,17 @@ TEST_F(StridemapTest, Indexing) {
// Since a change in batch index changes the height and width, it isn't
// necessarily true that the position is still valid, even when changing
// to another valid batch index.
if (index.IsLast(FD_BATCH)) EXPECT_FALSE(copy.AddOffset(1, FD_BATCH));
if (index.IsLast(FD_BATCH)) {
EXPECT_FALSE(copy.AddOffset(1, FD_BATCH));
}
copy = index;
EXPECT_EQ(index.IsLast(FD_HEIGHT), !copy.AddOffset(1, FD_HEIGHT));
copy = index;
EXPECT_EQ(index.IsLast(FD_WIDTH), !copy.AddOffset(1, FD_WIDTH));
copy = index;
if (index.index(FD_BATCH) == 0) EXPECT_FALSE(copy.AddOffset(-1, FD_BATCH));
if (index.index(FD_BATCH) == 0) {
EXPECT_FALSE(copy.AddOffset(-1, FD_BATCH));
}
copy = index;
EXPECT_EQ(index.index(FD_HEIGHT) == 0, !copy.AddOffset(-1, FD_HEIGHT));
copy = index;
@ -94,13 +111,13 @@ TEST_F(StridemapTest, Indexing) {
TEST_F(StridemapTest, Scaling) {
// This test verifies that with a batch of arrays of different sizes, the
// scaling/reduction functions work as expected.
std::vector<std::unique_ptr<Array2D<int>>> arrays;
std::vector<std::unique_ptr<xla::Array2D<int>>> arrays;
arrays.push_back(SetupArray(3, 4, 0)); // 0-11
arrays.push_back(SetupArray(4, 5, 12)); // 12-31
arrays.push_back(SetupArray(4, 4, 32)); // 32-47
arrays.push_back(SetupArray(3, 5, 48)); // 48-62
std::vector<std::pair<int, int>> h_w_sizes;
for (int i = 0; i < arrays.size(); ++i) {
for (size_t i = 0; i < arrays.size(); ++i) {
h_w_sizes.emplace_back(arrays[i].get()->height(), arrays[i].get()->width());
}
StrideMap stride_map;