Compute Library
 19.08
NEScale Class Reference

Basic function to run NEScaleKernel. More...

#include <NEScale.h>

Collaboration diagram for NEScale:
[legend]

Public Member Functions

 NEScale ()
 Constructor. More...
 
void configure (ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value=PixelValue(), SamplingPolicy sampling_policy=SamplingPolicy::CENTER, bool use_padding=true)
 Initialize the function's source, destination, interpolation type and border_mode. 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 *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value=PixelValue(), SamplingPolicy sampling_policy=SamplingPolicy::CENTER, bool use_padding=true)
 Static function to check if given info will lead to a valid configuration of NEScale. More...
 

Detailed Description

Basic function to run NEScaleKernel.

Definition at line 40 of file NEScale.h.

Constructor & Destructor Documentation

◆ NEScale()

NEScale ( )

Constructor.

Initialize NEScale

Definition at line 95 of file NEScale.cpp.

96  : _offsets(),
97  _dx(),
98  _dy(),
99  _scale_kernel(),
100  _border_handler(),
101  _use_padding(true)
102 {
103 }

Member Function Documentation

◆ configure()

void configure ( ITensor input,
ITensor output,
InterpolationPolicy  policy,
BorderMode  border_mode,
PixelValue  constant_border_value = PixelValue(),
SamplingPolicy  sampling_policy = SamplingPolicy::CENTER,
bool  use_padding = true 
)

Initialize the function's source, destination, interpolation type and border_mode.

Parameters
[in,out]inputSource tensor. Data type supported: U8/S16/F16/F32. (Written to only for border_mode != UNDEFINED)
[out]outputDestination tensor. Data type supported: Same as input. All but the lowest two dimensions must be the same size as in the input tensor, i.e. scaling is only performed within the XY-plane.
[in]policyThe interpolation type.
[in]border_modeStrategy to use for borders.
[in]constant_border_value(Optional) Constant value to use for borders if border_mode is set to CONSTANT.
[in]sampling_policy(Optional) Sampling policy used by the interpolation. Defaults to SamplingPolicy::CENTER
[in]use_padding(Optional) Is padding in use or not. Defaults to true.

Definition at line 105 of file NEScale.cpp.

106 {
107  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
108  ARM_COMPUTE_ERROR_THROW_ON(NEScale::validate(input->info(), output->info(), policy, border_mode, constant_border_value, sampling_policy, use_padding));
109 
110  _use_padding = use_padding;
111 
112  // Get data layout and width/height indices
113  const DataLayout data_layout = input->info()->data_layout();
116 
117  // Get the tensor shape
118  const TensorShape shape(output->info()->dimension(idx_width), output->info()->dimension(idx_height));
119 
120  // Compute the ratio between source width/height and destination width/height
121  const auto wr = static_cast<float>(input->info()->dimension(idx_width)) / static_cast<float>(output->info()->dimension(idx_width));
122  const auto hr = static_cast<float>(input->info()->dimension(idx_height)) / static_cast<float>(output->info()->dimension(idx_height));
123 
124  // Get the element size of the input image
125  const size_t input_element_size = input->info()->element_size();
126 
127  // Area interpolation behaves as Nearest Neighbour in case of up-sampling
128  if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
129  {
131  }
132 
133  switch(policy)
134  {
136  {
137  TensorInfo tensor_info_offsets(shape, Format::S32);
138  _offsets.allocator()->init(tensor_info_offsets);
139 
140  _scale_kernel.configure(input, nullptr, nullptr, &_offsets, output, policy, border_mode, constant_border_value, sampling_policy, use_padding);
141 
142  // Allocate once the configure methods have been called
143  _offsets.allocator()->allocate();
144 
145  // Pre-compute offsets for nearest interpolation
146  precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size, sampling_policy);
147  break;
148  }
150  {
151  TensorInfo tensor_info_offsets(shape, Format::S32);
152  TensorInfo tensor_info_dxdy(shape, Format::F32);
153 
154  _offsets.allocator()->init(tensor_info_offsets);
155  _dx.allocator()->init(tensor_info_dxdy);
156  _dy.allocator()->init(tensor_info_dxdy);
157 
158  _scale_kernel.configure(input, &_dx, &_dy, &_offsets, output, policy, border_mode, constant_border_value, sampling_policy, use_padding);
159 
160  // Allocate once the configure methods have been called
161  _offsets.allocator()->allocate();
162  _dx.allocator()->allocate();
163  _dy.allocator()->allocate();
164 
165  // Pre-compute dx, dy and offsets for bilinear interpolation
166  precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size, sampling_policy);
167  break;
168  }
170  {
171  _scale_kernel.configure(input, nullptr, nullptr, nullptr, output, policy, border_mode, constant_border_value);
172  break;
173  }
174  default:
175  ARM_COMPUTE_ERROR("Unsupported interpolation mode");
176  }
177  if(use_padding)
178  {
179  _border_handler.configure(input, _scale_kernel.border_size(), border_mode, constant_border_value);
180  }
181 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
static Status validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value=PixelValue(), SamplingPolicy sampling_policy=SamplingPolicy::CENTER, bool use_padding=true)
Static function to check if given info will lead to a valid configuration of NEScale.
Definition: NEScale.cpp:183
Shape of a tensor.
Definition: TensorShape.h:39
const DataLayout data_layout
Definition: Im2Col.cpp:146
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
1 channel, 1 F32 per channel
Output values are defined by bilinear interpolation between the pixels.
BorderSize border_size() const override
The size of the border for that kernel.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:327
void configure(ITensor *tensor, BorderSize border_size, BorderMode border_mode, const PixelValue &constant_border_value=PixelValue())
Initialise the function.
Output values are defined to match the source pixel whose center is nearest to the sample position.
TensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: Tensor.cpp:48
1 channel, 1 S32 per channel
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void configure(const ITensor *input, const ITensor *dx, const ITensor *dy, const ITensor *offsets, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value=PixelValue(), SamplingPolicy sampling_policy=SamplingPolicy::CENTER, bool use_padding=true)
Initialise the kernel's inputs, output and interpolation policy.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Store the tensor's metadata.
Definition: TensorInfo.h:45
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:326
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.

References TensorAllocator::allocate(), Tensor::allocator(), arm_compute::AREA, ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::BILINEAR, arm_compute::test::validation::border_mode, NEScaleKernel::border_size(), NEFillBorderKernel::configure(), NEScaleKernel::configure(), arm_compute::test::validation::data_layout, ITensorInfo::data_layout(), ITensorInfo::dimension(), ITensorInfo::element_size(), arm_compute::F32, arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, ITensor::info(), TensorAllocator::init(), arm_compute::NEAREST_NEIGHBOR, arm_compute::test::validation::policy, arm_compute::S32, arm_compute::test::validation::shape, NEScale::validate(), and arm_compute::WIDTH.

◆ 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 225 of file NEScale.cpp.

226 {
227  if(_use_padding)
228  {
229  NEScheduler::get().schedule(&_border_handler, Window::DimZ);
230  }
231  NEScheduler::get().schedule(&_scale_kernel, Window::DimY);
232 }
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
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
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:96

References Window::DimY, Window::DimZ, Scheduler::get(), and IScheduler::schedule().

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
InterpolationPolicy  policy,
BorderMode  border_mode,
PixelValue  constant_border_value = PixelValue(),
SamplingPolicy  sampling_policy = SamplingPolicy::CENTER,
bool  use_padding = true 
)
static

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

Parameters
[in]inputSource tensor. Data type supported: U8/S16/F16/F32. (Written to only for border_mode != UNDEFINED)
[in]outputDestination tensor. Data type supported: Same as input. All but the lowest two dimensions must be the same size as in the input tensor, i.e. scaling is only performed within the XY-plane.
[in]policyThe interpolation type.
[in]border_modeStrategy to use for borders.
[in]constant_border_value(Optional) Constant value to use for borders if border_mode is set to CONSTANT.
[in]sampling_policy(Optional) Sampling policy used by the interpolation. Defaults to SamplingPolicy::CENTER
[in]use_padding(Optional) Is padding in use or not. Defaults to true.
Returns
a status

Definition at line 183 of file NEScale.cpp.

185 {
187  ARM_COMPUTE_RETURN_ERROR_ON(sampling_policy != SamplingPolicy::CENTER && sampling_policy != SamplingPolicy::TOP_LEFT);
188  ARM_COMPUTE_UNUSED(border_mode, constant_border_value);
189 
190  ITensorInfo *offsets = nullptr;
191  ITensorInfo *dx = nullptr;
192  ITensorInfo *dy = nullptr;
193 
194  // Get data layout and width/height indices
195  const DataLayout data_layout = input->data_layout();
198 
199  // Get the tensor shape of auxilary buffers
200  const TensorShape shape(output->dimension(idx_width), output->dimension(idx_height));
201 
202  TensorInfo tensor_info_offsets(shape, Format::S32);
203  TensorInfo tensor_info_dx(shape, Format::F32);
204  TensorInfo tensor_info_dy(shape, Format::F32);
205 
206  switch(policy)
207  {
209  offsets = &tensor_info_offsets;
210  break;
212  offsets = &tensor_info_offsets;
213  dx = &tensor_info_dx;
214  dy = &tensor_info_dy;
215  break;
216  default:
217  break;
218  }
219 
220  ARM_COMPUTE_RETURN_ON_ERROR(NEScaleKernel::validate(input->clone().get(), dx, dy, offsets, output->clone().get(),
221  policy, border_mode, constant_border_value, sampling_policy, use_padding));
222  return Status{};
223 }
Shape of a tensor.
Definition: TensorShape.h:39
const DataLayout data_layout
Definition: Im2Col.cpp:146
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#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
Output values are defined by bilinear interpolation between the pixels.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
Output values are defined to match the source pixel whose center is nearest to the sample position.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:244
Samples are taken at pixel center.
1 channel, 1 S32 per channel
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
Samples are taken at pixel top left corner.
static Status validate(const ITensorInfo *input, const ITensorInfo *dx, const ITensorInfo *dy, const ITensorInfo *offsets, ITensorInfo *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value=PixelValue(), SamplingPolicy sampling_policy=SamplingPolicy::CENTER, bool use_padding=true)
Static function to check if given info will lead to a valid configuration of NEScaleKernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
Store the tensor's metadata.
Definition: TensorInfo.h:45
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:326
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.

References ARM_COMPUTE_RETURN_ERROR_ON, ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR, ARM_COMPUTE_RETURN_ON_ERROR, ARM_COMPUTE_UNUSED, arm_compute::BILINEAR, arm_compute::test::validation::border_mode, arm_compute::CENTER, ICloneable< T >::clone(), arm_compute::test::validation::data_layout, ITensorInfo::data_layout(), ITensorInfo::dimension(), arm_compute::F32, arm_compute::get_data_layout_dimension_index(), arm_compute::HEIGHT, arm_compute::NEAREST_NEIGHBOR, arm_compute::test::validation::policy, arm_compute::S32, arm_compute::test::validation::shape, arm_compute::TOP_LEFT, NEScaleKernel::validate(), and arm_compute::WIDTH.

Referenced by NEScale::configure().


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