giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1 | /* |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
giuros01 | 1887081 | 2018-09-13 09:31:40 +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_ROIALIGNLAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_ROIALIGNLAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 30 | #include "tests/AssetsLibrary.h" |
| 31 | #include "tests/Globals.h" |
| 32 | #include "tests/IAccessor.h" |
| 33 | #include "tests/framework/Asserts.h" |
| 34 | #include "tests/framework/Fixture.h" |
| 35 | #include "tests/validation/Helpers.h" |
| 36 | #include "tests/validation/reference/ROIAlignLayer.h" |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace test |
| 41 | { |
| 42 | namespace validation |
| 43 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 44 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 45 | class ROIAlignLayerGenericFixture : public framework::Fixture |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 46 | { |
| 47 | public: |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 48 | using TRois = typename std::conditional<std::is_same<typename std::decay<T>::type, uint8_t>::value, uint16_t, T>::type; |
| 49 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 50 | template <typename...> |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 51 | void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 52 | { |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 53 | _rois_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::QASYMM16 : data_type; |
| 54 | _target = compute_target(input_shape, data_type, data_layout, pool_info, rois_shape, qinfo, output_qinfo); |
| 55 | _reference = compute_reference(input_shape, data_type, pool_info, rois_shape, qinfo, output_qinfo); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | protected: |
| 59 | template <typename U> |
| 60 | void fill(U &&tensor) |
| 61 | { |
| 62 | library->fill_tensor_uniform(tensor, 0); |
| 63 | } |
| 64 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 65 | template <typename U> |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 66 | void generate_rois(U &&rois, const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, TensorShape rois_shape, DataLayout data_layout = DataLayout::NCHW) |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 67 | { |
| 68 | const size_t values_per_roi = rois_shape.x(); |
| 69 | const size_t num_rois = rois_shape.y(); |
| 70 | |
| 71 | std::mt19937 gen(library->seed()); |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 72 | TRois *rois_ptr = static_cast<TRois *>(rois.data()); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 73 | |
| 74 | const float pool_width = pool_info.pooled_width(); |
| 75 | const float pool_height = pool_info.pooled_height(); |
| 76 | const float roi_scale = pool_info.spatial_scale(); |
| 77 | |
| 78 | // Calculate distribution bounds |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 79 | const auto scaled_width = static_cast<float>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)] / roi_scale) / pool_width); |
| 80 | const auto scaled_height = static_cast<float>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)] / roi_scale) / pool_height); |
| 81 | const auto min_width = static_cast<float>(pool_width / roi_scale); |
| 82 | const auto min_height = static_cast<float>(pool_height / roi_scale); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 83 | |
| 84 | // Create distributions |
| 85 | std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1); |
| 86 | std::uniform_int_distribution<> dist_x1(0, scaled_width); |
| 87 | std::uniform_int_distribution<> dist_y1(0, scaled_height); |
| 88 | std::uniform_int_distribution<> dist_w(min_width, std::max(float(min_width), (pool_width - 2) * scaled_width)); |
| 89 | std::uniform_int_distribution<> dist_h(min_height, std::max(float(min_height), (pool_height - 2) * scaled_height)); |
| 90 | |
| 91 | for(unsigned int pw = 0; pw < num_rois; ++pw) |
| 92 | { |
| 93 | const auto batch_idx = dist_batch(gen); |
| 94 | const auto x1 = dist_x1(gen); |
| 95 | const auto y1 = dist_y1(gen); |
| 96 | const auto x2 = x1 + dist_w(gen); |
| 97 | const auto y2 = y1 + dist_h(gen); |
| 98 | |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 99 | rois_ptr[values_per_roi * pw] = batch_idx; |
| 100 | if(rois.data_type() == DataType::QASYMM16) |
| 101 | { |
| 102 | rois_ptr[values_per_roi * pw + 1] = quantize_qasymm16(static_cast<float>(x1), rois.quantization_info()); |
| 103 | rois_ptr[values_per_roi * pw + 2] = quantize_qasymm16(static_cast<float>(y1), rois.quantization_info()); |
| 104 | rois_ptr[values_per_roi * pw + 3] = quantize_qasymm16(static_cast<float>(x2), rois.quantization_info()); |
| 105 | rois_ptr[values_per_roi * pw + 4] = quantize_qasymm16(static_cast<float>(y2), rois.quantization_info()); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | rois_ptr[values_per_roi * pw + 1] = static_cast<TRois>(x1); |
| 110 | rois_ptr[values_per_roi * pw + 2] = static_cast<TRois>(y1); |
| 111 | rois_ptr[values_per_roi * pw + 3] = static_cast<TRois>(x2); |
| 112 | rois_ptr[values_per_roi * pw + 4] = static_cast<TRois>(y2); |
| 113 | } |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 117 | TensorType compute_target(TensorShape input_shape, |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 118 | DataType data_type, |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 119 | DataLayout data_layout, |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 120 | const ROIPoolingLayerInfo &pool_info, |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 121 | const TensorShape rois_shape, |
| 122 | const QuantizationInfo &qinfo, |
| 123 | const QuantizationInfo &output_qinfo) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 124 | { |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 125 | if(data_layout == DataLayout::NHWC) |
| 126 | { |
| 127 | permute(input_shape, PermutationVector(2U, 0U, 1U)); |
| 128 | } |
| 129 | |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 130 | const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo(); |
| 131 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 132 | // Create tensors |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 133 | TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, qinfo, data_layout); |
| 134 | TensorType rois_tensor = create_tensor<TensorType>(rois_shape, _rois_data_type, 1, rois_qinfo); |
| 135 | |
| 136 | const TensorShape dst_shape = misc::shape_calculator::compute_roi_align_shape(*(src.info()), *(rois_tensor.info()), pool_info); |
| 137 | TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 138 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 139 | // Create and configure function |
| 140 | FunctionType roi_align_layer; |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 141 | roi_align_layer.configure(&src, &rois_tensor, &dst, pool_info); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 142 | |
| 143 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 144 | ARM_COMPUTE_EXPECT(rois_tensor.info()->is_resizable(), framework::LogLevel::ERRORS); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 145 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 146 | |
| 147 | // Allocate tensors |
| 148 | src.allocator()->allocate(); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 149 | rois_tensor.allocator()->allocate(); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 150 | dst.allocator()->allocate(); |
| 151 | |
| 152 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 153 | ARM_COMPUTE_EXPECT(!rois_tensor.info()->is_resizable(), framework::LogLevel::ERRORS); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 154 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 155 | |
| 156 | // Fill tensors |
| 157 | fill(AccessorType(src)); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 158 | generate_rois(AccessorType(rois_tensor), input_shape, pool_info, rois_shape, data_layout); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 159 | |
| 160 | // Compute function |
| 161 | roi_align_layer.run(); |
| 162 | |
| 163 | return dst; |
| 164 | } |
| 165 | |
| 166 | SimpleTensor<T> compute_reference(const TensorShape &input_shape, |
| 167 | DataType data_type, |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 168 | const ROIPoolingLayerInfo &pool_info, |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 169 | const TensorShape rois_shape, |
| 170 | const QuantizationInfo &qinfo, |
| 171 | const QuantizationInfo &output_qinfo) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 172 | { |
| 173 | // Create reference tensor |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 174 | SimpleTensor<T> src{ input_shape, data_type, 1, qinfo }; |
| 175 | const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo(); |
| 176 | SimpleTensor<TRois> rois_tensor{ rois_shape, _rois_data_type, 1, rois_qinfo }; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 177 | |
| 178 | // Fill reference tensor |
| 179 | fill(src); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 180 | generate_rois(rois_tensor, input_shape, pool_info, rois_shape); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 181 | |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 182 | return reference::roi_align_layer(src, rois_tensor, pool_info, output_qinfo); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | TensorType _target{}; |
| 186 | SimpleTensor<T> _reference{}; |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 187 | DataType _rois_data_type{}; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 188 | }; |
| 189 | |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame^] | 190 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 191 | class ROIAlignLayerFixture : public ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 192 | { |
| 193 | public: |
| 194 | template <typename...> |
| 195 | void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout) |
| 196 | { |
| 197 | ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, pool_info, rois_shape, data_type, data_layout, |
| 198 | QuantizationInfo(), QuantizationInfo()); |
| 199 | } |
| 200 | }; |
| 201 | |
| 202 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 203 | class ROIAlignLayerQuantizedFixture : public ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 204 | { |
| 205 | public: |
| 206 | template <typename...> |
| 207 | void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, |
| 208 | DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo) |
| 209 | { |
| 210 | ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, pool_info, rois_shape, |
| 211 | data_type, data_layout, qinfo, output_qinfo); |
| 212 | } |
| 213 | }; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 214 | } // namespace validation |
| 215 | } // namespace test |
| 216 | } // namespace arm_compute |
| 217 | #endif /* ARM_COMPUTE_TEST_ROIALIGNLAYER_FIXTURE */ |