tesseract  5.0.0-alpha-619-ge9db
stridemap_test.cc
Go to the documentation of this file.
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 
12 #include <tensorflow/compiler/xla/array2d.h> // for xla::Array2D
13 #include "include_gunit.h"
14 #include "stridemap.h"
15 
21 
22 namespace {
23 
24 class StridemapTest : public ::testing::Test {
25  protected:
26  void SetUp() {
27  std::locale::global(std::locale(""));
28  }
29 
30  // Sets up an Array2d object of the given size, initialized to increasing
31  // values starting with start.
32  std::unique_ptr<xla::Array2D<int>> SetupArray(int ysize, int xsize, int start) {
33  std::unique_ptr<xla::Array2D<int>> a(new xla::Array2D<int>(ysize, xsize));
34  int value = start;
35  for (int y = 0; y < ysize; ++y) {
36  for (int x = 0; x < xsize; ++x) {
37  (*a)(y, x) = value++;
38  }
39  }
40  return a;
41  }
42 };
43 
44 TEST_F(StridemapTest, Indexing) {
45  // This test verifies that with a batch of arrays of different sizes, the
46  // iteration index each of them in turn, without going out of bounds.
47  std::vector<std::unique_ptr<xla::Array2D<int>>> arrays;
48  arrays.push_back(SetupArray(3, 4, 0));
49  arrays.push_back(SetupArray(4, 5, 12));
50  arrays.push_back(SetupArray(4, 4, 32));
51  arrays.push_back(SetupArray(3, 5, 48));
52  std::vector<std::pair<int, int>> h_w_sizes;
53  for (size_t i = 0; i < arrays.size(); ++i) {
54  h_w_sizes.emplace_back(arrays[i].get()->height(), arrays[i].get()->width());
55  }
56  StrideMap stride_map;
57  stride_map.SetStride(h_w_sizes);
58  StrideMap::Index index(stride_map);
59  int pos = 0;
60  do {
61  EXPECT_GE(index.t(), pos);
62  EXPECT_EQ((*arrays.at(index.index(FD_BATCH)))(index.index(FD_HEIGHT),
63  index.index(FD_WIDTH)),
64  pos);
65  EXPECT_EQ(index.IsLast(FD_BATCH),
66  index.index(FD_BATCH) == arrays.size() - 1);
67  EXPECT_EQ(
68  index.IsLast(FD_HEIGHT),
69  index.index(FD_HEIGHT) == arrays[index.index(FD_BATCH)]->height() - 1);
70  EXPECT_EQ(
71  index.IsLast(FD_WIDTH),
72  index.index(FD_WIDTH) == arrays[index.index(FD_BATCH)]->width() - 1);
73  EXPECT_TRUE(index.IsValid());
74  ++pos;
75  } while (index.Increment());
76  LOG(INFO) << "pos=" << pos;
77  index.InitToLast();
78  do {
79  --pos;
80  EXPECT_GE(index.t(), pos);
81  EXPECT_EQ((*arrays.at(index.index(FD_BATCH)))(index.index(FD_HEIGHT),
82  index.index(FD_WIDTH)),
83  pos);
84  StrideMap::Index copy(index);
85  // Since a change in batch index changes the height and width, it isn't
86  // necessarily true that the position is still valid, even when changing
87  // to another valid batch index.
88  if (index.IsLast(FD_BATCH)) {
89  EXPECT_FALSE(copy.AddOffset(1, FD_BATCH));
90  }
91  copy = index;
92  EXPECT_EQ(index.IsLast(FD_HEIGHT), !copy.AddOffset(1, FD_HEIGHT));
93  copy = index;
94  EXPECT_EQ(index.IsLast(FD_WIDTH), !copy.AddOffset(1, FD_WIDTH));
95  copy = index;
96  if (index.index(FD_BATCH) == 0) {
97  EXPECT_FALSE(copy.AddOffset(-1, FD_BATCH));
98  }
99  copy = index;
100  EXPECT_EQ(index.index(FD_HEIGHT) == 0, !copy.AddOffset(-1, FD_HEIGHT));
101  copy = index;
102  EXPECT_EQ(index.index(FD_WIDTH) == 0, !copy.AddOffset(-1, FD_WIDTH));
103  copy = index;
104  EXPECT_FALSE(copy.AddOffset(10, FD_WIDTH));
105  copy = index;
106  EXPECT_FALSE(copy.AddOffset(-10, FD_HEIGHT));
107  EXPECT_TRUE(index.IsValid());
108  } while (index.Decrement());
109 }
110 
111 TEST_F(StridemapTest, Scaling) {
112  // This test verifies that with a batch of arrays of different sizes, the
113  // scaling/reduction functions work as expected.
114  std::vector<std::unique_ptr<xla::Array2D<int>>> arrays;
115  arrays.push_back(SetupArray(3, 4, 0)); // 0-11
116  arrays.push_back(SetupArray(4, 5, 12)); // 12-31
117  arrays.push_back(SetupArray(4, 4, 32)); // 32-47
118  arrays.push_back(SetupArray(3, 5, 48)); // 48-62
119  std::vector<std::pair<int, int>> h_w_sizes;
120  for (size_t i = 0; i < arrays.size(); ++i) {
121  h_w_sizes.emplace_back(arrays[i].get()->height(), arrays[i].get()->width());
122  }
123  StrideMap stride_map;
124  stride_map.SetStride(h_w_sizes);
125 
126  // Scale x by 2, keeping y the same.
127  std::vector<int> values_x2 = {0, 1, 4, 5, 8, 9, 12, 13, 17, 18,
128  22, 23, 27, 28, 32, 33, 36, 37, 40, 41,
129  44, 45, 48, 49, 53, 54, 58, 59};
130  StrideMap test_map(stride_map);
131  test_map.ScaleXY(2, 1);
132  StrideMap::Index index(test_map);
133  int pos = 0;
134  do {
135  int expected_value = values_x2[pos++];
136  EXPECT_EQ((*arrays.at(index.index(FD_BATCH)))(index.index(FD_HEIGHT),
137  index.index(FD_WIDTH)),
138  expected_value);
139  } while (index.Increment());
140  EXPECT_EQ(pos, values_x2.size());
141 
142  test_map = stride_map;
143  // Scale y by 2, keeping x the same.
144  std::vector<int> values_y2 = {0, 1, 2, 3, 12, 13, 14, 15, 16,
145  17, 18, 19, 20, 21, 32, 33, 34, 35,
146  36, 37, 38, 39, 48, 49, 50, 51, 52};
147  test_map.ScaleXY(1, 2);
148  index.InitToFirst();
149  pos = 0;
150  do {
151  int expected_value = values_y2[pos++];
152  EXPECT_EQ((*arrays.at(index.index(FD_BATCH)))(index.index(FD_HEIGHT),
153  index.index(FD_WIDTH)),
154  expected_value);
155  } while (index.Increment());
156  EXPECT_EQ(pos, values_y2.size());
157 
158  test_map = stride_map;
159  // Scale x and y by 2.
160  std::vector<int> values_xy2 = {0, 1, 12, 13, 17, 18, 32, 33, 36, 37, 48, 49};
161  test_map.ScaleXY(2, 2);
162  index.InitToFirst();
163  pos = 0;
164  do {
165  int expected_value = values_xy2[pos++];
166  EXPECT_EQ((*arrays.at(index.index(FD_BATCH)))(index.index(FD_HEIGHT),
167  index.index(FD_WIDTH)),
168  expected_value);
169  } while (index.Increment());
170  EXPECT_EQ(pos, values_xy2.size());
171 
172  test_map = stride_map;
173  // Reduce Width to 1.
174  std::vector<int> values_x_to_1 = {0, 4, 8, 12, 17, 22, 27,
175  32, 36, 40, 44, 48, 53, 58};
176  test_map.ReduceWidthTo1();
177  index.InitToFirst();
178  pos = 0;
179  do {
180  int expected_value = values_x_to_1[pos++];
181  EXPECT_EQ((*arrays.at(index.index(FD_BATCH)))(index.index(FD_HEIGHT),
182  index.index(FD_WIDTH)),
183  expected_value);
184  } while (index.Increment());
185  EXPECT_EQ(pos, values_x_to_1.size());
186 }
187 
188 } // namespace
INFO
Definition: log.h:29
tesseract::StrideMap::ReduceWidthTo1
void ReduceWidthTo1()
Definition: stridemap.cpp:153
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
tesseract::FlexDimensions
FlexDimensions
Definition: stridemap.h:32
tesseract::StrideMap::SetStride
void SetStride(const std::vector< std::pair< int, int >> &h_w_pairs)
Definition: stridemap.cpp:126
tesseract::FD_WIDTH
Definition: stridemap.h:35
tesseract::StrideMap::ScaleXY
void ScaleXY(int x_factor, int y_factor)
Definition: stridemap.cpp:144
stridemap.h
tesseract::StrideMap
Definition: stridemap.h:41
tesseract::FD_HEIGHT
Definition: stridemap.h:34
LOG
Definition: cleanapi_test.cc:19
tesseract::FD_BATCH
Definition: stridemap.h:33