Compute Library
 21.02
NEROIPoolingLayerKernel Class Reference

Interface for the ROI pooling layer kernel. More...

#include <NEROIPoolingLayerKernel.h>

Collaboration diagram for NEROIPoolingLayerKernel:
[legend]

Public Member Functions

const char * name () const override
 Name of the kernel. More...
 
 NEROIPoolingLayerKernel ()
 Default constructor. More...
 
 NEROIPoolingLayerKernel (const NEROIPoolingLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NEROIPoolingLayerKerneloperator= (const NEROIPoolingLayerKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NEROIPoolingLayerKernel (NEROIPoolingLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
NEROIPoolingLayerKerneloperator= (NEROIPoolingLayerKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~NEROIPoolingLayerKernel ()=default
 Default destructor. More...
 
void configure (const ITensor *input, const ITensor *rois, ITensor *output, const ROIPoolingLayerInfo &pool_info)
 Set the input and output tensors. More...
 
void run (const Window &window, const ThreadInfo &info) override
 Execute the kernel on the passed window. More...
 
- Public Member Functions inherited from ICPPKernel
virtual ~ICPPKernel ()=default
 Default destructor. More...
 
virtual void run_nd (const Window &window, const ThreadInfo &info, const Window &thread_locator)
 legacy compatibility layer for implemantions which do not support thread_locator In these cases we simply narrow the interface down the legacy version More...
 
virtual void run_op (ITensorPack &tensors, const Window &window, const ThreadInfo &info)
 Execute the kernel on the passed window. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Detailed Description

Interface for the ROI pooling layer kernel.

Definition at line 36 of file NEROIPoolingLayerKernel.h.

Constructor & Destructor Documentation

◆ NEROIPoolingLayerKernel() [1/3]

Default constructor.

Definition at line 38 of file NEROIPoolingLayerKernel.cpp.

Referenced by NEROIPoolingLayerKernel::name().

39  : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
40 {
41 }

◆ NEROIPoolingLayerKernel() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ NEROIPoolingLayerKernel() [3/3]

Allow instances of this class to be moved.

◆ ~NEROIPoolingLayerKernel()

Default destructor.

Referenced by NEROIPoolingLayerKernel::name().

Member Function Documentation

◆ configure()

void configure ( const ITensor input,
const ITensor rois,
ITensor output,
const ROIPoolingLayerInfo pool_info 
)

Set the input and output tensors.

Parameters
[in]inputSource tensor. Data types supported: F32.
[in]roisROIs tensor, it is a 2D tensor of size [5, N] (where N is the number of ROIs) containing top left and bottom right corner as coordinate of an image and batch_id of ROI [ batch_id, x1, y1, x2, y2 ]. Data types supported: U16
[out]outputDestination tensor. Data types supported: Same as input.
[in]pool_infoContains pooling operation information described in ROIPoolingLayerInfo.
Note
The x and y dimensions of output tensor must be the same as that specified by pool_info 's pooled width and pooled height.
The z dimensions of output tensor and input tensor must be the same.
The fourth dimension of output tensor must be the same as the number of elements in rois tensor.

Definition at line 43 of file NEROIPoolingLayerKernel.cpp.

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_CPU_F16_UNSUPPORTED, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_ERROR_ON_NULLPTR, arm_compute::auto_init_if_empty(), ITensorInfo::data_type(), ITensorInfo::dimension(), Window::DimX, Window::DimY, arm_compute::F32, ITensor::info(), arm_compute::test::validation::input, ITensorInfo::num_dimensions(), arm_compute::test::validation::output_shape, ROIPoolingLayerInfo::pooled_height(), ROIPoolingLayerInfo::pooled_width(), Window::set(), Dimensions< T >::set_num_dimensions(), ITensorInfo::set_valid_region(), ITensorInfo::tensor_shape(), ITensorInfo::total_size(), arm_compute::U16, and IKernel::window().

Referenced by NEROIPoolingLayerKernel::name().

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);
51  ARM_COMPUTE_ERROR_ON(rois->info()->num_dimensions() > 2);
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 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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
1 channel, 1 U16 per channel
#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_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
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...
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
#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
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161

◆ name()

◆ operator=() [1/2]

NEROIPoolingLayerKernel& operator= ( const NEROIPoolingLayerKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

Referenced by NEROIPoolingLayerKernel::name().

◆ operator=() [2/2]

NEROIPoolingLayerKernel& operator= ( NEROIPoolingLayerKernel &&  )
default

Allow instances of this class to be moved.

◆ run()

void run ( const Window window,
const ThreadInfo info 
)
overridevirtual

Execute the kernel on the passed window.

Warning
If is_parallelisable() returns false then the passed window must be equal to window()
Note
The window has to be a region within the window returned by the window() method
The width of the window has to be a multiple of num_elems_processed_per_iteration().
Parameters
[in]windowRegion on which to execute the kernel. (Must be a region of the window returned by window())
[in]infoInfo about executing thread and CPU.

Reimplemented from ICPPKernel.

Definition at line 89 of file NEROIPoolingLayerKernel.cpp.

References ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, ITensor::buffer(), ITensorInfo::dimension(), Window::DimX, Window::DimY, Window::DimZ, Window::Dimension::end(), ITensor::info(), ROIPoolingLayerInfo::pooled_height(), ROIPoolingLayerInfo::pooled_width(), ITensor::ptr_to_element(), arm_compute::support::cpp11::round(), ROIPoolingLayerInfo::spatial_scale(), Window::Dimension::start(), IKernel::window(), and Window::x().

Referenced by NEROIPoolingLayerKernel::name().

90 {
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 }
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
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
unsigned int pooled_width() const
Get the pooled width of the layer.
Definition: Types.h:1324
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 uint8_t * buffer() const =0
Interface to be implemented by the child class to return a pointer to CPU memory. ...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
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)
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
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
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
#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

The documentation for this class was generated from the following files: