blob: 4b46a6176d7c1360f9a9c384c52c5a1d5639d685 [file] [log] [blame]
Suhail Munshiab840882021-02-09 16:31:00 +00001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2021, 2023 Arm Limited.
Suhail Munshiab840882021-02-09 16:31:00 +00003 *
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_ROIPOOLINGLAYER_FIXTURE
25#define ARM_COMPUTE_TEST_ROIPOOLINGLAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/core/utils/misc/ShapeCalculator.h"
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/ROIPoolingLayer.h"
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
45class ROIPoolingLayerGenericFixture : public framework::Fixture
46{
47public:
Suhail Munshiab840882021-02-09 16:31:00 +000048 void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo)
49 {
50 _target = compute_target(input_shape, data_type, data_layout, pool_info, rois_shape, qinfo, output_qinfo);
51 _reference = compute_reference(input_shape, data_type, pool_info, rois_shape, qinfo, output_qinfo);
52 }
53
54protected:
55 template <typename U>
56 void fill(U &&tensor)
57 {
58 library->fill_tensor_uniform(tensor, 0);
59 }
60
61 template <typename U>
62 void generate_rois(U &&rois, const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, TensorShape rois_shape, DataLayout data_layout = DataLayout::NCHW)
63 {
64 const size_t values_per_roi = rois_shape.x();
65 const size_t num_rois = rois_shape.y();
66
67 std::mt19937 gen(library->seed());
68 uint16_t *rois_ptr = static_cast<uint16_t *>(rois.data());
69
70 const float pool_width = pool_info.pooled_width();
71 const float pool_height = pool_info.pooled_height();
72 const float roi_scale = pool_info.spatial_scale();
73
74 // Calculate distribution bounds
75 const auto scaled_width = static_cast<float>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH)] / roi_scale) / pool_width);
76 const auto scaled_height = static_cast<float>((shape[get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT)] / roi_scale) / pool_height);
77 const auto min_width = static_cast<float>(pool_width / roi_scale);
78 const auto min_height = static_cast<float>(pool_height / roi_scale);
79
80 // Create distributions
81 std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1);
82 std::uniform_int_distribution<> dist_x1(0, scaled_width);
83 std::uniform_int_distribution<> dist_y1(0, scaled_height);
84 std::uniform_int_distribution<> dist_w(min_width, std::max(float(min_width), (pool_width - 2) * scaled_width));
85 std::uniform_int_distribution<> dist_h(min_height, std::max(float(min_height), (pool_height - 2) * scaled_height));
86
87 for(unsigned int pw = 0; pw < num_rois; ++pw)
88 {
89 const auto batch_idx = dist_batch(gen);
90 const auto x1 = dist_x1(gen);
91 const auto y1 = dist_y1(gen);
92 const auto x2 = x1 + dist_w(gen);
93 const auto y2 = y1 + dist_h(gen);
94
95 rois_ptr[values_per_roi * pw] = batch_idx;
96 rois_ptr[values_per_roi * pw + 1] = static_cast<uint16_t>(x1);
97 rois_ptr[values_per_roi * pw + 2] = static_cast<uint16_t>(y1);
98 rois_ptr[values_per_roi * pw + 3] = static_cast<uint16_t>(x2);
99 rois_ptr[values_per_roi * pw + 4] = static_cast<uint16_t>(y2);
100 }
101 }
102
103 TensorType compute_target(TensorShape input_shape,
104 DataType data_type,
105 DataLayout data_layout,
106 const ROIPoolingLayerInfo &pool_info,
107 const TensorShape rois_shape,
108 const QuantizationInfo &qinfo,
109 const QuantizationInfo &output_qinfo)
110 {
111 const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo();
112
113 // Create tensors
114 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, qinfo, data_layout);
115 TensorType rois_tensor = create_tensor<TensorType>(rois_shape, _rois_data_type, 1, rois_qinfo);
116
117 // Initialise shape and declare output tensor dst
118 const TensorShape dst_shape;
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100119 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, output_qinfo, data_layout);
Suhail Munshiab840882021-02-09 16:31:00 +0000120
121 // Create and configure function
122 FunctionType roi_pool_layer;
123 roi_pool_layer.configure(&src, &rois_tensor, &dst, pool_info);
124
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100125 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
126 ARM_COMPUTE_ASSERT(rois_tensor.info()->is_resizable());
127 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Suhail Munshiab840882021-02-09 16:31:00 +0000128
129 // Allocate tensors
130 src.allocator()->allocate();
131 rois_tensor.allocator()->allocate();
132 dst.allocator()->allocate();
133
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100134 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
135 ARM_COMPUTE_ASSERT(!rois_tensor.info()->is_resizable());
136 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Suhail Munshiab840882021-02-09 16:31:00 +0000137
138 // Fill tensors
139 fill(AccessorType(src));
140 generate_rois(AccessorType(rois_tensor), input_shape, pool_info, rois_shape, data_layout);
141
142 // Compute function
143 roi_pool_layer.run();
144
145 return dst;
146 }
147
148 SimpleTensor<T> compute_reference(const TensorShape &input_shape,
149 DataType data_type,
150 const ROIPoolingLayerInfo &pool_info,
151 const TensorShape rois_shape,
152 const QuantizationInfo &qinfo,
153 const QuantizationInfo &output_qinfo)
154 {
155 // Create reference tensor
156 SimpleTensor<T> src{ input_shape, data_type, 1, qinfo };
157 const QuantizationInfo rois_qinfo = is_data_type_quantized(data_type) ? QuantizationInfo(0.125f, 0) : QuantizationInfo();
158 SimpleTensor<uint16_t> rois_tensor{ rois_shape, _rois_data_type, 1, rois_qinfo };
159
160 // Fill reference tensor
161 fill(src);
162 generate_rois(rois_tensor, input_shape, pool_info, rois_shape);
163
164 return reference::roi_pool_layer(src, rois_tensor, pool_info, output_qinfo);
165 }
166
167 TensorType _target{};
168 SimpleTensor<T> _reference{};
169 const DataType _rois_data_type{ DataType::U16 };
170};
171
172template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
173class ROIPoolingLayerQuantizedFixture : public ROIPoolingLayerGenericFixture<TensorType, AccessorType, FunctionType, T>
174{
175public:
Suhail Munshiab840882021-02-09 16:31:00 +0000176 void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type,
177 DataLayout data_layout, QuantizationInfo qinfo, QuantizationInfo output_qinfo)
178 {
179 ROIPoolingLayerGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, pool_info, rois_shape,
180 data_type, data_layout, qinfo, output_qinfo);
181 }
182};
183
184template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
185class ROIPoolingLayerFixture : public ROIPoolingLayerGenericFixture<TensorType, AccessorType, FunctionType, T>
186{
187public:
Suhail Munshiab840882021-02-09 16:31:00 +0000188 void setup(TensorShape input_shape, const ROIPoolingLayerInfo pool_info, TensorShape rois_shape, DataType data_type, DataLayout data_layout)
189 {
190 ROIPoolingLayerGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, pool_info, rois_shape, data_type, data_layout,
191 QuantizationInfo(), QuantizationInfo());
192 }
193};
194
195} // namespace validation
196} // namespace test
197} // namespace arm_compute
198
199#endif /* ARM_COMPUTE_TEST_ROIPOOLINGLAYER_FIXTURE */