Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 1 | /* |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEPriorBoxLayerKernel.h" |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/core/Validate.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | |
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" |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 33 | |
| 34 | #include <arm_neon.h> |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace |
| 39 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 40 | Status validate_arguments(const ITensorInfo *input1, |
| 41 | const ITensorInfo *input2, |
| 42 | const ITensorInfo *output, |
| 43 | const PriorBoxLayerInfo &info) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::F32); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input1, input2); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2); |
| 49 | |
| 50 | // Check variances |
| 51 | const int var_size = info.variances().size(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | if (var_size > 1) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 53 | { |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size != 4, "Must provide 4 variance values"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | for (int i = 0; i < var_size; ++i) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 56 | { |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size <= 0, "Must be greater than 0"); |
| 58 | } |
| 59 | } |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[0] < 0.f, "Step x should be greater or equal to 0"); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[1] < 0.f, "Step y should be greater or equal to 0"); |
| 62 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 63 | if (!info.max_sizes().empty()) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 64 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes().size() != info.min_sizes().size(), |
| 66 | "Max and min sizes dimensions should match"); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | for (unsigned int i = 0; i < info.max_sizes().size(); ++i) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 70 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes()[i] < info.min_sizes()[i], |
| 72 | "Max size should be greater than min size"); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 75 | if (output != nullptr && output->total_size() != 0) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 76 | { |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) != 2); |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, output); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return Status{}; |
| 82 | } |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 83 | } // namespace |
| 84 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | NEPriorBoxLayerKernel::NEPriorBoxLayerKernel() : _input1(nullptr), _input2(nullptr), _output(nullptr), _info() |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 86 | { |
| 87 | } |
| 88 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | void NEPriorBoxLayerKernel::store_coordinates(float *out, |
| 90 | const int offset, |
| 91 | const float center_x, |
| 92 | const float center_y, |
| 93 | const float box_width, |
| 94 | const float box_height, |
| 95 | const int width, |
| 96 | const int height) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 97 | { |
| 98 | float xmin = (center_x - box_width / 2.f) / width; |
| 99 | float ymin = (center_y - box_height / 2.f) / height; |
| 100 | float xmax = (center_x + box_width / 2.f) / width; |
| 101 | float ymax = (center_y + box_height / 2.f) / height; |
| 102 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | float32x4_t vec_elements = {xmin, ymin, xmax, ymax}; |
| 104 | if (_info.clip()) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 105 | { |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 106 | static const float32x4_t CONST_0 = vdupq_n_f32(0.f); |
| 107 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 108 | vec_elements = vmaxq_f32(vminq_f32(vec_elements, CONST_1), CONST_0); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 109 | } |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 110 | vst1q_f32(out + offset, vec_elements); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 113 | void NEPriorBoxLayerKernel::calculate_prior_boxes(const Window &window) |
| 114 | { |
| 115 | const int num_priors = _info.aspect_ratios().size() * _info.min_sizes().size() + _info.max_sizes().size(); |
| 116 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 117 | const DataLayout data_layout = _input1->info()->data_layout(); |
| 118 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 119 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 120 | |
| 121 | const int layer_width = _input1->info()->dimension(width_idx); |
| 122 | const int layer_height = _input1->info()->dimension(height_idx); |
| 123 | |
| 124 | int img_width = _info.img_size().x; |
| 125 | int img_height = _info.img_size().y; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | if (img_width == 0 || img_height == 0) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 127 | { |
| 128 | img_width = _input2->info()->dimension(width_idx); |
| 129 | img_height = _input2->info()->dimension(height_idx); |
| 130 | } |
| 131 | |
| 132 | float step_x = _info.steps()[0]; |
| 133 | float step_y = _info.steps()[1]; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 134 | if (step_x == 0.f || step_y == 0.f) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 135 | { |
| 136 | step_x = static_cast<float>(img_width) / layer_width; |
| 137 | step_y = static_cast<float>(img_height) / layer_height; |
| 138 | } |
| 139 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 140 | Window slice = window.first_slice_window_2D(); |
| 141 | slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 2)); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 142 | |
| 143 | Iterator output(_output, slice); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 144 | execute_window_loop( |
| 145 | slice, |
| 146 | [&](const Coordinates &id) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 147 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 148 | float center_x = 0; |
| 149 | float center_y = 0; |
| 150 | int idx = id.x() / (4 * num_priors); |
| 151 | center_x = (static_cast<float>(idx % layer_width) + _info.offset()) * step_x; |
| 152 | center_y = (static_cast<float>(idx / layer_width) + _info.offset()) * step_y; |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 153 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | float box_width; |
| 155 | float box_height; |
| 156 | int offset = 0; |
| 157 | |
| 158 | auto out = reinterpret_cast<float *>(output.ptr()); |
| 159 | for (unsigned int i = 0; i < _info.min_sizes().size(); ++i) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 160 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 161 | const float min_size = _info.min_sizes().at(i); |
| 162 | box_width = min_size; |
| 163 | box_height = min_size; |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 164 | store_coordinates(out, offset, center_x, center_y, box_width, box_height, img_width, img_height); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 165 | offset += 4; |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 166 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 167 | if (!_info.max_sizes().empty()) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 168 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 169 | const float max_size = _info.max_sizes().at(i); |
| 170 | box_width = std::sqrt(min_size * max_size); |
| 171 | box_height = box_width; |
| 172 | |
| 173 | store_coordinates(out, offset, center_x, center_y, box_width, box_height, img_width, img_height); |
| 174 | offset += 4; |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 177 | // rest of priors |
| 178 | for (auto ar : _info.aspect_ratios()) |
| 179 | { |
| 180 | if (fabs(ar - 1.) < 1e-6) |
| 181 | { |
| 182 | continue; |
| 183 | } |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 184 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 185 | box_width = min_size * sqrt(ar); |
| 186 | box_height = min_size / sqrt(ar); |
| 187 | |
| 188 | store_coordinates(out, offset, center_x, center_y, box_width, box_height, img_width, img_height); |
| 189 | offset += 4; |
| 190 | } |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 191 | } |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 192 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 193 | // set the variance |
| 194 | out = reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(id.x(), 1))); |
| 195 | float32x4_t var; |
| 196 | if (_info.variances().size() == 1) |
| 197 | { |
| 198 | var = vdupq_n_f32(_info.variances().at(0)); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | const float32x4_t vars = {_info.variances().at(0), _info.variances().at(1), _info.variances().at(2), |
| 203 | _info.variances().at(3)}; |
| 204 | var = vars; |
| 205 | } |
| 206 | for (int i = 0; i < num_priors; ++i) |
| 207 | { |
| 208 | vst1q_f32(out + 4 * i, var); |
| 209 | } |
| 210 | }, |
| 211 | output); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 214 | void NEPriorBoxLayerKernel::configure(const ITensor *input1, |
| 215 | const ITensor *input2, |
| 216 | ITensor *output, |
| 217 | const PriorBoxLayerInfo &info) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 218 | { |
| 219 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 220 | |
| 221 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), info)); |
| 222 | |
| 223 | _input1 = input1; |
| 224 | _input2 = input2; |
| 225 | _info = info; |
| 226 | _output = output; |
| 227 | |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 228 | // Configure kernel window |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 229 | const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size(); |
| 230 | Window win = calculate_max_window(*output->info(), Steps(num_priors * 4)); |
Michalis Spyrou | 83dea96 | 2020-02-27 15:06:10 +0000 | [diff] [blame] | 231 | |
| 232 | INEKernel::configure(win); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 235 | Status NEPriorBoxLayerKernel::validate(const ITensorInfo *input1, |
| 236 | const ITensorInfo *input2, |
| 237 | const ITensorInfo *output, |
| 238 | const PriorBoxLayerInfo &info) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 239 | { |
| 240 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 241 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, info)); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 242 | |
| 243 | return Status{}; |
| 244 | } |
| 245 | void NEPriorBoxLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 246 | { |
| 247 | ARM_COMPUTE_UNUSED(info); |
| 248 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 249 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 250 | |
| 251 | // Run function |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 252 | calculate_prior_boxes(window); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 253 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 254 | } // namespace arm_compute |