Compute Library
 19.08
CLCropResize Class Reference

Function to perform cropping and resizing. More...

#include <CLCropResize.h>

Collaboration diagram for CLCropResize:
[legend]

Public Member Functions

 CLCropResize ()
 Default constructor. More...
 
 CLCropResize (const CLCropResize &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
CLCropResizeoperator= (const CLCropResize &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 CLCropResize (CLCropResize &&)=default
 Allow instances of this class to be moved. More...
 
CLCropResizeoperator= (CLCropResize &&)=default
 Allow instances of this class to be moved. More...
 
virtual ~CLCropResize ()=default
 Default destructor. More...
 
void configure (const ICLTensor *input, ICLTensor *boxes, ICLTensor *box_ind, ICLTensor *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, ITensorInfo *boxes, 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

const ICLTensor_input
 
ICLTensor_boxes
 
ICLTensor_box_ind
 
ICLTensor_output
 
size_t _num_boxes
 
InterpolationPolicy _method
 
float _extrapolation_value
 
std::vector< std::unique_ptr< CLScale > > _scale
 
std::vector< std::unique_ptr< CLCopyKernel > > _copy
 
std::vector< std::unique_ptr< CLTensor > > _crop_results
 
std::vector< std::unique_ptr< CLTensor > > _scaled_results
 

Detailed Description

Function to perform cropping and resizing.

Definition at line 43 of file CLCropResize.h.

Constructor & Destructor Documentation

◆ CLCropResize() [1/3]

Default constructor.

Definition at line 166 of file CLCropResize.cpp.

167  : _input(nullptr), _boxes(nullptr), _box_ind(nullptr), _output(nullptr), _num_boxes(0), _method(), _extrapolation_value(0), _scale(), _copy(), _crop_results(), _scaled_results()
168 {
169 }
std::vector< std::unique_ptr< CLTensor > > _crop_results
Definition: CLCropResize.h:110
std::vector< std::unique_ptr< CLCopyKernel > > _copy
Definition: CLCropResize.h:109
std::vector< std::unique_ptr< CLTensor > > _scaled_results
Definition: CLCropResize.h:111
InterpolationPolicy _method
Definition: CLCropResize.h:105
const ICLTensor * _input
Definition: CLCropResize.h:100
std::vector< std::unique_ptr< CLScale > > _scale
Definition: CLCropResize.h:108

◆ CLCropResize() [2/3]

CLCropResize ( const CLCropResize )
delete

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

◆ CLCropResize() [3/3]

CLCropResize ( CLCropResize &&  )
default

Allow instances of this class to be moved.

◆ ~CLCropResize()

virtual ~CLCropResize ( )
virtualdefault

Default destructor.

Member Function Documentation

◆ configure()

void configure ( const ICLTensor input,
ICLTensor boxes,
ICLTensor box_ind,
ICLTensor 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: 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 190 of file CLCropResize.cpp.

192 {
193  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
194  ARM_COMPUTE_ERROR_THROW_ON(CLCropResize::validate(input->info(), boxes->info(), box_ind->info(), output->info(), crop_size, method, extrapolation_value));
195 
196  _num_boxes = boxes->info()->tensor_shape()[1];
197  TensorShape out_shape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y);
198 
199  _input = input;
200  _boxes = boxes;
201  _box_ind = box_ind;
202  _output = output;
203  _method = method;
204  _extrapolation_value = extrapolation_value;
205 
206  // For each crop box:
207  // - The initial cropped image is produced as specified by boxes[i] from the 3D image input[box_ind[i]].
208  // Possibly using a CLCropKernel and up to four CLMemsetKernels.
209  // - A tensor is required to hold this initial cropped image.
210  // - A scale function is used to resize the cropped image to the size specified by crop_size.
211  // - A tensor is required to hold the final scaled image before it is copied into the 4D output
212  // that will hold all final cropped and scaled 3D images using CLCopyKernel.
213  for(unsigned int i = 0; i < _num_boxes; ++i)
214  {
215  auto crop_tensor = support::cpp14::make_unique<CLTensor>();
216  TensorInfo crop_result_info(1, DataType::F32);
217  crop_result_info.set_data_layout(DataLayout::NHWC);
218  crop_tensor->allocator()->init(crop_result_info);
219  _crop_results.emplace_back(std::move(crop_tensor));
220 
221  auto scale_tensor = support::cpp14::make_unique<CLTensor>();
222  TensorInfo scaled_result_info(out_shape, 1, DataType::F32);
223  scaled_result_info.set_data_layout(DataLayout::NHWC);
224  scale_tensor->allocator()->init(scaled_result_info);
225  _scaled_results.emplace_back(std::move(scale_tensor));
226  }
227 }
std::vector< std::unique_ptr< CLTensor > > _crop_results
Definition: CLCropResize.h:110
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
std::vector< std::unique_ptr< CLTensor > > _scaled_results
Definition: CLCropResize.h:111
InterpolationPolicy _method
Definition: CLCropResize.h:105
static Status validate(const ITensorInfo *input, ITensorInfo *boxes, 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.
const ICLTensor * _input
Definition: CLCropResize.h:100
int x
The x coordinate.
Definition: types.h:30
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Num samples, height, width, channels.
int y
The y coordinate.
Definition: types.h:31

References CLCropResize::_box_ind, CLCropResize::_boxes, CLCropResize::_crop_results, CLCropResize::_extrapolation_value, CLCropResize::_input, CLCropResize::_method, CLCropResize::_num_boxes, CLCropResize::_output, CLCropResize::_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(), CLCropResize::validate(), Coordinates2D::x, and Coordinates2D::y.

◆ operator=() [1/2]

CLCropResize& operator= ( const CLCropResize )
delete

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

◆ operator=() [2/2]

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

230 {
231  ARM_COMPUTE_ERROR_ON_MSG(_output == nullptr, "Unconfigured function");
232  // The contents of _boxes and _box_ind are required to calculate the shape
233  // of the initial cropped image and thus are required to configure the
234  // kernels used for cropping and scaling.
235  _boxes->map(CLScheduler::get().queue());
236  _box_ind->map(CLScheduler::get().queue());
237  for(unsigned int i = 0; i < _num_boxes; ++i)
238  {
239  // Size of the crop box in _boxes and thus the shape of _crop_results[i]
240  // may not be known until run-time and so the kernels cannot be configured until then.
241  uint32_t batch_index;
242  Coordinates start{};
243  Coordinates end{};
244  configure_crop(_input, _boxes, _box_ind, _crop_results[i].get(), i, start, end, batch_index);
245 
246  auto scale_kernel = support::cpp14::make_unique<CLScale>();
247  scale_kernel->configure(_crop_results[i].get(), _scaled_results[i].get(), _method, BorderMode::CONSTANT, PixelValue(_extrapolation_value), SamplingPolicy::TOP_LEFT);
248  _scale.emplace_back(std::move(scale_kernel));
249 
250  Window win = calculate_max_window(*_output->info());
251  win.set(3, Window::Dimension(i, i + 1, 1));
252 
253  auto copy_kernel = support::cpp14::make_unique<CLCopyKernel>();
254  copy_kernel->configure(_scaled_results[i].get(), _output, PaddingList(), &win);
255  _copy.emplace_back(std::move(copy_kernel));
256 
257  _crop_results[i]->allocator()->allocate();
258  _scaled_results[i]->allocator()->allocate();
259 
260  run_crop(_input, _crop_results[i].get(), batch_index, start, end, _extrapolation_value);
261  }
262  _boxes->unmap(CLScheduler::get().queue());
263  _box_ind->unmap(CLScheduler::get().queue());
265  for(auto &kernel : _scale)
266  {
267  kernel->run();
268  }
270  for(auto &kernel : _copy)
271  {
272  CLScheduler::get().enqueue(*kernel, true);
273  }
275 }
std::vector< std::unique_ptr< CLTensor > > _crop_results
Definition: CLCropResize.h:110
void map(cl::CommandQueue &q, bool blocking=true)
Enqueue a map operation of the allocated buffer on the given queue.
Definition: ICLTensor.cpp:35
std::vector< PaddingInfo > PaddingList
List of padding information.
Definition: Types.h:445
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:41
std::vector< std::unique_ptr< CLCopyKernel > > _copy
Definition: CLCropResize.h:109
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
std::vector< std::unique_ptr< CLTensor > > _scaled_results
Definition: CLCropResize.h:111
InterpolationPolicy _method
Definition: CLCropResize.h:105
void unmap(cl::CommandQueue &q)
Enqueue an unmap operation of the allocated and mapped buffer on the given queue.
Definition: ICLTensor.cpp:40
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
Samples are taken at pixel top left corner.
const ICLTensor * _input
Definition: CLCropResize.h:100
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:48
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: CLScheduler.cpp:95
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.h:151
std::vector< std::unique_ptr< CLScale > > _scale
Definition: CLCropResize.h:108
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References CLCropResize::_box_ind, CLCropResize::_boxes, CLCropResize::_copy, CLCropResize::_crop_results, CLCropResize::_extrapolation_value, CLCropResize::_input, CLCropResize::_method, CLCropResize::_num_boxes, CLCropResize::_output, CLCropResize::_scale, CLCropResize::_scaled_results, ARM_COMPUTE_ERROR_ON_MSG, arm_compute::calculate_max_window(), arm_compute::CONSTANT, CLScheduler::enqueue(), CLScheduler::get(), ITensor::info(), ICLTensor::map(), Window::set(), CLScheduler::sync(), arm_compute::TOP_LEFT, and ICLTensor::unmap().

◆ validate()

Status validate ( const ITensorInfo input,
ITensorInfo boxes,
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 info 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 171 of file CLCropResize.cpp.

173 {
174  ARM_COMPUTE_RETURN_ERROR_ON(crop_size.x <= 0 || crop_size.y <= 0);
176  ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
177  ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]);
178  TensorInfo temp_info;
179  ARM_COMPUTE_RETURN_ON_ERROR(CLCropKernel::validate(input->clone().get(), &temp_info, { 0, 0 }, { 1, 1 }, input->dimension(3) - 1, extrapolation_value));
180  if(output->total_size() > 0)
181  {
184  TensorShape out_shape(input->tensor_shape()[0], crop_size.x, crop_size.y, boxes->tensor_shape()[1]);
185  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), out_shape);
186  }
187  return Status{};
188 }
static Status validate(const ITensorInfo *input, const ITensorInfo *output, Coordinates2D start, Coordinates2D end, uint32_t batch_index, float extrapolation_value=0, Window *output_window=nullptr)
Static function to check if given info will lead to a valid configuration of CLStridedSliceKernel.
#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
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(), ITensorInfo::dimension(), arm_compute::F32, ITensorInfo::tensor_shape(), ITensorInfo::total_size(), CLCropKernel::validate(), Coordinates2D::x, and Coordinates2D::y.

Referenced by CLCropResize::configure().

Field Documentation

◆ _box_ind

ICLTensor* _box_ind

Definition at line 102 of file CLCropResize.h.

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

◆ _boxes

ICLTensor* _boxes

Definition at line 101 of file CLCropResize.h.

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

◆ _copy

std::vector<std::unique_ptr<CLCopyKernel> > _copy

Definition at line 109 of file CLCropResize.h.

Referenced by CLCropResize::run().

◆ _crop_results

std::vector<std::unique_ptr<CLTensor> > _crop_results

Definition at line 110 of file CLCropResize.h.

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

◆ _extrapolation_value

float _extrapolation_value

Definition at line 106 of file CLCropResize.h.

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

◆ _input

const ICLTensor* _input

Definition at line 100 of file CLCropResize.h.

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

◆ _method

Definition at line 105 of file CLCropResize.h.

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

◆ _num_boxes

size_t _num_boxes

Definition at line 104 of file CLCropResize.h.

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

◆ _output

ICLTensor* _output

Definition at line 103 of file CLCropResize.h.

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

◆ _scale

std::vector<std::unique_ptr<CLScale> > _scale

Definition at line 108 of file CLCropResize.h.

Referenced by CLCropResize::run().

◆ _scaled_results

std::vector<std::unique_ptr<CLTensor> > _scaled_results

Definition at line 111 of file CLCropResize.h.

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


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