blob: 68a465d18f378646c11b031af1b26d0b8bc841f9 [file] [log] [blame]
giuros0118870812018-09-13 09:31:40 +01001/*
2 * Copyright (c) 2018 ARM Limited.
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#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}
115} // namespace
116template <typename T>
117SimpleTensor<T> roi_align_layer(const SimpleTensor<T> &src, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info)
118{
119 const size_t num_rois = rois.size();
120 DataType dst_data_type = src.data_type();
121
122 TensorShape input_shape = src.shape();
123 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), src.shape()[2], num_rois);
124 SimpleTensor<T> dst(output_shape, dst_data_type);
125
126 // Iterate over every pixel of the input image
127 for(size_t px = 0; px < pool_info.pooled_width(); px++)
128 {
129 for(size_t py = 0; py < pool_info.pooled_height(); py++)
130 {
131 for(size_t pw = 0; pw < num_rois; pw++)
132 {
133 ROI roi = rois[pw];
134 const int roi_batch = roi.batch_idx;
135
136 const float roi_anchor_x = roi.rect.x * pool_info.spatial_scale();
137 const float roi_anchor_y = roi.rect.y * pool_info.spatial_scale();
138 const float roi_dims_x = std::max(roi.rect.width * pool_info.spatial_scale(), 1.0f);
139 const float roi_dims_y = std::max(roi.rect.height * pool_info.spatial_scale(), 1.0f);
140 ;
141
142 float bin_size_x = roi_dims_x / pool_info.pooled_width();
143 float bin_size_y = roi_dims_y / pool_info.pooled_height();
144 float region_start_x = px * bin_size_x + roi_anchor_x;
145 float region_start_y = py * bin_size_y + roi_anchor_y;
146 float region_end_x = (px + 1) * bin_size_x + roi_anchor_x;
147 float region_end_y = (py + 1) * bin_size_y + roi_anchor_y;
148
149 region_start_x = clamp(region_start_x, 0.0f, float(input_shape[0]));
150 region_start_y = clamp(region_start_y, 0.0f, float(input_shape[1]));
151 region_end_x = clamp(region_end_x, 0.0f, float(input_shape[0]));
152 region_end_y = clamp(region_end_y, 0.0f, float(input_shape[1]));
153
154 const int roi_bin_grid_x = (pool_info.sampling_ratio() > 0) ? pool_info.sampling_ratio() : int(ceil(bin_size_x));
155 const int roi_bin_grid_y = (pool_info.sampling_ratio() > 0) ? pool_info.sampling_ratio() : int(ceil(bin_size_y));
156
157 // Move input and output pointer across the fourth dimension
158 const size_t input_stride_w = input_shape[0] * input_shape[1] * input_shape[2];
159 const size_t output_stride_w = output_shape[0] * output_shape[1] * output_shape[2];
160 const T *input_ptr = src.data() + roi_batch * input_stride_w;
161 T *output_ptr = dst.data() + px + py * output_shape[0] + pw * output_stride_w;
162
163 for(int pz = 0; pz < int(input_shape[2]); ++pz)
164 {
165 // For every pixel pool over an aligned region
166 *(output_ptr + pz * output_shape[0] * output_shape[1]) = roi_align_1x1(input_ptr, input_shape,
167 region_start_x,
168 bin_size_x,
169 roi_bin_grid_x,
170 region_end_x,
171 region_start_y,
172 bin_size_y,
173 roi_bin_grid_y,
174 region_end_y, pz);
175 }
176 }
177 }
178 }
179 return dst;
180}
181template SimpleTensor<float> roi_align_layer(const SimpleTensor<float> &src, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info);
182template SimpleTensor<half> roi_align_layer(const SimpleTensor<half> &src, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info);
183} // namespace reference
184} // namespace validation
185} // namespace test
186} // namespace arm_compute