Compute Library
 22.11
NECropKernel Class Reference

Interface for the kernel to perform tensor cropping. More...

#include <NECropKernel.h>

Collaboration diagram for NECropKernel:
[legend]

Public Types

using InBoundsCropFunction = void(const ITensor *, const ITensor *, float *, Coordinates, int32_t, int32_t, int32_t, bool, bool)
 Function to use for in bounds crop for the particular tensor types passed to configure() More...
 

Public Member Functions

const char * name () const override
 Name of the kernel. More...
 
 NECropKernel ()
 Default constructor. More...
 
 NECropKernel (const NECropKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NECropKerneloperator= (const NECropKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NECropKernel (NECropKernel &&)=default
 Allow instances of this class to be moved. More...
 
NECropKerneloperator= (NECropKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~NECropKernel ()=default
 Default destructor. More...
 
void configure (const ITensor *input, const ITensor *crop_boxes, const ITensor *box_ind, ITensor *output, uint32_t crop_box_ind=0, float extrapolation_value=0)
 Configure kernel. More...
 
void configure_output_shape ()
 Configure output tensor's shape as this can only be determined at runtime. 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...
 
virtual size_t get_mws (const CPUInfo &platform, size_t thread_count) const
 Return minimum workload size of the relevant kernel. 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...
 
bool is_window_configured () const
 Function to check if the embedded window of this kernel has been configured. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *crop_boxes, const ITensorInfo *box_ind, const ITensorInfo *output, uint32_t crop_box_ind=0, float extrapolation_value=0)
 Static function to check if given info will lead to a valid configuration of CLStridedSliceKernel. More...
 

Additional Inherited Members

- Static Public Attributes inherited from ICPPKernel
static constexpr size_t default_mws = 1
 

Detailed Description

Interface for the kernel to perform tensor cropping.

Definition at line 37 of file NECropKernel.h.

Member Typedef Documentation

◆ InBoundsCropFunction

using InBoundsCropFunction = void(const ITensor *, const ITensor *, float *, Coordinates, int32_t, int32_t, int32_t, bool, bool)

Function to use for in bounds crop for the particular tensor types passed to configure()

Definition at line 94 of file NECropKernel.h.

Constructor & Destructor Documentation

◆ NECropKernel() [1/3]

Default constructor.

Definition at line 192 of file NECropKernel.cpp.

Referenced by NECropKernel::name().

193  : _input(nullptr), _crop_boxes(nullptr), _box_ind(nullptr), _output(nullptr), _start(), _end(), _crop_box_ind(0), _extrapolation_value(0), _rows_out_of_bounds(), _cols_out_of_bounds()
194 {
195 }

◆ NECropKernel() [2/3]

NECropKernel ( const NECropKernel )
delete

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

◆ NECropKernel() [3/3]

NECropKernel ( NECropKernel &&  )
default

Allow instances of this class to be moved.

◆ ~NECropKernel()

~NECropKernel ( )
default

Default destructor.

Referenced by NECropKernel::name().

Member Function Documentation

◆ configure()

void configure ( const ITensor input,
const ITensor crop_boxes,
const ITensor box_ind,
ITensor output,
uint32_t  crop_box_ind = 0,
float  extrapolation_value = 0 
)

Configure kernel.

Note
Supported tensor rank: up to 4
Padding not supported.
Parameters
[in]inputSource tensor. Data type supported: U8/U16/S16/U32/S32/F16/F32. Data layouts supported: NHWC.
[in]crop_boxesTensor containing all possible boxes used to crop the image, each represented by 4 normalized values. Data type supported: F32
[in]box_indOne dimensional tensor mapping the crop_box_ind to the index of the 3D image in input. Data type supported: F32
[out]outputDestination tensor. Data type supported: F32
[in]crop_box_indIndex of the crop box to be used from crop_boxes. Default is 0.
[in]extrapolation_valueValue to be used for values outside of the image. Default is 0.

Definition at line 197 of file NECropKernel.cpp.

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, ITensor::info(), arm_compute::test::validation::input, and NECropKernel::validate().

Referenced by NECropKernel::name().

198 {
200  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), crop_boxes->info(), box_ind->info(), output->info(), crop_box_ind, extrapolation_value));
201 
202  _input = input;
203  _crop_boxes = crop_boxes;
204  _box_ind = box_ind;
205  _output = output;
206  _crop_box_ind = crop_box_ind;
207  _extrapolation_value = extrapolation_value;
208 }
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
static Status validate(const ITensorInfo *input, const ITensorInfo *crop_boxes, const ITensorInfo *box_ind, const ITensorInfo *output, uint32_t crop_box_ind=0, float extrapolation_value=0)
Static function to check if given info will lead to a valid configuration of CLStridedSliceKernel.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157

◆ configure_output_shape()

void configure_output_shape ( )

Configure output tensor's shape as this can only be determined at runtime.

Definition at line 234 of file NECropKernel.cpp.

References arm_compute::calculate_max_window(), ITensorInfo::dimension(), ITensor::info(), ITensor::ptr_to_element(), ITensorInfo::set_tensor_shape(), and ITensorInfo::tensor_shape().

Referenced by NECropKernel::name().

235 {
236  // _crop_box_ind is used to index _crop_boxes and retrieve the appropriate crop box.
237  // The crop box is specified by normalized coordinates [y0, x0, y1, x1].
238  const float x0 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(1, _crop_box_ind)));
239  const float y0 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(0, _crop_box_ind)));
240  const float x1 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(3, _crop_box_ind)));
241  const float y1 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(2, _crop_box_ind)));
242  // The normalized coordiantes are scaled to retrieve the floating point image coordinates which are rounded to integers.
243  _start = Coordinates(std::floor(x0 * (_input->info()->tensor_shape()[1] - 1) + 0.5f),
244  std::floor(y0 * (_input->info()->tensor_shape()[2] - 1) + 0.5f));
245  _end = Coordinates(std::floor(x1 * (_input->info()->tensor_shape()[1] - 1) + 0.5f),
246  std::floor(y1 * (_input->info()->tensor_shape()[2] - 1) + 0.5f));
247  const TensorShape out_shape(_input->info()->tensor_shape()[0], abs(_end[0] - _start[0]) + 1, abs(_end[1] - _start[1]) + 1);
248  _output->info()->set_tensor_shape(out_shape);
249 
250  bool is_width_flipped = _end[0] < _start[0];
251  bool is_height_flipped = _end[1] < _start[1];
252  if(is_height_flipped)
253  {
254  _rows_out_of_bounds[0] = _start[1] >= static_cast<int32_t>(_input->info()->dimension(2)) ? std::min(static_cast<uint32_t>(_start[1] - _input->info()->dimension(2) + 1),
255  static_cast<uint32_t>(_output->info()->dimension(2))) :
256  0;
257  _rows_out_of_bounds[1] = _end[1] < 0 ? std::min(static_cast<uint32_t>(-_end[1]),
258  static_cast<uint32_t>(_output->info()->dimension(2))) :
259  0;
260  }
261  else
262  {
263  _rows_out_of_bounds[0] = _start[1] < 0 ? std::min(static_cast<uint32_t>(-_start[1]),
264  static_cast<uint32_t>(_output->info()->dimension(2))) :
265  0;
266  _rows_out_of_bounds[1] = _end[1] >= static_cast<int32_t>(_input->info()->dimension(2)) ? std::min(static_cast<uint32_t>(_end[1] - _input->info()->dimension(2) + 1),
267  static_cast<uint32_t>(_output->info()->dimension(2))) :
268  0;
269  }
270  if(is_width_flipped)
271  {
272  _cols_out_of_bounds[0] = _start[0] >= static_cast<int32_t>(_input->info()->dimension(1)) ? std::min(static_cast<uint32_t>(_start[0] - _input->info()->dimension(1) + 1),
273  static_cast<uint32_t>(_output->info()->dimension(1))) :
274  0;
275  _cols_out_of_bounds[1] = _end[0] < 0 ? std::min(static_cast<uint32_t>(-_end[0]),
276  static_cast<uint32_t>(_output->info()->dimension(1))) :
277  0;
278  }
279  else
280  {
281  _cols_out_of_bounds[0] = _start[0] < 0 ? std::min(static_cast<uint32_t>(-_start[0]),
282  static_cast<uint32_t>(_output->info()->dimension(1))) :
283  0;
284  _cols_out_of_bounds[1] = _end[0] >= static_cast<int32_t>(_input->info()->dimension(1)) ? std::min(static_cast<uint32_t>(_end[0] - _input->info()->dimension(1) + 1),
285  static_cast<uint32_t>(_output->info()->dimension(1))) :
286  0;
287  }
288 
289  INEKernel::configure(calculate_max_window(*_output->info()));
290 }
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
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.
virtual ITensorInfo & set_tensor_shape(const TensorShape &shape)=0
Set the shape of an already initialized tensor.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.

◆ name()

◆ operator=() [1/2]

NECropKernel& operator= ( const NECropKernel )
delete

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

Referenced by NECropKernel::name().

◆ operator=() [2/2]

NECropKernel& operator= ( NECropKernel &&  )
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 292 of file NECropKernel.cpp.

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, ARM_COMPUTE_UNUSED, ITensorInfo::data_type(), ITensorInfo::dimension(), ITensorInfo::has_padding(), ITensor::info(), ITensor::ptr_to_element(), and IKernel::window().

Referenced by NECropKernel::name().

293 {
297 
298  ARM_COMPUTE_ERROR_ON(_input->info()->has_padding());
299  ARM_COMPUTE_ERROR_ON(_output->info()->has_padding());
300 
301  const auto *uk = get_implementation(CropSelectorData{ _input->info()->data_type() });
302 
303  uint32_t batch_index = *(reinterpret_cast<int32_t *>(_box_ind->ptr_to_element(Coordinates(_crop_box_ind))));
304  Coordinates input_offset(0, _end[0] < _start[0] ? _start[0] - _cols_out_of_bounds[0] : _start[0] + _cols_out_of_bounds[0],
305  _end[1] < _start[1] ? _start[1] - _rows_out_of_bounds[0] : _start[1] + _rows_out_of_bounds[0], batch_index);
306  execute_window(_input, _output, input_offset, _extrapolation_value, _rows_out_of_bounds, _cols_out_of_bounds, uk->ukernel, _end[1] < _start[1],
307  _cols_out_of_bounds[0] + _cols_out_of_bounds[1] < _output->info()->dimension(1), _cols_out_of_bounds[0] > 0, _cols_out_of_bounds[1] > 0,
308  _start[0] <= _end[0], _end[0] < _start[0]);
309 }
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.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
#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
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
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:915
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
virtual bool has_padding() const =0
Checks if the tensor has been allocated with padding or not.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo crop_boxes,
const ITensorInfo box_ind,
const ITensorInfo output,
uint32_t  crop_box_ind = 0,
float  extrapolation_value = 0 
)
static

Static function to check if given info will lead to a valid configuration of CLStridedSliceKernel.

Note
Supported tensor rank: up to 4
Padding not supported.
Parameters
[in]inputSource tensor info. Data type supported: U8/U16/S16/U32/S32/F16/F32. Data layouts supported: NHWC.
[in]crop_boxesTensor info for tensor containing all possible boxes used to crop the image. Data type supported: F32
[in]box_indTensor info for the one dimensional tensor mapping the crop_box_ind to the index of the 3D image in input. Data type supported: F32
[in]outputDestination tensor. Data type supported: F32
[in]crop_box_indIndex of the crop box to be used from crop_boxes. Default is 0.
[in]extrapolation_valueValue to be used for values outside of the image. Default is 0.

Definition at line 210 of file NECropKernel.cpp.

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED, ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT, ARM_COMPUTE_UNUSED, ITensorInfo::data_type(), arm_compute::F16, arm_compute::F32, ITensorInfo::has_padding(), arm_compute::NHWC, Dimensions< T >::num_dimensions(), ITensorInfo::num_dimensions(), arm_compute::S16, arm_compute::S32, ITensorInfo::tensor_shape(), ITensorInfo::total_size(), arm_compute::U16, arm_compute::U32, and arm_compute::U8.

Referenced by NECropKernel::configure(), NECropKernel::name(), and NECropResize::validate().

211 {
212  ARM_COMPUTE_UNUSED(extrapolation_value);
213  const auto *uk = get_implementation(CropSelectorData{ input->data_type() });
214  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
215 
219  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
220  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[0] != 4);
221  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]);
222  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] <= crop_box_ind);
223  ARM_COMPUTE_RETURN_ERROR_ON(box_ind->tensor_shape()[0] <= crop_box_ind);
224  if(output->total_size() > 0)
225  {
228  ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() != 3);
229  ARM_COMPUTE_RETURN_ERROR_ON(output->has_padding());
230  }
231  return Status{};
232 }
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t,...)
Definition: Validate.h:742
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:490
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
1 channel, 1 U8 per channel
1 channel, 1 F32 per channel
1 channel, 1 U16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
1 channel, 1 U32 per channel
1 channel, 1 S16 per channel
Num samples, height, width, channels.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:690

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