blob: 8ca1b0c2045f1aad1b2c468cab2e9f7bb11a91e0 [file] [log] [blame]
John Richardson684cb0f2018-01-09 11:17:00 +00001/*
Michalis Spyroubcfd09a2019-05-01 13:03:59 +01002 * Copyright (c) 2018-2019 ARM Limited.
John Richardson684cb0f2018-01-09 11:17:00 +00003 *
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 "HOGDetector.h"
25
26namespace arm_compute
27{
28namespace test
29{
30namespace validation
31{
32namespace reference
33{
34namespace
35{
36/** Computes the number of detection windows to iterate over in the feature vector. */
37Size2D num_detection_windows(const TensorShape &shape, const Size2D &window_step, const HOGInfo &hog_info)
38{
39 const size_t num_block_strides_width = hog_info.detection_window_size().width / hog_info.block_stride().width;
40 const size_t num_block_strides_height = hog_info.detection_window_size().height / hog_info.block_stride().height;
41
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010042 return Size2D{ floor_to_multiple(shape.x() - num_block_strides_width, window_step.width) + window_step.width,
43 floor_to_multiple(shape.y() - num_block_strides_height, window_step.height) + window_step.height };
John Richardson684cb0f2018-01-09 11:17:00 +000044}
45} // namespace
46
47template <typename T>
48std::vector<DetectionWindow> hog_detector(const SimpleTensor<T> &src, const std::vector<T> &descriptor, unsigned int max_num_detection_windows,
49 const HOGInfo &hog_info, const Size2D &detection_window_stride, float threshold, uint16_t idx_class)
50{
51 ARM_COMPUTE_ERROR_ON_MSG((detection_window_stride.width % hog_info.block_stride().width != 0),
52 "Detection window stride width must be multiple of block stride width");
53 ARM_COMPUTE_ERROR_ON_MSG((detection_window_stride.height % hog_info.block_stride().height != 0),
54 "Detection window stride height must be multiple of block stride height");
55
56 // Create vector for identifying each detection window
57 std::vector<DetectionWindow> windows;
58
59 // Calculate detection window step
60 const Size2D window_step(detection_window_stride.width / hog_info.block_stride().width,
61 detection_window_stride.height / hog_info.block_stride().height);
62
63 // Calculate number of detection windows
64 const Size2D num_windows = num_detection_windows(src.shape(), window_step, hog_info);
65
66 // Calculate detection window and row offsets in feature vector
67 const size_t src_offset_x = window_step.width * hog_info.num_bins() * hog_info.num_cells_per_block().area();
68 const size_t src_offset_y = window_step.height * hog_info.num_bins() * hog_info.num_cells_per_block().area() * src.shape().x();
69 const size_t src_offset_row = src.num_channels() * src.shape().x();
70
71 // Calculate detection window attributes
72 const Size2D num_block_positions_per_detection_window = hog_info.num_block_positions_per_image(hog_info.detection_window_size());
73 const unsigned int num_bins_per_descriptor_x = num_block_positions_per_detection_window.width * src.num_channels();
74 const unsigned int num_blocks_per_descriptor_y = num_block_positions_per_detection_window.height;
75
76 ARM_COMPUTE_ERROR_ON((num_bins_per_descriptor_x * num_blocks_per_descriptor_y + 1) != hog_info.descriptor_size());
77
78 size_t win_id = 0;
79
80 // Traverse feature vector in detection window steps
81 for(auto win_y = 0u, offset_y = 0u; win_y < num_windows.height; win_y += window_step.height, offset_y += src_offset_y)
82 {
83 for(auto win_x = 0u, offset_x = 0u; win_x < num_windows.width; win_x += window_step.width, offset_x += src_offset_x)
84 {
85 // Reset the score
86 float score = 0.0f;
87
88 // Traverse detection window
89 for(auto y = 0u, offset_row = 0u; y < num_blocks_per_descriptor_y; ++y, offset_row += src_offset_row)
90 {
91 const int bin_offset = y * num_bins_per_descriptor_x;
92
93 for(auto x = 0u; x < num_bins_per_descriptor_x; ++x)
94 {
95 // Compute Linear SVM
96 const float a = src[x + offset_x + offset_y + offset_row];
97 const float b = descriptor[x + bin_offset];
98 score += a * b;
99 }
100 }
101
102 // Add the bias. The bias is located at the position (descriptor_size() - 1)
103 score += descriptor[num_bins_per_descriptor_x * num_blocks_per_descriptor_y];
104
105 if(score > threshold)
106 {
107 DetectionWindow window;
108
109 if(win_id++ < max_num_detection_windows)
110 {
111 window.x = win_x * hog_info.block_stride().width;
112 window.y = win_y * hog_info.block_stride().height;
113 window.width = hog_info.detection_window_size().width;
114 window.height = hog_info.detection_window_size().height;
115 window.idx_class = idx_class;
116 window.score = score;
117
118 windows.push_back(window);
119 }
120 }
121 }
122 }
123
124 return windows;
125}
126
127template std::vector<DetectionWindow> hog_detector(const SimpleTensor<float> &src, const std::vector<float> &descriptor, unsigned int max_num_detection_windows,
128 const HOGInfo &hog_info, const Size2D &detection_window_stride, float threshold, uint16_t idx_class);
129} // namespace reference
130} // namespace validation
131} // namespace test
132} // namespace arm_compute