blob: b71da8e97d61c17ab6cc7e395efed1189950343a [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +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_BOUNDINGBOXTRANSFORM_FIXTURE
25#define ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE
26
27#include "arm_compute/core/TensorShape.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/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
34#include "tests/validation/Helpers.h"
35#include "tests/validation/reference/BoundingBoxTransform.h"
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
44class BoundingBoxTransformFixture : public framework::Fixture
45{
46public:
47 template <typename...>
48 void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type)
49 {
50 std::mt19937 gen_target(library->seed());
51 _target = compute_target(deltas_shape, data_type, info, gen_target);
52
53 std::mt19937 gen_reference(library->seed());
54 _reference = compute_reference(deltas_shape, data_type, info, gen_reference);
55 }
56
57protected:
58 TensorType compute_target(const TensorShape &deltas_shape, DataType data_type,
59 const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen)
60 {
61 // Create tensors
62 TensorShape boxes_shape(4, deltas_shape[1]);
63 TensorType deltas = create_tensor<TensorType>(deltas_shape, data_type);
64 TensorType boxes = create_tensor<TensorType>(boxes_shape, data_type);
65 TensorType pred_boxes;
66
67 // Create and configure function
68 FunctionType bbox_transform;
69 bbox_transform.configure(&boxes, &pred_boxes, &deltas, bbox_info);
70
71 ARM_COMPUTE_EXPECT(deltas.info()->is_resizable(), framework::LogLevel::ERRORS);
72 ARM_COMPUTE_EXPECT(boxes.info()->is_resizable(), framework::LogLevel::ERRORS);
73 ARM_COMPUTE_EXPECT(pred_boxes.info()->is_resizable(), framework::LogLevel::ERRORS);
74
75 // Allocate tensors
76 deltas.allocator()->allocate();
77 boxes.allocator()->allocate();
78 pred_boxes.allocator()->allocate();
79
80 ARM_COMPUTE_EXPECT(!deltas.info()->is_resizable(), framework::LogLevel::ERRORS);
81 ARM_COMPUTE_EXPECT(!boxes.info()->is_resizable(), framework::LogLevel::ERRORS);
82
83 // Fill tensors
84 TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
85 generate_boxes(AccessorType(boxes), img_shape, boxes_shape[1], gen);
86 generate_deltas(AccessorType(deltas), AccessorType(boxes), img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
87
88 // Compute function
89 bbox_transform.run();
90
91 return pred_boxes;
92 }
93
94 SimpleTensor<T> compute_reference(const TensorShape &deltas_shape,
95 DataType data_type,
96 const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen)
97 {
98 // Create reference tensor
99 TensorShape boxes_shape(4, deltas_shape[1]);
100 SimpleTensor<T> boxes{ boxes_shape, data_type };
101 SimpleTensor<T> deltas{ deltas_shape, data_type };
102
103 // Fill reference tensor
104 TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
105 generate_boxes(boxes, img_shape, boxes_shape[1], gen);
106 generate_deltas(deltas, boxes, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
107
108 return reference::bounding_box_transform(boxes, deltas, bbox_info);
109 }
110
111 TensorType _target{};
112 SimpleTensor<T> _reference{};
113
114private:
115 template <typename U>
116 void generate_deltas(U &&deltas, U &&boxes, const TensorShape &image_shape, size_t num_boxes, size_t num_classes, std::mt19937 &gen)
117 {
118 T *deltas_ptr = static_cast<T *>(deltas.data());
119 T *boxes_ptr = static_cast<T *>(boxes.data());
120
121 std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1);
122 std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1);
123 std::uniform_int_distribution<> dist_w(1, image_shape[0]);
124 std::uniform_int_distribution<> dist_h(1, image_shape[1]);
125
126 for(size_t i = 0; i < num_boxes; ++i)
127 {
128 const T ex_width = boxes_ptr[4 * i + 2] - boxes_ptr[4 * i] + T(1);
129 const T ex_height = boxes_ptr[4 * i + 3] - boxes_ptr[4 * i + 1] + T(1);
130 const T ex_ctr_x = boxes_ptr[4 * i] + T(0.5) * ex_width;
131 const T ex_ctr_y = boxes_ptr[4 * i + 1] + T(0.5) * ex_height;
132
133 for(size_t j = 0; j < num_classes; ++j)
134 {
135 const T x1 = T(dist_x1(gen));
136 const T y1 = T(dist_y1(gen));
137 const T width = T(dist_w(gen));
138 const T height = T(dist_h(gen));
139 const T ctr_x = x1 + T(0.5) * width;
140 const T ctr_y = y1 + T(0.5) * height;
141
142 deltas_ptr[4 * num_classes * i + 4 * j] = (ctr_x - ex_ctr_x) / ex_width;
143 deltas_ptr[4 * num_classes * i + 4 * j + 1] = (ctr_y - ex_ctr_y) / ex_height;
144 deltas_ptr[4 * num_classes * i + 4 * j + 2] = log(width / ex_width);
145 deltas_ptr[4 * num_classes * i + 4 * j + 3] = log(height / ex_height);
146 }
147 }
148 }
149
150 template <typename U>
151 void generate_boxes(U &&boxes, const TensorShape &image_shape, size_t num_boxes, std::mt19937 &gen)
152 {
153 T *boxes_ptr = (T *)boxes.data();
154
155 std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1);
156 std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1);
157 std::uniform_int_distribution<> dist_w(1, image_shape[0]);
158 std::uniform_int_distribution<> dist_h(1, image_shape[1]);
159
160 for(size_t i = 0; i < num_boxes; ++i)
161 {
162 boxes_ptr[4 * i] = dist_x1(gen);
163 boxes_ptr[4 * i + 1] = dist_y1(gen);
164 boxes_ptr[4 * i + 2] = boxes_ptr[4 * i] + dist_w(gen) - 1;
165 boxes_ptr[4 * i + 3] = boxes_ptr[4 * i + 1] + dist_h(gen) - 1;
166 }
167 }
168};
169
170} // namespace validation
171} // namespace test
172} // namespace arm_compute
173#endif /* ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE */