blob: 529f7bfb3e5b22b0f128558c2df78e0fad14ed9b [file] [log] [blame]
George Wort894066d2019-02-15 15:12:52 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 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"
George Wort894066d2019-02-15 15:12:52 +000028
29#include <cstddef>
30
31namespace arm_compute
32{
33namespace
34{
35inline 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)
36{
37 batch_index = *(reinterpret_cast<int32_t *>(box_ind->ptr_to_element(Coordinates(crop_box_ind))));
38
39 // _crop_box_ind is used to index crop_boxes and retrieve the appropriate crop box.
40 // The crop box is specified by normalized coordinates [y0, x0, y1, x1].
41 const float x0 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(1, crop_box_ind)));
42 const float y0 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(0, crop_box_ind)));
43 const float x1 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(3, crop_box_ind)));
44 const float y1 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(2, crop_box_ind)));
45 // The normalized coordinates are scaled to retrieve the floating point image coordinates which are rounded to integers.
46 start = Coordinates(std::floor(x0 * (input->info()->tensor_shape()[1] - 1) + 0.5f),
47 std::floor(y0 * (input->info()->tensor_shape()[2] - 1) + 0.5f));
48 end = Coordinates(std::floor(x1 * (input->info()->tensor_shape()[1] - 1) + 0.5f),
49 std::floor(y1 * (input->info()->tensor_shape()[2] - 1) + 0.5f));
Michalis Spyroufae513c2019-10-16 17:41:33 +010050 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 +000051 output->info()->set_tensor_shape(out_shape);
52}
George Wort894066d2019-02-15 15:12:52 +000053} // namespace
54
55CLCropResize::CLCropResize()
Manuel Bottini2b84be52020-04-08 10:15:51 +010056 : _input(nullptr), _boxes(nullptr), _box_ind(nullptr), _output(nullptr), _num_boxes(0), _method(), _extrapolation_value(0), _scale(), _copy(), _crop_results(), _scaled_results(), _internal_kernels()
George Wort894066d2019-02-15 15:12:52 +000057{
58}
59
60Status CLCropResize::validate(const ITensorInfo *input, ITensorInfo *boxes, ITensorInfo *box_ind, const ITensorInfo *output,
61 Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value)
62{
63 ARM_COMPUTE_RETURN_ERROR_ON(crop_size.x <= 0 || crop_size.y <= 0);
64 ARM_COMPUTE_RETURN_ERROR_ON(method == InterpolationPolicy::AREA);
65 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
66 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]);
67 TensorInfo temp_info;
68 ARM_COMPUTE_RETURN_ON_ERROR(CLCropKernel::validate(input->clone().get(), &temp_info, { 0, 0 }, { 1, 1 }, input->dimension(3) - 1, extrapolation_value));
69 if(output->total_size() > 0)
70 {
71 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32);
72 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
73 TensorShape out_shape(input->tensor_shape()[0], crop_size.x, crop_size.y, boxes->tensor_shape()[1]);
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), out_shape);
75 }
76 return Status{};
77}
78
79void CLCropResize::configure(const ICLTensor *input, ICLTensor *boxes, ICLTensor *box_ind, ICLTensor *output, Coordinates2D crop_size,
80 InterpolationPolicy method, float extrapolation_value)
81{
Manuel Bottini2b84be52020-04-08 10:15:51 +010082 configure(CLKernelLibrary::get().get_compile_context(), input, boxes, box_ind, output, crop_size, method, extrapolation_value);
83}
84
85void CLCropResize::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *boxes, ICLTensor *box_ind, ICLTensor *output, Coordinates2D crop_size,
86 InterpolationPolicy method, float extrapolation_value)
87{
88 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, boxes, box_ind);
George Wort894066d2019-02-15 15:12:52 +000089 ARM_COMPUTE_ERROR_THROW_ON(CLCropResize::validate(input->info(), boxes->info(), box_ind->info(), output->info(), crop_size, method, extrapolation_value));
90
Manuel Bottini2b84be52020-04-08 10:15:51 +010091 TensorShape output_shape = TensorShape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y, boxes->info()->tensor_shape()[1]);
92 auto_init_if_empty(*output->info(), output_shape, 1, DataType::F32);
93
George Wort894066d2019-02-15 15:12:52 +000094 _num_boxes = boxes->info()->tensor_shape()[1];
95 TensorShape out_shape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y);
96
97 _input = input;
98 _boxes = boxes;
99 _box_ind = box_ind;
100 _output = output;
101 _method = method;
102 _extrapolation_value = extrapolation_value;
103
104 // For each crop box:
105 // - The initial cropped image is produced as specified by boxes[i] from the 3D image input[box_ind[i]].
106 // Possibly using a CLCropKernel and up to four CLMemsetKernels.
107 // - A tensor is required to hold this initial cropped image.
108 // - A scale function is used to resize the cropped image to the size specified by crop_size.
109 // - A tensor is required to hold the final scaled image before it is copied into the 4D output
110 // that will hold all final cropped and scaled 3D images using CLCopyKernel.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100111
112 // The contents of _boxes and _box_ind are required to calculate the shape
113 // of the initial cropped image and thus are required to configure the
114 // kernels used for cropping and scaling.
115 _boxes->map(CLScheduler::get().queue());
116 _box_ind->map(CLScheduler::get().queue());
117 for(unsigned int num_box = 0; num_box < _num_boxes; ++num_box)
George Wort894066d2019-02-15 15:12:52 +0000118 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100119 auto crop_tensor = support::cpp14::make_unique<CLTensor>();
George Wort894066d2019-02-15 15:12:52 +0000120 TensorInfo crop_result_info(1, DataType::F32);
121 crop_result_info.set_data_layout(DataLayout::NHWC);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100122 crop_tensor->allocator()->init(crop_result_info);
123 _crop_results.emplace_back(std::move(crop_tensor));
George Wort894066d2019-02-15 15:12:52 +0000124
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100125 auto scale_tensor = support::cpp14::make_unique<CLTensor>();
George Wort894066d2019-02-15 15:12:52 +0000126 TensorInfo scaled_result_info(out_shape, 1, DataType::F32);
127 scaled_result_info.set_data_layout(DataLayout::NHWC);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100128 scale_tensor->allocator()->init(scaled_result_info);
129 _scaled_results.emplace_back(std::move(scale_tensor));
Manuel Bottini2b84be52020-04-08 10:15:51 +0100130
131 // Size of the crop box in _boxes has to be given before the configure
132 uint32_t batch_index;
133 Coordinates start{};
134 Coordinates end{};
135 configure_crop(_input, _boxes, _box_ind, _crop_results[num_box].get(), num_box, start, end, batch_index);
136
137 auto scale_kernel = support::cpp14::make_unique<CLScale>();
Sang-Hoon Parkccd94962020-06-09 12:09:24 +0100138 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 +0100139 _scale.emplace_back(std::move(scale_kernel));
140
141 Window win = calculate_max_window(*_output->info());
142 win.set(3, Window::Dimension(num_box, num_box + 1, 1));
143
144 auto copy_kernel = support::cpp14::make_unique<CLCopyKernel>();
145 copy_kernel->configure(compile_context, _scaled_results[num_box].get(), _output, PaddingList(), &win);
146 _copy.emplace_back(std::move(copy_kernel));
147
148 _crop_results[num_box]->allocator()->allocate();
149 _scaled_results[num_box]->allocator()->allocate();
150
151 bool is_width_flipped = end[0] < start[0];
152 bool is_height_flipped = end[1] < start[1];
153 /** The number of rows out of bounds at the start and end of _crop_results[num_box].get(). */
154 std::array<int32_t, 2> rows_out_of_bounds{ 0 };
155 /** The number of columns out of bounds at the start and end of _crop_results[num_box].get(). */
156 std::array<int32_t, 2> cols_out_of_bounds{ 0 };
157 if(is_height_flipped)
158 {
159 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;
160 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;
161 }
162 else
163 {
164 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;
165 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;
166 }
167 if(is_width_flipped)
168 {
169 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;
170 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;
171 }
172 else
173 {
174 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;
175 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;
176 }
177
178 Window full_window = calculate_max_window(*_crop_results[num_box].get()->info());
179
180 // Full _crop_results[num_box].get() window:
181 // --------------------------------
182 // | Out of bounds |
183 // | rows before |
184 // |------------------------------|
185 // | Out of | In | Out of |
186 // | bounds | bounds | bounds |
187 // | cols | elements | cols |
188 // | before | copied | after |
189 // | | from input | |
190 // |------------------------------|
191 // | Out of bounds |
192 // | rows after |
193 // |------------------------------|
194 // Use a separate _crop_results[num_box].get() window for each section of the full _crop_results[num_box].get() window.
195 // Fill all _crop_results[num_box].get() rows that have no elements that are within the input bounds
196 // with the extrapolation value using memset.
197 // First for the rows before the in bounds rows.
198 if(rows_out_of_bounds[0] > 0)
199 {
200 Window slice_fill_rows_before(full_window);
201 slice_fill_rows_before.set(2, Window::Dimension(0, rows_out_of_bounds[0], 1));
202 auto kernel = arm_compute::support::cpp14::make_unique<CLMemsetKernel>();
203 kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, &slice_fill_rows_before);
204 _internal_kernels.push_back(std::move(kernel));
205 }
206
207 Window slice_in(full_window);
208 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));
209 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));
210
211 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];
212 if(rows_in_bounds > 0)
213 {
214 // Fill all elements that share a row with an in bounds element with the extrapolation value.
215 if(cols_out_of_bounds[0] > 0)
216 {
217 Window slice_fill_cols_before(slice_in);
218 slice_fill_cols_before.set(1, Window::Dimension(0, cols_out_of_bounds[0], 1));
219 auto kernel = arm_compute::support::cpp14::make_unique<CLMemsetKernel>();
220 kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, &slice_fill_cols_before);
221 _internal_kernels.push_back(std::move(kernel));
222 }
223
224 if(cols_out_of_bounds[1] > 0)
225 {
226 Window slice_fill_cols_after(slice_in);
227 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));
228 auto kernel = arm_compute::support::cpp14::make_unique<CLMemsetKernel>();
229 kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, &slice_fill_cols_after);
230 _internal_kernels.push_back(std::move(kernel));
231 }
232
233 // Copy all elements within the input bounds from the input tensor.
234 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];
235 if(cols_in_bounds > 0)
236 {
237 Coordinates2D start_in{ is_width_flipped ? start[0] - cols_out_of_bounds[0] : start[0] + cols_out_of_bounds[0],
238 is_height_flipped ? start[1] - rows_out_of_bounds[0] : start[1] + rows_out_of_bounds[0] };
239 Coordinates2D end_in{ is_width_flipped ? start_in.x - cols_in_bounds + 1 : start_in.x + cols_in_bounds - 1,
240 is_height_flipped ? start_in.y - rows_in_bounds + 1 : start_in.y + rows_in_bounds - 1 };
241 auto kernel = arm_compute::support::cpp14::make_unique<CLCropKernel>();
242
243 kernel->configure(compile_context, _input, _crop_results[num_box].get(), start_in, end_in, batch_index, extrapolation_value, &slice_in);
244 _internal_kernels.push_back(std::move(kernel));
245 }
246 }
247
248 // Fill all rows after the in bounds elements with the extrapolation value.
249 if(rows_out_of_bounds[1] > 0)
250 {
251 Window slice_fill_rows_after(full_window);
252 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));
253 auto kernel = arm_compute::support::cpp14::make_unique<CLMemsetKernel>();
254 kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, &slice_fill_rows_after);
255 _internal_kernels.push_back(std::move(kernel));
256 }
George Wort894066d2019-02-15 15:12:52 +0000257 }
Manuel Bottini2b84be52020-04-08 10:15:51 +0100258 _boxes->unmap(CLScheduler::get().queue());
259 _box_ind->unmap(CLScheduler::get().queue());
260 CLScheduler::get().sync();
George Wort894066d2019-02-15 15:12:52 +0000261}
262
263void CLCropResize::run()
264{
265 ARM_COMPUTE_ERROR_ON_MSG(_output == nullptr, "Unconfigured function");
Manuel Bottini2b84be52020-04-08 10:15:51 +0100266
267 for(unsigned int i = 0; i < _internal_kernels.size(); ++i)
George Wort894066d2019-02-15 15:12:52 +0000268 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100269 CLScheduler::get().enqueue(*(_internal_kernels[i]));
George Wort894066d2019-02-15 15:12:52 +0000270 }
Manuel Bottini2b84be52020-04-08 10:15:51 +0100271
George Wort894066d2019-02-15 15:12:52 +0000272 CLScheduler::get().sync();
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100273 for(auto &kernel : _scale)
George Wort894066d2019-02-15 15:12:52 +0000274 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100275 kernel->run();
George Wort894066d2019-02-15 15:12:52 +0000276 }
277 CLScheduler::get().sync();
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100278 for(auto &kernel : _copy)
George Wort894066d2019-02-15 15:12:52 +0000279 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100280 CLScheduler::get().enqueue(*kernel, true);
George Wort894066d2019-02-15 15:12:52 +0000281 }
282 CLScheduler::get().sync();
283}
284} // namespace arm_compute