blob: c14ab98c28e2c9c4cc5b8171080b5e6c1d1b2212 [file] [log] [blame]
Georgios Pinitasdc460f12017-08-24 19:02:44 +01001/*
Michalis Spyroubd0e6122018-01-23 09:52:16 +00002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitasdc460f12017-08-24 19:02:44 +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 "PoolingLayer.h"
25
Georgios Pinitas583137c2017-08-31 18:12:42 +010026#include "arm_compute/core/Types.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010027#include "tests/validation/FixedPoint.h"
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +000028#include "tests/validation/Helpers.h"
Georgios Pinitasdc460f12017-08-24 19:02:44 +010029
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
38namespace
39{
Isabella Gottardi6e464c32018-01-26 12:32:45 +000040TensorShape calculate_output_shape(TensorShape shape, const PoolingLayerInfo &info)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010041{
Isabella Gottardi6e464c32018-01-26 12:32:45 +000042 TensorShape dst_shape = shape;
43 const int pool_size_x = info.is_global_pooling() ? shape.x() : info.pool_size().width;
44 const int pool_size_y = info.is_global_pooling() ? shape.y() : info.pool_size().height;
Georgios Pinitasdc460f12017-08-24 19:02:44 +010045 const std::pair<unsigned int, unsigned int> scaled_dims = arm_compute::scaled_dimensions(shape.x(),
46 shape.y(),
Isabella Gottardi6e464c32018-01-26 12:32:45 +000047 pool_size_x,
48 pool_size_y,
Georgios Pinitasdc460f12017-08-24 19:02:44 +010049 info.pad_stride_info());
50 dst_shape.set(0, scaled_dims.first);
51 dst_shape.set(1, scaled_dims.second);
52
53 return dst_shape;
54}
55} // namespace
56
57template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
Isabella Gottardi6e464c32018-01-26 12:32:45 +000058SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info)
Georgios Pinitasdc460f12017-08-24 19:02:44 +010059{
Georgios Pinitas4c2dd542017-11-13 12:58:41 +000060 ARM_COMPUTE_ERROR_ON(info.is_global_pooling() && (src.shape().x() != src.shape().y()));
61
Isabella Gottardi6e464c32018-01-26 12:32:45 +000062 const int pool_size_x = info.is_global_pooling() ? src.shape().x() : info.pool_size().width;
63 const int pool_size_y = info.is_global_pooling() ? src.shape().y() : info.pool_size().height;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000064 PoolingType type = info.pool_type();
65 int pool_stride_x = info.pad_stride_info().stride().first;
66 int pool_stride_y = info.pad_stride_info().stride().second;
Michalis Spyroubd0e6122018-01-23 09:52:16 +000067 int pad_left = info.pad_stride_info().pad_left();
68 int pad_top = info.pad_stride_info().pad_top();
69 int pad_right = info.pad_stride_info().pad_right();
70 int pad_bottom = info.pad_stride_info().pad_bottom();
Georgios Pinitasadaae7e2017-10-30 15:56:32 +000071 bool exclude_padding = info.exclude_padding();
Georgios Pinitasdc460f12017-08-24 19:02:44 +010072
73 const auto w_src = static_cast<int>(src.shape()[0]);
74 const auto h_src = static_cast<int>(src.shape()[1]);
75 const int upper_dims = src.shape().total_size() / (w_src * h_src);
76
77 // Create reference
78 SimpleTensor<T> dst{ calculate_output_shape(src.shape(), info), src.data_type(), 1, src.fixed_point_position() };
79
80 const auto w_dst = static_cast<int>(dst.shape()[0]);
81 const auto h_dst = static_cast<int>(dst.shape()[1]);
82
83 if(type == PoolingType::MAX)
84 {
85 for(int r = 0; r < upper_dims; ++r)
86 {
87 for(int h = 0; h < h_dst; ++h)
88 {
89 for(int w = 0; w < w_dst; ++w)
90 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +000091 int wstart = w * pool_stride_x - pad_left;
92 int hstart = h * pool_stride_y - pad_top;
Isabella Gottardi6e464c32018-01-26 12:32:45 +000093 int wend = std::min(wstart + pool_size_x, w_src);
94 int hend = std::min(hstart + pool_size_y, h_src);
Georgios Pinitasdc460f12017-08-24 19:02:44 +010095 wstart = std::max(wstart, 0);
96 hstart = std::max(hstart, 0);
97
98 T max_val = std::numeric_limits<T>::lowest();
99 for(int y = hstart; y < hend; ++y)
100 {
101 for(int x = wstart; x < wend; ++x)
102 {
103 const T val = src[r * h_src * w_src + y * w_src + x];
104 if(val > max_val)
105 {
106 max_val = val;
107 }
108 }
109 }
110
111 dst[r * h_dst * w_dst + h * w_dst + w] = max_val;
112 }
113 }
114 }
115 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100116 else // Average or l2 pooling
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100117 {
118 for(int r = 0; r < upper_dims; ++r)
119 {
120 for(int h = 0; h < h_dst; ++h)
121 {
122 for(int w = 0; w < w_dst; ++w)
123 {
124 T avg_val(0);
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000125 int wstart = w * pool_stride_x - pad_left;
126 int hstart = h * pool_stride_y - pad_top;
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000127 int wend = std::min(wstart + pool_size_x, w_src + pad_right);
128 int hend = std::min(hstart + pool_size_y, h_src + pad_bottom);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100129 int pool = (hend - hstart) * (wend - wstart);
130 wstart = std::max(wstart, 0);
131 hstart = std::max(hstart, 0);
132 wend = std::min(wend, w_src);
133 hend = std::min(hend, h_src);
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000134 // Exclude padding pixels from the average
135 if(exclude_padding)
136 {
137 pool = (hend - hstart) * (wend - wstart);
138 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100139
Georgios Pinitascdf51452017-08-31 14:21:36 +0100140 if(type == PoolingType::AVG)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100141 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100142 for(int y = hstart; y < hend; ++y)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100143 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100144 for(int x = wstart; x < wend; ++x)
145 {
146 avg_val += src[r * h_src * w_src + y * w_src + x];
147 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100148 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100149 dst[r * h_dst * w_dst + h * w_dst + w] = avg_val / pool;
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100150 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100151 else
152 {
153 for(int y = hstart; y < hend; ++y)
154 {
155 for(int x = wstart; x < wend; ++x)
156 {
157 const T val = src[r * h_src * w_src + y * w_src + x];
158 avg_val += val * val;
159 }
160 }
161 dst[r * h_dst * w_dst + h * w_dst + w] = std::sqrt(avg_val / pool);
162 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100163 }
164 }
165 }
166 }
167
168 return dst;
169}
170
171template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type>
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000172SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100173{
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000174 ARM_COMPUTE_ERROR_ON(info.is_global_pooling() && (src.shape().x() != src.shape().y()));
175
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000176 const int pool_size_x = info.is_global_pooling() ? src.shape().x() : info.pool_size().width;
177 const int pool_size_y = info.is_global_pooling() ? src.shape().y() : info.pool_size().height;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000178 PoolingType type = info.pool_type();
179 int pool_stride_x = info.pad_stride_info().stride().first;
180 int pool_stride_y = info.pad_stride_info().stride().second;
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000181 int pad_left = info.pad_stride_info().pad_left();
182 int pad_top = info.pad_stride_info().pad_top();
183 int pad_right = info.pad_stride_info().pad_right();
184 int pad_bottom = info.pad_stride_info().pad_bottom();
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000185 bool exclude_padding = info.exclude_padding();
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100186
187 const auto w_src = static_cast<int>(src.shape()[0]);
188 const auto h_src = static_cast<int>(src.shape()[1]);
189 const int upper_dims = src.shape().total_size() / (w_src * h_src);
190
191 // Create reference
192 SimpleTensor<T> dst{ calculate_output_shape(src.shape(), info), src.data_type(), 1, src.fixed_point_position() };
193
194 const auto w_dst = static_cast<int>(dst.shape()[0]);
195 const auto h_dst = static_cast<int>(dst.shape()[1]);
196
197 if(type == PoolingType::MAX)
198 {
199 for(int r = 0; r < upper_dims; ++r)
200 {
201 for(int h = 0; h < h_dst; ++h)
202 {
203 for(int w = 0; w < w_dst; ++w)
204 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000205 int wstart = w * pool_stride_x - pad_left;
206 int hstart = h * pool_stride_y - pad_top;
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000207 int wend = std::min(wstart + pool_size_x, w_src);
208 int hend = std::min(hstart + pool_size_y, h_src);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100209 wstart = std::max(wstart, 0);
210 hstart = std::max(hstart, 0);
211
212 T max_val = std::numeric_limits<T>::lowest();
213 for(int y = hstart; y < hend; ++y)
214 {
215 for(int x = wstart; x < wend; ++x)
216 {
217 const T val = src[r * h_src * w_src + y * w_src + x];
218 if(val > max_val)
219 {
220 max_val = val;
221 }
222 }
223 }
224
225 dst[r * h_dst * w_dst + h * w_dst + w] = max_val;
226 }
227 }
228 }
229 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100230 else // Average or l2 pooling
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100231 {
232 for(int r = 0; r < upper_dims; ++r)
233 {
234 for(int h = 0; h < h_dst; ++h)
235 {
236 for(int w = 0; w < w_dst; ++w)
237 {
Michalis Spyroubd0e6122018-01-23 09:52:16 +0000238 int wstart = w * pool_stride_x - pad_left;
239 int hstart = h * pool_stride_y - pad_top;
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000240 int wend = std::min(wstart + pool_size_x, w_src + pad_right);
241 int hend = std::min(hstart + pool_size_y, h_src + pad_bottom);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100242 int pool = (hend - hstart) * (wend - wstart);
243 wstart = std::max(wstart, 0);
244 hstart = std::max(hstart, 0);
245 wend = std::min(wend, w_src);
246 hend = std::min(hend, h_src);
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000247 // Exclude padding pixels from the average
248 if(exclude_padding)
249 {
250 pool = (hend - hstart) * (wend - wstart);
251 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100252
253 using namespace fixed_point_arithmetic;
254
255 const int fixed_point_position = src.fixed_point_position();
Georgios Pinitascdf51452017-08-31 14:21:36 +0100256 const fixed_point<T> const_1(1, fixed_point_position);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100257 const fixed_point<T> invpool_fp(1.f / static_cast<float>(pool), fixed_point_position);
258 fixed_point<T> avg_val(0, fixed_point_position, true);
259
Georgios Pinitascdf51452017-08-31 14:21:36 +0100260 if(type == PoolingType::AVG)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100261 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100262 for(int y = hstart; y < hend; ++y)
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100263 {
Georgios Pinitascdf51452017-08-31 14:21:36 +0100264 for(int x = wstart; x < wend; ++x)
265 {
266 const fixed_point<T> in_fp(src[r * h_src * w_src + y * w_src + x], fixed_point_position, true);
267 avg_val = add(avg_val, in_fp);
268 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100269 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100270 dst[r * h_dst * w_dst + h * w_dst + w] = mul(avg_val, invpool_fp).raw();
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100271 }
Georgios Pinitascdf51452017-08-31 14:21:36 +0100272 else
273 {
274 for(int y = hstart; y < hend; ++y)
275 {
276 for(int x = wstart; x < wend; ++x)
277 {
278 const fixed_point<T> in_fp(src[r * h_src * w_src + y * w_src + x], fixed_point_position, true);
279 avg_val = add(avg_val, mul(in_fp, in_fp));
280 }
281 }
282 auto res = div(const_1, (inv_sqrt(mul(avg_val, invpool_fp))));
283 dst[r * h_dst * w_dst + h * w_dst + w] = res.raw();
284 }
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100285 }
286 }
287 }
288 }
289
290 return dst;
291}
292
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000293template <>
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000294SimpleTensor<uint8_t> pooling_layer<uint8_t>(const SimpleTensor<uint8_t> &src, const PoolingLayerInfo &info)
Anton Lokhmotovaf6204c2017-11-08 09:34:19 +0000295{
296 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
297 SimpleTensor<float> dst_tmp = pooling_layer<float>(src_tmp, info);
298 SimpleTensor<uint8_t> dst = convert_to_asymmetric(dst_tmp, src.quantization_info());
299 return dst;
300}
301
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000302template SimpleTensor<float> pooling_layer(const SimpleTensor<float> &src, const PoolingLayerInfo &info);
303template SimpleTensor<half> pooling_layer(const SimpleTensor<half> &src, const PoolingLayerInfo &info);
304template SimpleTensor<qint8_t> pooling_layer(const SimpleTensor<qint8_t> &src, const PoolingLayerInfo &info);
305template SimpleTensor<qint16_t> pooling_layer(const SimpleTensor<qint16_t> &src, const PoolingLayerInfo &info);
Georgios Pinitasdc460f12017-08-24 19:02:44 +0100306} // namespace reference
307} // namespace validation
308} // namespace test
309} // namespace arm_compute