blob: c2d0514d1145d911453af4538d53e6ff27fa47c5 [file] [log] [blame]
John Richardson684cb0f2018-01-09 11:17:00 +00001/*
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#ifndef ARM_COMPUTE_TEST_HOG_DETECTOR_FIXTURE
25#define ARM_COMPUTE_TEST_HOG_DETECTOR_FIXTURE
26
27#include "arm_compute/core/HOGInfo.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/IHOGAccessor.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
35#include "tests/validation/fixtures/HOGDescriptorFixture.h"
36#include "tests/validation/reference/HOGDetector.h"
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44template <typename TensorType,
45 typename HOGType,
46 typename DetectionWindowArrayType,
47 typename HOGDescriptorType,
48 typename AccessorType,
49 typename ArrayAccessorType,
50 typename HOGAccessorType,
51 typename HOGDetectorType,
52 typename T,
53 typename U>
54class HOGDetectorValidationFixture : public HOGDescriptorValidationFixture<TensorType, HOGType, AccessorType, HOGDescriptorType, T, U>
55{
56public:
57 template <typename...>
58 void setup(Size2D detection_window_stride, std::string image, HOGInfo hog_info, Format format, BorderMode border_mode)
59 {
60 using HDF = HOGDescriptorValidationFixture<TensorType, HOGType, AccessorType, HOGDescriptorType, T, U>;
61 HDF::setup(image, hog_info, format, border_mode);
62
63 const unsigned int max_num_detection_windows = 100000;
64
65 // Initialise descriptor (linear SVM coefficients).
66 // NOTE: Fixed values are used to keep the number of detection windows detected
67 // consistent in order to have meaningful validation tolerances.
68 // The values are "unbalanced" to reduce the number of detected objects
69 std::random_device::result_type seed = 0;
70 std::vector<U> descriptor = generate_random_real(hog_info.descriptor_size(), -0.505f, 0.495f, seed);
71
72 // Compute target and reference values using feature vector from descriptor kernel
73 _target = compute_target(HDF::_target, descriptor, max_num_detection_windows, hog_info, detection_window_stride);
74 _reference = compute_reference(HDF::_reference, descriptor, max_num_detection_windows, hog_info, detection_window_stride);
75 }
76
77protected:
78 std::vector<DetectionWindow> compute_target(const TensorType &src, const std::vector<U> &descriptor, unsigned int max_num_detection_windows,
79 const HOGInfo &hog_info, const Size2D &detection_window_stride)
80 {
81 // Create HOG
82 HOGType hog = create_HOG<HOGType>(hog_info);
83
84 // Create array of detection windows
85 DetectionWindowArrayType detection_windows(max_num_detection_windows);
86
87 // Copy HOG descriptor values to HOG memory
88 {
89 HOGAccessorType hog_accessor(hog);
90 std::memcpy(hog_accessor.descriptor(), descriptor.data(), descriptor.size() * sizeof(U));
91 }
92
93 // Create and configure function
94 HOGDetectorType hog_detector;
95 hog_detector.configure(&src, &hog, &detection_windows, detection_window_stride);
96
97 // Reset detection windows
98 detection_windows.clear();
99
100 // Compute function
101 hog_detector.run();
102
103 // Create array of detection windows
104 std::vector<DetectionWindow> windows;
105
106 // Copy detection windows
107 ArrayAccessorType accessor(detection_windows);
108
109 for(size_t i = 0; i < accessor.num_values(); i++)
110 {
111 DetectionWindow win;
112 win.x = accessor.at(i).x;
113 win.y = accessor.at(i).y;
114 win.width = accessor.at(i).width;
115 win.height = accessor.at(i).height;
116 win.idx_class = accessor.at(i).idx_class;
117 win.score = accessor.at(i).score;
118
119 windows.push_back(win);
120 }
121
122 return windows;
123 }
124
125 std::vector<DetectionWindow> compute_reference(const SimpleTensor<U> &src, const std::vector<U> &descriptor, unsigned int max_num_detection_windows,
126 const HOGInfo &hog_info, const Size2D &detection_window_stride)
127 {
128 // Assumes defaults value of zero for threshold and class_idx.
129 return reference::hog_detector(src, descriptor, max_num_detection_windows, hog_info, detection_window_stride);
130 }
131
132 std::vector<DetectionWindow> _target{};
133 std::vector<DetectionWindow> _reference{};
134};
135} // namespace validation
136} // namespace test
137} // namespace arm_compute
138#endif /* ARM_COMPUTE_TEST_HOG_DETECTOR_FIXTURE */