blob: 77c44d539b7ee29c4b7cd9de1e35a19fa90f28af [file] [log] [blame]
George Wort894066d2019-02-15 15:12:52 +00001/*
Sheri Zhang7e20e292021-02-02 11:49:34 +00002 * Copyright (c) 2019-2021 Arm Limited.
George Wort894066d2019-02-15 15:12:52 +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 */
Manuel Bottini2b84be52020-04-08 10:15:51 +010024#include "arm_compute/runtime/CL/functions/CLCropResize.h"
George Wort894066d2019-02-15 15:12:52 +000025
26#include "arm_compute/core/CL/CLHelpers.h"
George Wort894066d2019-02-15 15:12:52 +000027#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010028#include "src/core/CL/kernels/CLFillBorderKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/helpers/AutoConfiguration.h"
30#include "src/core/helpers/WindowHelpers.h"
31
George Wort894066d2019-02-15 15:12:52 +000032#include <cstddef>
33
34namespace arm_compute
35{
36namespace
37{
38inline void configure_crop(const ICLTensor *input, ICLTensor *crop_boxes, ICLTensor *box_ind, ICLTensor *output, uint32_t crop_box_ind, Coordinates &start, Coordinates &end, uint32_t &batch_index)
39{
40 batch_index = *(reinterpret_cast<int32_t *>(box_ind->ptr_to_element(Coordinates(crop_box_ind))));
41
42 // _crop_box_ind is used to index crop_boxes and retrieve the appropriate crop box.
43 // The crop box is specified by normalized coordinates [y0, x0, y1, x1].
44 const float x0 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(1, crop_box_ind)));
45 const float y0 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(0, crop_box_ind)));
46 const float x1 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(3, crop_box_ind)));
47 const float y1 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(2, crop_box_ind)));
48 // The normalized coordinates are scaled to retrieve the floating point image coordinates which are rounded to integers.
49 start = Coordinates(std::floor(x0 * (input->info()->tensor_shape()[1] - 1) + 0.5f),
50 std::floor(y0 * (input->info()->tensor_shape()[2] - 1) + 0.5f));
51 end = Coordinates(std::floor(x1 * (input->info()->tensor_shape()[1] - 1) + 0.5f),
52 std::floor(y1 * (input->info()->tensor_shape()[2] - 1) + 0.5f));
Michalis Spyroufae513c2019-10-16 17:41:33 +010053 const TensorShape out_shape(input->info()->tensor_shape()[0], static_cast<uint32_t>(abs(end[0] - start[0])) + 1, static_cast<uint32_t>(abs(end[1] - start[1])) + 1);
George Wort894066d2019-02-15 15:12:52 +000054 output->info()->set_tensor_shape(out_shape);
55}
George Wort894066d2019-02-15 15:12:52 +000056} // namespace
57
58CLCropResize::CLCropResize()
Sheri Zhang7e20e292021-02-02 11:49:34 +000059 : _input(nullptr), _boxes(nullptr), _box_ind(nullptr), _output(nullptr), _num_boxes(0), _method(), _extrapolation_value(0), _scale(), _copy(), _crop_results(), _scaled_results(), _internal_functions()
George Wort894066d2019-02-15 15:12:52 +000060{
61}
62
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010063CLCropResize::~CLCropResize() = default;
64
George Wort894066d2019-02-15 15:12:52 +000065Status CLCropResize::validate(const ITensorInfo *input, ITensorInfo *boxes, ITensorInfo *box_ind, const ITensorInfo *output,
66 Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value)
67{
68 ARM_COMPUTE_RETURN_ERROR_ON(crop_size.x <= 0 || crop_size.y <= 0);
69 ARM_COMPUTE_RETURN_ERROR_ON(method == InterpolationPolicy::AREA);
70 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
71 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]);
72 TensorInfo temp_info;
Sheri Zhang7e20e292021-02-02 11:49:34 +000073 ARM_COMPUTE_RETURN_ON_ERROR(CLCrop::validate(input->clone().get(), &temp_info, { 0, 0 }, { 1, 1 }, input->dimension(3) - 1, extrapolation_value));
George Wort894066d2019-02-15 15:12:52 +000074 if(output->total_size() > 0)
75 {
76 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32);
77 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
78 TensorShape out_shape(input->tensor_shape()[0], crop_size.x, crop_size.y, boxes->tensor_shape()[1]);
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), out_shape);
80 }
81 return Status{};
82}
83
84void CLCropResize::configure(const ICLTensor *input, ICLTensor *boxes, ICLTensor *box_ind, ICLTensor *output, Coordinates2D crop_size,
85 InterpolationPolicy method, float extrapolation_value)
86{
Manuel Bottini2b84be52020-04-08 10:15:51 +010087 configure(CLKernelLibrary::get().get_compile_context(), input, boxes, box_ind, output, crop_size, method, extrapolation_value);
88}
89
90void CLCropResize::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *boxes, ICLTensor *box_ind, ICLTensor *output, Coordinates2D crop_size,
91 InterpolationPolicy method, float extrapolation_value)
92{
93 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, boxes, box_ind);
George Wort894066d2019-02-15 15:12:52 +000094 ARM_COMPUTE_ERROR_THROW_ON(CLCropResize::validate(input->info(), boxes->info(), box_ind->info(), output->info(), crop_size, method, extrapolation_value));
95
Manuel Bottini2b84be52020-04-08 10:15:51 +010096 TensorShape output_shape = TensorShape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y, boxes->info()->tensor_shape()[1]);
97 auto_init_if_empty(*output->info(), output_shape, 1, DataType::F32);
98
George Wort894066d2019-02-15 15:12:52 +000099 _num_boxes = boxes->info()->tensor_shape()[1];
100 TensorShape out_shape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y);
101
102 _input = input;
103 _boxes = boxes;
104 _box_ind = box_ind;
105 _output = output;
106 _method = method;
107 _extrapolation_value = extrapolation_value;
108
109 // For each crop box:
110 // - The initial cropped image is produced as specified by boxes[i] from the 3D image input[box_ind[i]].
Sheri Zhang7e20e292021-02-02 11:49:34 +0000111 // Possibly using a CLCrop and up to four CLFills.
George Wort894066d2019-02-15 15:12:52 +0000112 // - A tensor is required to hold this initial cropped image.
113 // - A scale function is used to resize the cropped image to the size specified by crop_size.
114 // - A tensor is required to hold the final scaled image before it is copied into the 4D output
Sheri Zhang7e20e292021-02-02 11:49:34 +0000115 // that will hold all final cropped and scaled 3D images using CLCopy.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100116
117 // The contents of _boxes and _box_ind are required to calculate the shape
118 // of the initial cropped image and thus are required to configure the
119 // kernels used for cropping and scaling.
120 _boxes->map(CLScheduler::get().queue());
121 _box_ind->map(CLScheduler::get().queue());
122 for(unsigned int num_box = 0; num_box < _num_boxes; ++num_box)
George Wort894066d2019-02-15 15:12:52 +0000123 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000124 auto crop_tensor = std::make_unique<CLTensor>();
George Wort894066d2019-02-15 15:12:52 +0000125 TensorInfo crop_result_info(1, DataType::F32);
126 crop_result_info.set_data_layout(DataLayout::NHWC);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100127 crop_tensor->allocator()->init(crop_result_info);
128 _crop_results.emplace_back(std::move(crop_tensor));
George Wort894066d2019-02-15 15:12:52 +0000129
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000130 auto scale_tensor = std::make_unique<CLTensor>();
George Wort894066d2019-02-15 15:12:52 +0000131 TensorInfo scaled_result_info(out_shape, 1, DataType::F32);
132 scaled_result_info.set_data_layout(DataLayout::NHWC);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100133 scale_tensor->allocator()->init(scaled_result_info);
134 _scaled_results.emplace_back(std::move(scale_tensor));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100135
136 // Size of the crop box in _boxes has to be given before the configure
137 uint32_t batch_index;
138 Coordinates start{};
139 Coordinates end{};
140 configure_crop(_input, _boxes, _box_ind, _crop_results[num_box].get(), num_box, start, end, batch_index);
141
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000142 auto scale_kernel = std::make_unique<CLScale>();
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100143 scale_kernel->configure(compile_context, _crop_results[num_box].get(), _scaled_results[num_box].get(), ScaleKernelInfo{ _method, BorderMode::CONSTANT, PixelValue(_extrapolation_value), SamplingPolicy::TOP_LEFT });
Manuel Bottini2b84be52020-04-08 10:15:51 +0100144 _scale.emplace_back(std::move(scale_kernel));
145
146 Window win = calculate_max_window(*_output->info());
147 win.set(3, Window::Dimension(num_box, num_box + 1, 1));
148
Sheri Zhang7e20e292021-02-02 11:49:34 +0000149 auto copy_kernel = std::make_unique<CLCopy>();
SiCong Li3580c752020-10-14 17:00:56 +0100150 copy_kernel->configure(compile_context, _scaled_results[num_box].get(), _output, &win);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100151 _copy.emplace_back(std::move(copy_kernel));
152
153 _crop_results[num_box]->allocator()->allocate();
154 _scaled_results[num_box]->allocator()->allocate();
155
156 bool is_width_flipped = end[0] < start[0];
157 bool is_height_flipped = end[1] < start[1];
158 /** The number of rows out of bounds at the start and end of _crop_results[num_box].get(). */
159 std::array<int32_t, 2> rows_out_of_bounds{ 0 };
160 /** The number of columns out of bounds at the start and end of _crop_results[num_box].get(). */
161 std::array<int32_t, 2> cols_out_of_bounds{ 0 };
162 if(is_height_flipped)
163 {
164 rows_out_of_bounds[0] = start[1] >= static_cast<int32_t>(_input->info()->dimension(2)) ? std::min(start[1] - _input->info()->dimension(2) + 1, _crop_results[num_box].get()->info()->dimension(2)) : 0;
165 rows_out_of_bounds[1] = end[1] < 0 ? std::min(-end[1], static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(2))) : 0;
166 }
167 else
168 {
169 rows_out_of_bounds[0] = start[1] < 0 ? std::min(-start[1], static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(2))) : 0;
170 rows_out_of_bounds[1] = end[1] >= static_cast<int32_t>(_input->info()->dimension(2)) ? std::min(end[1] - _input->info()->dimension(2) + 1, _crop_results[num_box].get()->info()->dimension(2)) : 0;
171 }
172 if(is_width_flipped)
173 {
174 cols_out_of_bounds[0] = start[0] >= static_cast<int32_t>(_input->info()->dimension(1)) ? std::min(start[0] - _input->info()->dimension(1) + 1, _crop_results[num_box].get()->info()->dimension(1)) : 0;
175 cols_out_of_bounds[1] = end[0] < 0 ? std::min(-end[0], static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(1))) : 0;
176 }
177 else
178 {
179 cols_out_of_bounds[0] = start[0] < 0 ? std::min(-start[0], static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(1))) : 0;
180 cols_out_of_bounds[1] = end[0] >= static_cast<int32_t>(_input->info()->dimension(1)) ? std::min(end[0] - _input->info()->dimension(1) + 1, _crop_results[num_box].get()->info()->dimension(1)) : 0;
181 }
182
183 Window full_window = calculate_max_window(*_crop_results[num_box].get()->info());
184
185 // Full _crop_results[num_box].get() window:
186 // --------------------------------
187 // | Out of bounds |
188 // | rows before |
189 // |------------------------------|
190 // | Out of | In | Out of |
191 // | bounds | bounds | bounds |
192 // | cols | elements | cols |
193 // | before | copied | after |
194 // | | from input | |
195 // |------------------------------|
196 // | Out of bounds |
197 // | rows after |
198 // |------------------------------|
199 // Use a separate _crop_results[num_box].get() window for each section of the full _crop_results[num_box].get() window.
200 // Fill all _crop_results[num_box].get() rows that have no elements that are within the input bounds
201 // with the extrapolation value using memset.
202 // First for the rows before the in bounds rows.
203 if(rows_out_of_bounds[0] > 0)
204 {
205 Window slice_fill_rows_before(full_window);
206 slice_fill_rows_before.set(2, Window::Dimension(0, rows_out_of_bounds[0], 1));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000207 auto kernel = std::make_unique<CLFill>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100208 kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, &slice_fill_rows_before);
Sheri Zhang7e20e292021-02-02 11:49:34 +0000209 //_internal_functions.emplace_back(std::move(kernel));
210 _internal_functions.push_back(std::move(kernel));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100211 }
212
213 Window slice_in(full_window);
214 slice_in.set(2, Window::Dimension(rows_out_of_bounds[0], _crop_results[num_box].get()->info()->dimension(2) - rows_out_of_bounds[1], 1));
215 slice_in.set(1, Window::Dimension(cols_out_of_bounds[0], _crop_results[num_box].get()->info()->dimension(1) - cols_out_of_bounds[1], 1));
216
217 int rows_in_bounds = static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(2)) - rows_out_of_bounds[0] - rows_out_of_bounds[1];
218 if(rows_in_bounds > 0)
219 {
220 // Fill all elements that share a row with an in bounds element with the extrapolation value.
221 if(cols_out_of_bounds[0] > 0)
222 {
223 Window slice_fill_cols_before(slice_in);
224 slice_fill_cols_before.set(1, Window::Dimension(0, cols_out_of_bounds[0], 1));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000225 auto kernel = std::make_unique<CLFill>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100226 kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, &slice_fill_cols_before);
Sheri Zhang7e20e292021-02-02 11:49:34 +0000227 //_internal_functions.emplace_back(std::move(kernel));
228 _internal_functions.push_back(std::move(kernel));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100229 }
230
231 if(cols_out_of_bounds[1] > 0)
232 {
233 Window slice_fill_cols_after(slice_in);
234 slice_fill_cols_after.set(1, Window::Dimension(_crop_results[num_box].get()->info()->dimension(1) - cols_out_of_bounds[1], _crop_results[num_box].get()->info()->dimension(1), 1));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000235 auto kernel = std::make_unique<CLFill>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100236 kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, &slice_fill_cols_after);
Sheri Zhang7e20e292021-02-02 11:49:34 +0000237 //_internal_functions.emplace_back(std::move(kernel));
238 _internal_functions.push_back(std::move(kernel));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100239 }
240
241 // Copy all elements within the input bounds from the input tensor.
242 int cols_in_bounds = static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(1)) - cols_out_of_bounds[0] - cols_out_of_bounds[1];
243 if(cols_in_bounds > 0)
244 {
245 Coordinates2D start_in{ is_width_flipped ? start[0] - cols_out_of_bounds[0] : start[0] + cols_out_of_bounds[0],
246 is_height_flipped ? start[1] - rows_out_of_bounds[0] : start[1] + rows_out_of_bounds[0] };
247 Coordinates2D end_in{ is_width_flipped ? start_in.x - cols_in_bounds + 1 : start_in.x + cols_in_bounds - 1,
248 is_height_flipped ? start_in.y - rows_in_bounds + 1 : start_in.y + rows_in_bounds - 1 };
Sheri Zhang7e20e292021-02-02 11:49:34 +0000249 auto kernel = std::make_unique<CLCrop>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100250
251 kernel->configure(compile_context, _input, _crop_results[num_box].get(), start_in, end_in, batch_index, extrapolation_value, &slice_in);
Sheri Zhang7e20e292021-02-02 11:49:34 +0000252 //_internal_functions.emplace_back(std::move(kernel));
253 _internal_functions.push_back(std::move(kernel));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100254 }
255 }
256
257 // Fill all rows after the in bounds elements with the extrapolation value.
258 if(rows_out_of_bounds[1] > 0)
259 {
260 Window slice_fill_rows_after(full_window);
261 slice_fill_rows_after.set(2, Window::Dimension(_crop_results[num_box].get()->info()->dimension(2) - rows_out_of_bounds[1], _crop_results[num_box].get()->info()->dimension(2), 1));
Sheri Zhang7e20e292021-02-02 11:49:34 +0000262 auto kernel = std::make_unique<CLFill>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100263 kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, &slice_fill_rows_after);
Sheri Zhang7e20e292021-02-02 11:49:34 +0000264 //_internal_functions.emplace_back(std::move(kernel));
265 _internal_functions.push_back(std::move(kernel));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100266 }
George Wort894066d2019-02-15 15:12:52 +0000267 }
Manuel Bottini2b84be52020-04-08 10:15:51 +0100268 _boxes->unmap(CLScheduler::get().queue());
269 _box_ind->unmap(CLScheduler::get().queue());
270 CLScheduler::get().sync();
George Wort894066d2019-02-15 15:12:52 +0000271}
272
273void CLCropResize::run()
274{
275 ARM_COMPUTE_ERROR_ON_MSG(_output == nullptr, "Unconfigured function");
Manuel Bottini2b84be52020-04-08 10:15:51 +0100276
Sheri Zhang7e20e292021-02-02 11:49:34 +0000277 for(unsigned int i = 0; i < _internal_functions.size(); ++i)
George Wort894066d2019-02-15 15:12:52 +0000278 {
Sheri Zhang7e20e292021-02-02 11:49:34 +0000279 _internal_functions[i]->run();
George Wort894066d2019-02-15 15:12:52 +0000280 }
Manuel Bottini2b84be52020-04-08 10:15:51 +0100281
George Wort894066d2019-02-15 15:12:52 +0000282 CLScheduler::get().sync();
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100283 for(auto &kernel : _scale)
George Wort894066d2019-02-15 15:12:52 +0000284 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100285 kernel->run();
George Wort894066d2019-02-15 15:12:52 +0000286 }
287 CLScheduler::get().sync();
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100288 for(auto &kernel : _copy)
George Wort894066d2019-02-15 15:12:52 +0000289 {
Sheri Zhang7e20e292021-02-02 11:49:34 +0000290 kernel->run();
George Wort894066d2019-02-15 15:12:52 +0000291 }
292 CLScheduler::get().sync();
293}
294} // namespace arm_compute