blob: 69734545c9d6b1fa839284db871f6a29cf3c6e0b [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Anthony Barbier06ea0482018-02-22 15:45:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +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#include "PoolingLayer.h"
25
26#include "arm_compute/core/Types.h"
Jenkinsb3a371b2018-05-23 11:36:53 +010027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000028#include "tests/validation/FixedPoint.h"
29#include "tests/validation/Helpers.h"
30
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
Jenkinsb3a371b2018-05-23 11:36:53 +010039using namespace arm_compute::misc::shape_calculator;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000040
41template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
Anthony Barbier06ea0482018-02-22 15:45:35 +000042SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000043{
44 ARM_COMPUTE_ERROR_ON(info.is_global_pooling() && (src.shape().x() != src.shape().y()));
45
Jenkinsb3a371b2018-05-23 11:36:53 +010046 // Create reference
47 SimpleTensor<T> dst{ compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type(), src.fixed_point_position()), info), src.data_type(), 1, src.fixed_point_position() };
48
Anthony Barbier06ea0482018-02-22 15:45:35 +000049 const int pool_size_x = info.is_global_pooling() ? src.shape().x() : info.pool_size().width;
50 const int pool_size_y = info.is_global_pooling() ? src.shape().y() : info.pool_size().height;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000051 PoolingType type = info.pool_type();
52 int pool_stride_x = info.pad_stride_info().stride().first;
53 int pool_stride_y = info.pad_stride_info().stride().second;
Anthony Barbier06ea0482018-02-22 15:45:35 +000054 int pad_left = info.pad_stride_info().pad_left();
55 int pad_top = info.pad_stride_info().pad_top();
56 int pad_right = info.pad_stride_info().pad_right();
57 int pad_bottom = info.pad_stride_info().pad_bottom();
Anthony Barbier8140e1e2017-12-14 23:48:46 +000058 bool exclude_padding = info.exclude_padding();
59
60 const auto w_src = static_cast<int>(src.shape()[0]);
61 const auto h_src = static_cast<int>(src.shape()[1]);
62 const int upper_dims = src.shape().total_size() / (w_src * h_src);
63
Anthony Barbier8140e1e2017-12-14 23:48:46 +000064 const auto w_dst = static_cast<int>(dst.shape()[0]);
65 const auto h_dst = static_cast<int>(dst.shape()[1]);
66
67 if(type == PoolingType::MAX)
68 {
69 for(int r = 0; r < upper_dims; ++r)
70 {
71 for(int h = 0; h < h_dst; ++h)
72 {
73 for(int w = 0; w < w_dst; ++w)
74 {
Anthony Barbier06ea0482018-02-22 15:45:35 +000075 int wstart = w * pool_stride_x - pad_left;
76 int hstart = h * pool_stride_y - pad_top;
77 int wend = std::min(wstart + pool_size_x, w_src);
78 int hend = std::min(hstart + pool_size_y, h_src);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000079 wstart = std::max(wstart, 0);
80 hstart = std::max(hstart, 0);
81
82 T max_val = std::numeric_limits<T>::lowest();
83 for(int y = hstart; y < hend; ++y)
84 {
85 for(int x = wstart; x < wend; ++x)
86 {
87 const T val = src[r * h_src * w_src + y * w_src + x];
88 if(val > max_val)
89 {
90 max_val = val;
91 }
92 }
93 }
94
95 dst[r * h_dst * w_dst + h * w_dst + w] = max_val;
96 }
97 }
98 }
99 }
100 else // Average or l2 pooling
101 {
102 for(int r = 0; r < upper_dims; ++r)
103 {
104 for(int h = 0; h < h_dst; ++h)
105 {
106 for(int w = 0; w < w_dst; ++w)
107 {
108 T avg_val(0);
Anthony Barbier06ea0482018-02-22 15:45:35 +0000109 int wstart = w * pool_stride_x - pad_left;
110 int hstart = h * pool_stride_y - pad_top;
111 int wend = std::min(wstart + pool_size_x, w_src + pad_right);
112 int hend = std::min(hstart + pool_size_y, h_src + pad_bottom);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000113 int pool = (hend - hstart) * (wend - wstart);
114 wstart = std::max(wstart, 0);
115 hstart = std::max(hstart, 0);
116 wend = std::min(wend, w_src);
117 hend = std::min(hend, h_src);
118 // Exclude padding pixels from the average
119 if(exclude_padding)
120 {
121 pool = (hend - hstart) * (wend - wstart);
122 }
123
124 if(type == PoolingType::AVG)
125 {
126 for(int y = hstart; y < hend; ++y)
127 {
128 for(int x = wstart; x < wend; ++x)
129 {
130 avg_val += src[r * h_src * w_src + y * w_src + x];
131 }
132 }
133 dst[r * h_dst * w_dst + h * w_dst + w] = avg_val / pool;
134 }
135 else
136 {
137 for(int y = hstart; y < hend; ++y)
138 {
139 for(int x = wstart; x < wend; ++x)
140 {
141 const T val = src[r * h_src * w_src + y * w_src + x];
142 avg_val += val * val;
143 }
144 }
145 dst[r * h_dst * w_dst + h * w_dst + w] = std::sqrt(avg_val / pool);
146 }
147 }
148 }
149 }
150 }
151
152 return dst;
153}
154
155template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type>
Anthony Barbier06ea0482018-02-22 15:45:35 +0000156SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000157{
158 ARM_COMPUTE_ERROR_ON(info.is_global_pooling() && (src.shape().x() != src.shape().y()));
159
Jenkinsb3a371b2018-05-23 11:36:53 +0100160 const auto w_src = static_cast<int>(src.shape()[0]);
161 const auto h_src = static_cast<int>(src.shape()[1]);
162 const int upper_dims = src.shape().total_size() / (w_src * h_src);
163
Anthony Barbier06ea0482018-02-22 15:45:35 +0000164 const int pool_size_x = info.is_global_pooling() ? src.shape().x() : info.pool_size().width;
165 const int pool_size_y = info.is_global_pooling() ? src.shape().y() : info.pool_size().height;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000166 PoolingType type = info.pool_type();
167 int pool_stride_x = info.pad_stride_info().stride().first;
168 int pool_stride_y = info.pad_stride_info().stride().second;
Anthony Barbier06ea0482018-02-22 15:45:35 +0000169 int pad_left = info.pad_stride_info().pad_left();
170 int pad_top = info.pad_stride_info().pad_top();
171 int pad_right = info.pad_stride_info().pad_right();
172 int pad_bottom = info.pad_stride_info().pad_bottom();
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000173 bool exclude_padding = info.exclude_padding();
174
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000175 // Create reference
Jenkinsb3a371b2018-05-23 11:36:53 +0100176 SimpleTensor<T> dst{ compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type(), src.fixed_point_position()), info), src.data_type(), 1, src.fixed_point_position() };
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000177
178 const auto w_dst = static_cast<int>(dst.shape()[0]);
179 const auto h_dst = static_cast<int>(dst.shape()[1]);
180
181 if(type == PoolingType::MAX)
182 {
183 for(int r = 0; r < upper_dims; ++r)
184 {
185 for(int h = 0; h < h_dst; ++h)
186 {
187 for(int w = 0; w < w_dst; ++w)
188 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000189 int wstart = w * pool_stride_x - pad_left;
190 int hstart = h * pool_stride_y - pad_top;
191 int wend = std::min(wstart + pool_size_x, w_src);
192 int hend = std::min(hstart + pool_size_y, h_src);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000193 wstart = std::max(wstart, 0);
194 hstart = std::max(hstart, 0);
195
196 T max_val = std::numeric_limits<T>::lowest();
197 for(int y = hstart; y < hend; ++y)
198 {
199 for(int x = wstart; x < wend; ++x)
200 {
201 const T val = src[r * h_src * w_src + y * w_src + x];
202 if(val > max_val)
203 {
204 max_val = val;
205 }
206 }
207 }
208
209 dst[r * h_dst * w_dst + h * w_dst + w] = max_val;
210 }
211 }
212 }
213 }
214 else // Average or l2 pooling
215 {
216 for(int r = 0; r < upper_dims; ++r)
217 {
218 for(int h = 0; h < h_dst; ++h)
219 {
220 for(int w = 0; w < w_dst; ++w)
221 {
Anthony Barbier06ea0482018-02-22 15:45:35 +0000222 int wstart = w * pool_stride_x - pad_left;
223 int hstart = h * pool_stride_y - pad_top;
224 int wend = std::min(wstart + pool_size_x, w_src + pad_right);
225 int hend = std::min(hstart + pool_size_y, h_src + pad_bottom);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000226 int pool = (hend - hstart) * (wend - wstart);
227 wstart = std::max(wstart, 0);
228 hstart = std::max(hstart, 0);
229 wend = std::min(wend, w_src);
230 hend = std::min(hend, h_src);
231 // Exclude padding pixels from the average
232 if(exclude_padding)
233 {
234 pool = (hend - hstart) * (wend - wstart);
235 }
236
237 using namespace fixed_point_arithmetic;
238
239 const int fixed_point_position = src.fixed_point_position();
240 const fixed_point<T> const_1(1, fixed_point_position);
241 const fixed_point<T> invpool_fp(1.f / static_cast<float>(pool), fixed_point_position);
242 fixed_point<T> avg_val(0, fixed_point_position, true);
243
244 if(type == PoolingType::AVG)
245 {
246 for(int y = hstart; y < hend; ++y)
247 {
248 for(int x = wstart; x < wend; ++x)
249 {
250 const fixed_point<T> in_fp(src[r * h_src * w_src + y * w_src + x], fixed_point_position, true);
251 avg_val = add(avg_val, in_fp);
252 }
253 }
254 dst[r * h_dst * w_dst + h * w_dst + w] = mul(avg_val, invpool_fp).raw();
255 }
256 else
257 {
258 for(int y = hstart; y < hend; ++y)
259 {
260 for(int x = wstart; x < wend; ++x)
261 {
262 const fixed_point<T> in_fp(src[r * h_src * w_src + y * w_src + x], fixed_point_position, true);
263 avg_val = add(avg_val, mul(in_fp, in_fp));
264 }
265 }
266 auto res = div(const_1, (inv_sqrt(mul(avg_val, invpool_fp))));
267 dst[r * h_dst * w_dst + h * w_dst + w] = res.raw();
268 }
269 }
270 }
271 }
272 }
273
274 return dst;
275}
276
277template <>
Anthony Barbier06ea0482018-02-22 15:45:35 +0000278SimpleTensor<uint8_t> pooling_layer<uint8_t>(const SimpleTensor<uint8_t> &src, const PoolingLayerInfo &info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000279{
280 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
281 SimpleTensor<float> dst_tmp = pooling_layer<float>(src_tmp, info);
282 SimpleTensor<uint8_t> dst = convert_to_asymmetric(dst_tmp, src.quantization_info());
283 return dst;
284}
285
Anthony Barbier06ea0482018-02-22 15:45:35 +0000286template SimpleTensor<float> pooling_layer(const SimpleTensor<float> &src, const PoolingLayerInfo &info);
287template SimpleTensor<half> pooling_layer(const SimpleTensor<half> &src, const PoolingLayerInfo &info);
288template SimpleTensor<qint8_t> pooling_layer(const SimpleTensor<qint8_t> &src, const PoolingLayerInfo &info);
289template SimpleTensor<qint16_t> pooling_layer(const SimpleTensor<qint16_t> &src, const PoolingLayerInfo &info);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000290} // namespace reference
291} // namespace validation
292} // namespace test
293} // namespace arm_compute