1 /* 2 * Copyright (c) 2017-2019 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 #ifndef ARM_COMPUTE_TEST_WARP_PERSPECTIVE_FIXTURE 25 #define ARM_COMPUTE_TEST_WARP_PERSPECTIVE_FIXTURE 26 27 #include "arm_compute/core/TensorShape.h" 28 #include "arm_compute/core/Types.h" 29 #include "tests/AssetsLibrary.h" 30 #include "tests/Globals.h" 31 #include "tests/IAccessor.h" 32 #include "tests/framework/Asserts.h" 33 #include "tests/framework/Fixture.h" 34 #include "tests/validation/reference/Utils.h" 35 #include "tests/validation/reference/WarpPerspective.h" 36 37 #include <random> 38 39 namespace arm_compute 40 { 41 namespace test 42 { 43 namespace validation 44 { 45 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 46 class WarpPerspectiveValidationFixture : public framework::Fixture 47 { 48 public: 49 template <typename...> setup(TensorShape input_shape,DataType data_type,InterpolationPolicy policy,BorderMode border_mode)50 void setup(TensorShape input_shape, DataType data_type, InterpolationPolicy policy, BorderMode border_mode) 51 { 52 uint8_t constant_border_value = 0; 53 // Generate a random constant value if border_mode is constant 54 if(border_mode == BorderMode::CONSTANT) 55 { 56 std::mt19937 gen(library->seed()); 57 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); 58 constant_border_value = distribution_u8(gen); 59 } 60 61 // Create the matrix 62 std::array<float, 9> matrix = { { 0 } }; 63 fill_warp_matrix<9>(matrix); 64 65 _target = compute_target(input_shape, matrix, policy, border_mode, constant_border_value, data_type); 66 _reference = compute_reference(input_shape, matrix, policy, border_mode, constant_border_value, data_type); 67 } 68 69 protected: 70 template <typename U> fill(U && tensor)71 void fill(U &&tensor) 72 { 73 library->fill_tensor_uniform(tensor, 0); 74 } 75 compute_target(const TensorShape & shape,const std::array<float,9> & matrix,InterpolationPolicy policy,BorderMode border_mode,uint8_t constant_border_value,DataType data_type)76 TensorType compute_target(const TensorShape &shape, const std::array<float, 9> &matrix, InterpolationPolicy policy, BorderMode border_mode, 77 uint8_t constant_border_value, 78 DataType data_type) 79 { 80 // Create tensors 81 TensorType src = create_tensor<TensorType>(shape, data_type); 82 TensorType dst = create_tensor<TensorType>(shape, data_type); 83 84 // Create and configure function 85 FunctionType warp_perspective; 86 warp_perspective.configure(&src, &dst, matrix, policy, border_mode, constant_border_value); 87 88 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); 89 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); 90 91 // Allocate tensors 92 src.allocator()->allocate(); 93 dst.allocator()->allocate(); 94 95 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); 96 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); 97 98 // Fill tensors 99 fill(AccessorType(src)); 100 101 // Compute function 102 warp_perspective.run(); 103 104 return dst; 105 } 106 compute_reference(const TensorShape & shape,const std::array<float,9> & matrix,InterpolationPolicy policy,BorderMode border_mode,uint8_t constant_border_value,DataType data_type)107 SimpleTensor<T> compute_reference(const TensorShape &shape, const std::array<float, 9> &matrix, InterpolationPolicy policy, BorderMode border_mode, 108 uint8_t constant_border_value, 109 DataType data_type) 110 { 111 ARM_COMPUTE_ERROR_ON(data_type != DataType::U8); 112 113 // Create reference 114 SimpleTensor<T> src{ shape, data_type }; 115 116 // Create the valid mask Tensor 117 _valid_mask = SimpleTensor<T>(shape, data_type); 118 119 // Fill reference 120 fill(src); 121 122 // Compute reference 123 return reference::warp_perspective<T>(src, _valid_mask, matrix.data(), policy, border_mode, constant_border_value); 124 } 125 126 TensorType _target{}; 127 SimpleTensor<T> _reference{}; 128 BorderMode _border_mode{}; 129 SimpleTensor<T> _valid_mask{}; 130 }; 131 } // namespace validation 132 } // namespace test 133 } // namespace arm_compute 134 #endif /* ARM_COMPUTE_TEST_WARP_PERSPECTIVE_FIXTURE */ 135