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