tesseract  5.0.0-alpha-619-ge9db
networkio_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 "include_gunit.h"
13 #include "networkio.h"
14 #include "stridemap.h"
15 #include <tensorflow/compiler/xla/array2d.h> // for xla::Array2D
16 
23 
24 namespace {
25 
26 class NetworkioTest : public ::testing::Test {
27  protected:
28  void SetUp() override {
29  std::locale::global(std::locale(""));
30  }
31 
32  // Sets up an Array2d object of the given size, initialized to increasing
33  // values starting with start.
34  std::unique_ptr<xla::Array2D<int>> SetupArray(int ysize, int xsize, int start) {
35  std::unique_ptr<xla::Array2D<int>> a(new xla::Array2D<int>(ysize, xsize));
36  int value = start;
37  for (int y = 0; y < ysize; ++y) {
38  for (int x = 0; x < xsize; ++x) {
39  (*a)(y, x) = value++;
40  }
41  }
42  return a;
43  }
44  // Sets up a NetworkIO with a batch of 2 "images" of known values.
45  void SetupNetworkIO(NetworkIO* nio) {
46  std::vector<std::unique_ptr<xla::Array2D<int>>> arrays;
47  arrays.push_back(SetupArray(3, 4, 0));
48  arrays.push_back(SetupArray(4, 5, 12));
49  std::vector<std::pair<int, int>> h_w_sizes;
50  for (size_t i = 0; i < arrays.size(); ++i) {
51  h_w_sizes.emplace_back(arrays[i].get()->height(),
52  arrays[i].get()->width());
53  }
54  StrideMap stride_map;
55  stride_map.SetStride(h_w_sizes);
56  nio->ResizeToMap(true, stride_map, 2);
57  // Iterate over the map, setting nio's contents from the arrays.
58  StrideMap::Index index(stride_map);
59  do {
60  int value = (*arrays[index.index(FD_BATCH)])(index.index(FD_HEIGHT),
61  index.index(FD_WIDTH));
62  nio->SetPixel(index.t(), 0, 128 + value, 0.0f, 128.0f);
63  nio->SetPixel(index.t(), 1, 128 - value, 0.0f, 128.0f);
64  } while (index.Increment());
65  }
66 };
67 
68 // Tests that the initialization via SetPixel works and the resize correctly
69 // fills with zero where image sizes don't match.
70 TEST_F(NetworkioTest, InitWithZeroFill) {
71  NetworkIO nio;
72  nio.Resize2d(true, 32, 2);
73  int width = nio.Width();
74  for (int t = 0; t < width; ++t) {
75  nio.SetPixel(t, 0, 0, 0.0f, 128.0f);
76  nio.SetPixel(t, 1, 0, 0.0f, 128.0f);
77  }
78  // The initialization will wipe out all previously set values.
79  SetupNetworkIO(&nio);
80  nio.ZeroInvalidElements();
81  StrideMap::Index index(nio.stride_map());
82  int next_t = 0;
83  int pos = 0;
84  do {
85  int t = index.t();
86  // The indexed values just increase monotonically.
87  int value = nio.i(t)[0];
88  EXPECT_EQ(value, pos);
89  value = nio.i(t)[1];
90  EXPECT_EQ(value, -pos);
91  // When we skip t values, the data is always 0.
92  while (next_t < t) {
93  EXPECT_EQ(nio.i(next_t)[0], 0);
94  EXPECT_EQ(nio.i(next_t)[1], 0);
95  ++next_t;
96  }
97  ++pos;
98  ++next_t;
99  } while (index.Increment());
100  EXPECT_EQ(pos, 32);
101  EXPECT_EQ(next_t, 40);
102 }
103 
104 // Tests that CopyWithYReversal works.
105 TEST_F(NetworkioTest, CopyWithYReversal) {
106  NetworkIO nio;
107  SetupNetworkIO(&nio);
108  NetworkIO copy;
109  copy.CopyWithYReversal(nio);
110  StrideMap::Index index(copy.stride_map());
111  int next_t = 0;
112  int pos = 0;
113  std::vector<int> expected_values = {
114  8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, 27, 28, 29, 30,
115  31, 22, 23, 24, 25, 26, 17, 18, 19, 20, 21, 12, 13, 14, 15, 16};
116  do {
117  int t = index.t();
118  // The indexed values match the expected values.
119  int value = copy.i(t)[0];
120  EXPECT_EQ(value, expected_values[pos]);
121  value = copy.i(t)[1];
122  EXPECT_EQ(value, -expected_values[pos]);
123  // When we skip t values, the data is always 0.
124  while (next_t < t) {
125  EXPECT_EQ(copy.i(next_t)[0], 0) << "Failure t = " << next_t;
126  EXPECT_EQ(copy.i(next_t)[1], 0) << "Failure t = " << next_t;
127  ++next_t;
128  }
129  ++pos;
130  ++next_t;
131  } while (index.Increment());
132  EXPECT_EQ(pos, 32);
133  EXPECT_EQ(next_t, 40);
134 }
135 
136 // Tests that CopyWithXReversal works.
137 TEST_F(NetworkioTest, CopyWithXReversal) {
138  NetworkIO nio;
139  SetupNetworkIO(&nio);
140  NetworkIO copy;
141  copy.CopyWithXReversal(nio);
142  StrideMap::Index index(copy.stride_map());
143  int next_t = 0;
144  int pos = 0;
145  std::vector<int> expected_values = {
146  3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 16, 15, 14, 13,
147  12, 21, 20, 19, 18, 17, 26, 25, 24, 23, 22, 31, 30, 29, 28, 27};
148  do {
149  int t = index.t();
150  // The indexed values match the expected values.
151  int value = copy.i(t)[0];
152  EXPECT_EQ(value, expected_values[pos]);
153  value = copy.i(t)[1];
154  EXPECT_EQ(value, -expected_values[pos]);
155  // When we skip t values, the data is always 0.
156  while (next_t < t) {
157  EXPECT_EQ(copy.i(next_t)[0], 0) << "Failure t = " << next_t;
158  EXPECT_EQ(copy.i(next_t)[1], 0) << "Failure t = " << next_t;
159  ++next_t;
160  }
161  ++pos;
162  ++next_t;
163  } while (index.Increment());
164  EXPECT_EQ(pos, 32);
165  EXPECT_EQ(next_t, 40);
166 }
167 
168 // Tests that CopyWithXYTranspose works.
169 TEST_F(NetworkioTest, CopyWithXYTranspose) {
170  NetworkIO nio;
171  SetupNetworkIO(&nio);
172  NetworkIO copy;
173  copy.CopyWithXYTranspose(nio);
174  StrideMap::Index index(copy.stride_map());
175  int next_t = 0;
176  int pos = 0;
177  std::vector<int> expected_values = {
178  0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11, 12, 17, 22, 27,
179  13, 18, 23, 28, 14, 19, 24, 29, 15, 20, 25, 30, 16, 21, 26, 31};
180  do {
181  int t = index.t();
182  // The indexed values match the expected values.
183  int value = copy.i(t)[0];
184  EXPECT_EQ(value, expected_values[pos]);
185  value = copy.i(t)[1];
186  EXPECT_EQ(value, -expected_values[pos]);
187  // When we skip t values, the data is always 0.
188  while (next_t < t) {
189  EXPECT_EQ(copy.i(next_t)[0], 0);
190  EXPECT_EQ(copy.i(next_t)[1], 0);
191  ++next_t;
192  }
193  ++pos;
194  ++next_t;
195  } while (index.Increment());
196  EXPECT_EQ(pos, 32);
197  EXPECT_EQ(next_t, 40);
198 }
199 
200 } // namespace
tesseract::NetworkIO::i
const int8_t * i(int t) const
Definition: networkio.h:123
tesseract::NetworkIO::ZeroInvalidElements
void ZeroInvalidElements()
Definition: networkio.cpp:88
networkio.h
tesseract::NetworkIO::CopyWithYReversal
void CopyWithYReversal(const NetworkIO &src)
Definition: networkio.cpp:853
tesseract::NetworkIO::Width
int Width() const
Definition: networkio.h:107
tesseract::NetworkIO::stride_map
const StrideMap & stride_map() const
Definition: networkio.h:133
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
tesseract::NetworkIO::CopyWithXYTranspose
void CopyWithXYTranspose(const NetworkIO &src)
Definition: networkio.cpp:891
tesseract::NetworkIO::CopyWithXReversal
void CopyWithXReversal(const NetworkIO &src)
Definition: networkio.cpp:872
tesseract::NetworkIO::Resize2d
void Resize2d(bool int_mode, int width, int num_features)
Definition: networkio.cpp:35
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::NetworkIO::SetPixel
void SetPixel(int t, int f, int pixel, float black, float contrast)
Definition: networkio.cpp:275
tesseract::NetworkIO
Definition: networkio.h:39
stridemap.h
tesseract::NetworkIO::ResizeToMap
void ResizeToMap(bool int_mode, const StrideMap &stride_map, int num_features)
Definition: networkio.cpp:46
tesseract::StrideMap
Definition: stridemap.h:41
tesseract::FD_HEIGHT
Definition: stridemap.h:34
tesseract::FD_BATCH
Definition: stridemap.h:33