Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEHOGDetectorKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/HOGInfo.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | |
| 32 | #include <arm_neon.h> |
| 33 | |
| 34 | using namespace arm_compute; |
| 35 | |
| 36 | NEHOGDetectorKernel::NEHOGDetectorKernel() |
| 37 | : _input(nullptr), _detection_windows(), _hog_descriptor(nullptr), _bias(0.0f), _threshold(0.0f), _idx_class(0), _num_bins_per_descriptor_x(0), _num_blocks_per_descriptor_y(0), _block_stride_width(0), |
| 38 | _block_stride_height(0), _detection_window_width(0), _detection_window_height(0), _max_num_detection_windows(0), _mutex() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void NEHOGDetectorKernel::configure(const ITensor *input, const IHOG *hog, IDetectionWindowArray *detection_windows, const Size2D &detection_window_stride, float threshold, uint16_t idx_class) |
| 43 | { |
| 44 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(input, DataType::F32); |
| 45 | ARM_COMPUTE_ERROR_ON(hog == nullptr); |
| 46 | ARM_COMPUTE_ERROR_ON(detection_windows == nullptr); |
| 47 | ARM_COMPUTE_ERROR_ON((detection_window_stride.width % hog->info()->block_stride().width) != 0); |
| 48 | ARM_COMPUTE_ERROR_ON((detection_window_stride.height % hog->info()->block_stride().height) != 0); |
| 49 | |
| 50 | const Size2D &detection_window_size = hog->info()->detection_window_size(); |
| 51 | const Size2D &block_size = hog->info()->block_size(); |
| 52 | const Size2D &block_stride = hog->info()->block_stride(); |
| 53 | |
| 54 | _input = input; |
| 55 | _detection_windows = detection_windows; |
| 56 | _threshold = threshold; |
| 57 | _idx_class = idx_class; |
| 58 | _hog_descriptor = hog->descriptor(); |
| 59 | _bias = _hog_descriptor[hog->info()->descriptor_size() - 1]; |
| 60 | _num_bins_per_descriptor_x = ((detection_window_size.width - block_size.width) / block_stride.width + 1) * input->info()->num_channels(); |
| 61 | _num_blocks_per_descriptor_y = (detection_window_size.height - block_size.height) / block_stride.height + 1; |
| 62 | _block_stride_width = block_stride.width; |
| 63 | _block_stride_height = block_stride.height; |
| 64 | _detection_window_width = detection_window_size.width; |
| 65 | _detection_window_height = detection_window_size.height; |
| 66 | _max_num_detection_windows = detection_windows->max_num_values(); |
| 67 | |
| 68 | ARM_COMPUTE_ERROR_ON((_num_bins_per_descriptor_x * _num_blocks_per_descriptor_y + 1) != hog->info()->descriptor_size()); |
| 69 | |
| 70 | // Get the number of blocks along the x and y directions of the input tensor |
| 71 | const ValidRegion &valid_region = input->info()->valid_region(); |
| 72 | const size_t num_blocks_x = valid_region.shape[0]; |
| 73 | const size_t num_blocks_y = valid_region.shape[1]; |
| 74 | |
| 75 | // Get the number of blocks along the x and y directions of the detection window |
| 76 | const size_t num_blocks_per_detection_window_x = detection_window_size.width / block_stride.width; |
| 77 | const size_t num_blocks_per_detection_window_y = detection_window_size.height / block_stride.height; |
| 78 | |
| 79 | const size_t window_step_x = detection_window_stride.width / block_stride.width; |
| 80 | const size_t window_step_y = detection_window_stride.height / block_stride.height; |
| 81 | |
| 82 | // Configure kernel window |
| 83 | Window win; |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 84 | win.set(Window::DimX, Window::Dimension(0, floor_to_multiple(num_blocks_x - num_blocks_per_detection_window_x, window_step_x) + window_step_x, window_step_x)); |
| 85 | win.set(Window::DimY, Window::Dimension(0, floor_to_multiple(num_blocks_y - num_blocks_per_detection_window_y, window_step_y) + window_step_y, window_step_y)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | |
| 87 | constexpr unsigned int num_elems_read_per_iteration = 1; |
| 88 | const unsigned int num_rows_read_per_iteration = _num_blocks_per_descriptor_y; |
| 89 | |
| 90 | update_window_and_padding(win, AccessWindowRectangle(input->info(), 0, 0, num_elems_read_per_iteration, num_rows_read_per_iteration)); |
| 91 | |
| 92 | INEKernel::configure(win); |
| 93 | } |
| 94 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 95 | void NEHOGDetectorKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 97 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 99 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 100 | ARM_COMPUTE_ERROR_ON(_hog_descriptor == nullptr); |
| 101 | |
| 102 | const size_t in_step_y = _input->info()->strides_in_bytes()[Window::DimY] / data_size_from_type(_input->info()->data_type()); |
| 103 | |
| 104 | Iterator in(_input, window); |
| 105 | |
| 106 | execute_window_loop(window, [&](const Coordinates & id) |
| 107 | { |
| 108 | const auto *in_row_ptr = reinterpret_cast<const float *>(in.ptr()); |
| 109 | |
| 110 | // Init score_f32 with 0 |
| 111 | float32x4_t score_f32 = vdupq_n_f32(0.0f); |
| 112 | |
| 113 | // Init score with bias |
| 114 | float score = _bias; |
| 115 | |
| 116 | // Compute Linear SVM |
| 117 | for(size_t yb = 0; yb < _num_blocks_per_descriptor_y; ++yb, in_row_ptr += in_step_y) |
| 118 | { |
| 119 | int32_t xb = 0; |
| 120 | |
| 121 | const int32_t offset_y = yb * _num_bins_per_descriptor_x; |
| 122 | |
| 123 | for(; xb < static_cast<int32_t>(_num_bins_per_descriptor_x) - 16; xb += 16) |
| 124 | { |
| 125 | // Load descriptor values |
| 126 | const float32x4x4_t a_f32 = |
| 127 | { |
| 128 | { |
| 129 | vld1q_f32(&in_row_ptr[xb + 0]), |
| 130 | vld1q_f32(&in_row_ptr[xb + 4]), |
| 131 | vld1q_f32(&in_row_ptr[xb + 8]), |
| 132 | vld1q_f32(&in_row_ptr[xb + 12]) |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | // Load detector values |
| 137 | const float32x4x4_t b_f32 = |
| 138 | { |
| 139 | { |
| 140 | vld1q_f32(&_hog_descriptor[xb + 0 + offset_y]), |
| 141 | vld1q_f32(&_hog_descriptor[xb + 4 + offset_y]), |
| 142 | vld1q_f32(&_hog_descriptor[xb + 8 + offset_y]), |
| 143 | vld1q_f32(&_hog_descriptor[xb + 12 + offset_y]) |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | // Multiply accumulate |
| 148 | score_f32 = vmlaq_f32(score_f32, a_f32.val[0], b_f32.val[0]); |
| 149 | score_f32 = vmlaq_f32(score_f32, a_f32.val[1], b_f32.val[1]); |
| 150 | score_f32 = vmlaq_f32(score_f32, a_f32.val[2], b_f32.val[2]); |
| 151 | score_f32 = vmlaq_f32(score_f32, a_f32.val[3], b_f32.val[3]); |
| 152 | } |
| 153 | |
| 154 | for(; xb < static_cast<int32_t>(_num_bins_per_descriptor_x); ++xb) |
| 155 | { |
| 156 | const float a = in_row_ptr[xb]; |
| 157 | const float b = _hog_descriptor[xb + offset_y]; |
| 158 | |
| 159 | score += a * b; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | score += vgetq_lane_f32(score_f32, 0); |
| 164 | score += vgetq_lane_f32(score_f32, 1); |
| 165 | score += vgetq_lane_f32(score_f32, 2); |
| 166 | score += vgetq_lane_f32(score_f32, 3); |
| 167 | |
| 168 | if(score > _threshold) |
| 169 | { |
| 170 | if(_detection_windows->num_values() < _max_num_detection_windows) |
| 171 | { |
| 172 | DetectionWindow win; |
| 173 | win.x = (id.x() * _block_stride_width); |
| 174 | win.y = (id.y() * _block_stride_height); |
| 175 | win.width = _detection_window_width; |
| 176 | win.height = _detection_window_height; |
| 177 | win.idx_class = _idx_class; |
| 178 | win.score = score; |
| 179 | |
Michalis Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 180 | std::unique_lock<arm_compute::Mutex> lock(_mutex); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | _detection_windows->push_back(win); |
| 182 | lock.unlock(); |
| 183 | } |
| 184 | } |
| 185 | }, |
| 186 | in); |
| 187 | } |