Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +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 "arm_compute/core/NEON/kernels/NEROIPoolingLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CPP/Validate.h" |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | #include "support/ToolchainSupport.h" |
| 35 | |
| 36 | #include <cfloat> |
| 37 | #include <cmath> |
| 38 | |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 41 | NEROIPoolingLayerKernel::NEROIPoolingLayerKernel() |
| 42 | : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f) |
| 43 | { |
| 44 | } |
| 45 | |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 46 | void NEROIPoolingLayerKernel::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info) |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 47 | { |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 49 | |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 50 | //Validate arguments |
| 51 | ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), rois->info(), output->info()); |
| 52 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::U16); |
| 53 | ARM_COMPUTE_ERROR_ON(rois->info()->dimension(0) != 5); |
| 54 | ARM_COMPUTE_ERROR_ON(rois->info()->num_dimensions() > 2); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16); |
| 57 | ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0)); |
| 58 | |
| 59 | if(output->info()->total_size() != 0) |
| 60 | { |
| 61 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 62 | ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height())); |
| 63 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2)); |
| 64 | ARM_COMPUTE_ERROR_ON(rois->info()->dimension(1) != output->info()->dimension(3)); |
| 65 | } |
| 66 | |
| 67 | // Output auto initialization if not yet initialized |
| 68 | TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->info()->dimension(1)); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 69 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 70 | |
| 71 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 72 | ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height())); |
| 73 | |
| 74 | // Set instance variables |
| 75 | _input = input; |
| 76 | _rois = rois; |
| 77 | _output = output; |
| 78 | _pool_info = pool_info; |
| 79 | |
| 80 | // Configure kernel window |
| 81 | Window window; |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 82 | window.set(Window::DimX, Window::Dimension(0, rois->info()->dimension(1))); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 83 | window.set(Window::DimY, Window::Dimension(0, 1)); |
| 84 | |
| 85 | AccessWindowStatic input_access(input->info(), |
| 86 | input->info()->valid_region().start(0), |
| 87 | input->info()->valid_region().start(1), |
| 88 | input->info()->valid_region().end(0), |
| 89 | input->info()->valid_region().end(1)); |
| 90 | AccessWindowStatic output_access(output->info(), 0, 0, pool_info.pooled_width(), pool_info.pooled_height()); |
| 91 | |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 92 | ARM_COMPUTE_UNUSED(update_window_and_padding(window, input_access, output_access)); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 93 | output_access.set_valid_region(window, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 94 | INEKernel::configure(window); |
| 95 | } |
| 96 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 97 | void NEROIPoolingLayerKernel::run(const Window &window, const ThreadInfo &info) |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 98 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 99 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 100 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 101 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 102 | |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 103 | const size_t values_per_roi = _rois->info()->dimension(0); |
| 104 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 105 | const int roi_list_start = window.x().start(); |
| 106 | const int roi_list_end = window.x().end(); |
| 107 | const int width = _input->info()->dimension(Window::DimX); |
| 108 | const int height = _input->info()->dimension(Window::DimY); |
| 109 | const int fms = _input->info()->dimension(Window::DimZ); |
| 110 | const int pooled_w = _pool_info.pooled_width(); |
| 111 | const int pooled_h = _pool_info.pooled_height(); |
| 112 | const float spatial_scale = _pool_info.spatial_scale(); |
| 113 | |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 114 | const auto *rois_ptr = reinterpret_cast<const uint16_t *>(_rois->buffer()); |
| 115 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 116 | for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx) |
| 117 | { |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 118 | const unsigned int roi_batch = rois_ptr[values_per_roi * roi_indx]; |
| 119 | const auto x1 = rois_ptr[values_per_roi * roi_indx + 1]; |
| 120 | const auto y1 = rois_ptr[values_per_roi * roi_indx + 2]; |
| 121 | const auto x2 = rois_ptr[values_per_roi * roi_indx + 3]; |
| 122 | const auto y2 = rois_ptr[values_per_roi * roi_indx + 4]; |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 123 | |
| 124 | // Scale ROI |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 125 | const int roi_anchor_x = support::cpp11::round(x1 * spatial_scale); |
| 126 | const int roi_anchor_y = support::cpp11::round(y1 * spatial_scale); |
| 127 | const int roi_width = std::max(support::cpp11::round((x2 - x1) * spatial_scale), 1.f); |
| 128 | const int roi_height = std::max(support::cpp11::round((y2 - y1) * spatial_scale), 1.f); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 129 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 130 | // Iterate through all feature maps |
| 131 | for(int fm = 0; fm < fms; ++fm) |
| 132 | { |
| 133 | // Iterate through all output pixels |
| 134 | for(int py = 0; py < pooled_h; ++py) |
| 135 | { |
| 136 | for(int px = 0; px < pooled_w; ++px) |
| 137 | { |
SiCong Li | cfb6553 | 2017-09-12 19:06:28 +0100 | [diff] [blame] | 138 | auto region_start_x = static_cast<int>(std::floor((static_cast<float>(px) / pooled_w) * roi_width)); |
| 139 | auto region_end_x = static_cast<int>(std::floor((static_cast<float>(px + 1) / pooled_w) * roi_width)); |
| 140 | auto region_start_y = static_cast<int>(std::floor((static_cast<float>(py) / pooled_h) * roi_height)); |
| 141 | auto region_end_y = static_cast<int>(std::floor((static_cast<float>(py + 1) / pooled_h) * roi_height)); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 142 | |
| 143 | region_start_x = std::min(std::max(region_start_x + roi_anchor_x, 0), width); |
| 144 | region_end_x = std::min(std::max(region_end_x + roi_anchor_x, 0), width); |
| 145 | region_start_y = std::min(std::max(region_start_y + roi_anchor_y, 0), height); |
| 146 | region_end_y = std::min(std::max(region_end_y + roi_anchor_y, 0), height); |
| 147 | |
| 148 | // Iterate through the pooling region |
| 149 | if((region_end_x <= region_start_x) || (region_end_y <= region_start_y)) |
| 150 | { |
| 151 | *reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = 0; |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | float curr_max = -FLT_MAX; |
| 156 | for(int j = region_start_y; j < region_end_y; ++j) |
| 157 | { |
| 158 | for(int i = region_start_x; i < region_end_x; ++i) |
| 159 | { |
| 160 | const auto val = *reinterpret_cast<const float *>(_input->ptr_to_element(Coordinates(i, j, fm, roi_batch))); |
| 161 | curr_max = std::max(val, curr_max); |
| 162 | } |
| 163 | } |
| 164 | *reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = curr_max; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 171 | } // namespace arm_compute |