Isabella Gottardi | 6203153 | 2017-07-04 11:21:28 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "AssetsLibrary.h" |
| 25 | #include "CL/CLAccessor.h" |
| 26 | #include "Globals.h" |
| 27 | #include "PaddingCalculator.h" |
| 28 | #include "TypePrinter.h" |
| 29 | #include "Utils.h" |
| 30 | #include "validation/Datasets.h" |
| 31 | #include "validation/Helpers.h" |
| 32 | #include "validation/Reference.h" |
| 33 | #include "validation/Validation.h" |
| 34 | #include "validation/ValidationUserConfiguration.h" |
| 35 | |
| 36 | #include "arm_compute/core/Helpers.h" |
| 37 | #include "arm_compute/core/Types.h" |
| 38 | #include "arm_compute/runtime/CL/functions/CLWarpPerspective.h" |
| 39 | #include "arm_compute/runtime/Tensor.h" |
| 40 | #include "arm_compute/runtime/TensorAllocator.h" |
| 41 | |
| 42 | #include "boost_wrapper.h" |
| 43 | |
| 44 | #include <random> |
| 45 | #include <string> |
| 46 | |
| 47 | using namespace arm_compute; |
| 48 | using namespace arm_compute::test; |
| 49 | using namespace arm_compute::test::validation; |
| 50 | |
| 51 | namespace |
| 52 | { |
| 53 | /** Compute Warp Perspective function. |
| 54 | * |
| 55 | * @param[in] input Shape of the input and output tensors. |
| 56 | * @param[in] matrix The perspective matrix. Must be 3x3 of type float. |
| 57 | * @param[in] policy The interpolation type. |
| 58 | * @param[in] border_mode Strategy to use for borders. |
| 59 | * @param[in] constant_border_value Constant value to use for borders if border_mode is set to CONSTANT. |
| 60 | * |
| 61 | * @return Computed output tensor. |
| 62 | */ |
| 63 | CLTensor compute_warp_perspective(const TensorShape &shape, const float *matrix, InterpolationPolicy policy, |
| 64 | BorderMode border_mode, uint8_t constant_border_value) |
| 65 | { |
| 66 | // Create tensors |
| 67 | CLTensor src = create_tensor<CLTensor>(shape, DataType::U8); |
| 68 | CLTensor dst = create_tensor<CLTensor>(shape, DataType::U8); |
| 69 | |
| 70 | // Create and configure function |
| 71 | CLWarpPerspective warp_perspective; |
| 72 | warp_perspective.configure(&src, &dst, matrix, policy, border_mode, constant_border_value); |
| 73 | |
| 74 | // Allocate tensors |
| 75 | src.allocator()->allocate(); |
| 76 | dst.allocator()->allocate(); |
| 77 | |
| 78 | BOOST_TEST(!src.info()->is_resizable()); |
| 79 | BOOST_TEST(!dst.info()->is_resizable()); |
| 80 | |
| 81 | // Fill tensors |
| 82 | library->fill_tensor_uniform(CLAccessor(src), 0); |
| 83 | |
| 84 | // Compute function |
| 85 | warp_perspective.run(); |
| 86 | |
| 87 | return dst; |
| 88 | } |
| 89 | } // namespace |
| 90 | |
| 91 | #ifndef DOXYGEN_SKIP_THIS |
| 92 | BOOST_AUTO_TEST_SUITE(CL) |
| 93 | BOOST_AUTO_TEST_SUITE(WarpPerspective) |
| 94 | |
| 95 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) |
| 96 | BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) |
| 97 | * boost::unit_test::data::make({ InterpolationPolicy::BILINEAR, InterpolationPolicy::NEAREST_NEIGHBOR }) * BorderModes(), |
| 98 | shape, policy, border_mode) |
| 99 | { |
| 100 | uint8_t constant_border_value = 0; |
| 101 | |
| 102 | // Generate a random constant value if border_mode is constant |
| 103 | if(border_mode == BorderMode::CONSTANT) |
| 104 | { |
| 105 | std::mt19937 gen(user_config.seed.get()); |
| 106 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
| 107 | constant_border_value = distribution_u8(gen); |
| 108 | } |
| 109 | |
| 110 | std::array<float, 9> matrix; |
| 111 | fill_warp_matrix<9>(matrix, 3, 3); |
| 112 | |
| 113 | // Create tensors |
| 114 | CLTensor src = create_tensor<CLTensor>(shape, DataType::U8); |
| 115 | CLTensor dst = create_tensor<CLTensor>(shape, DataType::U8); |
| 116 | |
| 117 | BOOST_TEST(src.info()->is_resizable()); |
| 118 | BOOST_TEST(dst.info()->is_resizable()); |
| 119 | |
| 120 | // Create and configure function |
| 121 | CLWarpPerspective warp_perspective; |
| 122 | warp_perspective.configure(&src, &dst, matrix.data(), policy, border_mode, constant_border_value); |
| 123 | |
| 124 | // Validate valid region |
| 125 | const ValidRegion valid_region = shape_to_valid_region(shape); |
| 126 | |
| 127 | validate(src.info()->valid_region(), valid_region); |
| 128 | validate(dst.info()->valid_region(), valid_region); |
| 129 | |
| 130 | // Validate padding |
| 131 | PaddingCalculator calculator(shape.x(), 4); |
| 132 | calculator.set_border_mode(border_mode); |
| 133 | |
| 134 | const PaddingSize read_padding(1); |
| 135 | const PaddingSize write_padding = calculator.required_padding(PaddingCalculator::Option::EXCLUDE_BORDER); |
| 136 | |
| 137 | validate(src.info()->padding(), read_padding); |
| 138 | validate(dst.info()->padding(), write_padding); |
| 139 | } |
| 140 | |
| 141 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 142 | BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() |
| 143 | * boost::unit_test::data::make({ InterpolationPolicy::BILINEAR, InterpolationPolicy::NEAREST_NEIGHBOR }) |
| 144 | * BorderModes(), |
| 145 | shape, policy, border_mode) |
| 146 | { |
| 147 | uint8_t constant_border_value = 0; |
| 148 | |
| 149 | // Generate a random constant value if border_mode is constant |
| 150 | if(border_mode == BorderMode::CONSTANT) |
| 151 | { |
| 152 | std::mt19937 gen(user_config.seed.get()); |
| 153 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
| 154 | constant_border_value = distribution_u8(gen); |
| 155 | } |
| 156 | |
| 157 | // Create the valid mask Tensor |
| 158 | RawTensor valid_mask(shape, DataType::U8); |
| 159 | |
| 160 | // Create the matrix |
| 161 | std::array<float, 9> matrix; |
| 162 | fill_warp_matrix<9>(matrix, 3, 3); |
| 163 | |
| 164 | // Compute function |
| 165 | CLTensor dst = compute_warp_perspective(shape, matrix.data(), policy, border_mode, constant_border_value); |
| 166 | |
| 167 | // Compute reference |
| 168 | RawTensor ref_dst = Reference::compute_reference_warp_perspective(shape, valid_mask, matrix.data(), policy, border_mode, constant_border_value); |
| 169 | |
| 170 | // Validate output |
| 171 | validate(CLAccessor(dst), ref_dst, valid_mask, 1, 0.2f); |
| 172 | } |
| 173 | |
| 174 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
| 175 | BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() |
| 176 | * boost::unit_test::data::make({ InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR }) * BorderModes(), |
| 177 | shape, policy, border_mode) |
| 178 | { |
| 179 | uint8_t constant_border_value = 0; |
| 180 | |
| 181 | // Generate a random constant value if border_mode is constant |
| 182 | if(border_mode == BorderMode::CONSTANT) |
| 183 | { |
| 184 | std::mt19937 gen(user_config.seed.get()); |
| 185 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
| 186 | constant_border_value = distribution_u8(gen); |
| 187 | } |
| 188 | |
| 189 | // Create the valid mask Tensor |
| 190 | RawTensor valid_mask(shape, DataType::U8); |
| 191 | |
| 192 | // Create the matrix |
| 193 | std::array<float, 9> matrix; |
| 194 | fill_warp_matrix<9>(matrix, 3, 3); |
| 195 | |
| 196 | // Compute function |
| 197 | CLTensor dst = compute_warp_perspective(shape, matrix.data(), policy, border_mode, constant_border_value); |
| 198 | |
| 199 | // Compute reference |
| 200 | RawTensor ref_dst = Reference::compute_reference_warp_perspective(shape, valid_mask, matrix.data(), policy, border_mode, constant_border_value); |
| 201 | |
| 202 | // Validate output |
| 203 | validate(CLAccessor(dst), ref_dst, valid_mask, 1, 0.2f); |
| 204 | } |
| 205 | |
| 206 | BOOST_AUTO_TEST_SUITE_END() |
| 207 | BOOST_AUTO_TEST_SUITE_END() |
| 208 | #endif /* DOXYGEN_SKIP_THIS */ |