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

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

Referenced by NECropKernel::name().

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

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

243 {
245  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), crop_boxes->info(), box_ind->info(), output->info(), crop_box_ind, extrapolation_value));
246 
247  _input = input;
248  _crop_boxes = crop_boxes;
249  _box_ind = box_ind;
250  _output = output;
251  _crop_box_ind = crop_box_ind;
252  _extrapolation_value = extrapolation_value;
253 
254  switch(input->info()->data_type())
255  {
256  case DataType::F32:
257  _in_bounds_crop_function = &in_bounds_crop_window<float>;
258  break;
259 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
260  case DataType::F16:
261  _in_bounds_crop_function = &in_bounds_crop_window<float16_t>;
262  break;
263 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
264  case DataType::U32:
265  _in_bounds_crop_function = &in_bounds_crop_window<uint32_t>;
266  break;
267  case DataType::S32:
268  _in_bounds_crop_function = &in_bounds_crop_window<int32_t>;
269  break;
270  case DataType::U16:
271  _in_bounds_crop_function = &in_bounds_crop_window<uint16_t>;
272  break;
273  case DataType::S16:
274  _in_bounds_crop_function = &in_bounds_crop_window<int16_t>;
275  break;
276  case DataType::U8:
277  _in_bounds_crop_function = &in_bounds_crop_window<uint8_t>;
278  break;
279  default:
280  ARM_COMPUTE_ERROR("Datatype not supported");
281  }
282 }
#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:157

◆ configure_output_shape()

void configure_output_shape ( )

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

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

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

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

285 {
286  ARM_COMPUTE_UNUSED(extrapolation_value);
290  ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().num_dimensions() > 4);
291  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[0] != 4);
292  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]);
293  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] <= crop_box_ind);
294  ARM_COMPUTE_RETURN_ERROR_ON(box_ind->tensor_shape()[0] <= crop_box_ind);
295  if(output->total_size() > 0)
296  {
299  ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() != 3);
300  ARM_COMPUTE_RETURN_ERROR_ON(output->has_padding());
301  }
302  return Status{};
303 }
#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: