Compute Library
 21.02
NECropResize.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 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 <cstddef>
30 
31 namespace arm_compute
32 {
33 NECropResize::~NECropResize() = default;
34 
36  : _output(nullptr), _num_boxes(0), _method(), _extrapolation_value(0), _crop(), _scale(), _crop_results(), _scaled_results()
37 {
38 }
39 
40 Status NECropResize::validate(const ITensorInfo *input, const ITensorInfo *boxes, const ITensorInfo *box_ind, const ITensorInfo *output,
41  Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value)
42 {
43  ARM_COMPUTE_RETURN_ERROR_ON(crop_size.x <= 0 || crop_size.y <= 0);
45  TensorInfo temp_info;
46  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));
47  if(output->total_size() > 0)
48  {
51  TensorShape out_shape(input->tensor_shape()[0], crop_size.x, crop_size.y, boxes->tensor_shape()[1]);
53  }
54  return Status{};
55 }
56 
57 void NECropResize::configure(const ITensor *input, const ITensor *boxes, const ITensor *box_ind, ITensor *output, Coordinates2D crop_size,
58  InterpolationPolicy method, float extrapolation_value)
59 {
60  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
61  ARM_COMPUTE_ERROR_THROW_ON(NECropResize::validate(input->info(), boxes->info(), box_ind->info(), output->info(), crop_size, method, extrapolation_value));
62 
63  _num_boxes = boxes->info()->tensor_shape()[1];
64  TensorShape out_shape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y);
65 
66  _output = output;
67  _method = method;
68  _extrapolation_value = extrapolation_value;
69 
70  // For each crop box:
71  // - A crop kernel is used to extract the initial cropped image as specified by boxes[i] from the 3D image input[box_ind[i]].
72  // - A tensor is required to hold this initial cropped image.
73  // - A scale function is used to resize the cropped image to the size specified by crop_size.
74  // - A tensor is required to hold the final scaled image before it is copied into the 4D output
75  // that will hold all final cropped and scaled 3D images.
76  _crop.reserve(_num_boxes);
77  _crop_results.reserve(_num_boxes);
78  _scaled_results.reserve(_num_boxes);
79  _scale.reserve(_num_boxes);
80 
81  for(unsigned int i = 0; i < _num_boxes; ++i)
82  {
83  auto crop_tensor = std::make_unique<Tensor>();
84  TensorInfo crop_result_info(1, DataType::F32);
85  crop_result_info.set_data_layout(DataLayout::NHWC);
86  crop_tensor->allocator()->init(crop_result_info);
87 
88  auto scale_tensor = std::make_unique<Tensor>();
89  TensorInfo scaled_result_info(out_shape, 1, DataType::F32);
90  scaled_result_info.set_data_layout(DataLayout::NHWC);
91  scale_tensor->allocator()->init(scaled_result_info);
92 
93  auto crop_kernel = std::make_unique<NECropKernel>();
94  auto scale_kernel = std::make_unique<NEScale>();
95  crop_kernel->configure(input, boxes, box_ind, crop_tensor.get(), i, _extrapolation_value);
96 
97  _crop.emplace_back(std::move(crop_kernel));
98  _scaled_results.emplace_back(std::move(scale_tensor));
99  _crop_results.emplace_back(std::move(crop_tensor));
100  _scale.emplace_back(std::move(scale_kernel));
101  }
102 }
103 
105 {
106  ARM_COMPUTE_ERROR_ON_MSG(_output == nullptr, "Unconfigured function");
107 
108  for(unsigned int i = 0; i < _num_boxes; ++i)
109  {
110  // Size of the crop box in _boxes and thus the shape of _crop_results[i]
111  // may not be known until run-time and so the kernels cannot be configured until then.
112  _crop[i]->configure_output_shape();
113  _crop_results[i]->allocator()->allocate();
115 
116  // Scale the cropped image.
118  _scaled_results[i]->allocator()->allocate();
119  _scale[i]->run();
120 
121  // Copy scaled image into output.
122  std::copy_n(_scaled_results[i]->buffer(), _scaled_results[i]->info()->total_size(), _output->ptr_to_element(Coordinates(0, 0, 0, i)));
123  }
124 }
125 } // namespace arm_compute
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
InterpolationPolicy
Interpolation method.
Definition: Types.h:392
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
Shape of a tensor.
Definition: TensorShape.h:39
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.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
~NECropResize()
Default destructor.
std::vector< std::unique_ptr< NECropKernel > > _crop
Definition: NECropResize.h:100
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
1 channel, 1 F32 per channel
std::vector< std::unique_ptr< Tensor > > _scaled_results
Definition: NECropResize.h:103
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for Neon tensor.
Definition: ITensor.h:36
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
Copyright (c) 2017-2021 Arm Limited.
int32_t x
X coordinates.
Definition: Types.h:465
int32_t y
Y coordinates.
Definition: Types.h:466
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
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.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Coordinates of an item.
Definition: Coordinates.h:37
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Samples are taken at pixel top left corner.
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.
Output values are determined by averaging the source pixels whose areas fall under the area of the de...
std::vector< std::unique_ptr< NEScale > > _scale
Definition: NECropResize.h:101
NECropResize()
Default constructor.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
virtual void schedule(ICPPKernel *kernel, const Hints &hints)=0
Runs the kernel in the same thread as the caller synchronously.
Coordinate type.
Definition: Types.h:463
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
void run() override
Run the kernels contained in the function.
Num samples, height, width, channels.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
InterpolationPolicy _method
Definition: NECropResize.h:97
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:694
std::vector< std::unique_ptr< Tensor > > _crop_results
Definition: NECropResize.h:102
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94