blob: f78187570a9aae99849faeedd7ab42e7a5ec47f0 [file] [log] [blame]
John Richardsonec4c3202018-05-18 13:25:52 +01001/*
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_OPTICAL_FLOW_FIXTURE
25#define ARM_COMPUTE_TEST_OPTICAL_FLOW_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/Globals.h"
30#include "tests/Types.h"
31#include "tests/Utils.h"
32#include "tests/framework/Fixture.h"
33
34namespace arm_compute
35{
36namespace test
37{
38namespace benchmark
39{
40template <typename TensorType,
41 typename Function,
42 typename Accessor,
43 typename ArrayType,
44 typename ArrayAccessor,
45 typename PyramidType,
46 typename PyramidFunctionType>
47class OpticalFlowFixture : public framework::Fixture
48{
49public:
50 template <typename...>
51 void setup(std::string old_image_name, std::string new_image_name, OpticalFlowParameters params,
52 size_t num_levels, size_t num_keypoints, Format format, BorderMode border_mode)
53 {
54 const uint8_t constant_border_value = 0;
55 const std::random_device::result_type seed = 0;
56
57 // Create keypoints
58 old_keypoints = generate_random_keypoints(library->get_image_shape(old_image_name), num_keypoints, seed, num_levels);
59 new_keypoints_estimates = old_keypoints;
60
61 // Create tensors
62 old_image = create_tensor<TensorType>(library->get_image_shape(old_image_name), format);
63 new_image = create_tensor<TensorType>(library->get_image_shape(new_image_name), format);
64
65 // Load keypoints
66 fill_array(ArrayAccessor(old_points), old_keypoints);
67 fill_array(ArrayAccessor(new_points_estimates), new_keypoints_estimates);
68
69 // Create pyramid images
70 PyramidInfo pyramid_info(num_levels, SCALE_PYRAMID_HALF, old_image.info()->tensor_shape(), format);
71 old_pyramid = create_pyramid<PyramidType>(pyramid_info);
72 new_pyramid = create_pyramid<PyramidType>(pyramid_info);
73
74 // Create and configure pyramid functions
75 old_gaussian_pyramid_func.configure(&old_image, &old_pyramid, border_mode, constant_border_value);
76 new_gaussian_pyramid_func.configure(&new_image, &new_pyramid, border_mode, constant_border_value);
77
78 optical_flow_func.configure(&old_pyramid,
79 &new_pyramid,
80 &old_points,
81 &new_points_estimates,
82 &new_points,
83 params.termination,
84 params.epsilon,
85 params.num_iterations,
86 params.window_dimension,
87 params.use_initial_estimate,
88 border_mode,
89 constant_border_value);
90
91 // Allocate input tensors
92 old_image.allocator()->allocate();
93 new_image.allocator()->allocate();
94
95 // Allocate pyramids
96 old_pyramid.allocate();
97 new_pyramid.allocate();
98
99 // Copy image data to tensors
100 library->fill(Accessor(old_image), old_image_name, format);
101 library->fill(Accessor(new_image), new_image_name, format);
102
103 // Compute gaussian pyramids
104 old_gaussian_pyramid_func.run();
105 new_gaussian_pyramid_func.run();
106 }
107
108 void run()
109 {
110 optical_flow_func.run();
111 }
112
113 void sync()
114 {
115 sync_if_necessary<TensorType>();
116 }
117
118 void teardown()
119 {
120 old_image.allocator()->free();
121 new_image.allocator()->free();
122 }
123
124private:
125 static const size_t max_keypoints = 10000;
126
127 std::vector<KeyPoint> old_keypoints{};
128 std::vector<KeyPoint> new_keypoints_estimates{};
129
130 TensorType old_image{};
131 TensorType new_image{};
132
133 ArrayType old_points{ max_keypoints };
134 ArrayType new_points{ max_keypoints };
135 ArrayType new_points_estimates{ max_keypoints };
136
137 PyramidType old_pyramid{};
138 PyramidType new_pyramid{};
139
140 PyramidFunctionType old_gaussian_pyramid_func{};
141 PyramidFunctionType new_gaussian_pyramid_func{};
142
143 Function optical_flow_func{};
144};
145} // namespace benchmark
146} // namespace test
147} // namespace arm_compute
148#endif /* ARM_COMPUTE_TEST_OPTICAL_FLOW_FIXTURE */