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" |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.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 | { |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 38 | using namespace arm_compute::misc::shape_calculator; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 39 | |
Isabella Gottardi | 651540f | 2018-09-13 15:33:35 +0100 | [diff] [blame] | 40 | template <typename T> |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 41 | SimpleTensor<T> pooling_layer(const SimpleTensor<T> &src, const PoolingLayerInfo &info) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 42 | { |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 43 | ARM_COMPUTE_ERROR_ON(info.is_global_pooling() && (src.shape().x() != src.shape().y())); |
| 44 | |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 45 | // Create reference |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 46 | SimpleTensor<T> dst{ compute_pool_shape(TensorInfo(src.shape(), 1, src.data_type()), info), src.data_type(), 1 }; |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 47 | |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 48 | const int pool_size_x = info.is_global_pooling() ? src.shape().x() : info.pool_size().width; |
| 49 | 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] | 50 | PoolingType type = info.pool_type(); |
| 51 | int pool_stride_x = info.pad_stride_info().stride().first; |
| 52 | int pool_stride_y = info.pad_stride_info().stride().second; |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 53 | int pad_left = info.pad_stride_info().pad_left(); |
| 54 | int pad_top = info.pad_stride_info().pad_top(); |
| 55 | int pad_right = info.pad_stride_info().pad_right(); |
| 56 | int pad_bottom = info.pad_stride_info().pad_bottom(); |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 57 | bool exclude_padding = info.exclude_padding(); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 58 | |
| 59 | const auto w_src = static_cast<int>(src.shape()[0]); |
| 60 | const auto h_src = static_cast<int>(src.shape()[1]); |
| 61 | const int upper_dims = src.shape().total_size() / (w_src * h_src); |
| 62 | |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 63 | const auto w_dst = static_cast<int>(dst.shape()[0]); |
| 64 | const auto h_dst = static_cast<int>(dst.shape()[1]); |
| 65 | |
| 66 | if(type == PoolingType::MAX) |
| 67 | { |
| 68 | for(int r = 0; r < upper_dims; ++r) |
| 69 | { |
| 70 | for(int h = 0; h < h_dst; ++h) |
| 71 | { |
| 72 | for(int w = 0; w < w_dst; ++w) |
| 73 | { |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 74 | int wstart = w * pool_stride_x - pad_left; |
| 75 | int hstart = h * pool_stride_y - pad_top; |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 76 | int wend = std::min(wstart + pool_size_x, w_src); |
| 77 | int hend = std::min(hstart + pool_size_y, h_src); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 78 | wstart = std::max(wstart, 0); |
| 79 | hstart = std::max(hstart, 0); |
| 80 | |
| 81 | T max_val = std::numeric_limits<T>::lowest(); |
| 82 | for(int y = hstart; y < hend; ++y) |
| 83 | { |
| 84 | for(int x = wstart; x < wend; ++x) |
| 85 | { |
| 86 | const T val = src[r * h_src * w_src + y * w_src + x]; |
| 87 | if(val > max_val) |
| 88 | { |
| 89 | max_val = val; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | dst[r * h_dst * w_dst + h * w_dst + w] = max_val; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 99 | else // Average or l2 pooling |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 100 | { |
| 101 | for(int r = 0; r < upper_dims; ++r) |
| 102 | { |
| 103 | for(int h = 0; h < h_dst; ++h) |
| 104 | { |
| 105 | for(int w = 0; w < w_dst; ++w) |
| 106 | { |
| 107 | T avg_val(0); |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 108 | int wstart = w * pool_stride_x - pad_left; |
| 109 | int hstart = h * pool_stride_y - pad_top; |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 110 | int wend = std::min(wstart + pool_size_x, w_src + pad_right); |
| 111 | int hend = std::min(hstart + pool_size_y, h_src + pad_bottom); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 112 | int pool = (hend - hstart) * (wend - wstart); |
| 113 | wstart = std::max(wstart, 0); |
| 114 | hstart = std::max(hstart, 0); |
| 115 | wend = std::min(wend, w_src); |
| 116 | hend = std::min(hend, h_src); |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 117 | // Exclude padding pixels from the average |
| 118 | if(exclude_padding) |
| 119 | { |
| 120 | pool = (hend - hstart) * (wend - wstart); |
| 121 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 122 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 123 | if(type == PoolingType::AVG) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 124 | { |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 125 | for(int y = hstart; y < hend; ++y) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 126 | { |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 127 | for(int x = wstart; x < wend; ++x) |
| 128 | { |
| 129 | avg_val += src[r * h_src * w_src + y * w_src + x]; |
| 130 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 131 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 132 | 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] | 133 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 134 | else |
| 135 | { |
| 136 | for(int y = hstart; y < hend; ++y) |
| 137 | { |
| 138 | for(int x = wstart; x < wend; ++x) |
| 139 | { |
| 140 | const T val = src[r * h_src * w_src + y * w_src + x]; |
| 141 | avg_val += val * val; |
| 142 | } |
| 143 | } |
| 144 | dst[r * h_dst * w_dst + h * w_dst + w] = std::sqrt(avg_val / pool); |
| 145 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return dst; |
| 152 | } |
| 153 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 154 | template <> |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 155 | 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] | 156 | { |
| 157 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 158 | SimpleTensor<float> dst_tmp = pooling_layer<float>(src_tmp, info); |
| 159 | SimpleTensor<uint8_t> dst = convert_to_asymmetric(dst_tmp, src.quantization_info()); |
| 160 | return dst; |
| 161 | } |
| 162 | |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 163 | template SimpleTensor<float> pooling_layer(const SimpleTensor<float> &src, const PoolingLayerInfo &info); |
| 164 | template SimpleTensor<half> pooling_layer(const SimpleTensor<half> &src, const PoolingLayerInfo &info); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 165 | } // namespace reference |
| 166 | } // namespace validation |
| 167 | } // namespace test |
| 168 | } // namespace arm_compute |