"Fossies" - the Fresh Open Source Software Archive

Member "tesseract-5.2.0/unittest/stridemap_test.cc" (6 Jul 2022, 7276 Bytes) of package /linux/misc/tesseract-5.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stridemap_test.cc" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 4.1.3_vs_5.0.0.

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