Compute Library
 21.02
NEROIPoolingLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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  */
25 
29 #include "src/core/CPP/Validate.h"
33 
34 #include <cfloat>
35 
36 namespace arm_compute
37 {
39  : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
40 {
41 }
42 
43 void NEROIPoolingLayerKernel::configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info)
44 {
45  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
46 
47  //Validate arguments
48  ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), rois->info(), output->info());
50  ARM_COMPUTE_ERROR_ON(rois->info()->dimension(0) != 5);
54  ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
55 
56  if(output->info()->total_size() != 0)
57  {
59  ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height()));
60  ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2));
61  ARM_COMPUTE_ERROR_ON(rois->info()->dimension(1) != output->info()->dimension(3));
62  }
63 
64  // Output auto initialization if not yet initialized
65  TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->info()->dimension(1));
66  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
67 
69  ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height()));
70 
71  // Set instance variables
72  _input = input;
73  _rois = rois;
74  _output = output;
75  _pool_info = pool_info;
76 
77  // Configure kernel window
78  Window window;
79  window.set(Window::DimX, Window::Dimension(0, rois->info()->dimension(1)));
80  window.set(Window::DimY, Window::Dimension(0, 1));
81 
82  Coordinates coord;
83  coord.set_num_dimensions(output->info()->num_dimensions());
84  output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
85 
86  INEKernel::configure(window);
87 }
88 
90 {
91  ARM_COMPUTE_UNUSED(info);
94 
95  const size_t values_per_roi = _rois->info()->dimension(0);
96 
97  const int roi_list_start = window.x().start();
98  const int roi_list_end = window.x().end();
99  const int width = _input->info()->dimension(Window::DimX);
100  const int height = _input->info()->dimension(Window::DimY);
101  const int fms = _input->info()->dimension(Window::DimZ);
102  const int pooled_w = _pool_info.pooled_width();
103  const int pooled_h = _pool_info.pooled_height();
104  const float spatial_scale = _pool_info.spatial_scale();
105 
106  const auto *rois_ptr = reinterpret_cast<const uint16_t *>(_rois->buffer());
107 
108  for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx)
109  {
110  const unsigned int roi_batch = rois_ptr[values_per_roi * roi_indx];
111  const auto x1 = rois_ptr[values_per_roi * roi_indx + 1];
112  const auto y1 = rois_ptr[values_per_roi * roi_indx + 2];
113  const auto x2 = rois_ptr[values_per_roi * roi_indx + 3];
114  const auto y2 = rois_ptr[values_per_roi * roi_indx + 4];
115 
116  // Scale ROI
117  const int roi_anchor_x = support::cpp11::round(x1 * spatial_scale);
118  const int roi_anchor_y = support::cpp11::round(y1 * spatial_scale);
119  const int roi_width = std::max(support::cpp11::round((x2 - x1) * spatial_scale), 1.f);
120  const int roi_height = std::max(support::cpp11::round((y2 - y1) * spatial_scale), 1.f);
121 
122  // Iterate through all feature maps
123  for(int fm = 0; fm < fms; ++fm)
124  {
125  // Iterate through all output pixels
126  for(int py = 0; py < pooled_h; ++py)
127  {
128  for(int px = 0; px < pooled_w; ++px)
129  {
130  auto region_start_x = static_cast<int>(std::floor((static_cast<float>(px) / pooled_w) * roi_width));
131  auto region_end_x = static_cast<int>(std::floor((static_cast<float>(px + 1) / pooled_w) * roi_width));
132  auto region_start_y = static_cast<int>(std::floor((static_cast<float>(py) / pooled_h) * roi_height));
133  auto region_end_y = static_cast<int>(std::floor((static_cast<float>(py + 1) / pooled_h) * roi_height));
134 
135  region_start_x = std::min(std::max(region_start_x + roi_anchor_x, 0), width);
136  region_end_x = std::min(std::max(region_end_x + roi_anchor_x, 0), width);
137  region_start_y = std::min(std::max(region_start_y + roi_anchor_y, 0), height);
138  region_end_y = std::min(std::max(region_end_y + roi_anchor_y, 0), height);
139 
140  // Iterate through the pooling region
141  if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
142  {
143  *reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = 0;
144  }
145  else
146  {
147  float curr_max = -FLT_MAX;
148  for(int j = region_start_y; j < region_end_y; ++j)
149  {
150  for(int i = region_start_x; i < region_end_x; ++i)
151  {
152  const auto val = *reinterpret_cast<const float *>(_input->ptr_to_element(Coordinates(i, j, fm, roi_batch)));
153  curr_max = std::max(val, curr_max);
154  }
155  }
156  *reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(px, py, fm, roi_indx))) = curr_max;
157  }
158  }
159  }
160  }
161  }
162 }
163 } // namespace arm_compute
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
Shape of a tensor.
Definition: TensorShape.h:39
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
1 channel, 1 U16 per channel
unsigned int pooled_width() const
Get the pooled width of the layer.
Definition: Types.h:1324
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
#define ARM_COMPUTE_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:105
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
Coordinates of an item.
Definition: Coordinates.h:37
virtual uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory. ...
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void configure(const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info)
Set the input and output tensors.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
unsigned int pooled_height() const
Get the pooled height of the layer.
Definition: Types.h:1329
ROI Pooling Layer Information class.
Definition: Types.h:1309
T round(T value)
Round floating-point value with half value rounding away from zero.
float spatial_scale() const
Get the spatial scale.
Definition: Types.h:1334
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
Container for valid region of a window.
Definition: Types.h:188
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145