blob: e4470c99a0a0237548e196a4017ad9483d06570d [file] [log] [blame]
giuros0118870812018-09-13 09:31:40 +01001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * Copyright (c) 2018-2020 ARM Limited.
giuros0118870812018-09-13 09:31:40 +01003 *
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 Giorgio578a9fc2019-08-23 11:49:04 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
giuros0118870812018-09-13 09:31:40 +010030#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
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
Manuel Bottini8481d832019-12-10 15:28:40 +000044template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename TRois>
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010045class ROIAlignLayerGenericFixture : public framework::Fixture
giuros0118870812018-09-13 09:31:40 +010046{
47public:
48 template <typename...>
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010049 void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo)
giuros0118870812018-09-13 09:31:40 +010050 {
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010051 _rois_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::QASYMM16 : data_type;
52 _target = compute_target(input_shape, data_type, data_layout, pool_info, rois_shape, qinfo, output_qinfo);
53 _reference = compute_reference(input_shape, data_type, pool_info, rois_shape, qinfo, output_qinfo);
giuros0118870812018-09-13 09:31:40 +010054 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor)
59 {
60 library->fill_tensor_uniform(tensor, 0);
61 }
62
Manuel Bottini60f0a412018-10-24 17:27:02 +010063 template <typename U>
George Wort44b4e972019-01-08 11:41:54 +000064 void generate_rois(U &&rois, const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, TensorShape rois_shape, DataLayout data_layout = DataLayout::NCHW)
Manuel Bottini60f0a412018-10-24 17:27:02 +010065 {
66 const size_t values_per_roi = rois_shape.x();
67 const size_t num_rois = rois_shape.y();
68
69 std::mt19937 gen(library->seed());
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010070 TRois *rois_ptr = static_cast<TRois *>(rois.data());
Manuel Bottini60f0a412018-10-24 17:27:02 +010071
72 const float pool_width = pool_info.pooled_width();
73 const float pool_height = pool_info.pooled_height();
74 const float roi_scale = pool_info.spatial_scale();
75
76 // Calculate distribution bounds
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010077 const auto scaled_width = static_cast<float>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)] / roi_scale) / pool_width);
78 const auto scaled_height = static_cast<float>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)] / roi_scale) / pool_height);
79 const auto min_width = static_cast<float>(pool_width / roi_scale);
80 const auto min_height = static_cast<float>(pool_height / roi_scale);
Manuel Bottini60f0a412018-10-24 17:27:02 +010081
82 // Create distributions
83 std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1);
84 std::uniform_int_distribution<> dist_x1(0, scaled_width);
85 std::uniform_int_distribution<> dist_y1(0, scaled_height);
86 std::uniform_int_distribution<> dist_w(min_width, std::max(float(min_width), (pool_width - 2) * scaled_width));
87 std::uniform_int_distribution<> dist_h(min_height, std::max(float(min_height), (pool_height - 2) * scaled_height));
88
89 for(unsigned int pw = 0; pw < num_rois; ++pw)
90 {
91 const auto batch_idx = dist_batch(gen);
92 const auto x1 = dist_x1(gen);
93 const auto y1 = dist_y1(gen);
94 const auto x2 = x1 + dist_w(gen);
95 const auto y2 = y1 + dist_h(gen);
96
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010097 rois_ptr[values_per_roi * pw] = batch_idx;
98 if(rois.data_type() == DataType::QASYMM16)
99 {
100 rois_ptr[values_per_roi * pw + 1] = quantize_qasymm16(static_cast<float>(x1), rois.quantization_info());
101 rois_ptr[values_per_roi * pw + 2] = quantize_qasymm16(static_cast<float>(y1), rois.quantization_info());
102 rois_ptr[values_per_roi * pw + 3] = quantize_qasymm16(static_cast<float>(x2), rois.quantization_info());
103 rois_ptr[values_per_roi * pw + 4] = quantize_qasymm16(static_cast<float>(y2), rois.quantization_info());
104 }
105 else
106 {
107 rois_ptr[values_per_roi * pw + 1] = static_cast<TRois>(x1);
108 rois_ptr[values_per_roi * pw + 2] = static_cast<TRois>(y1);
109 rois_ptr[values_per_roi * pw + 3] = static_cast<TRois>(x2);
110 rois_ptr[values_per_roi * pw + 4] = static_cast<TRois>(y2);
111 }
Manuel Bottini60f0a412018-10-24 17:27:02 +0100112 }
113 }
114
George Wort44b4e972019-01-08 11:41:54 +0000115 TensorType compute_target(TensorShape input_shape,
giuros0118870812018-09-13 09:31:40 +0100116 DataType data_type,
George Wort44b4e972019-01-08 11:41:54 +0000117 DataLayout data_layout,
Manuel Bottini60f0a412018-10-24 17:27:02 +0100118 const ROIPoolingLayerInfo &pool_info,
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100119 const TensorShape rois_shape,
120 const QuantizationInfo &qinfo,
121 const QuantizationInfo &output_qinfo)
giuros0118870812018-09-13 09:31:40 +0100122 {
George Wort44b4e972019-01-08 11:41:54 +0000123 if(data_layout == DataLayout::NHWC)
124 {
125 permute(input_shape, PermutationVector(2U, 0U, 1U));
126 }
127
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100128 const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo();
129
giuros0118870812018-09-13 09:31:40 +0100130 // Create tensors
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100131 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, qinfo, data_layout);
132 TensorType rois_tensor = create_tensor<TensorType>(rois_shape, _rois_data_type, 1, rois_qinfo);
133
134 const TensorShape dst_shape = misc::shape_calculator::compute_roi_align_shape(*(src.info()), *(rois_tensor.info()), pool_info);
135 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout);
giuros0118870812018-09-13 09:31:40 +0100136
giuros0118870812018-09-13 09:31:40 +0100137 // Create and configure function
138 FunctionType roi_align_layer;
Manuel Bottini60f0a412018-10-24 17:27:02 +0100139 roi_align_layer.configure(&src, &rois_tensor, &dst, pool_info);
giuros0118870812018-09-13 09:31:40 +0100140
141 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini60f0a412018-10-24 17:27:02 +0100142 ARM_COMPUTE_EXPECT(rois_tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
giuros0118870812018-09-13 09:31:40 +0100143 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
144
145 // Allocate tensors
146 src.allocator()->allocate();
Manuel Bottini60f0a412018-10-24 17:27:02 +0100147 rois_tensor.allocator()->allocate();
giuros0118870812018-09-13 09:31:40 +0100148 dst.allocator()->allocate();
149
150 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
Manuel Bottini60f0a412018-10-24 17:27:02 +0100151 ARM_COMPUTE_EXPECT(!rois_tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
giuros0118870812018-09-13 09:31:40 +0100152 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
153
154 // Fill tensors
155 fill(AccessorType(src));
George Wort44b4e972019-01-08 11:41:54 +0000156 generate_rois(AccessorType(rois_tensor), input_shape, pool_info, rois_shape, data_layout);
giuros0118870812018-09-13 09:31:40 +0100157
158 // Compute function
159 roi_align_layer.run();
160
161 return dst;
162 }
163
164 SimpleTensor<T> compute_reference(const TensorShape &input_shape,
165 DataType data_type,
Manuel Bottini60f0a412018-10-24 17:27:02 +0100166 const ROIPoolingLayerInfo &pool_info,
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100167 const TensorShape rois_shape,
168 const QuantizationInfo &qinfo,
169 const QuantizationInfo &output_qinfo)
giuros0118870812018-09-13 09:31:40 +0100170 {
171 // Create reference tensor
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100172 SimpleTensor<T> src{ input_shape, data_type, 1, qinfo };
173 const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo();
174 SimpleTensor<TRois> rois_tensor{ rois_shape, _rois_data_type, 1, rois_qinfo };
giuros0118870812018-09-13 09:31:40 +0100175
176 // Fill reference tensor
177 fill(src);
Manuel Bottini60f0a412018-10-24 17:27:02 +0100178 generate_rois(rois_tensor, input_shape, pool_info, rois_shape);
giuros0118870812018-09-13 09:31:40 +0100179
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100180 return reference::roi_align_layer(src, rois_tensor, pool_info, output_qinfo);
giuros0118870812018-09-13 09:31:40 +0100181 }
182
183 TensorType _target{};
184 SimpleTensor<T> _reference{};
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100185 DataType _rois_data_type{};
giuros0118870812018-09-13 09:31:40 +0100186};
187
Manuel Bottini8481d832019-12-10 15:28:40 +0000188template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename TRois>
189class ROIAlignLayerFixture : public ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T, TRois>
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100190{
191public:
192 template <typename...>
193 void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout)
194 {
Manuel Bottini8481d832019-12-10 15:28:40 +0000195 ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T, TRois>::setup(input_shape, pool_info, rois_shape, data_type, data_layout,
196 QuantizationInfo(), QuantizationInfo());
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100197 }
198};
199
Manuel Bottini8481d832019-12-10 15:28:40 +0000200template <typename TensorType, typename AccessorType, typename FunctionType, typename T, typename TRois>
201class ROIAlignLayerQuantizedFixture : public ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T, TRois>
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100202{
203public:
204 template <typename...>
205 void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type,
206 DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo)
207 {
Manuel Bottini8481d832019-12-10 15:28:40 +0000208 ROIAlignLayerGenericFixture<TensorType, AccessorType, FunctionType, T, TRois>::setup(input_shape, pool_info, rois_shape,
209 data_type, data_layout, qinfo, output_qinfo);
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100210 }
211};
giuros0118870812018-09-13 09:31:40 +0100212} // namespace validation
213} // namespace test
214} // namespace arm_compute
215#endif /* ARM_COMPUTE_TEST_ROIALIGNLAYER_FIXTURE */