Compute Library
 23.11
NECropResize.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
28 
29 #include "src/common/utils/Log.h"
31 
32 #include <cstddef>
33 
34 namespace arm_compute
35 {
36 NECropResize::~NECropResize() = default;
37 
39  : _output(nullptr),
40  _num_boxes(0),
41  _method(),
42  _extrapolation_value(0),
43  _crop(),
44  _scale(),
45  _crop_results(),
46  _scaled_results()
47 {
48 }
49 
51  const ITensorInfo *boxes,
52  const ITensorInfo *box_ind,
53  const ITensorInfo *output,
54  Coordinates2D crop_size,
55  InterpolationPolicy method,
56  float extrapolation_value)
57 {
58  ARM_COMPUTE_RETURN_ERROR_ON(crop_size.x <= 0 || crop_size.y <= 0);
60  TensorInfo temp_info;
61  ARM_COMPUTE_RETURN_ON_ERROR(NECropKernel::validate(input->clone().get(), boxes->clone().get(),
62  box_ind->clone().get(), &temp_info, boxes->tensor_shape()[1] - 1,
63  extrapolation_value));
64  if (output->total_size() > 0)
65  {
68  TensorShape out_shape(input->tensor_shape()[0], crop_size.x, crop_size.y, boxes->tensor_shape()[1]);
70  }
71  return Status{};
72 }
73 
75  const ITensor *boxes,
76  const ITensor *box_ind,
77  ITensor *output,
78  Coordinates2D crop_size,
79  InterpolationPolicy method,
80  float extrapolation_value)
81 {
83  ARM_COMPUTE_ERROR_THROW_ON(NECropResize::validate(input->info(), boxes->info(), box_ind->info(), output->info(),
84  crop_size, method, extrapolation_value));
85  ARM_COMPUTE_LOG_PARAMS(input, boxes, box_ind, output, crop_size, method, extrapolation_value);
86 
87  _num_boxes = boxes->info()->tensor_shape()[1];
88  TensorShape out_shape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y);
89 
90  _output = output;
91  _method = method;
92  _extrapolation_value = extrapolation_value;
93 
94  // For each crop box:
95  // - A crop kernel is used to extract the initial cropped image as specified by boxes[i] from the 3D image input[box_ind[i]].
96  // - A tensor is required to hold this initial cropped image.
97  // - A scale function is used to resize the cropped image to the size specified by crop_size.
98  // - A tensor is required to hold the final scaled image before it is copied into the 4D output
99  // that will hold all final cropped and scaled 3D images.
100  _crop.reserve(_num_boxes);
101  _crop_results.reserve(_num_boxes);
102  _scaled_results.reserve(_num_boxes);
103  _scale.reserve(_num_boxes);
104 
105  for (unsigned int i = 0; i < _num_boxes; ++i)
106  {
107  auto crop_tensor = std::make_unique<Tensor>();
108  TensorInfo crop_result_info(1, DataType::F32);
109  crop_result_info.set_data_layout(DataLayout::NHWC);
110  crop_tensor->allocator()->init(crop_result_info);
111 
112  auto scale_tensor = std::make_unique<Tensor>();
113  TensorInfo scaled_result_info(out_shape, 1, DataType::F32);
114  scaled_result_info.set_data_layout(DataLayout::NHWC);
115  scale_tensor->allocator()->init(scaled_result_info);
116 
117  auto crop_kernel = std::make_unique<NECropKernel>();
118  auto scale_kernel = std::make_unique<NEScale>();
119  crop_kernel->configure(input, boxes, box_ind, crop_tensor.get(), i, _extrapolation_value);
120 
121  _crop.emplace_back(std::move(crop_kernel));
122  _scaled_results.emplace_back(std::move(scale_tensor));
123  _crop_results.emplace_back(std::move(crop_tensor));
124  _scale.emplace_back(std::move(scale_kernel));
125  }
126 }
127 
129 {
130  ARM_COMPUTE_ERROR_ON_MSG(_output == nullptr, "Unconfigured function");
131 
132  for (unsigned int i = 0; i < _num_boxes; ++i)
133  {
134  // Size of the crop box in _boxes and thus the shape of _crop_results[i]
135  // may not be known until run-time and so the kernels cannot be configured until then.
136  _crop[i]->configure_output_shape();
137  _crop_results[i]->allocator()->allocate();
139 
140  // Scale the cropped image.
141  _scale[i]->configure(_crop_results[i].get(), _scaled_results[i].get(),
143  SamplingPolicy::TOP_LEFT, false});
144  _scaled_results[i]->allocator()->allocate();
145  _scale[i]->run();
146 
147  // Copy scaled image into output.
148  std::copy_n(_scaled_results[i]->buffer(), _scaled_results[i]->info()->total_size(),
149  _output->ptr_to_element(Coordinates(0, 0, 0, i)));
150  }
151 }
152 } // namespace arm_compute
arm_compute::BorderMode::CONSTANT
@ CONSTANT
Pixels outside the image are assumed to have a constant value.
arm_compute::ITensorInfo::tensor_shape
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
arm_compute::PixelValue
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:35
arm_compute::Coordinates2D::y
int32_t y
Y coordinates.
Definition: Types.h:387
arm_compute::DataLayout::NHWC
@ NHWC
Num samples, height, width, channels.
arm_compute::SamplingPolicy::TOP_LEFT
@ TOP_LEFT
Samples are taken at pixel top left corner.
arm_compute::InterpolationPolicy::AREA
@ AREA
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
arm_compute::TensorShape
Shape of a tensor.
Definition: TensorShape.h:39
NECropResize.h
arm_compute::NECropResize::_scaled_results
std::vector< std::unique_ptr< Tensor > > _scaled_results
Definition: NECropResize.h:122
arm_compute::InterpolationPolicy
InterpolationPolicy
Interpolation method.
Definition: Types.h:360
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
arm_compute::NECropResize::validate
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.
Definition: NECropResize.cpp:50
NECropKernel.h
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:205
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::NECropResize::_extrapolation_value
float _extrapolation_value
Definition: NECropResize.h:117
arm_compute::ITensor::info
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:838
ARM_COMPUTE_ERROR_ON_MSG
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
ARM_COMPUTE_RETURN_ERROR_ON
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:298
arm_compute::Scheduler::get
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94
arm_compute::NECropResize::_output
ITensor * _output
Definition: NECropResize.h:114
arm_compute::TensorInfo::set_data_layout
ITensorInfo & set_data_layout(const DataLayout &data_layout) override
Set the data layout of the tensor.
Definition: TensorInfo.cpp:404
arm_compute::Status
Status class.
Definition: Error.h:52
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:625
arm_compute::NECropKernel::validate
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.
Definition: NECropKernel.cpp:222
arm_compute::NECropResize::~NECropResize
~NECropResize()
Default destructor.
arm_compute::NECropResize::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.
Definition: NECropResize.cpp:74
arm_compute::Coordinates
Coordinates of an item.
Definition: Coordinates.h:37
Tensor.h
arm_compute::Coordinates2D::x
int32_t x
X coordinates.
Definition: Types.h:386
arm_compute::NECropResize::NECropResize
NECropResize()
Default constructor.
Definition: NECropResize.cpp:38
arm_compute::NECropResize::_method
InterpolationPolicy _method
Definition: NECropResize.h:116
arm_compute::NECropResize::_scale
std::vector< std::unique_ptr< NEScale > > _scale
Definition: NECropResize.h:120
arm_compute::IScheduler::schedule
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
arm_compute::misc::ICloneable::clone
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
NEScheduler.h
arm_compute::TensorInfo
Store the tensor's metadata.
Definition: TensorInfo.h:41
arm_compute::NECropResize::_num_boxes
size_t _num_boxes
Definition: NECropResize.h:115
arm_compute::NECropResize::_crop
std::vector< std::unique_ptr< NECropKernel > > _crop
Definition: NECropResize.h:119
arm_compute::NECropResize::_crop_results
std::vector< std::unique_ptr< Tensor > > _crop_results
Definition: NECropResize.h:121
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::ITensor::ptr_to_element
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
arm_compute::ScaleKernelInfo
Definition: KernelDescriptors.h:199
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:294
arm_compute::Window::DimZ
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Log.h
arm_compute::Coordinates2D
Coordinate type.
Definition: Types.h:384
arm_compute::NECropResize::run
void run() override
Run the kernels contained in the function.
Definition: NECropResize.cpp:128
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
arm_compute::DataType::F32
@ F32
32-bit floating-point number
arm_compute::test::validation::info
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ARM_COMPUTE_LOG_PARAMS
#define ARM_COMPUTE_LOG_PARAMS(...)
Definition: Log.h:35
arm_compute::ITensorInfo::total_size
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486