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 | { |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) != 2); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return Status{}; |
| 74 | } |
| 75 | |
| 76 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output, const PriorBoxLayerInfo &info) |
| 77 | { |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 78 | ARM_COMPUTE_UNUSED(input1, input2); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 79 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 80 | const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size(); |
| 81 | const unsigned int num_elems_processed_per_iteration = 4 * num_priors; |
| 82 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 83 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 84 | bool window_changed = update_window_and_padding(win, output_access); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 85 | |
| 86 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 87 | return std::make_pair(err, win); |
| 88 | } |
| 89 | } // namespace |
| 90 | |
| 91 | NEPriorBoxLayerKernel::NEPriorBoxLayerKernel() |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 92 | : _input1(nullptr), _input2(nullptr), _output(nullptr), _info() |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 93 | { |
| 94 | } |
| 95 | |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 96 | 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, |
| 97 | const int height) |
| 98 | { |
| 99 | float xmin = (center_x - box_width / 2.f) / width; |
| 100 | float ymin = (center_y - box_height / 2.f) / height; |
| 101 | float xmax = (center_x + box_width / 2.f) / width; |
| 102 | float ymax = (center_y + box_height / 2.f) / height; |
| 103 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 104 | float32x4_t vec_elements = { xmin, ymin, xmax, ymax }; |
| 105 | if(_info.clip()) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 106 | { |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 107 | static const float32x4_t CONST_0 = vdupq_n_f32(0.f); |
| 108 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 109 | vec_elements = vmaxq_f32(vminq_f32(vec_elements, CONST_1), CONST_0); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 110 | } |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 111 | vst1q_f32(out + offset, vec_elements); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 114 | void NEPriorBoxLayerKernel::calculate_prior_boxes(const Window &window) |
| 115 | { |
| 116 | const int num_priors = _info.aspect_ratios().size() * _info.min_sizes().size() + _info.max_sizes().size(); |
| 117 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 118 | const DataLayout data_layout = _input1->info()->data_layout(); |
| 119 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 120 | 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] | 121 | |
| 122 | const int layer_width = _input1->info()->dimension(width_idx); |
| 123 | const int layer_height = _input1->info()->dimension(height_idx); |
| 124 | |
| 125 | int img_width = _info.img_size().x; |
| 126 | int img_height = _info.img_size().y; |
| 127 | if(img_width == 0 || img_height == 0) |
| 128 | { |
| 129 | img_width = _input2->info()->dimension(width_idx); |
| 130 | img_height = _input2->info()->dimension(height_idx); |
| 131 | } |
| 132 | |
| 133 | float step_x = _info.steps()[0]; |
| 134 | float step_y = _info.steps()[1]; |
| 135 | if(step_x == 0.f || step_y == 0.f) |
| 136 | { |
| 137 | step_x = static_cast<float>(img_width) / layer_width; |
| 138 | step_y = static_cast<float>(img_height) / layer_height; |
| 139 | } |
| 140 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 141 | Window slice = window.first_slice_window_2D(); |
| 142 | slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 2)); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 143 | |
| 144 | Iterator output(_output, slice); |
| 145 | execute_window_loop(slice, [&](const Coordinates & id) |
| 146 | { |
| 147 | float center_x = 0; |
| 148 | float center_y = 0; |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 149 | int idx = id.x() / (4 * num_priors); |
| 150 | center_x = (static_cast<float>(idx % layer_width) + _info.offset()) * step_x; |
| 151 | center_y = (static_cast<float>(idx / layer_width) + _info.offset()) * step_y; |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 152 | |
| 153 | float box_width; |
| 154 | float box_height; |
| 155 | int offset = 0; |
| 156 | |
| 157 | auto out = reinterpret_cast<float *>(output.ptr()); |
| 158 | for(unsigned int i = 0; i < _info.min_sizes().size(); ++i) |
| 159 | { |
| 160 | const float min_size = _info.min_sizes().at(i); |
| 161 | box_width = min_size; |
| 162 | box_height = min_size; |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 163 | 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] | 164 | offset += 4; |
| 165 | |
| 166 | if(!_info.max_sizes().empty()) |
| 167 | { |
| 168 | const float max_size = _info.max_sizes().at(i); |
| 169 | box_width = std::sqrt(min_size * max_size); |
| 170 | box_height = box_width; |
| 171 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 172 | 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] | 173 | offset += 4; |
| 174 | } |
| 175 | |
| 176 | // rest of priors |
| 177 | for(auto ar : _info.aspect_ratios()) |
| 178 | { |
| 179 | if(fabs(ar - 1.) < 1e-6) |
| 180 | { |
| 181 | continue; |
| 182 | } |
| 183 | |
| 184 | box_width = min_size * sqrt(ar); |
| 185 | box_height = min_size / sqrt(ar); |
| 186 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 187 | 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] | 188 | offset += 4; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // set the variance |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 193 | out = reinterpret_cast<float *>(_output->ptr_to_element(Coordinates(id.x(), 1))); |
| 194 | float32x4_t var; |
| 195 | if(_info.variances().size() == 1) |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 196 | { |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 197 | var = vdupq_n_f32(_info.variances().at(0)); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 198 | } |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 199 | else |
| 200 | { |
| 201 | const float32x4_t vars = { _info.variances().at(0), _info.variances().at(1), _info.variances().at(2), _info.variances().at(3) }; |
| 202 | var = vars; |
| 203 | } |
| 204 | for(int i = 0; i < num_priors; ++i) |
| 205 | { |
| 206 | vst1q_f32(out + 4 * i, var); |
| 207 | } |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 208 | }, |
| 209 | output); |
| 210 | } |
| 211 | |
| 212 | void NEPriorBoxLayerKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, const PriorBoxLayerInfo &info) |
| 213 | { |
| 214 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 215 | |
| 216 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), info)); |
| 217 | |
| 218 | _input1 = input1; |
| 219 | _input2 = input2; |
| 220 | _info = info; |
| 221 | _output = output; |
| 222 | |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 223 | // Configure kernel window |
| 224 | auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info(), info); |
| 225 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 226 | INEKernel::configure(win_config.second); |
| 227 | } |
| 228 | |
| 229 | Status NEPriorBoxLayerKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const PriorBoxLayerInfo &info) |
| 230 | { |
| 231 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 232 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, info)); |
| 233 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get(), info) |
| 234 | .first); |
| 235 | |
| 236 | return Status{}; |
| 237 | } |
| 238 | void NEPriorBoxLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 239 | { |
| 240 | ARM_COMPUTE_UNUSED(info); |
| 241 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 242 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 243 | |
| 244 | // Run function |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 245 | calculate_prior_boxes(window); |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 246 | } |
| 247 | } // namespace arm_compute |