Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | #include "arm_compute/core/NEON/kernels/NEPriorBoxLayerKernel.h" |
| 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" |
| 30 | |
| 31 | #include <arm_neon.h> |
| 32 | #include <cstdint> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace |
| 37 | { |
| 38 | Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info) |
| 39 | { |
| 40 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 41 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::F32); |
| 42 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input1, input2); |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2); |
| 44 | |
| 45 | // Check variances |
| 46 | const int var_size = info.variances().size(); |
| 47 | if(var_size > 1) |
| 48 | { |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size != 4, "Must provide 4 variance values"); |
| 50 | for(int i = 0; i < var_size; ++i) |
| 51 | { |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size <= 0, "Must be greater than 0"); |
| 53 | } |
| 54 | } |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[0] < 0.f, "Step x should be greater or equal to 0"); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[1] < 0.f, "Step y should be greater or equal to 0"); |
| 57 | |
| 58 | if(!info.max_sizes().empty()) |
| 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes().size() != info.min_sizes().size(), "Max and min sizes dimensions should match"); |
| 61 | } |
| 62 | |
| 63 | for(unsigned int i = 0; i < info.max_sizes().size(); ++i) |
| 64 | { |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes()[i] < info.min_sizes()[i], "Max size should be greater than min size"); |
| 66 | } |
| 67 | |
| 68 | if(output != nullptr && output->total_size() != 0) |
| 69 | { |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(get_data_layout_dimension_index(input1->data_layout(), DataLayoutDimension::HEIGHT)) != 2); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input1, output); |
| 72 | } |
| 73 | |
| 74 | return Status{}; |
| 75 | } |
| 76 | |
| 77 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output, const PriorBoxLayerInfo &info) |
| 78 | { |
| 79 | ARM_COMPUTE_UNUSED(input2); |
| 80 | |
| 81 | Window win = {}; |
| 82 | bool window_changed = false; |
| 83 | switch(input1->data_layout()) |
| 84 | { |
| 85 | case DataLayout::NCHW: |
| 86 | { |
| 87 | const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size(); |
| 88 | const unsigned int num_elems_processed_per_iteration = 4 * num_priors; |
| 89 | win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 90 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 91 | window_changed = update_window_and_padding(win, output_access); |
| 92 | break; |
| 93 | } |
| 94 | case DataLayout::NHWC: |
| 95 | { |
| 96 | win = calculate_max_window(*output, Steps()); |
| 97 | break; |
| 98 | } |
| 99 | default: |
| 100 | ARM_COMPUTE_ERROR("Not implemented"); |
| 101 | }; |
| 102 | |
| 103 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 104 | return std::make_pair(err, win); |
| 105 | } |
| 106 | } // namespace |
| 107 | |
| 108 | NEPriorBoxLayerKernel::NEPriorBoxLayerKernel() |
| 109 | : _func(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _info() |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | template <DataLayout DL> |
| 114 | void NEPriorBoxLayerKernel::store_coordinates(float *out, const int offset, const float center_x, const float center_y, const float box_width, const float box_height, const int width, |
| 115 | const int height) |
| 116 | { |
| 117 | float xmin = (center_x - box_width / 2.f) / width; |
| 118 | float ymin = (center_y - box_height / 2.f) / height; |
| 119 | float xmax = (center_x + box_width / 2.f) / width; |
| 120 | float ymax = (center_y + box_height / 2.f) / height; |
| 121 | |
| 122 | switch(DL) |
| 123 | { |
| 124 | case DataLayout::NCHW: |
| 125 | { |
| 126 | float32x4_t vec_elements = { xmin, ymin, xmax, ymax }; |
| 127 | if(_info.clip()) |
| 128 | { |
| 129 | static const float32x4_t CONST_0 = vdupq_n_f32(0.f); |
| 130 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 131 | vec_elements = vmaxq_f32(vminq_f32(vec_elements, CONST_1), CONST_0); |
| 132 | } |
| 133 | vst1q_f32(out + offset, vec_elements); |
| 134 | } |
| 135 | break; |
| 136 | case DataLayout::NHWC: |
| 137 | { |
| 138 | const int output_offset = _output->info()->strides_in_bytes()[1] / _output->info()->element_size(); |
| 139 | if(_info.clip()) |
| 140 | { |
| 141 | xmin = std::min(std::max(xmin, 0.f), 1.f); |
| 142 | ymin = std::min(std::max(ymin, 0.f), 1.f); |
| 143 | xmax = std::min(std::max(xmax, 0.f), 1.f); |
| 144 | ymax = std::min(std::max(ymax, 0.f), 1.f); |
| 145 | } |
| 146 | |
| 147 | *(out + output_offset * offset) = xmin; |
| 148 | *(out + output_offset * (offset + 1)) = ymin; |
| 149 | *(out + output_offset * (offset + 2)) = xmax; |
| 150 | *(out + output_offset * (offset + 3)) = ymax; |
| 151 | } |
| 152 | break; |
| 153 | default: |
| 154 | ARM_COMPUTE_ERROR("Not implemented"); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | template <DataLayout DL> |
| 159 | void NEPriorBoxLayerKernel::calculate_prior_boxes(const Window &window) |
| 160 | { |
| 161 | const int num_priors = _info.aspect_ratios().size() * _info.min_sizes().size() + _info.max_sizes().size(); |
| 162 | |
| 163 | const int width_idx = get_data_layout_dimension_index(DL, DataLayoutDimension::WIDTH); |
| 164 | const int height_idx = get_data_layout_dimension_index(DL, DataLayoutDimension::HEIGHT); |
| 165 | |
| 166 | const int layer_width = _input1->info()->dimension(width_idx); |
| 167 | const int layer_height = _input1->info()->dimension(height_idx); |
| 168 | |
| 169 | int img_width = _info.img_size().x; |
| 170 | int img_height = _info.img_size().y; |
| 171 | if(img_width == 0 || img_height == 0) |
| 172 | { |
| 173 | img_width = _input2->info()->dimension(width_idx); |
| 174 | img_height = _input2->info()->dimension(height_idx); |
| 175 | } |
| 176 | |
| 177 | float step_x = _info.steps()[0]; |
| 178 | float step_y = _info.steps()[1]; |
| 179 | if(step_x == 0.f || step_y == 0.f) |
| 180 | { |
| 181 | step_x = static_cast<float>(img_width) / layer_width; |
| 182 | step_y = static_cast<float>(img_height) / layer_height; |
| 183 | } |
| 184 | |
| 185 | Window slice = {}; |
| 186 | |
| 187 | switch(DL) |
| 188 | { |
| 189 | case DataLayout::NCHW: |
| 190 | slice = window.first_slice_window_2D(); |
| 191 | slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 2)); |
| 192 | break; |
| 193 | case DataLayout::NHWC: |
| 194 | slice = window.first_slice_window_3D(); |
| 195 | slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 4 * num_priors)); |
| 196 | slice.set(Window::DimZ, Window::Dimension(0, _output->info()->dimension(2), 2)); |
| 197 | break; |
| 198 | default: |
| 199 | ARM_COMPUTE_ERROR("Not implemented"); |
| 200 | } |
| 201 | |
| 202 | Iterator output(_output, slice); |
| 203 | execute_window_loop(slice, [&](const Coordinates & id) |
| 204 | { |
| 205 | float center_x = 0; |
| 206 | float center_y = 0; |
| 207 | int idx = 0; |
| 208 | switch(DL) |
| 209 | { |
| 210 | case DataLayout::NCHW: |
| 211 | idx = id.x() / (4 * num_priors); |
| 212 | center_x = (static_cast<float>(idx % layer_width) + _info.offset()) * step_x; |
| 213 | center_y = (static_cast<float>(idx / layer_width) + _info.offset()) * step_y; |
| 214 | break; |
| 215 | case DataLayout::NHWC: |
| 216 | idx = id.y() / (4 * num_priors); |
| 217 | center_x = (static_cast<float>(idx % layer_width) + _info.offset()) * step_x; |
| 218 | center_y = (static_cast<float>(idx / layer_width) + _info.offset()) * step_y; |
| 219 | break; |
| 220 | default: |
| 221 | ARM_COMPUTE_ERROR("Not implemented"); |
| 222 | } |
| 223 | |
| 224 | float box_width; |
| 225 | float box_height; |
| 226 | int offset = 0; |
| 227 | |
| 228 | auto out = reinterpret_cast<float *>(output.ptr()); |
| 229 | for(unsigned int i = 0; i < _info.min_sizes().size(); ++i) |
| 230 | { |
| 231 | const float min_size = _info.min_sizes().at(i); |
| 232 | box_width = min_size; |
| 233 | box_height = min_size; |
| 234 | store_coordinates<DL>(out, offset, center_x, center_y, box_width, box_height, img_width, img_height); |
| 235 | offset += 4; |
| 236 | |
| 237 | if(!_info.max_sizes().empty()) |
| 238 | { |
| 239 | const float max_size = _info.max_sizes().at(i); |
| 240 | box_width = std::sqrt(min_size * max_size); |
| 241 | box_height = box_width; |
| 242 | |
| 243 | store_coordinates<DL>(out, offset, center_x, center_y, box_width, box_height, img_width, img_height); |
| 244 | offset += 4; |
| 245 | } |
| 246 | |
| 247 | // rest of priors |
| 248 | for(auto ar : _info.aspect_ratios()) |
| 249 | { |
| 250 | if(fabs(ar - 1.) < 1e-6) |
| 251 | { |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | box_width = min_size * sqrt(ar); |
| 256 | box_height = min_size / sqrt(ar); |
| 257 | |
| 258 | store_coordinates<DL>(out, offset, center_x, center_y, box_width, box_height, img_width, img_height); |
| 259 | offset += 4; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // set the variance |
| 264 | switch(DL) |
| 265 | { |
| 266 | case DataLayout::NCHW: |
| 267 | { |
| 268 | out = reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(id.x(), 1))); |
| 269 | float32x4_t var; |
| 270 | if(_info.variances().size() == 1) |
| 271 | { |
| 272 | var = vdupq_n_f32(_info.variances().at(0)); |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | const float32x4_t vars = { _info.variances().at(0), _info.variances().at(1), _info.variances().at(2), _info.variances().at(3) }; |
| 277 | var = vars; |
| 278 | } |
| 279 | for(int i = 0; i < num_priors; ++i) |
| 280 | { |
| 281 | vst1q_f32(out + 4 * i, var); |
| 282 | } |
| 283 | } |
| 284 | break; |
| 285 | case DataLayout::NHWC: |
| 286 | { |
| 287 | for(int i = 0; i < num_priors; ++i) |
| 288 | { |
| 289 | const int prior_offset = 4 * i; |
| 290 | const bool single_var = _info.variances().size() == 1; |
| 291 | *(reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(0, id.y() + prior_offset + 0, 1)))) = _info.variances().at(0); |
| 292 | *(reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(0, id.y() + prior_offset + 1, 1)))) = single_var ? _info.variances().at(0) : _info.variances().at(1); |
| 293 | *(reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(0, id.y() + prior_offset + 2, 1)))) = single_var ? _info.variances().at(0) : _info.variances().at(2); |
| 294 | *(reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(0, id.y() + prior_offset + 3, 1)))) = single_var ? _info.variances().at(0) : _info.variances().at(3); |
| 295 | } |
| 296 | } |
| 297 | break; |
| 298 | default: |
| 299 | ARM_COMPUTE_ERROR("Not implemented"); |
| 300 | } |
| 301 | |
| 302 | }, |
| 303 | output); |
| 304 | } |
| 305 | |
| 306 | void NEPriorBoxLayerKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, const PriorBoxLayerInfo &info) |
| 307 | { |
| 308 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 309 | |
| 310 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), info)); |
| 311 | |
| 312 | _input1 = input1; |
| 313 | _input2 = input2; |
| 314 | _info = info; |
| 315 | _output = output; |
| 316 | |
| 317 | switch(input1->info()->data_layout()) |
| 318 | { |
| 319 | case DataLayout::NCHW: |
| 320 | { |
| 321 | _func = &NEPriorBoxLayerKernel::calculate_prior_boxes<DataLayout::NCHW>; |
| 322 | break; |
| 323 | } |
| 324 | case DataLayout::NHWC: |
| 325 | { |
| 326 | _func = &NEPriorBoxLayerKernel::calculate_prior_boxes<DataLayout::NHWC>; |
| 327 | break; |
| 328 | } |
| 329 | default: |
| 330 | ARM_COMPUTE_ERROR("Not implemented."); |
| 331 | } |
| 332 | |
| 333 | // Configure kernel window |
| 334 | auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info(), info); |
| 335 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 336 | INEKernel::configure(win_config.second); |
| 337 | } |
| 338 | |
| 339 | Status NEPriorBoxLayerKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info) |
| 340 | { |
| 341 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 342 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, info)); |
| 343 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get(), info) |
| 344 | .first); |
| 345 | |
| 346 | return Status{}; |
| 347 | } |
| 348 | void NEPriorBoxLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 349 | { |
| 350 | ARM_COMPUTE_UNUSED(info); |
| 351 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 352 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 353 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 354 | |
| 355 | // Run function |
| 356 | (this->*_func)(window); |
| 357 | } |
| 358 | } // namespace arm_compute |