blob: cca8b400eed5649d91d9b3f9a1d702650ea426f9 [file] [log] [blame]
George Wort05398a92019-01-25 15:38:33 +00001/*
Manuel Bottini10b38262021-02-19 18:16:44 +00002 * Copyright (c) 2019-2021 Arm Limited.
George Wort05398a92019-01-25 15:38:33 +00003 *
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 */
24#include "arm_compute/runtime/NEON/NEScheduler.h"
25
26#include "arm_compute/runtime/NEON/functions/NECropResize.h"
Manuel Bottini10b38262021-02-19 18:16:44 +000027#include "arm_compute/runtime/Tensor.h"
ramelg01cbbb0382021-09-17 17:36:57 +010028#include "src/common/utils/Log.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010029#include "src/core/NEON/kernels/NECropKernel.h"
George Wort05398a92019-01-25 15:38:33 +000030
31#include <cstddef>
32
33namespace arm_compute
34{
Michalis Spyrouebcebf12020-10-21 00:04:14 +010035NECropResize::~NECropResize() = default;
36
George Wort05398a92019-01-25 15:38:33 +000037NECropResize::NECropResize()
Michalis Spyrou299fdd32019-05-01 13:03:59 +010038 : _output(nullptr), _num_boxes(0), _method(), _extrapolation_value(0), _crop(), _scale(), _crop_results(), _scaled_results()
George Wort05398a92019-01-25 15:38:33 +000039{
40}
41
42Status NECropResize::validate(const ITensorInfo *input, const ITensorInfo *boxes, const ITensorInfo *box_ind, const ITensorInfo *output,
43 Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON(crop_size.x <= 0 || crop_size.y <= 0);
46 ARM_COMPUTE_RETURN_ERROR_ON(method == InterpolationPolicy::AREA);
47 TensorInfo temp_info;
48 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));
49 if(output->total_size() > 0)
50 {
51 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32);
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
53 TensorShape out_shape(input->tensor_shape()[0], crop_size.x, crop_size.y, boxes->tensor_shape()[1]);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), out_shape);
55 }
56 return Status{};
57}
58
59void NECropResize::configure(const ITensor *input, const ITensor *boxes, const ITensor *box_ind, ITensor *output, Coordinates2D crop_size,
60 InterpolationPolicy method, float extrapolation_value)
61{
62 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
63 ARM_COMPUTE_ERROR_THROW_ON(NECropResize::validate(input->info(), boxes->info(), box_ind->info(), output->info(), crop_size, method, extrapolation_value));
ramelg01cbbb0382021-09-17 17:36:57 +010064 ARM_COMPUTE_LOG_PARAMS(input, boxes, box_ind, output, crop_size, method, extrapolation_value);
George Wort05398a92019-01-25 15:38:33 +000065
66 _num_boxes = boxes->info()->tensor_shape()[1];
67 TensorShape out_shape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y);
68
69 _output = output;
70 _method = method;
71 _extrapolation_value = extrapolation_value;
72
73 // For each crop box:
74 // - A crop kernel is used to extract the initial cropped image as specified by boxes[i] from the 3D image input[box_ind[i]].
75 // - A tensor is required to hold this initial cropped image.
76 // - A scale function is used to resize the cropped image to the size specified by crop_size.
77 // - A tensor is required to hold the final scaled image before it is copied into the 4D output
78 // that will hold all final cropped and scaled 3D images.
Michalis Spyrou299fdd32019-05-01 13:03:59 +010079 _crop.reserve(_num_boxes);
80 _crop_results.reserve(_num_boxes);
81 _scaled_results.reserve(_num_boxes);
82 _scale.reserve(_num_boxes);
George Wort05398a92019-01-25 15:38:33 +000083
84 for(unsigned int i = 0; i < _num_boxes; ++i)
85 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000086 auto crop_tensor = std::make_unique<Tensor>();
George Wort05398a92019-01-25 15:38:33 +000087 TensorInfo crop_result_info(1, DataType::F32);
88 crop_result_info.set_data_layout(DataLayout::NHWC);
Michalis Spyrou299fdd32019-05-01 13:03:59 +010089 crop_tensor->allocator()->init(crop_result_info);
George Wort05398a92019-01-25 15:38:33 +000090
Georgios Pinitas40f51a62020-11-21 03:04:18 +000091 auto scale_tensor = std::make_unique<Tensor>();
George Wort05398a92019-01-25 15:38:33 +000092 TensorInfo scaled_result_info(out_shape, 1, DataType::F32);
93 scaled_result_info.set_data_layout(DataLayout::NHWC);
Michalis Spyrou299fdd32019-05-01 13:03:59 +010094 scale_tensor->allocator()->init(scaled_result_info);
George Wort05398a92019-01-25 15:38:33 +000095
Georgios Pinitas40f51a62020-11-21 03:04:18 +000096 auto crop_kernel = std::make_unique<NECropKernel>();
97 auto scale_kernel = std::make_unique<NEScale>();
Michalis Spyrou299fdd32019-05-01 13:03:59 +010098 crop_kernel->configure(input, boxes, box_ind, crop_tensor.get(), i, _extrapolation_value);
99
100 _crop.emplace_back(std::move(crop_kernel));
101 _scaled_results.emplace_back(std::move(scale_tensor));
102 _crop_results.emplace_back(std::move(crop_tensor));
103 _scale.emplace_back(std::move(scale_kernel));
George Wort05398a92019-01-25 15:38:33 +0000104 }
105}
106
107void NECropResize::run()
108{
109 ARM_COMPUTE_ERROR_ON_MSG(_output == nullptr, "Unconfigured function");
110
111 for(unsigned int i = 0; i < _num_boxes; ++i)
112 {
113 // Size of the crop box in _boxes and thus the shape of _crop_results[i]
114 // may not be known until run-time and so the kernels cannot be configured until then.
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100115 _crop[i]->configure_output_shape();
116 _crop_results[i]->allocator()->allocate();
117 NEScheduler::get().schedule(_crop[i].get(), Window::DimZ);
George Wort05398a92019-01-25 15:38:33 +0000118
119 // Scale the cropped image.
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100120 _scale[i]->configure(_crop_results[i].get(), _scaled_results[i].get(), ScaleKernelInfo{ _method, BorderMode::CONSTANT, PixelValue(_extrapolation_value), SamplingPolicy::TOP_LEFT, false });
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100121 _scaled_results[i]->allocator()->allocate();
122 _scale[i]->run();
George Wort05398a92019-01-25 15:38:33 +0000123
124 // Copy scaled image into output.
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100125 std::copy_n(_scaled_results[i]->buffer(), _scaled_results[i]->info()->total_size(), _output->ptr_to_element(Coordinates(0, 0, 0, i)));
George Wort05398a92019-01-25 15:38:33 +0000126 }
127}
128} // namespace arm_compute