Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 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_SCALE_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_SCALE_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 34 | #include "tests/validation/reference/Scale.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace validation |
| 41 | { |
| 42 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 43 | class ScaleValidationFixture : public framework::Fixture |
| 44 | { |
| 45 | public: |
| 46 | template <typename...> |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 47 | void setup(TensorShape shape, DataType data_type, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 48 | { |
Gian Marco Iodice | b11e269 | 2017-09-25 11:31:42 +0100 | [diff] [blame] | 49 | constexpr float max_width = 8192.0f; |
| 50 | constexpr float max_height = 6384.0f; |
| 51 | |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 52 | _shape = shape; |
| 53 | _policy = policy; |
| 54 | _border_mode = border_mode; |
| 55 | _sampling_policy = sampling_policy; |
| 56 | _data_type = data_type; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 57 | |
Gian Marco Iodice | b11e269 | 2017-09-25 11:31:42 +0100 | [diff] [blame] | 58 | std::mt19937 generator(library->seed()); |
| 59 | std::uniform_real_distribution<float> distribution_float(0.25, 3); |
| 60 | float scale_x = distribution_float(generator); |
| 61 | float scale_y = distribution_float(generator); |
| 62 | |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 63 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 64 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 65 | |
| 66 | scale_x = ((shape[idx_width] * scale_x) > max_width) ? (max_width / shape[idx_width]) : scale_x; |
| 67 | scale_y = ((shape[idx_height] * scale_y) > max_height) ? (max_height / shape[idx_height]) : scale_y; |
Gian Marco Iodice | b11e269 | 2017-09-25 11:31:42 +0100 | [diff] [blame] | 68 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 69 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 70 | T constant_border_value = static_cast<T>(distribution_u8(generator)); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 71 | |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 72 | _target = compute_target(shape, data_layout, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy); |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 73 | _reference = compute_reference(shape, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | protected: |
| 77 | template <typename U> |
| 78 | void fill(U &&tensor) |
| 79 | { |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 80 | if(is_data_type_float(_data_type)) |
| 81 | { |
| 82 | library->fill_tensor_uniform(tensor, 0); |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | // Restrict range for float to avoid any floating point issues |
| 87 | std::uniform_real_distribution<> distribution(-5.0f, 5.0f); |
| 88 | library->fill(tensor, distribution, 0); |
| 89 | } |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 92 | TensorType compute_target(TensorShape shape, DataLayout data_layout, const float scale_x, const float scale_y, |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 93 | InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 94 | { |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 95 | // Change shape in case of NHWC. |
| 96 | if(data_layout == DataLayout::NHWC) |
| 97 | { |
| 98 | permute(shape, PermutationVector(2U, 0U, 1U)); |
| 99 | } |
| 100 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 101 | // Create tensors |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 102 | TensorType src = create_tensor<TensorType>(shape, _data_type, 1, 0, QuantizationInfo(), data_layout); |
| 103 | |
| 104 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 105 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 106 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 107 | TensorShape shape_scaled(shape); |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 108 | shape_scaled.set(idx_width, shape[idx_width] * scale_x); |
| 109 | shape_scaled.set(idx_height, shape[idx_height] * scale_y); |
| 110 | TensorType dst = create_tensor<TensorType>(shape_scaled, _data_type, 1, 0, QuantizationInfo(), data_layout); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 111 | |
| 112 | // Create and configure function |
| 113 | FunctionType scale; |
| 114 | |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 115 | scale.configure(&src, &dst, policy, border_mode, constant_border_value, sampling_policy); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 116 | |
| 117 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 118 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 119 | |
| 120 | // Allocate tensors |
| 121 | src.allocator()->allocate(); |
| 122 | dst.allocator()->allocate(); |
| 123 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 124 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 125 | |
| 126 | // Fill tensors |
| 127 | fill(AccessorType(src)); |
| 128 | |
| 129 | // Compute function |
| 130 | scale.run(); |
| 131 | |
| 132 | return dst; |
| 133 | } |
| 134 | |
| 135 | SimpleTensor<T> compute_reference(const TensorShape &shape, const float scale_x, const float scale_y, |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 136 | InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 137 | { |
| 138 | // Create reference |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 139 | SimpleTensor<T> src{ shape, _data_type, 1, 0, QuantizationInfo() }; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 140 | |
| 141 | // Fill reference |
| 142 | fill(src); |
| 143 | |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 144 | return reference::scale<T>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TensorType _target{}; |
| 148 | SimpleTensor<T> _reference{}; |
| 149 | TensorShape _shape{}; |
| 150 | InterpolationPolicy _policy{}; |
| 151 | BorderMode _border_mode{}; |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 152 | SamplingPolicy _sampling_policy{}; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 153 | DataType _data_type{}; |
| 154 | }; |
| 155 | } // namespace validation |
| 156 | } // namespace test |
| 157 | } // namespace arm_compute |
steniu01 | f81652d | 2017-09-11 15:29:12 +0100 | [diff] [blame] | 158 | #endif /* ARM_COMPUTE_TEST_SCALE_FIXTURE */ |