George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 1 | /* |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 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 | */ |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 24 | #include "arm_compute/runtime/CL/functions/CLCropResize.h" |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
| 29 | #include "src/common/utils/Log.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 30 | #include "src/core/CL/kernels/CLFillBorderKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 31 | #include "src/core/helpers/AutoConfiguration.h" |
| 32 | #include "src/core/helpers/WindowHelpers.h" |
| 33 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 34 | #include <cstddef> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace |
| 39 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 40 | inline void configure_crop(const ICLTensor *input, |
| 41 | ICLTensor *crop_boxes, |
| 42 | ICLTensor *box_ind, |
| 43 | ICLTensor *output, |
| 44 | uint32_t crop_box_ind, |
| 45 | Coordinates &start, |
| 46 | Coordinates &end, |
| 47 | uint32_t &batch_index) |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 48 | { |
| 49 | batch_index = *(reinterpret_cast<int32_t *>(box_ind->ptr_to_element(Coordinates(crop_box_ind)))); |
| 50 | |
| 51 | // _crop_box_ind is used to index crop_boxes and retrieve the appropriate crop box. |
| 52 | // The crop box is specified by normalized coordinates [y0, x0, y1, x1]. |
| 53 | const float x0 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(1, crop_box_ind))); |
| 54 | const float y0 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(0, crop_box_ind))); |
| 55 | const float x1 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(3, crop_box_ind))); |
| 56 | const float y1 = *reinterpret_cast<const float *>(crop_boxes->ptr_to_element(Coordinates(2, crop_box_ind))); |
| 57 | // The normalized coordinates are scaled to retrieve the floating point image coordinates which are rounded to integers. |
| 58 | start = Coordinates(std::floor(x0 * (input->info()->tensor_shape()[1] - 1) + 0.5f), |
| 59 | std::floor(y0 * (input->info()->tensor_shape()[2] - 1) + 0.5f)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | end = Coordinates(std::floor(x1 * (input->info()->tensor_shape()[1] - 1) + 0.5f), |
| 61 | std::floor(y1 * (input->info()->tensor_shape()[2] - 1) + 0.5f)); |
| 62 | const TensorShape out_shape(input->info()->tensor_shape()[0], static_cast<uint32_t>(abs(end[0] - start[0])) + 1, |
| 63 | static_cast<uint32_t>(abs(end[1] - start[1])) + 1); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 64 | output->info()->set_tensor_shape(out_shape); |
| 65 | } |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 66 | } // namespace |
| 67 | |
| 68 | CLCropResize::CLCropResize() |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | : _input(nullptr), |
| 70 | _boxes(nullptr), |
| 71 | _box_ind(nullptr), |
| 72 | _output(nullptr), |
| 73 | _num_boxes(0), |
| 74 | _method(), |
| 75 | _extrapolation_value(0), |
| 76 | _scale(), |
| 77 | _copy(), |
| 78 | _crop_results(), |
| 79 | _scaled_results(), |
| 80 | _internal_functions() |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 81 | { |
| 82 | } |
| 83 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 84 | CLCropResize::~CLCropResize() = default; |
| 85 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | Status CLCropResize::validate(const ITensorInfo *input, |
| 87 | ITensorInfo *boxes, |
| 88 | ITensorInfo *box_ind, |
| 89 | const ITensorInfo *output, |
| 90 | Coordinates2D crop_size, |
| 91 | InterpolationPolicy method, |
| 92 | float extrapolation_value) |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 93 | { |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON(crop_size.x <= 0 || crop_size.y <= 0); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON(method == InterpolationPolicy::AREA); |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4); |
| 97 | ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]); |
| 98 | TensorInfo temp_info; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 99 | ARM_COMPUTE_RETURN_ON_ERROR(CLCrop::validate(input->clone().get(), &temp_info, {0, 0}, {1, 1}, |
| 100 | input->dimension(3) - 1, extrapolation_value)); |
| 101 | if (output->total_size() > 0) |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 102 | { |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(output, DataType::F32); |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 105 | TensorShape out_shape(input->tensor_shape()[0], crop_size.x, crop_size.y, boxes->tensor_shape()[1]); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), out_shape); |
| 107 | } |
| 108 | return Status{}; |
| 109 | } |
| 110 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 111 | void CLCropResize::configure(const ICLTensor *input, |
| 112 | ICLTensor *boxes, |
| 113 | ICLTensor *box_ind, |
| 114 | ICLTensor *output, |
| 115 | Coordinates2D crop_size, |
| 116 | InterpolationPolicy method, |
| 117 | float extrapolation_value) |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 118 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 119 | configure(CLKernelLibrary::get().get_compile_context(), input, boxes, box_ind, output, crop_size, method, |
| 120 | extrapolation_value); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | void CLCropResize::configure(const CLCompileContext &compile_context, |
| 124 | const ICLTensor *input, |
| 125 | ICLTensor *boxes, |
| 126 | ICLTensor *box_ind, |
| 127 | ICLTensor *output, |
| 128 | Coordinates2D crop_size, |
| 129 | InterpolationPolicy method, |
| 130 | float extrapolation_value) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 131 | { |
| 132 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, boxes, box_ind); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 133 | ARM_COMPUTE_ERROR_THROW_ON(CLCropResize::validate(input->info(), boxes->info(), box_ind->info(), output->info(), |
| 134 | crop_size, method, extrapolation_value)); |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_LOG_PARAMS(input, boxes, box_ind, output, crop_size, method, extrapolation_value); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 136 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 137 | TensorShape output_shape = |
| 138 | TensorShape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y, boxes->info()->tensor_shape()[1]); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 139 | auto_init_if_empty(*output->info(), output_shape, 1, DataType::F32); |
| 140 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 141 | _num_boxes = boxes->info()->tensor_shape()[1]; |
| 142 | TensorShape out_shape(input->info()->tensor_shape()[0], crop_size.x, crop_size.y); |
| 143 | |
| 144 | _input = input; |
| 145 | _boxes = boxes; |
| 146 | _box_ind = box_ind; |
| 147 | _output = output; |
| 148 | _method = method; |
| 149 | _extrapolation_value = extrapolation_value; |
| 150 | |
| 151 | // For each crop box: |
| 152 | // - The initial cropped image is produced as specified by boxes[i] from the 3D image input[box_ind[i]]. |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 153 | // Possibly using a CLCrop and up to four CLFills. |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 154 | // - A tensor is required to hold this initial cropped image. |
| 155 | // - A scale function is used to resize the cropped image to the size specified by crop_size. |
| 156 | // - A tensor is required to hold the final scaled image before it is copied into the 4D output |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 157 | // that will hold all final cropped and scaled 3D images using CLCopy. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 158 | |
| 159 | // The contents of _boxes and _box_ind are required to calculate the shape |
| 160 | // of the initial cropped image and thus are required to configure the |
| 161 | // kernels used for cropping and scaling. |
| 162 | _boxes->map(CLScheduler::get().queue()); |
| 163 | _box_ind->map(CLScheduler::get().queue()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 164 | for (unsigned int num_box = 0; num_box < _num_boxes; ++num_box) |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 165 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 166 | auto crop_tensor = std::make_unique<CLTensor>(); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 167 | TensorInfo crop_result_info(1, DataType::F32); |
| 168 | crop_result_info.set_data_layout(DataLayout::NHWC); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 169 | crop_tensor->allocator()->init(crop_result_info); |
| 170 | _crop_results.emplace_back(std::move(crop_tensor)); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 171 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 172 | auto scale_tensor = std::make_unique<CLTensor>(); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 173 | TensorInfo scaled_result_info(out_shape, 1, DataType::F32); |
| 174 | scaled_result_info.set_data_layout(DataLayout::NHWC); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 175 | scale_tensor->allocator()->init(scaled_result_info); |
| 176 | _scaled_results.emplace_back(std::move(scale_tensor)); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 177 | |
| 178 | // Size of the crop box in _boxes has to be given before the configure |
| 179 | uint32_t batch_index; |
| 180 | Coordinates start{}; |
| 181 | Coordinates end{}; |
| 182 | configure_crop(_input, _boxes, _box_ind, _crop_results[num_box].get(), num_box, start, end, batch_index); |
| 183 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 184 | auto scale_kernel = std::make_unique<CLScale>(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 185 | scale_kernel->configure( |
| 186 | compile_context, _crop_results[num_box].get(), _scaled_results[num_box].get(), |
| 187 | ScaleKernelInfo{_method, BorderMode::CONSTANT, PixelValue(_extrapolation_value), SamplingPolicy::TOP_LEFT}); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 188 | _scale.emplace_back(std::move(scale_kernel)); |
| 189 | |
| 190 | Window win = calculate_max_window(*_output->info()); |
| 191 | win.set(3, Window::Dimension(num_box, num_box + 1, 1)); |
| 192 | |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 193 | auto copy_kernel = std::make_unique<CLCopy>(); |
SiCong Li | 3580c75 | 2020-10-14 17:00:56 +0100 | [diff] [blame] | 194 | copy_kernel->configure(compile_context, _scaled_results[num_box].get(), _output, &win); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 195 | _copy.emplace_back(std::move(copy_kernel)); |
| 196 | |
| 197 | _crop_results[num_box]->allocator()->allocate(); |
| 198 | _scaled_results[num_box]->allocator()->allocate(); |
| 199 | |
| 200 | bool is_width_flipped = end[0] < start[0]; |
| 201 | bool is_height_flipped = end[1] < start[1]; |
| 202 | /** The number of rows out of bounds at the start and end of _crop_results[num_box].get(). */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 203 | std::array<int32_t, 2> rows_out_of_bounds{0}; |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 204 | /** The number of columns out of bounds at the start and end of _crop_results[num_box].get(). */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 205 | std::array<int32_t, 2> cols_out_of_bounds{0}; |
| 206 | if (is_height_flipped) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 207 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 208 | rows_out_of_bounds[0] = start[1] >= static_cast<int32_t>(_input->info()->dimension(2)) |
| 209 | ? std::min(start[1] - _input->info()->dimension(2) + 1, |
| 210 | _crop_results[num_box].get()->info()->dimension(2)) |
| 211 | : 0; |
| 212 | rows_out_of_bounds[1] = |
| 213 | end[1] < 0 ? std::min(-end[1], static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(2))) |
| 214 | : 0; |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 215 | } |
| 216 | else |
| 217 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 218 | rows_out_of_bounds[0] = |
| 219 | start[1] < 0 |
| 220 | ? std::min(-start[1], static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(2))) |
| 221 | : 0; |
| 222 | rows_out_of_bounds[1] = end[1] >= static_cast<int32_t>(_input->info()->dimension(2)) |
| 223 | ? std::min(end[1] - _input->info()->dimension(2) + 1, |
| 224 | _crop_results[num_box].get()->info()->dimension(2)) |
| 225 | : 0; |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 226 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 227 | if (is_width_flipped) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 228 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 229 | cols_out_of_bounds[0] = start[0] >= static_cast<int32_t>(_input->info()->dimension(1)) |
| 230 | ? std::min(start[0] - _input->info()->dimension(1) + 1, |
| 231 | _crop_results[num_box].get()->info()->dimension(1)) |
| 232 | : 0; |
| 233 | cols_out_of_bounds[1] = |
| 234 | end[0] < 0 ? std::min(-end[0], static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(1))) |
| 235 | : 0; |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 236 | } |
| 237 | else |
| 238 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 239 | cols_out_of_bounds[0] = |
| 240 | start[0] < 0 |
| 241 | ? std::min(-start[0], static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(1))) |
| 242 | : 0; |
| 243 | cols_out_of_bounds[1] = end[0] >= static_cast<int32_t>(_input->info()->dimension(1)) |
| 244 | ? std::min(end[0] - _input->info()->dimension(1) + 1, |
| 245 | _crop_results[num_box].get()->info()->dimension(1)) |
| 246 | : 0; |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | Window full_window = calculate_max_window(*_crop_results[num_box].get()->info()); |
| 250 | |
| 251 | // Full _crop_results[num_box].get() window: |
| 252 | // -------------------------------- |
| 253 | // | Out of bounds | |
| 254 | // | rows before | |
| 255 | // |------------------------------| |
| 256 | // | Out of | In | Out of | |
| 257 | // | bounds | bounds | bounds | |
| 258 | // | cols | elements | cols | |
| 259 | // | before | copied | after | |
| 260 | // | | from input | | |
| 261 | // |------------------------------| |
| 262 | // | Out of bounds | |
| 263 | // | rows after | |
| 264 | // |------------------------------| |
| 265 | // Use a separate _crop_results[num_box].get() window for each section of the full _crop_results[num_box].get() window. |
| 266 | // Fill all _crop_results[num_box].get() rows that have no elements that are within the input bounds |
| 267 | // with the extrapolation value using memset. |
| 268 | // First for the rows before the in bounds rows. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 269 | if (rows_out_of_bounds[0] > 0) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 270 | { |
| 271 | Window slice_fill_rows_before(full_window); |
| 272 | slice_fill_rows_before.set(2, Window::Dimension(0, rows_out_of_bounds[0], 1)); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 273 | auto kernel = std::make_unique<CLFill>(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 274 | kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, |
| 275 | &slice_fill_rows_before); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 276 | //_internal_functions.emplace_back(std::move(kernel)); |
| 277 | _internal_functions.push_back(std::move(kernel)); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | Window slice_in(full_window); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 281 | slice_in.set(2, |
| 282 | Window::Dimension(rows_out_of_bounds[0], |
| 283 | _crop_results[num_box].get()->info()->dimension(2) - rows_out_of_bounds[1], 1)); |
| 284 | slice_in.set(1, |
| 285 | Window::Dimension(cols_out_of_bounds[0], |
| 286 | _crop_results[num_box].get()->info()->dimension(1) - cols_out_of_bounds[1], 1)); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 287 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 288 | int rows_in_bounds = static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(2)) - |
| 289 | rows_out_of_bounds[0] - rows_out_of_bounds[1]; |
| 290 | if (rows_in_bounds > 0) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 291 | { |
| 292 | // Fill all elements that share a row with an in bounds element with the extrapolation value. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 293 | if (cols_out_of_bounds[0] > 0) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 294 | { |
| 295 | Window slice_fill_cols_before(slice_in); |
| 296 | slice_fill_cols_before.set(1, Window::Dimension(0, cols_out_of_bounds[0], 1)); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 297 | auto kernel = std::make_unique<CLFill>(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 298 | kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, |
| 299 | &slice_fill_cols_before); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 300 | //_internal_functions.emplace_back(std::move(kernel)); |
| 301 | _internal_functions.push_back(std::move(kernel)); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 302 | } |
| 303 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 304 | if (cols_out_of_bounds[1] > 0) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 305 | { |
| 306 | Window slice_fill_cols_after(slice_in); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 307 | slice_fill_cols_after.set( |
| 308 | 1, Window::Dimension(_crop_results[num_box].get()->info()->dimension(1) - cols_out_of_bounds[1], |
| 309 | _crop_results[num_box].get()->info()->dimension(1), 1)); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 310 | auto kernel = std::make_unique<CLFill>(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 311 | kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, |
| 312 | &slice_fill_cols_after); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 313 | //_internal_functions.emplace_back(std::move(kernel)); |
| 314 | _internal_functions.push_back(std::move(kernel)); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Copy all elements within the input bounds from the input tensor. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 318 | int cols_in_bounds = static_cast<int32_t>(_crop_results[num_box].get()->info()->dimension(1)) - |
| 319 | cols_out_of_bounds[0] - cols_out_of_bounds[1]; |
| 320 | if (cols_in_bounds > 0) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 321 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 322 | Coordinates2D start_in{ |
| 323 | is_width_flipped ? start[0] - cols_out_of_bounds[0] : start[0] + cols_out_of_bounds[0], |
| 324 | is_height_flipped ? start[1] - rows_out_of_bounds[0] : start[1] + rows_out_of_bounds[0]}; |
| 325 | Coordinates2D end_in{ |
| 326 | is_width_flipped ? start_in.x - cols_in_bounds + 1 : start_in.x + cols_in_bounds - 1, |
| 327 | is_height_flipped ? start_in.y - rows_in_bounds + 1 : start_in.y + rows_in_bounds - 1}; |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 328 | auto kernel = std::make_unique<CLCrop>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 329 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 330 | kernel->configure(compile_context, _input, _crop_results[num_box].get(), start_in, end_in, batch_index, |
| 331 | extrapolation_value, &slice_in); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 332 | //_internal_functions.emplace_back(std::move(kernel)); |
| 333 | _internal_functions.push_back(std::move(kernel)); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | // Fill all rows after the in bounds elements with the extrapolation value. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 338 | if (rows_out_of_bounds[1] > 0) |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 339 | { |
| 340 | Window slice_fill_rows_after(full_window); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 341 | slice_fill_rows_after.set( |
| 342 | 2, Window::Dimension(_crop_results[num_box].get()->info()->dimension(2) - rows_out_of_bounds[1], |
| 343 | _crop_results[num_box].get()->info()->dimension(2), 1)); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 344 | auto kernel = std::make_unique<CLFill>(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 345 | kernel->configure(compile_context, _crop_results[num_box].get(), extrapolation_value, |
| 346 | &slice_fill_rows_after); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 347 | //_internal_functions.emplace_back(std::move(kernel)); |
| 348 | _internal_functions.push_back(std::move(kernel)); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 349 | } |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 350 | } |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 351 | _boxes->unmap(CLScheduler::get().queue()); |
| 352 | _box_ind->unmap(CLScheduler::get().queue()); |
| 353 | CLScheduler::get().sync(); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void CLCropResize::run() |
| 357 | { |
| 358 | ARM_COMPUTE_ERROR_ON_MSG(_output == nullptr, "Unconfigured function"); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 359 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 360 | for (unsigned int i = 0; i < _internal_functions.size(); ++i) |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 361 | { |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 362 | _internal_functions[i]->run(); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 363 | } |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 364 | |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 365 | CLScheduler::get().sync(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 366 | for (auto &kernel : _scale) |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 367 | { |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 368 | kernel->run(); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 369 | } |
| 370 | CLScheduler::get().sync(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 371 | for (auto &kernel : _copy) |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 372 | { |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 373 | kernel->run(); |
George Wort | 894066d | 2019-02-15 15:12:52 +0000 | [diff] [blame] | 374 | } |
| 375 | CLScheduler::get().sync(); |
| 376 | } |
ramelg01 | 6d89157 | 2021-09-29 10:05:09 +0100 | [diff] [blame] | 377 | } // namespace arm_compute |