Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
| 33 | #include "support/ToolchainSupport.h" |
| 34 | |
| 35 | #include <cfloat> |
| 36 | #include <cmath> |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | NEROIPoolingLayerKernel::NEROIPoolingLayerKernel() |
| 41 | : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void NEROIPoolingLayerKernel::configure(const ITensor *input, const IROIArray *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info) |
| 46 | { |
| 47 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, rois, output); |
| 48 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 49 | ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0)); |
| 50 | ARM_COMPUTE_ERROR_ON(rois->num_values() == 0); |
| 51 | |
| 52 | // Output auto inizialitation if not yet initialized |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 53 | TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->num_values()); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 54 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 55 | |
| 56 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 57 | ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height())); |
| 58 | |
| 59 | // Set instance variables |
| 60 | _input = input; |
| 61 | _rois = rois; |
| 62 | _output = output; |
| 63 | _pool_info = pool_info; |
| 64 | |
| 65 | // Configure kernel window |
| 66 | Window window; |
| 67 | window.set(Window::DimX, Window::Dimension(0, rois->num_values())); |
| 68 | window.set(Window::DimY, Window::Dimension(0, 1)); |
| 69 | |
| 70 | AccessWindowStatic input_access(input->info(), |
| 71 | input->info()->valid_region().start(0), |
| 72 | input->info()->valid_region().start(1), |
| 73 | input->info()->valid_region().end(0), |
| 74 | input->info()->valid_region().end(1)); |
| 75 | AccessWindowStatic output_access(output->info(), 0, 0, pool_info.pooled_width(), pool_info.pooled_height()); |
| 76 | |
| 77 | update_window_and_padding(window, input_access, output_access); |
| 78 | output_access.set_valid_region(window, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 79 | INEKernel::configure(window); |
| 80 | } |
| 81 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 82 | void NEROIPoolingLayerKernel::run(const Window &window, const ThreadInfo &info) |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 83 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 84 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 86 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 87 | |
| 88 | const int roi_list_start = window.x().start(); |
| 89 | const int roi_list_end = window.x().end(); |
| 90 | const int width = _input->info()->dimension(Window::DimX); |
| 91 | const int height = _input->info()->dimension(Window::DimY); |
| 92 | const int fms = _input->info()->dimension(Window::DimZ); |
| 93 | const int pooled_w = _pool_info.pooled_width(); |
| 94 | const int pooled_h = _pool_info.pooled_height(); |
| 95 | const float spatial_scale = _pool_info.spatial_scale(); |
| 96 | |
| 97 | for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx) |
| 98 | { |
| 99 | const ROI &curr_roi = _rois->at(roi_indx); |
| 100 | |
| 101 | // Scale ROI |
| 102 | const int roi_batch = curr_roi.batch_idx; |
| 103 | const int roi_anchor_x = support::cpp11::round(curr_roi.rect.x * spatial_scale); |
| 104 | const int roi_anchor_y = support::cpp11::round(curr_roi.rect.y * spatial_scale); |
| 105 | const int roi_width = std::max(support::cpp11::round(curr_roi.rect.width * spatial_scale), 1.f); |
| 106 | const int roi_height = std::max(support::cpp11::round(curr_roi.rect.height * spatial_scale), 1.f); |
| 107 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 108 | // Iterate through all feature maps |
| 109 | for(int fm = 0; fm < fms; ++fm) |
| 110 | { |
| 111 | // Iterate through all output pixels |
| 112 | for(int py = 0; py < pooled_h; ++py) |
| 113 | { |
| 114 | for(int px = 0; px < pooled_w; ++px) |
| 115 | { |
SiCong Li | cfb6553 | 2017-09-12 19:06:28 +0100 | [diff] [blame] | 116 | auto region_start_x = static_cast<int>(std::floor((static_cast<float>(px) / pooled_w) * roi_width)); |
| 117 | auto region_end_x = static_cast<int>(std::floor((static_cast<float>(px + 1) / pooled_w) * roi_width)); |
| 118 | auto region_start_y = static_cast<int>(std::floor((static_cast<float>(py) / pooled_h) * roi_height)); |
| 119 | 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] | 120 | |
| 121 | region_start_x = std::min(std::max(region_start_x + roi_anchor_x, 0), width); |
| 122 | region_end_x = std::min(std::max(region_end_x + roi_anchor_x, 0), width); |
| 123 | region_start_y = std::min(std::max(region_start_y + roi_anchor_y, 0), height); |
| 124 | region_end_y = std::min(std::max(region_end_y + roi_anchor_y, 0), height); |
| 125 | |
| 126 | // Iterate through the pooling region |
| 127 | if((region_end_x <= region_start_x) || (region_end_y <= region_start_y)) |
| 128 | { |
| 129 | *reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = 0; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | float curr_max = -FLT_MAX; |
| 134 | for(int j = region_start_y; j < region_end_y; ++j) |
| 135 | { |
| 136 | for(int i = region_start_x; i < region_end_x; ++i) |
| 137 | { |
| 138 | const auto val = *reinterpret_cast<const float *>(_input->ptr_to_element(Coordinates(i, j, fm, roi_batch))); |
| 139 | curr_max = std::max(val, curr_max); |
| 140 | } |
| 141 | } |
| 142 | *reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = curr_max; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |