Compute Library
 19.08
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...
 
- 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 41 of file NEROIPoolingLayerKernel.cpp.

42  : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
43 {
44 }

◆ 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.

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 46 of file NEROIPoolingLayerKernel.cpp.

47 {
48  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
49 
50  //Validate arguments
51  ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), rois->info(), output->info());
53  ARM_COMPUTE_ERROR_ON(rois->info()->dimension(0) != 5);
54  ARM_COMPUTE_ERROR_ON(rois->info()->num_dimensions() > 2);
57  ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
58 
59  if(output->info()->total_size() != 0)
60  {
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));
69  auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
70 
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;
82  window.set(Window::DimX, Window::Dimension(0, rois->info()->dimension(1)));
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 
92  ARM_COMPUTE_UNUSED(update_window_and_padding(window, input_access, output_access));
93  output_access.set_valid_region(window, ValidRegion(Coordinates(), output->info()->tensor_shape()));
94  INEKernel::configure(window);
95 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
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:337
#define ARM_COMPUTE_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:68
1 channel, 1 U16 per channel
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...
Definition: Helpers.inl:201
1 channel, 1 F16 per channel
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: Helpers.h:402
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:48
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:789
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

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_UNUSED, arm_compute::auto_init_if_empty(), ITensorInfo::data_type(), ITensorInfo::dimension(), Window::DimX, Window::DimY, ValidRegion::end(), arm_compute::F16, arm_compute::F32, ITensor::info(), ITensorInfo::num_dimensions(), arm_compute::test::validation::output_shape, ROIPoolingLayerInfo::pooled_height(), ROIPoolingLayerInfo::pooled_width(), Window::set(), ValidRegion::start(), ITensorInfo::tensor_shape(), ITensorInfo::total_size(), arm_compute::U16, arm_compute::update_window_and_padding(), ITensorInfo::valid_region(), and IKernel::window().

Referenced by NEROIPoolingLayer::configure().

◆ name()

const char* name ( ) const
inlineoverridevirtual

Name of the kernel.

Returns
Kernel name

Implements ICPPKernel.

Definition at line 39 of file NEROIPoolingLayerKernel.h.

40  {
41  return "NEROIPoolingLayerKernel";
42  }

◆ operator=() [1/2]

NEROIPoolingLayerKernel& operator= ( const NEROIPoolingLayerKernel )
delete

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

◆ 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.

Implements ICPPKernel.

Definition at line 97 of file NEROIPoolingLayerKernel.cpp.

98 {
102 
103  const size_t values_per_roi = _rois->info()->dimension(0);
104 
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 
114  const auto *rois_ptr = reinterpret_cast<const uint16_t *>(_rois->buffer());
115 
116  for(int roi_indx = roi_list_start; roi_indx < roi_list_end; ++roi_indx)
117  {
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];
123 
124  // Scale ROI
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);
129 
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  {
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));
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 }
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:1291
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:160
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's metadata.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
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:1296
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:1301
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:97
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:92
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:143

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(), arm_compute::test::validation::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().


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