Compute Library
 19.08
NECropResize Class Reference

Function to perform cropping and resizing. More...

#include <NECropResize.h>

Collaboration diagram for NECropResize:
[legend]

Public Member Functions

 NECropResize ()
 Default constructor. More...
 
 NECropResize (const NECropResize &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
NECropResizeoperator= (const NECropResize &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 NECropResize (NECropResize &&)=default
 Allow instances of this class to be moved. More...
 
NECropResizeoperator= (NECropResize &&)=default
 Allow instances of this class to be moved. More...
 
virtual ~NECropResize ()=default
 Default destructor. More...
 
void configure (const ITensor *input, const ITensor *boxes, const ITensor *box_ind, ITensor *output, Coordinates2D crop_size, InterpolationPolicy method=InterpolationPolicy::BILINEAR, float extrapolation_value=0)
 Configure kernel. More...
 
void run () override
 Run the kernels contained in the function. More...
 
- Public Member Functions inherited from IFunction
virtual ~IFunction ()=default
 Destructor. More...
 
virtual void prepare ()
 Prepare the function for executing. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *boxes, const ITensorInfo *box_ind, const ITensorInfo *output, Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value)
 Static function to check if given info will lead to a valid configuration of NESlice. More...
 

Data Fields

ITensor_output
 
size_t _num_boxes
 
InterpolationPolicy _method
 
float _extrapolation_value
 
std::vector< std::unique_ptr< NECropKernel > > _crop
 
std::vector< std::unique_ptr< NEScale > > _scale
 
std::vector< std::unique_ptr< Tensor > > _crop_results
 
std::vector< std::unique_ptr< Tensor > > _scaled_results
 

Detailed Description

Function to perform cropping and resizing.

Definition at line 39 of file NECropResize.h.

Constructor & Destructor Documentation

◆ NECropResize() [1/3]

Default constructor.

Definition at line 32 of file NECropResize.cpp.

34 {
35 }
std::vector< std::unique_ptr< NECropKernel > > _crop
Definition: NECropResize.h:101
std::vector< std::unique_ptr< Tensor > > _scaled_results
Definition: NECropResize.h:104
std::vector< std::unique_ptr< NEScale > > _scale
Definition: NECropResize.h:102
InterpolationPolicy _method
Definition: NECropResize.h:98
std::vector< std::unique_ptr< Tensor > > _crop_results
Definition: NECropResize.h:103

◆ NECropResize() [2/3]

NECropResize ( const NECropResize )
delete

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

◆ NECropResize() [3/3]

NECropResize ( NECropResize &&  )
default

Allow instances of this class to be moved.

◆ ~NECropResize()

virtual ~NECropResize ( )
virtualdefault

Default destructor.

Member Function Documentation

◆ configure()

void configure ( const ITensor input,
const ITensor boxes,
const ITensor box_ind,
ITensor output,
Coordinates2D  crop_size,
InterpolationPolicy  method = InterpolationPolicy::BILINEAR,
float  extrapolation_value = 0 
)

Configure kernel.

Note
Supported tensor rank: up to 4
Box indices may be outside of the bounds, in which case extrapolation_value is used.
Start and end indices of boxes are inclusive.
Parameters
[in]inputSource tensor containing N batches of 3D images to be cropped. Data type supported: U16/S16/U32/S32/F16/F32
[in]boxesTensor containing the boxes used to crop the images. Data type supported: F32
[in]box_indOne dimensional tensor containing the batch index of the 3D image in input that the corresponding box in boxes will be applied to. Data type supported: F32
[out]outputDestination tensor containing a cropped and resized image for each box in boxes. Data type supported: F32
[in]crop_sizeThe dimensions that each cropped image will be resized to.
[in]methodThe policy to be used when resizing image. Default is bilinear.
[in]extrapolation_valueValue to be used for values outside of the image for cropping and resizing. Default is 0.

Definition at line 54 of file NECropResize.cpp.

56 {
57  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
58  ARM_COMPUTE_ERROR_THROW_ON(NECropResize::validate(input->info(), boxes->info(), box_ind->info(), output->info(), crop_size, method, extrapolation_value));
59 
60  _num_boxes = boxes->info()->tensor_shape()[1];
61  TensorShape out_shape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y);
62 
63  _output = output;
64  _method = method;
65  _extrapolation_value = extrapolation_value;
66 
67  // For each crop box:
68  // - A crop kernel is used to extract the initial cropped image as specified by boxes[i] from the 3D image input[box_ind[i]].
69  // - A tensor is required to hold this initial cropped image.
70  // - A scale function is used to resize the cropped image to the size specified by crop_size.
71  // - A tensor is required to hold the final scaled image before it is copied into the 4D output
72  // that will hold all final cropped and scaled 3D images.
73  _crop.reserve(_num_boxes);
74  _crop_results.reserve(_num_boxes);
75  _scaled_results.reserve(_num_boxes);
76  _scale.reserve(_num_boxes);
77 
78  for(unsigned int i = 0; i < _num_boxes; ++i)
79  {
80  auto crop_tensor = support::cpp14::make_unique<Tensor>();
81  TensorInfo crop_result_info(1, DataType::F32);
82  crop_result_info.set_data_layout(DataLayout::NHWC);
83  crop_tensor->allocator()->init(crop_result_info);
84 
85  auto scale_tensor = support::cpp14::make_unique<Tensor>();
86  TensorInfo scaled_result_info(out_shape, 1, DataType::F32);
87  scaled_result_info.set_data_layout(DataLayout::NHWC);
88  scale_tensor->allocator()->init(scaled_result_info);
89 
90  auto crop_kernel = support::cpp14::make_unique<NECropKernel>();
91  auto scale_kernel = support::cpp14::make_unique<NEScale>();
92  crop_kernel->configure(input, boxes, box_ind, crop_tensor.get(), i, _extrapolation_value);
93 
94  _crop.emplace_back(std::move(crop_kernel));
95  _scaled_results.emplace_back(std::move(scale_tensor));
96  _crop_results.emplace_back(std::move(crop_tensor));
97  _scale.emplace_back(std::move(scale_kernel));
98  }
99 }
std::vector< std::unique_ptr< NECropKernel > > _crop
Definition: NECropResize.h:101
1 channel, 1 F32 per channel
std::vector< std::unique_ptr< Tensor > > _scaled_results
Definition: NECropResize.h:104
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
static Status validate(const ITensorInfo *input, const ITensorInfo *boxes, const ITensorInfo *box_ind, const ITensorInfo *output, Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value)
Static function to check if given info will lead to a valid configuration of NESlice.
int x
The x coordinate.
Definition: types.h:30
std::vector< std::unique_ptr< NEScale > > _scale
Definition: NECropResize.h:102
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Num samples, height, width, channels.
int y
The y coordinate.
Definition: types.h:31
InterpolationPolicy _method
Definition: NECropResize.h:98
std::vector< std::unique_ptr< Tensor > > _crop_results
Definition: NECropResize.h:103

References NECropResize::_crop, NECropResize::_crop_results, NECropResize::_extrapolation_value, NECropResize::_method, NECropResize::_num_boxes, NECropResize::_output, NECropResize::_scale, NECropResize::_scaled_results, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::F32, ITensor::info(), arm_compute::NHWC, TensorInfo::set_data_layout(), ITensorInfo::tensor_shape(), NECropResize::validate(), Coordinates2D::x, and Coordinates2D::y.

◆ operator=() [1/2]

NECropResize& operator= ( const NECropResize )
delete

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

◆ operator=() [2/2]

NECropResize& operator= ( NECropResize &&  )
default

Allow instances of this class to be moved.

◆ run()

void run ( )
overridevirtual

Run the kernels contained in the function.

For NEON kernels:

  • Multi-threading is used for the kernels which are parallelisable.
  • By default std::thread::hardware_concurrency() threads are used.
Note
CPPScheduler::set_num_threads() can be used to manually set the number of threads

For OpenCL kernels:

  • All the kernels are enqueued on the queue associated with CLScheduler.
  • The queue is then flushed.
Note
The function will not block until the kernels are executed. It is the user's responsibility to wait.
Will call prepare() on first run if hasn't been done

Implements IFunction.

Definition at line 101 of file NECropResize.cpp.

102 {
103  ARM_COMPUTE_ERROR_ON_MSG(_output == nullptr, "Unconfigured function");
104 
105  for(unsigned int i = 0; i < _num_boxes; ++i)
106  {
107  // Size of the crop box in _boxes and thus the shape of _crop_results[i]
108  // may not be known until run-time and so the kernels cannot be configured until then.
109  _crop[i]->configure_output_shape();
110  _crop_results[i]->allocator()->allocate();
112 
113  // Scale the cropped image.
114  _scale[i]->configure(_crop_results[i].get(), _scaled_results[i].get(), _method, BorderMode::CONSTANT, PixelValue(_extrapolation_value), SamplingPolicy::TOP_LEFT, false);
115  _scaled_results[i]->allocator()->allocate();
116  _scale[i]->run();
117 
118  // Copy scaled image into output.
119  std::copy_n(_scaled_results[i]->buffer(), _scaled_results[i]->info()->total_size(), _output->ptr_to_element(Coordinates(0, 0, 0, i)));
120  }
121 }
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
std::vector< std::unique_ptr< NECropKernel > > _crop
Definition: NECropResize.h:101
std::vector< std::unique_ptr< Tensor > > _scaled_results
Definition: NECropResize.h:104
Samples are taken at pixel top left corner.
std::vector< std::unique_ptr< NEScale > > _scale
Definition: NECropResize.h:102
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
InterpolationPolicy _method
Definition: NECropResize.h:98
std::vector< std::unique_ptr< Tensor > > _crop_results
Definition: NECropResize.h:103
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:96
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References NECropResize::_crop, NECropResize::_crop_results, NECropResize::_extrapolation_value, NECropResize::_method, NECropResize::_num_boxes, NECropResize::_output, NECropResize::_scale, NECropResize::_scaled_results, ARM_COMPUTE_ERROR_ON_MSG, arm_compute::CONSTANT, Window::DimZ, Scheduler::get(), arm_compute::test::validation::info, ITensor::ptr_to_element(), IScheduler::schedule(), and arm_compute::TOP_LEFT.

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo boxes,
const ITensorInfo box_ind,
const ITensorInfo output,
Coordinates2D  crop_size,
InterpolationPolicy  method,
float  extrapolation_value 
)
static

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

Note
Supported tensor rank: up to 4
Box indices may be outside of the bounds, in which case extrapolation_value is used.
Start and end indices of boxes are inclusive.
Parameters
[in]inputSource tensor containing N batches of 3D images to be cropped. Data type supported: U16/S16/U32/S32/F16/F32
[in]boxesTensor info for the tensor containing the boxes used to crop the images. Data type supported: F32
[in]box_indTensor info for the one dimensional tensor containing the batch index of the 3D image in input that the corresponding box in boxes will be applied to. Data type supported: F32
[in]outputTensor info for the destination tensor containing a cropped and resized image for each box in boxes. Data type supported: F32
[in]crop_sizeThe dimensions that each cropped image will be resized to.
[in]methodThe policy to be used when resizing image. Default is bilinear.
[in]extrapolation_valueValue to be used for values outside of the image for cropping and resizing. Default is 0.
Returns
A status

Definition at line 37 of file NECropResize.cpp.

39 {
40  ARM_COMPUTE_RETURN_ERROR_ON(crop_size.x <= 0 || crop_size.y <= 0);
42  TensorInfo temp_info;
43  ARM_COMPUTE_RETURN_ON_ERROR(NECropKernel::validate(input->clone().get(), boxes->clone().get(), box_ind->clone().get(), &temp_info, boxes->tensor_shape()[1] - 1, extrapolation_value));
44  if(output->total_size() > 0)
45  {
48  TensorShape out_shape(input->tensor_shape()[0], crop_size.x, crop_size.y, boxes->tensor_shape()[1]);
49  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), out_shape);
50  }
51  return Status{};
52 }
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
1 channel, 1 F32 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:693
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
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.
int x
The x coordinate.
Definition: types.h:30
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
int y
The y coordinate.
Definition: types.h:31

References arm_compute::AREA, ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT, ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS, ARM_COMPUTE_RETURN_ON_ERROR, ICloneable< T >::clone(), arm_compute::F32, ITensorInfo::tensor_shape(), ITensorInfo::total_size(), NECropKernel::validate(), Coordinates2D::x, and Coordinates2D::y.

Referenced by NECropResize::configure().

Field Documentation

◆ _crop

std::vector<std::unique_ptr<NECropKernel> > _crop

Definition at line 101 of file NECropResize.h.

Referenced by NECropResize::configure(), and NECropResize::run().

◆ _crop_results

std::vector<std::unique_ptr<Tensor> > _crop_results

Definition at line 103 of file NECropResize.h.

Referenced by NECropResize::configure(), and NECropResize::run().

◆ _extrapolation_value

float _extrapolation_value

Definition at line 99 of file NECropResize.h.

Referenced by NECropResize::configure(), and NECropResize::run().

◆ _method

Definition at line 98 of file NECropResize.h.

Referenced by NECropResize::configure(), and NECropResize::run().

◆ _num_boxes

size_t _num_boxes

Definition at line 97 of file NECropResize.h.

Referenced by NECropResize::configure(), and NECropResize::run().

◆ _output

ITensor* _output

Definition at line 96 of file NECropResize.h.

Referenced by NECropResize::configure(), and NECropResize::run().

◆ _scale

std::vector<std::unique_ptr<NEScale> > _scale

Definition at line 102 of file NECropResize.h.

Referenced by NECropResize::configure(), and NECropResize::run().

◆ _scaled_results

std::vector<std::unique_ptr<Tensor> > _scaled_results

Definition at line 104 of file NECropResize.h.

Referenced by NECropResize::configure(), and NECropResize::run().


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