Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 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 "PoolingLayer.h" |
| 25 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 27 | #include "tests/validation/FixedPoint.h" |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 28 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace validation |
| 35 | { |
| 36 | namespace reference |
| 37 | { |
| 38 | namespace |
| 39 | { |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 40 | TensorShape calculate_output_shape(TensorShape shape, const PoolingLayerInfo &info) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 41 | { |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 42 | 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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 45 | const std::pair<unsigned int, unsigned int> scaled_dims = arm_compute::scaled_dimensions(shape.x(), |
| 46 | shape.y(), |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 47 | pool_size_x, |
| 48 | pool_size_y, |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 49 | 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 | |
| 57 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type> |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 58 | SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 59 | { |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 60 | ARM_COMPUTE_ERROR_ON(info.is_global_pooling() && (src.shape().x() != src.shape().y())); |
| 61 | |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 62 | 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 Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 64 | 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 Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 67 | 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 Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 71 | bool exclude_padding = info.exclude_padding(); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 72 | |
| 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 Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 91 | int wstart = w * pool_stride_x - pad_left; |
| 92 | int hstart = h * pool_stride_y - pad_top; |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 93 | int wend = std::min(wstart + pool_size_x, w_src); |
| 94 | int hend = std::min(hstart + pool_size_y, h_src); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 95 | 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 Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 116 | else // Average or l2 pooling |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 117 | { |
| 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 Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 125 | int wstart = w * pool_stride_x - pad_left; |
| 126 | int hstart = h * pool_stride_y - pad_top; |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 127 | 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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 129 | 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 Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 134 | // Exclude padding pixels from the average |
| 135 | if(exclude_padding) |
| 136 | { |
| 137 | pool = (hend - hstart) * (wend - wstart); |
| 138 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 139 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 140 | if(type == PoolingType::AVG) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 141 | { |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 142 | for(int y = hstart; y < hend; ++y) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 143 | { |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 144 | for(int x = wstart; x < wend; ++x) |
| 145 | { |
| 146 | avg_val += src[r * h_src * w_src + y * w_src + x]; |
| 147 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 148 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 149 | dst[r * h_dst * w_dst + h * w_dst + w] = avg_val / pool; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 150 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 151 | 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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return dst; |
| 169 | } |
| 170 | |
| 171 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type> |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 172 | SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 173 | { |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 174 | ARM_COMPUTE_ERROR_ON(info.is_global_pooling() && (src.shape().x() != src.shape().y())); |
| 175 | |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 176 | 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 Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 178 | 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 Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 181 | 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 Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 185 | bool exclude_padding = info.exclude_padding(); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 186 | |
| 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 Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 205 | int wstart = w * pool_stride_x - pad_left; |
| 206 | int hstart = h * pool_stride_y - pad_top; |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 207 | int wend = std::min(wstart + pool_size_x, w_src); |
| 208 | int hend = std::min(hstart + pool_size_y, h_src); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 209 | 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 Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 230 | else // Average or l2 pooling |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 231 | { |
| 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 Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 238 | int wstart = w * pool_stride_x - pad_left; |
| 239 | int hstart = h * pool_stride_y - pad_top; |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 240 | 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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 242 | 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 Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 247 | // Exclude padding pixels from the average |
| 248 | if(exclude_padding) |
| 249 | { |
| 250 | pool = (hend - hstart) * (wend - wstart); |
| 251 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 252 | |
| 253 | using namespace fixed_point_arithmetic; |
| 254 | |
| 255 | const int fixed_point_position = src.fixed_point_position(); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 256 | const fixed_point<T> const_1(1, fixed_point_position); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 257 | 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 Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 260 | if(type == PoolingType::AVG) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 261 | { |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 262 | for(int y = hstart; y < hend; ++y) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 263 | { |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 264 | 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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 269 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 270 | dst[r * h_dst * w_dst + h * w_dst + w] = mul(avg_val, invpool_fp).raw(); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 271 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 272 | 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 Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return dst; |
| 291 | } |
| 292 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 293 | template <> |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 294 | SimpleTensor<uint8_t> pooling_layer<uint8_t>(const SimpleTensor<uint8_t> &src, const PoolingLayerInfo &info) |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 295 | { |
| 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 Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 302 | template SimpleTensor<float> pooling_layer(const SimpleTensor<float> &src, const PoolingLayerInfo &info); |
| 303 | template SimpleTensor<half> pooling_layer(const SimpleTensor<half> &src, const PoolingLayerInfo &info); |
| 304 | template SimpleTensor<qint8_t> pooling_layer(const SimpleTensor<qint8_t> &src, const PoolingLayerInfo &info); |
| 305 | template SimpleTensor<qint16_t> pooling_layer(const SimpleTensor<qint16_t> &src, const PoolingLayerInfo &info); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 306 | } // namespace reference |
| 307 | } // namespace validation |
| 308 | } // namespace test |
| 309 | } // namespace arm_compute |