Compute Library
 21.02
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...
 
- 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...
 

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

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 237 of file NECropKernel.cpp.

Referenced by NECropKernel::name().

238  : _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(),
239  _in_bounds_crop_function(nullptr)
240 {
241 }

◆ 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 243 of file NECropKernel.cpp.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, ITensorInfo::data_type(), arm_compute::F16, arm_compute::F32, ITensor::info(), arm_compute::test::validation::input, arm_compute::S16, arm_compute::S32, arm_compute::U16, arm_compute::U32, arm_compute::U8, and NECropKernel::validate().

Referenced by NECropKernel::name().

244 {
246  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), crop_boxes->info(), box_ind->info(), output->info(), crop_box_ind, extrapolation_value));
247 
248  _input = input;
249  _crop_boxes = crop_boxes;
250  _box_ind = box_ind;
251  _output = output;
252  _crop_box_ind = crop_box_ind;
253  _extrapolation_value = extrapolation_value;
254 
255  switch(input->info()->data_type())
256  {
257  case DataType::F32:
258  _in_bounds_crop_function = &in_bounds_crop_window<float>;
259  break;
260 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
261  case DataType::F16:
262  _in_bounds_crop_function = &in_bounds_crop_window<float16_t>;
263  break;
264 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
265  case DataType::U32:
266  _in_bounds_crop_function = &in_bounds_crop_window<uint32_t>;
267  break;
268  case DataType::S32:
269  _in_bounds_crop_function = &in_bounds_crop_window<int32_t>;
270  break;
271  case DataType::U16:
272  _in_bounds_crop_function = &in_bounds_crop_window<uint16_t>;
273  break;
274  case DataType::S16:
275  _in_bounds_crop_function = &in_bounds_crop_window<int16_t>;
276  break;
277  case DataType::U8:
278  _in_bounds_crop_function = &in_bounds_crop_window<uint8_t>;
279  break;
280  default:
281  ARM_COMPUTE_ERROR("Datatype not supported");
282  }
283 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
1 channel, 1 U8 per channel
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
1 channel, 1 U16 per channel
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
1 channel, 1 U32 per channel
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.
1 channel, 1 S16 per channel
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161

◆ configure_output_shape()

void configure_output_shape ( )

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

Definition at line 306 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().

307 {
308  // _crop_box_ind is used to index _crop_boxes and retrieve the appropriate crop box.
309  // The crop box is specified by normalized coordinates [y0, x0, y1, x1].
310  const float x0 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(1, _crop_box_ind)));
311  const float y0 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(0, _crop_box_ind)));
312  const float x1 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(3, _crop_box_ind)));
313  const float y1 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(2, _crop_box_ind)));
314  // The normalized coordiantes are scaled to retrieve the floating point image coordinates which are rounded to integers.
315  _start = Coordinates(std::floor(x0 * (_input->info()->tensor_shape()[1] - 1) + 0.5f),
316  std::floor(y0 * (_input->info()->tensor_shape()[2] - 1) + 0.5f));
317  _end = Coordinates(std::floor(x1 * (_input->info()->tensor_shape()[1] - 1) + 0.5f),
318  std::floor(y1 * (_input->info()->tensor_shape()[2] - 1) + 0.5f));
319  const TensorShape out_shape(_input->info()->tensor_shape()[0], abs(_end[0] - _start[0]) + 1, abs(_end[1] - _start[1]) + 1);
320  _output->info()->set_tensor_shape(out_shape);
321 
322  bool is_width_flipped = _end[0] < _start[0];
323  bool is_height_flipped = _end[1] < _start[1];
324  if(is_height_flipped)
325  {
326  _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),
327  static_cast<uint32_t>(_output->info()->dimension(2))) :
328  0;
329  _rows_out_of_bounds[1] = _end[1] < 0 ? std::min(static_cast<uint32_t>(-_end[1]),
330  static_cast<uint32_t>(_output->info()->dimension(2))) :
331  0;
332  }
333  else
334  {
335  _rows_out_of_bounds[0] = _start[1] < 0 ? std::min(static_cast<uint32_t>(-_start[1]),
336  static_cast<uint32_t>(_output->info()->dimension(2))) :
337  0;
338  _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),
339  static_cast<uint32_t>(_output->info()->dimension(2))) :
340  0;
341  }
342  if(is_width_flipped)
343  {
344  _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),
345  static_cast<uint32_t>(_output->info()->dimension(1))) :
346  0;
347  _cols_out_of_bounds[1] = _end[0] < 0 ? std::min(static_cast<uint32_t>(-_end[0]),
348  static_cast<uint32_t>(_output->info()->dimension(1))) :
349  0;
350  }
351  else
352  {
353  _cols_out_of_bounds[0] = _start[0] < 0 ? std::min(static_cast<uint32_t>(-_start[0]),
354  static_cast<uint32_t>(_output->info()->dimension(1))) :
355  0;
356  _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),
357  static_cast<uint32_t>(_output->info()->dimension(1))) :
358  0;
359  }
360 
361  INEKernel::configure(calculate_max_window(*_output->info()));
362 }
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 364 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::dimension(), ITensorInfo::has_padding(), ITensor::info(), ITensor::ptr_to_element(), and IKernel::window().

Referenced by NECropKernel::name().

365 {
369 
370  ARM_COMPUTE_ERROR_ON(_input->info()->has_padding());
371  ARM_COMPUTE_ERROR_ON(_output->info()->has_padding());
372 
373  uint32_t batch_index = *(reinterpret_cast<int32_t *>(_box_ind->ptr_to_element(Coordinates(_crop_box_ind))));
374  Coordinates input_offset(0, _end[0] < _start[0] ? _start[0] - _cols_out_of_bounds[0] : _start[0] + _cols_out_of_bounds[0],
375  _end[1] < _start[1] ? _start[1] - _rows_out_of_bounds[0] : _start[1] + _rows_out_of_bounds[0], batch_index);
376  execute_window(_input, _output, input_offset, _extrapolation_value, _rows_out_of_bounds, _cols_out_of_bounds, _in_bounds_crop_function, _end[1] < _start[1],
377  _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,
378  _start[0] <= _end[0], _end[0] < _start[0]);
379 }
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.
#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:941
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:205

◆ 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 285 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, 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().

286 {
287  ARM_COMPUTE_UNUSED(extrapolation_value);
291  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
292  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[0] != 4);
293  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]);
294  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] <= crop_box_ind);
295  ARM_COMPUTE_RETURN_ERROR_ON(box_ind->tensor_shape()[0] <= crop_box_ind);
296  if(output->total_size() > 0)
297  {
300  ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() != 3);
301  ARM_COMPUTE_RETURN_ERROR_ON(output->has_padding());
302  }
303  return Status{};
304 }
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t,...)
Definition: Validate.h:746
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:108
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:792
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:694

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