blob: b75415c6cb35549772b5e726bf5ae9fcc75e244d [file] [log] [blame]
giuros0118870812018-09-13 09:31:40 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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#include "ROIAlignLayer.h"
25
26#include "arm_compute/core/Types.h"
27#include "arm_compute/core/utils/misc/ShapeCalculator.h"
28#include "tests/validation/Helpers.h"
29
30#include <algorithm>
31
32namespace arm_compute
33{
34namespace test
35{
36namespace validation
37{
38namespace reference
39{
40namespace
41{
42/** Average pooling over an aligned window */
43template <typename T>
44inline T roi_align_1x1(const T *input, TensorShape input_shape,
45 float region_start_x,
46 float bin_size_x,
47 int grid_size_x,
48 float region_end_x,
49 float region_start_y,
50 float bin_size_y,
51 int grid_size_y,
52 float region_end_y,
53 int pz)
54{
55 if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
56 {
57 return T(0);
58 }
59 else
60 {
61 float avg = 0;
62 // Iterate through the aligned pooling region
63 for(int iy = 0; iy < grid_size_y; ++iy)
64 {
65 for(int ix = 0; ix < grid_size_x; ++ix)
66 {
67 // Align the window in the middle of every bin
68 float y = region_start_y + (iy + 0.5) * bin_size_y / float(grid_size_y);
69 float x = region_start_x + (ix + 0.5) * bin_size_x / float(grid_size_x);
70
71 // Interpolation in the [0,0] [0,1] [1,0] [1,1] square
72 const int y_low = y;
73 const int x_low = x;
74 const int y_high = y_low + 1;
75 const int x_high = x_low + 1;
76
77 const float ly = y - y_low;
78 const float lx = x - x_low;
79 const float hy = 1. - ly;
80 const float hx = 1. - lx;
81
82 const float w1 = hy * hx;
83 const float w2 = hy * lx;
84 const float w3 = ly * hx;
85 const float w4 = ly * lx;
86
87 const size_t idx1 = coord2index(input_shape, Coordinates(x_low, y_low, pz));
88 T data1 = input[idx1];
89
90 const size_t idx2 = coord2index(input_shape, Coordinates(x_high, y_low, pz));
91 T data2 = input[idx2];
92
93 const size_t idx3 = coord2index(input_shape, Coordinates(x_low, y_high, pz));
94 T data3 = input[idx3];
95
96 const size_t idx4 = coord2index(input_shape, Coordinates(x_high, y_high, pz));
97 T data4 = input[idx4];
98
99 avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4;
100 }
101 }
102
103 avg /= grid_size_x * grid_size_y;
104
105 return T(avg);
106 }
107}
108
109/** Clamp the value between lower and upper */
110template <typename T>
111T clamp(T value, T lower, T upper)
112{
113 return std::max(lower, std::min(value, upper));
114}
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100115
116SimpleTensor<float> convert_rois_from_asymmetric(SimpleTensor<uint16_t> rois)
117{
118 const UniformQuantizationInfo &quantization_info = rois.quantization_info().uniform();
119 SimpleTensor<float> dst{ rois.shape(), DataType::F32, 1, QuantizationInfo(), rois.data_layout() };
120
121 for(int i = 0; i < rois.num_elements(); i += 5)
122 {
123 dst[i] = static_cast<float>(rois[i]); // batch idx
124 dst[i + 1] = dequantize_qasymm16(rois[i + 1], quantization_info);
125 dst[i + 2] = dequantize_qasymm16(rois[i + 2], quantization_info);
126 dst[i + 3] = dequantize_qasymm16(rois[i + 3], quantization_info);
127 dst[i + 4] = dequantize_qasymm16(rois[i + 4], quantization_info);
128 }
129 return dst;
130}
giuros0118870812018-09-13 09:31:40 +0100131} // namespace
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100132template <typename T, typename TRois>
133SimpleTensor<T> roi_align_layer(const SimpleTensor<T> &src, const SimpleTensor<TRois> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo)
giuros0118870812018-09-13 09:31:40 +0100134{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100135 ARM_COMPUTE_UNUSED(output_qinfo);
136
Manuel Bottini60f0a412018-10-24 17:27:02 +0100137 const size_t values_per_roi = rois.shape()[0];
138 const size_t num_rois = rois.shape()[1];
139 DataType dst_data_type = src.data_type();
140
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100141 const auto *rois_ptr = static_cast<const TRois *>(rois.data());
giuros0118870812018-09-13 09:31:40 +0100142
143 TensorShape input_shape = src.shape();
144 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), src.shape()[2], num_rois);
145 SimpleTensor<T> dst(output_shape, dst_data_type);
146
147 // Iterate over every pixel of the input image
Manuel Bottini60f0a412018-10-24 17:27:02 +0100148 for(size_t px = 0; px < pool_info.pooled_width(); ++px)
giuros0118870812018-09-13 09:31:40 +0100149 {
Manuel Bottini60f0a412018-10-24 17:27:02 +0100150 for(size_t py = 0; py < pool_info.pooled_height(); ++py)
giuros0118870812018-09-13 09:31:40 +0100151 {
Manuel Bottini60f0a412018-10-24 17:27:02 +0100152 for(size_t pw = 0; pw < num_rois; ++pw)
giuros0118870812018-09-13 09:31:40 +0100153 {
Manuel Bottini60f0a412018-10-24 17:27:02 +0100154 const unsigned int roi_batch = rois_ptr[values_per_roi * pw];
155 const auto x1 = float(rois_ptr[values_per_roi * pw + 1]);
156 const auto y1 = float(rois_ptr[values_per_roi * pw + 2]);
157 const auto x2 = float(rois_ptr[values_per_roi * pw + 3]);
158 const auto y2 = float(rois_ptr[values_per_roi * pw + 4]);
giuros0118870812018-09-13 09:31:40 +0100159
Manuel Bottini60f0a412018-10-24 17:27:02 +0100160 const float roi_anchor_x = x1 * pool_info.spatial_scale();
161 const float roi_anchor_y = y1 * pool_info.spatial_scale();
162 const float roi_dims_x = std::max((x2 - x1) * pool_info.spatial_scale(), 1.0f);
163 const float roi_dims_y = std::max((y2 - y1) * pool_info.spatial_scale(), 1.0f);
giuros0118870812018-09-13 09:31:40 +0100164
165 float bin_size_x = roi_dims_x / pool_info.pooled_width();
166 float bin_size_y = roi_dims_y / pool_info.pooled_height();
167 float region_start_x = px * bin_size_x + roi_anchor_x;
168 float region_start_y = py * bin_size_y + roi_anchor_y;
169 float region_end_x = (px + 1) * bin_size_x + roi_anchor_x;
170 float region_end_y = (py + 1) * bin_size_y + roi_anchor_y;
171
172 region_start_x = clamp(region_start_x, 0.0f, float(input_shape[0]));
173 region_start_y = clamp(region_start_y, 0.0f, float(input_shape[1]));
174 region_end_x = clamp(region_end_x, 0.0f, float(input_shape[0]));
175 region_end_y = clamp(region_end_y, 0.0f, float(input_shape[1]));
176
177 const int roi_bin_grid_x = (pool_info.sampling_ratio() > 0) ? pool_info.sampling_ratio() : int(ceil(bin_size_x));
178 const int roi_bin_grid_y = (pool_info.sampling_ratio() > 0) ? pool_info.sampling_ratio() : int(ceil(bin_size_y));
179
180 // Move input and output pointer across the fourth dimension
181 const size_t input_stride_w = input_shape[0] * input_shape[1] * input_shape[2];
182 const size_t output_stride_w = output_shape[0] * output_shape[1] * output_shape[2];
183 const T *input_ptr = src.data() + roi_batch * input_stride_w;
184 T *output_ptr = dst.data() + px + py * output_shape[0] + pw * output_stride_w;
185
186 for(int pz = 0; pz < int(input_shape[2]); ++pz)
187 {
188 // For every pixel pool over an aligned region
189 *(output_ptr + pz * output_shape[0] * output_shape[1]) = roi_align_1x1(input_ptr, input_shape,
190 region_start_x,
191 bin_size_x,
192 roi_bin_grid_x,
193 region_end_x,
194 region_start_y,
195 bin_size_y,
196 roi_bin_grid_y,
197 region_end_y, pz);
198 }
199 }
200 }
201 }
202 return dst;
203}
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100204
205template SimpleTensor<float> roi_align_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo);
206template SimpleTensor<half> roi_align_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo);
207
208template <>
209SimpleTensor<uint8_t> roi_align_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint16_t> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo)
210{
211 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
212 SimpleTensor<float> rois_tmp = convert_rois_from_asymmetric(rois);
213 SimpleTensor<float> dst_tmp = roi_align_layer<float, float>(src_tmp, rois_tmp, pool_info, output_qinfo);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100214 SimpleTensor<uint8_t> dst = convert_to_asymmetric<uint8_t>(dst_tmp, output_qinfo);
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100215 return dst;
216}
Manuel Bottini8481d832019-12-10 15:28:40 +0000217template <>
218SimpleTensor<int8_t> roi_align_layer(const SimpleTensor<int8_t> &src, const SimpleTensor<uint16_t> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo)
219{
220 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
221 SimpleTensor<float> rois_tmp = convert_rois_from_asymmetric(rois);
222 SimpleTensor<float> dst_tmp = roi_align_layer<float, float>(src_tmp, rois_tmp, pool_info, output_qinfo);
223 SimpleTensor<int8_t> dst = convert_to_asymmetric<int8_t>(dst_tmp, output_qinfo);
224 return dst;
225}
giuros0118870812018-09-13 09:31:40 +0100226} // namespace reference
227} // namespace validation
228} // namespace test
229} // namespace arm_compute