blob: 5e1278df5be1326697d14c3720d20b212f2a7e7a [file] [log] [blame]
George Wort894066d2019-02-15 15:12:52 +00001/*
2 * Copyright (c) 2019 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 */
24
25#include "arm_compute/core/CL/CLHelpers.h"
26
27#include "arm_compute/runtime/CL/CLScheduler.h"
28#include "arm_compute/runtime/CL/functions/CLCropResize.h"
29
30#include <cstddef>
31
32namespace arm_compute
33{
34namespace
35{
36inline 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)
37{
38 batch_index = *(reinterpret_cast<int32_t *>(box_ind->ptr_to_element(Coordinates(crop_box_ind))));
39
40 // _crop_box_ind is used to index crop_boxes and retrieve the appropriate crop box.
41 // The crop box is specified by normalized coordinates [y0, x0, y1, x1].
42 const float x0 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(1, crop_box_ind)));
43 const float y0 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(0, crop_box_ind)));
44 const float x1 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(3, crop_box_ind)));
45 const float y1 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(2, crop_box_ind)));
46 // The normalized coordinates are scaled to retrieve the floating point image coordinates which are rounded to integers.
47 start = Coordinates(std::floor(x0 * (input->info()->tensor_shape()[1] - 1) + 0.5f),
48 std::floor(y0 * (input->info()->tensor_shape()[2] - 1) + 0.5f));
49 end = Coordinates(std::floor(x1 * (input->info()->tensor_shape()[1] - 1) + 0.5f),
50 std::floor(y1 * (input->info()->tensor_shape()[2] - 1) + 0.5f));
Michalis Spyroufae513c2019-10-16 17:41:33 +010051 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 +000052 output->info()->set_tensor_shape(out_shape);
53}
54
55inline void run_crop(const ICLTensor *input, ICLTensor *output, uint32_t batch_index, Coordinates start, Coordinates end, float extrapolation_value)
56{
57 bool is_width_flipped = end[0] < start[0];
58 bool is_height_flipped = end[1] < start[1];
59 /** The number of rows out of bounds at the start and end of output. */
Michalis Spyroua4f378d2019-04-26 14:54:54 +010060 std::array<int32_t, 2> rows_out_of_bounds{ 0 };
George Wort894066d2019-02-15 15:12:52 +000061 /** The number of columns out of bounds at the start and end of output. */
Michalis Spyroua4f378d2019-04-26 14:54:54 +010062 std::array<int32_t, 2> cols_out_of_bounds{ 0 };
George Wort894066d2019-02-15 15:12:52 +000063 if(is_height_flipped)
64 {
65 rows_out_of_bounds[0] = start[1] >= static_cast<int32_t>(input->info()->dimension(2)) ? std::min(start[1] - input->info()->dimension(2) + 1, output->info()->dimension(2)) : 0;
66 rows_out_of_bounds[1] = end[1] < 0 ? std::min(-end[1], static_cast<int32_t>(output->info()->dimension(2))) : 0;
67 }
68 else
69 {
70 rows_out_of_bounds[0] = start[1] < 0 ? std::min(-start[1], static_cast<int32_t>(output->info()->dimension(2))) : 0;
71 rows_out_of_bounds[1] = end[1] >= static_cast<int32_t>(input->info()->dimension(2)) ? std::min(end[1] - input->info()->dimension(2) + 1, output->info()->dimension(2)) : 0;
72 }
73 if(is_width_flipped)
74 {
75 cols_out_of_bounds[0] = start[0] >= static_cast<int32_t>(input->info()->dimension(1)) ? std::min(start[0] - input->info()->dimension(1) + 1, output->info()->dimension(1)) : 0;
76 cols_out_of_bounds[1] = end[0] < 0 ? std::min(-end[0], static_cast<int32_t>(output->info()->dimension(1))) : 0;
77 }
78 else
79 {
80 cols_out_of_bounds[0] = start[0] < 0 ? std::min(-start[0], static_cast<int32_t>(output->info()->dimension(1))) : 0;
81 cols_out_of_bounds[1] = end[0] >= static_cast<int32_t>(input->info()->dimension(1)) ? std::min(end[0] - input->info()->dimension(1) + 1, output->info()->dimension(1)) : 0;
82 }
83
84 Window full_window = calculate_max_window(*output->info());
85
86 // Full output window:
87 // --------------------------------
88 // | Out of bounds |
89 // | rows before |
90 // |------------------------------|
91 // | Out of | In | Out of |
92 // | bounds | bounds | bounds |
93 // | cols | elements | cols |
94 // | before | copied | after |
95 // | | from input | |
96 // |------------------------------|
97 // | Out of bounds |
98 // | rows after |
99 // |------------------------------|
100 // Use a separate output window for each section of the full output window.
101 // Fill all output rows that have no elements that are within the input bounds
102 // with the extrapolation value using memset.
103 // First for the rows before the in bounds rows.
104 if(rows_out_of_bounds[0] > 0)
105 {
106 Window slice_fill_rows_before(full_window);
107 slice_fill_rows_before.set(2, Window::Dimension(0, rows_out_of_bounds[0], 1));
108 auto kernel = arm_compute::support::cpp14::make_unique<CLMemsetKernel>();
109 kernel->configure(output, extrapolation_value, &slice_fill_rows_before);
110 CLScheduler::get().enqueue(*kernel);
111 }
112
113 Window slice_in(full_window);
114 slice_in.set(2, Window::Dimension(rows_out_of_bounds[0], output->info()->dimension(2) - rows_out_of_bounds[1], 1));
115 slice_in.set(1, Window::Dimension(cols_out_of_bounds[0], output->info()->dimension(1) - cols_out_of_bounds[1], 1));
116
117 int rows_in_bounds = static_cast<int32_t>(output->info()->dimension(2)) - rows_out_of_bounds[0] - rows_out_of_bounds[1];
118 if(rows_in_bounds > 0)
119 {
120 // Fill all elements that share a row with an in bounds element with the extrapolation value.
121 if(cols_out_of_bounds[0] > 0)
122 {
123 Window slice_fill_cols_before(slice_in);
124 slice_fill_cols_before.set(1, Window::Dimension(0, cols_out_of_bounds[0], 1));
125 auto kernel = arm_compute::support::cpp14::make_unique<CLMemsetKernel>();
126 kernel->configure(output, extrapolation_value, &slice_fill_cols_before);
127 CLScheduler::get().enqueue(*kernel);
128 }
129
130 if(cols_out_of_bounds[1] > 0)
131 {
132 Window slice_fill_cols_after(slice_in);
133 slice_fill_cols_after.set(1, Window::Dimension(output->info()->dimension(1) - cols_out_of_bounds[1], output->info()->dimension(1), 1));
134 auto kernel = arm_compute::support::cpp14::make_unique<CLMemsetKernel>();
135 kernel->configure(output, extrapolation_value, &slice_fill_cols_after);
136 CLScheduler::get().enqueue(*kernel);
137 }
138
139 // Copy all elements within the input bounds from the input tensor.
140 int cols_in_bounds = static_cast<int32_t>(output->info()->dimension(1)) - cols_out_of_bounds[0] - cols_out_of_bounds[1];
141 if(cols_in_bounds > 0)
142 {
143 Coordinates2D start_in{ is_width_flipped ? start[0] - cols_out_of_bounds[0] : start[0] + cols_out_of_bounds[0],
144 is_height_flipped ? start[1] - rows_out_of_bounds[0] : start[1] + rows_out_of_bounds[0] };
145 Coordinates2D end_in{ is_width_flipped ? start_in.x - cols_in_bounds + 1 : start_in.x + cols_in_bounds - 1,
146 is_height_flipped ? start_in.y - rows_in_bounds + 1 : start_in.y + rows_in_bounds - 1 };
147 auto kernel = arm_compute::support::cpp14::make_unique<CLCropKernel>();
148
149 kernel->configure(input, output, start_in, end_in, batch_index, extrapolation_value, &slice_in);
150 CLScheduler::get().enqueue(*kernel);
151 }
152 }
153
154 // Fill all rows after the in bounds elements with the extrapolation value.
155 if(rows_out_of_bounds[1] > 0)
156 {
157 Window slice_fill_rows_after(full_window);
158 slice_fill_rows_after.set(2, Window::Dimension(output->info()->dimension(2) - rows_out_of_bounds[1], output->info()->dimension(2), 1));
159 auto kernel = arm_compute::support::cpp14::make_unique<CLMemsetKernel>();
160 kernel->configure(output, extrapolation_value, &slice_fill_rows_after);
161 CLScheduler::get().enqueue(*kernel);
162 }
163}
164} // namespace
165
166CLCropResize::CLCropResize()
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100167 : _input(nullptr), _boxes(nullptr), _box_ind(nullptr), _output(nullptr), _num_boxes(0), _method(), _extrapolation_value(0), _scale(), _copy(), _crop_results(), _scaled_results()
George Wort894066d2019-02-15 15:12:52 +0000168{
169}
170
171Status CLCropResize::validate(const ITensorInfo *input, ITensorInfo *boxes, ITensorInfo *box_ind, const ITensorInfo *output,
172 Coordinates2D crop_size, InterpolationPolicy method, float extrapolation_value)
173{
174 ARM_COMPUTE_RETURN_ERROR_ON(crop_size.x <= 0 || crop_size.y <= 0);
175 ARM_COMPUTE_RETURN_ERROR_ON(method == InterpolationPolicy::AREA);
176 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
177 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]);
178 TensorInfo temp_info;
179 ARM_COMPUTE_RETURN_ON_ERROR(CLCropKernel::validate(input->clone().get(), &temp_info, { 0, 0 }, { 1, 1 }, input->dimension(3) - 1, extrapolation_value));
180 if(output->total_size() > 0)
181 {
182 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32);
183 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
184 TensorShape out_shape(input->tensor_shape()[0], crop_size.x, crop_size.y, boxes->tensor_shape()[1]);
185 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), out_shape);
186 }
187 return Status{};
188}
189
190void CLCropResize::configure(const ICLTensor *input, ICLTensor *boxes, ICLTensor *box_ind, ICLTensor *output, Coordinates2D crop_size,
191 InterpolationPolicy method, float extrapolation_value)
192{
193 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
194 ARM_COMPUTE_ERROR_THROW_ON(CLCropResize::validate(input->info(), boxes->info(), box_ind->info(), output->info(), crop_size, method, extrapolation_value));
195
196 _num_boxes = boxes->info()->tensor_shape()[1];
197 TensorShape out_shape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y);
198
199 _input = input;
200 _boxes = boxes;
201 _box_ind = box_ind;
202 _output = output;
203 _method = method;
204 _extrapolation_value = extrapolation_value;
205
206 // For each crop box:
207 // - The initial cropped image is produced as specified by boxes[i] from the 3D image input[box_ind[i]].
208 // Possibly using a CLCropKernel and up to four CLMemsetKernels.
209 // - A tensor is required to hold this initial cropped image.
210 // - A scale function is used to resize the cropped image to the size specified by crop_size.
211 // - A tensor is required to hold the final scaled image before it is copied into the 4D output
212 // that will hold all final cropped and scaled 3D images using CLCopyKernel.
George Wort894066d2019-02-15 15:12:52 +0000213 for(unsigned int i = 0; i < _num_boxes; ++i)
214 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100215 auto crop_tensor = support::cpp14::make_unique<CLTensor>();
George Wort894066d2019-02-15 15:12:52 +0000216 TensorInfo crop_result_info(1, DataType::F32);
217 crop_result_info.set_data_layout(DataLayout::NHWC);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100218 crop_tensor->allocator()->init(crop_result_info);
219 _crop_results.emplace_back(std::move(crop_tensor));
George Wort894066d2019-02-15 15:12:52 +0000220
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100221 auto scale_tensor = support::cpp14::make_unique<CLTensor>();
George Wort894066d2019-02-15 15:12:52 +0000222 TensorInfo scaled_result_info(out_shape, 1, DataType::F32);
223 scaled_result_info.set_data_layout(DataLayout::NHWC);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100224 scale_tensor->allocator()->init(scaled_result_info);
225 _scaled_results.emplace_back(std::move(scale_tensor));
George Wort894066d2019-02-15 15:12:52 +0000226 }
227}
228
229void CLCropResize::run()
230{
231 ARM_COMPUTE_ERROR_ON_MSG(_output == nullptr, "Unconfigured function");
232 // The contents of _boxes and _box_ind are required to calculate the shape
233 // of the initial cropped image and thus are required to configure the
234 // kernels used for cropping and scaling.
235 _boxes->map(CLScheduler::get().queue());
236 _box_ind->map(CLScheduler::get().queue());
237 for(unsigned int i = 0; i < _num_boxes; ++i)
238 {
239 // Size of the crop box in _boxes and thus the shape of _crop_results[i]
240 // may not be known until run-time and so the kernels cannot be configured until then.
241 uint32_t batch_index;
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100242 Coordinates start{};
243 Coordinates end{};
244 configure_crop(_input, _boxes, _box_ind, _crop_results[i].get(), i, start, end, batch_index);
245
246 auto scale_kernel = support::cpp14::make_unique<CLScale>();
247 scale_kernel->configure(_crop_results[i].get(), _scaled_results[i].get(), _method, BorderMode::CONSTANT, PixelValue(_extrapolation_value), SamplingPolicy::TOP_LEFT);
248 _scale.emplace_back(std::move(scale_kernel));
George Wort894066d2019-02-15 15:12:52 +0000249
250 Window win = calculate_max_window(*_output->info());
251 win.set(3, Window::Dimension(i, i + 1, 1));
George Wort894066d2019-02-15 15:12:52 +0000252
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100253 auto copy_kernel = support::cpp14::make_unique<CLCopyKernel>();
254 copy_kernel->configure(_scaled_results[i].get(), _output, PaddingList(), &win);
255 _copy.emplace_back(std::move(copy_kernel));
George Wort894066d2019-02-15 15:12:52 +0000256
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100257 _crop_results[i]->allocator()->allocate();
258 _scaled_results[i]->allocator()->allocate();
259
260 run_crop(_input, _crop_results[i].get(), batch_index, start, end, _extrapolation_value);
George Wort894066d2019-02-15 15:12:52 +0000261 }
262 _boxes->unmap(CLScheduler::get().queue());
263 _box_ind->unmap(CLScheduler::get().queue());
264 CLScheduler::get().sync();
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100265 for(auto &kernel : _scale)
George Wort894066d2019-02-15 15:12:52 +0000266 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100267 kernel->run();
George Wort894066d2019-02-15 15:12:52 +0000268 }
269 CLScheduler::get().sync();
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100270 for(auto &kernel : _copy)
George Wort894066d2019-02-15 15:12:52 +0000271 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100272 CLScheduler::get().enqueue(*kernel, true);
George Wort894066d2019-02-15 15:12:52 +0000273 }
274 CLScheduler::get().sync();
275}
276} // namespace arm_compute