blob: 6a960c74dc0e7c9b48f3bd2e7ccbed6979b2d7fc [file] [log] [blame]
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01003 *
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
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000026#include "arm_compute/core/CPP/Validate.h"
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010027#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/core/Window.h"
30#include "support/ToolchainSupport.h"
31
32#include <cfloat>
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010033
Manuel Bottinicc5171b2019-01-09 17:04:39 +000034namespace arm_compute
35{
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010036NEROIPoolingLayerKernel::NEROIPoolingLayerKernel()
37 : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
38{
39}
40
Manuel Bottinicc5171b2019-01-09 17:04:39 +000041void NEROIPoolingLayerKernel::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info)
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010042{
Manuel Bottinicc5171b2019-01-09 17:04:39 +000043 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010044
Manuel Bottinicc5171b2019-01-09 17:04:39 +000045 //Validate arguments
46 ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), rois->info(), output->info());
47 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::U16);
48 ARM_COMPUTE_ERROR_ON(rois->info()->dimension(0) != 5);
49 ARM_COMPUTE_ERROR_ON(rois->info()->num_dimensions() > 2);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000050 ARM_COMPUTE_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +010051 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
Manuel Bottinicc5171b2019-01-09 17:04:39 +000052 ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
53
54 if(output->info()->total_size() != 0)
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 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2));
59 ARM_COMPUTE_ERROR_ON(rois->info()->dimension(1) != output->info()->dimension(3));
60 }
61
62 // Output auto initialization if not yet initialized
63 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->info()->dimension(1));
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010064 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010065
66 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
67 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height()));
68
69 // Set instance variables
70 _input = input;
71 _rois = rois;
72 _output = output;
73 _pool_info = pool_info;
74
75 // Configure kernel window
76 Window window;
Manuel Bottinicc5171b2019-01-09 17:04:39 +000077 window.set(Window::DimX, Window::Dimension(0, rois->info()->dimension(1)));
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010078 window.set(Window::DimY, Window::Dimension(0, 1));
79
Manuel Bottini16bd6dd2020-06-01 14:40:54 +010080 Coordinates coord;
81 coord.set_num_dimensions(output->info()->num_dimensions());
82 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010083
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010084 INEKernel::configure(window);
85}
86
Moritz Pflanzerc186b572017-09-07 09:48:04 +010087void NEROIPoolingLayerKernel::run(const Window &window, const ThreadInfo &info)
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010088{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010089 ARM_COMPUTE_UNUSED(info);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010090 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
91 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
92
Manuel Bottinicc5171b2019-01-09 17:04:39 +000093 const size_t values_per_roi = _rois->info()->dimension(0);
94
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010095 const int roi_list_start = window.x().start();
96 const int roi_list_end = window.x().end();
97 const int width = _input->info()->dimension(Window::DimX);
98 const int height = _input->info()->dimension(Window::DimY);
99 const int fms = _input->info()->dimension(Window::DimZ);
100 const int pooled_w = _pool_info.pooled_width();
101 const int pooled_h = _pool_info.pooled_height();
102 const float spatial_scale = _pool_info.spatial_scale();
103
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000104 const auto *rois_ptr = reinterpret_cast<const uint16_t *>(_rois->buffer());
105
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100106 for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx)
107 {
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000108 const unsigned int roi_batch = rois_ptr[values_per_roi * roi_indx];
109 const auto x1 = rois_ptr[values_per_roi * roi_indx + 1];
110 const auto y1 = rois_ptr[values_per_roi * roi_indx + 2];
111 const auto x2 = rois_ptr[values_per_roi * roi_indx + 3];
112 const auto y2 = rois_ptr[values_per_roi * roi_indx + 4];
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100113
114 // Scale ROI
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000115 const int roi_anchor_x = support::cpp11::round(x1 * spatial_scale);
116 const int roi_anchor_y = support::cpp11::round(y1 * spatial_scale);
117 const int roi_width = std::max(support::cpp11::round((x2 - x1) * spatial_scale), 1.f);
118 const int roi_height = std::max(support::cpp11::round((y2 - y1) * spatial_scale), 1.f);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100119
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100120 // Iterate through all feature maps
121 for(int fm = 0; fm < fms; ++fm)
122 {
123 // Iterate through all output pixels
124 for(int py = 0; py < pooled_h; ++py)
125 {
126 for(int px = 0; px < pooled_w; ++px)
127 {
SiCong Licfb65532017-09-12 19:06:28 +0100128 auto region_start_x = static_cast<int>(std::floor((static_cast<float>(px) / pooled_w) * roi_width));
129 auto region_end_x = static_cast<int>(std::floor((static_cast<float>(px + 1) / pooled_w) * roi_width));
130 auto region_start_y = static_cast<int>(std::floor((static_cast<float>(py) / pooled_h) * roi_height));
131 auto region_end_y = static_cast<int>(std::floor((static_cast<float>(py + 1) / pooled_h) * roi_height));
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100132
133 region_start_x = std::min(std::max(region_start_x + roi_anchor_x, 0), width);
134 region_end_x = std::min(std::max(region_end_x + roi_anchor_x, 0), width);
135 region_start_y = std::min(std::max(region_start_y + roi_anchor_y, 0), height);
136 region_end_y = std::min(std::max(region_end_y + roi_anchor_y, 0), height);
137
138 // Iterate through the pooling region
139 if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
140 {
141 *reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = 0;
142 }
143 else
144 {
145 float curr_max = -FLT_MAX;
146 for(int j = region_start_y; j < region_end_y; ++j)
147 {
148 for(int i = region_start_x; i < region_end_x; ++i)
149 {
150 const auto val = *reinterpret_cast<const float *>(_input->ptr_to_element(Coordinates(i, j, fm, roi_batch)));
151 curr_max = std::max(val, curr_max);
152 }
153 }
154 *reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = curr_max;
155 }
156 }
157 }
158 }
159 }
160}
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000161} // namespace arm_compute