giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
giuros01 | 1887081 | 2018-09-13 09:31:40 +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 "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 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | namespace reference |
| 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | /** Average pooling over an aligned window */ |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 43 | inline float roi_align_1x1(const float *input, TensorShape input_shape, |
| 44 | float region_start_x, |
| 45 | float bin_size_x, |
| 46 | int grid_size_x, |
| 47 | float region_end_x, |
| 48 | float region_start_y, |
| 49 | float bin_size_y, |
| 50 | int grid_size_y, |
| 51 | float region_end_y, |
| 52 | int pz) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 53 | { |
| 54 | if((region_end_x <= region_start_x) || (region_end_y <= region_start_y)) |
| 55 | { |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 56 | return 0; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 57 | } |
| 58 | else |
| 59 | { |
| 60 | float avg = 0; |
| 61 | // Iterate through the aligned pooling region |
| 62 | for(int iy = 0; iy < grid_size_y; ++iy) |
| 63 | { |
| 64 | for(int ix = 0; ix < grid_size_x; ++ix) |
| 65 | { |
| 66 | // Align the window in the middle of every bin |
| 67 | float y = region_start_y + (iy + 0.5) * bin_size_y / float(grid_size_y); |
| 68 | float x = region_start_x + (ix + 0.5) * bin_size_x / float(grid_size_x); |
| 69 | |
| 70 | // Interpolation in the [0,0] [0,1] [1,0] [1,1] square |
| 71 | const int y_low = y; |
| 72 | const int x_low = x; |
| 73 | const int y_high = y_low + 1; |
| 74 | const int x_high = x_low + 1; |
| 75 | |
| 76 | const float ly = y - y_low; |
| 77 | const float lx = x - x_low; |
| 78 | const float hy = 1. - ly; |
| 79 | const float hx = 1. - lx; |
| 80 | |
| 81 | const float w1 = hy * hx; |
| 82 | const float w2 = hy * lx; |
| 83 | const float w3 = ly * hx; |
| 84 | const float w4 = ly * lx; |
| 85 | |
| 86 | const size_t idx1 = coord2index(input_shape, Coordinates(x_low, y_low, pz)); |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 87 | float data1 = input[idx1]; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 88 | |
| 89 | const size_t idx2 = coord2index(input_shape, Coordinates(x_high, y_low, pz)); |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 90 | float data2 = input[idx2]; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 91 | |
| 92 | const size_t idx3 = coord2index(input_shape, Coordinates(x_low, y_high, pz)); |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 93 | float data3 = input[idx3]; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 94 | |
| 95 | const size_t idx4 = coord2index(input_shape, Coordinates(x_high, y_high, pz)); |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 96 | float data4 = input[idx4]; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 97 | |
| 98 | avg += w1 * data1 + w2 * data2 + w3 * data3 + w4 * data4; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | avg /= grid_size_x * grid_size_y; |
| 103 | |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 104 | return avg; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 108 | template <typename TI, typename TO> |
| 109 | SimpleTensor<TO> float_converter(const SimpleTensor<TI> &tensor, DataType dst_dt) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 110 | { |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 111 | SimpleTensor<TO> dst{ tensor.shape(), dst_dt, 1, QuantizationInfo(), tensor.data_layout() }; |
| 112 | #if defined(_OPENMP) |
| 113 | #pragma omp parallel for |
| 114 | #endif /* _OPENMP */ |
| 115 | for(int i = 0; i < tensor.num_elements(); ++i) |
| 116 | { |
| 117 | dst[i] = tensor[i]; |
| 118 | } |
| 119 | return dst; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 120 | } |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 121 | |
| 122 | SimpleTensor<float> convert_rois_from_asymmetric(SimpleTensor<uint16_t> rois) |
| 123 | { |
| 124 | const UniformQuantizationInfo &quantization_info = rois.quantization_info().uniform(); |
| 125 | SimpleTensor<float> dst{ rois.shape(), DataType::F32, 1, QuantizationInfo(), rois.data_layout() }; |
| 126 | |
| 127 | for(int i = 0; i < rois.num_elements(); i += 5) |
| 128 | { |
| 129 | dst[i] = static_cast<float>(rois[i]); // batch idx |
| 130 | dst[i + 1] = dequantize_qasymm16(rois[i + 1], quantization_info); |
| 131 | dst[i + 2] = dequantize_qasymm16(rois[i + 2], quantization_info); |
| 132 | dst[i + 3] = dequantize_qasymm16(rois[i + 3], quantization_info); |
| 133 | dst[i + 4] = dequantize_qasymm16(rois[i + 4], quantization_info); |
| 134 | } |
| 135 | return dst; |
| 136 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 137 | } // namespace |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 138 | |
| 139 | template <> |
| 140 | SimpleTensor<float> roi_align_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 141 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 142 | ARM_COMPUTE_UNUSED(output_qinfo); |
| 143 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 144 | const size_t values_per_roi = rois.shape()[0]; |
| 145 | const size_t num_rois = rois.shape()[1]; |
| 146 | DataType dst_data_type = src.data_type(); |
| 147 | |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 148 | const auto *rois_ptr = static_cast<const float *>(rois.data()); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 149 | |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 150 | TensorShape input_shape = src.shape(); |
| 151 | TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), src.shape()[2], num_rois); |
| 152 | SimpleTensor<float> dst(output_shape, dst_data_type); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 153 | |
| 154 | // Iterate over every pixel of the input image |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 155 | for(size_t px = 0; px < pool_info.pooled_width(); ++px) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 156 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 157 | for(size_t py = 0; py < pool_info.pooled_height(); ++py) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 158 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 159 | for(size_t pw = 0; pw < num_rois; ++pw) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 160 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 161 | const unsigned int roi_batch = rois_ptr[values_per_roi * pw]; |
| 162 | const auto x1 = float(rois_ptr[values_per_roi * pw + 1]); |
| 163 | const auto y1 = float(rois_ptr[values_per_roi * pw + 2]); |
| 164 | const auto x2 = float(rois_ptr[values_per_roi * pw + 3]); |
| 165 | const auto y2 = float(rois_ptr[values_per_roi * pw + 4]); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 166 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 167 | const float roi_anchor_x = x1 * pool_info.spatial_scale(); |
| 168 | const float roi_anchor_y = y1 * pool_info.spatial_scale(); |
| 169 | const float roi_dims_x = std::max((x2 - x1) * pool_info.spatial_scale(), 1.0f); |
| 170 | const float roi_dims_y = std::max((y2 - y1) * pool_info.spatial_scale(), 1.0f); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 171 | |
| 172 | float bin_size_x = roi_dims_x / pool_info.pooled_width(); |
| 173 | float bin_size_y = roi_dims_y / pool_info.pooled_height(); |
| 174 | float region_start_x = px * bin_size_x + roi_anchor_x; |
| 175 | float region_start_y = py * bin_size_y + roi_anchor_y; |
| 176 | float region_end_x = (px + 1) * bin_size_x + roi_anchor_x; |
| 177 | float region_end_y = (py + 1) * bin_size_y + roi_anchor_y; |
| 178 | |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 179 | region_start_x = utility::clamp(region_start_x, 0.0f, float(input_shape[0])); |
| 180 | region_start_y = utility::clamp(region_start_y, 0.0f, float(input_shape[1])); |
| 181 | region_end_x = utility::clamp(region_end_x, 0.0f, float(input_shape[0])); |
| 182 | region_end_y = utility::clamp(region_end_y, 0.0f, float(input_shape[1])); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 183 | |
| 184 | const int roi_bin_grid_x = (pool_info.sampling_ratio() > 0) ? pool_info.sampling_ratio() : int(ceil(bin_size_x)); |
| 185 | const int roi_bin_grid_y = (pool_info.sampling_ratio() > 0) ? pool_info.sampling_ratio() : int(ceil(bin_size_y)); |
| 186 | |
| 187 | // Move input and output pointer across the fourth dimension |
| 188 | const size_t input_stride_w = input_shape[0] * input_shape[1] * input_shape[2]; |
| 189 | const size_t output_stride_w = output_shape[0] * output_shape[1] * output_shape[2]; |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 190 | const float *input_ptr = src.data() + roi_batch * input_stride_w; |
| 191 | float *output_ptr = dst.data() + px + py * output_shape[0] + pw * output_stride_w; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 192 | |
| 193 | for(int pz = 0; pz < int(input_shape[2]); ++pz) |
| 194 | { |
| 195 | // For every pixel pool over an aligned region |
| 196 | *(output_ptr + pz * output_shape[0] * output_shape[1]) = roi_align_1x1(input_ptr, input_shape, |
| 197 | region_start_x, |
| 198 | bin_size_x, |
| 199 | roi_bin_grid_x, |
| 200 | region_end_x, |
| 201 | region_start_y, |
| 202 | bin_size_y, |
| 203 | roi_bin_grid_y, |
| 204 | region_end_y, pz); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | return dst; |
| 210 | } |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 211 | |
Georgios Pinitas | 1643a45 | 2020-09-22 02:24:25 +0100 | [diff] [blame] | 212 | template <> |
| 213 | SimpleTensor<half> roi_align_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo) |
| 214 | { |
| 215 | SimpleTensor<float> src_tmp = float_converter<half, float>(src, DataType::F32); |
| 216 | SimpleTensor<float> rois_tmp = float_converter<half, float>(rois, DataType::F32); |
| 217 | SimpleTensor<float> dst_tmp = roi_align_layer<float, float>(src_tmp, rois_tmp, pool_info, output_qinfo); |
| 218 | SimpleTensor<half> dst = float_converter<float, half>(dst_tmp, DataType::F16); |
| 219 | return dst; |
| 220 | } |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 221 | |
| 222 | template <> |
| 223 | SimpleTensor<uint8_t> roi_align_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint16_t> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo) |
| 224 | { |
| 225 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 226 | SimpleTensor<float> rois_tmp = convert_rois_from_asymmetric(rois); |
| 227 | SimpleTensor<float> dst_tmp = roi_align_layer<float, float>(src_tmp, rois_tmp, pool_info, output_qinfo); |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 228 | SimpleTensor<uint8_t> dst = convert_to_asymmetric<uint8_t>(dst_tmp, output_qinfo); |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 229 | return dst; |
| 230 | } |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 231 | template <> |
| 232 | SimpleTensor<int8_t> roi_align_layer(const SimpleTensor<int8_t> &src, const SimpleTensor<uint16_t> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo) |
| 233 | { |
| 234 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 235 | SimpleTensor<float> rois_tmp = convert_rois_from_asymmetric(rois); |
| 236 | SimpleTensor<float> dst_tmp = roi_align_layer<float, float>(src_tmp, rois_tmp, pool_info, output_qinfo); |
| 237 | SimpleTensor<int8_t> dst = convert_to_asymmetric<int8_t>(dst_tmp, output_qinfo); |
| 238 | return dst; |
| 239 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 240 | } // namespace reference |
| 241 | } // namespace validation |
| 242 | } // namespace test |
| 243 | } // namespace arm_compute |