Compute Library
 21.02
HOGDetector.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019 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 "HOGDetector.h"
25 
26 namespace arm_compute
27 {
28 namespace test
29 {
30 namespace validation
31 {
32 namespace reference
33 {
34 namespace
35 {
36 /** Computes the number of detection windows to iterate over in the feature vector. */
37 Size2D 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 
42  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 };
44 }
45 } // namespace
46 
47 template <typename T>
48 std::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 
127 template 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
size_t num_bins() const
The number of histogram bins for each cell.
Definition: HOGInfo.cpp:111
const Size2D & detection_window_size() const
The detection window size in pixels.
Definition: HOGInfo.cpp:101
uint16_t x
Top-left x coordinate.
Definition: Types.h:592
float score
Confidence value for the detection window.
Definition: Types.h:597
SimpleTensor< float > b
Definition: DFT.cpp:157
Store the HOG&#39;s metadata.
Definition: HOGInfo.h:35
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
auto floor_to_multiple(S value, T divisor) -> decltype((value/divisor) *divisor)
Computes the largest number smaller or equal to value that is a multiple of divisor.
Definition: Utils.h:85
const Size2D & block_stride() const
The block stride in pixels.
Definition: HOGInfo.cpp:106
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:90
Size2D num_cells_per_block() const
Calculates the number of cells for each block.
Definition: HOGInfo.cpp:67
uint16_t width
Width of the detection window.
Definition: Types.h:594
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
uint16_t idx_class
Index of the class.
Definition: Types.h:596
uint16_t height
Height of the detection window.
Definition: Types.h:595
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
std::vector< DetectionWindow > hog_detector(const SimpleTensor< T > &src, const std::vector< T > &descriptor, unsigned int max_num_detection_windows, const HOGInfo &hog_info, const Size2D &detection_window_stride, float threshold, uint16_t idx_class)
Definition: HOGDetector.cpp:48
int num_channels() const override
Number of channels of the tensor.
Definition: SimpleTensor.h:370
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:89
Size2D num_block_positions_per_image(const Size2D &image_size) const
Calculates the number of block positions for the given image size.
Definition: HOGInfo.cpp:83
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Detection window used for the object detection.
Definition: Types.h:590
uint16_t y
Top-left y coordinate.
Definition: Types.h:593
size_t descriptor_size() const
The size of HOG descriptor.
Definition: HOGInfo.cpp:131
SimpleTensor< T > threshold(const SimpleTensor< T > &src, T threshold, T false_value, T true_value, ThresholdType type, T upper)
Definition: Threshold.cpp:35
size_t area() const
The area of the image or rectangle calculated as (width * height)
Definition: Size2D.h:53