blob: 03edaeab16a57a460d788f30e825bb80daeca942 [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
giuros01c04a0e82018-10-03 12:44:35 +01003 *
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{
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010043namespace
44{
45std::vector<float> generate_deltas(std::vector<float> &boxes, const TensorShape &image_shape, size_t num_boxes, size_t num_classes, std::mt19937 &gen)
46{
47 std::vector<float> deltas(num_boxes * 4 * num_classes);
48
49 std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1);
50 std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1);
51 std::uniform_int_distribution<> dist_w(1, image_shape[0]);
52 std::uniform_int_distribution<> dist_h(1, image_shape[1]);
53
54 for(size_t i = 0; i < num_boxes; ++i)
55 {
56 const float ex_width = boxes[4 * i + 2] - boxes[4 * i] + 1.f;
57 const float ex_height = boxes[4 * i + 3] - boxes[4 * i + 1] + 1.f;
58 const float ex_ctr_x = boxes[4 * i] + 0.5f * ex_width;
59 const float ex_ctr_y = boxes[4 * i + 1] + 0.5f * ex_height;
60
61 for(size_t j = 0; j < num_classes; ++j)
62 {
63 const float x1 = dist_x1(gen);
64 const float y1 = dist_y1(gen);
65 const float width = dist_w(gen);
66 const float height = dist_h(gen);
67 const float ctr_x = x1 + 0.5f * width;
68 const float ctr_y = y1 + 0.5f * height;
69
70 deltas[4 * num_classes * i + 4 * j] = (ctr_x - ex_ctr_x) / ex_width;
71 deltas[4 * num_classes * i + 4 * j + 1] = (ctr_y - ex_ctr_y) / ex_height;
72 deltas[4 * num_classes * i + 4 * j + 2] = log(width / ex_width);
73 deltas[4 * num_classes * i + 4 * j + 3] = log(height / ex_height);
74 }
75 }
76 return deltas;
77}
78
79std::vector<float> generate_boxes(const TensorShape &image_shape, size_t num_boxes, std::mt19937 &gen)
80{
81 std::vector<float> boxes(num_boxes * 4);
82
83 std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1);
84 std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1);
85 std::uniform_int_distribution<> dist_w(1, image_shape[0]);
86 std::uniform_int_distribution<> dist_h(1, image_shape[1]);
87
88 for(size_t i = 0; i < num_boxes; ++i)
89 {
90 boxes[4 * i] = dist_x1(gen);
91 boxes[4 * i + 1] = dist_y1(gen);
92 boxes[4 * i + 2] = boxes[4 * i] + dist_w(gen) - 1;
93 boxes[4 * i + 3] = boxes[4 * i + 1] + dist_h(gen) - 1;
94 }
95 return boxes;
96}
97} // namespace
98
giuros01c04a0e82018-10-03 12:44:35 +010099template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100100class BoundingBoxTransformGenericFixture : public framework::Fixture
giuros01c04a0e82018-10-03 12:44:35 +0100101{
102public:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100103 using TDeltas = typename std::conditional<std::is_same<typename std::decay<T>::type, uint16_t>::value, uint8_t, T>::type;
104
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100105 void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo)
giuros01c04a0e82018-10-03 12:44:35 +0100106 {
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100107 const bool is_qasymm16 = data_type == DataType::QASYMM16;
108 _data_type_deltas = (is_qasymm16) ? DataType::QASYMM8 : data_type;
109 _boxes_qinfo = (is_qasymm16) ? QuantizationInfo(.125f, 0) : QuantizationInfo();
110
giuros01c04a0e82018-10-03 12:44:35 +0100111 std::mt19937 gen_target(library->seed());
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100112 _target = compute_target(deltas_shape, data_type, info, gen_target, deltas_qinfo);
giuros01c04a0e82018-10-03 12:44:35 +0100113
114 std::mt19937 gen_reference(library->seed());
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100115 _reference = compute_reference(deltas_shape, data_type, info, gen_reference, deltas_qinfo);
giuros01c04a0e82018-10-03 12:44:35 +0100116 }
117
118protected:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100119 template <typename data_type, typename U>
120 void fill(U &&tensor, std::vector<float> values)
121 {
122 data_type *data_ptr = reinterpret_cast<data_type *>(tensor.data());
123 switch(tensor.data_type())
124 {
125 case DataType::QASYMM8:
126 for(size_t i = 0; i < values.size(); ++i)
127 {
128 data_ptr[i] = quantize_qasymm8(values[i], tensor.quantization_info());
129 }
130 break;
131 case DataType::QASYMM16:
132 for(size_t i = 0; i < values.size(); ++i)
133 {
134 data_ptr[i] = quantize_qasymm16(values[i], tensor.quantization_info());
135 }
136 break;
137 default:
138 for(size_t i = 0; i < values.size(); ++i)
139 {
140 data_ptr[i] = static_cast<data_type>(values[i]);
141 }
142 }
143 }
144
giuros01c04a0e82018-10-03 12:44:35 +0100145 TensorType compute_target(const TensorShape &deltas_shape, DataType data_type,
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100146 const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen,
147 QuantizationInfo deltas_qinfo)
giuros01c04a0e82018-10-03 12:44:35 +0100148 {
149 // Create tensors
150 TensorShape boxes_shape(4, deltas_shape[1]);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100151 TensorType deltas = create_tensor<TensorType>(deltas_shape, _data_type_deltas, 1, deltas_qinfo);
152 TensorType boxes = create_tensor<TensorType>(boxes_shape, data_type, 1, _boxes_qinfo);
giuros01c04a0e82018-10-03 12:44:35 +0100153 TensorType pred_boxes;
154
155 // Create and configure function
156 FunctionType bbox_transform;
157 bbox_transform.configure(&boxes, &pred_boxes, &deltas, bbox_info);
158
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100159 ARM_COMPUTE_ASSERT(deltas.info()->is_resizable());
160 ARM_COMPUTE_ASSERT(boxes.info()->is_resizable());
161 ARM_COMPUTE_ASSERT(pred_boxes.info()->is_resizable());
giuros01c04a0e82018-10-03 12:44:35 +0100162
163 // Allocate tensors
164 deltas.allocator()->allocate();
165 boxes.allocator()->allocate();
166 pred_boxes.allocator()->allocate();
167
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100168 ARM_COMPUTE_ASSERT(!deltas.info()->is_resizable());
169 ARM_COMPUTE_ASSERT(!boxes.info()->is_resizable());
giuros01c04a0e82018-10-03 12:44:35 +0100170
171 // Fill tensors
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100172 TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
173 std::vector<float> boxes_vec = generate_boxes(img_shape, boxes_shape[1], gen);
174 std::vector<float> deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
175 fill<T>(AccessorType(boxes), boxes_vec);
176 fill<TDeltas>(AccessorType(deltas), deltas_vec);
giuros01c04a0e82018-10-03 12:44:35 +0100177
178 // Compute function
179 bbox_transform.run();
180
181 return pred_boxes;
182 }
183
184 SimpleTensor<T> compute_reference(const TensorShape &deltas_shape,
185 DataType data_type,
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100186 const BoundingBoxTransformInfo &bbox_info,
187 std::mt19937 &gen,
188 QuantizationInfo deltas_qinfo)
giuros01c04a0e82018-10-03 12:44:35 +0100189 {
190 // Create reference tensor
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100191 TensorShape boxes_shape(4, deltas_shape[1]);
192 SimpleTensor<T> boxes{ boxes_shape, data_type, 1, _boxes_qinfo };
193 SimpleTensor<TDeltas> deltas{ deltas_shape, _data_type_deltas, 1, deltas_qinfo };
giuros01c04a0e82018-10-03 12:44:35 +0100194
195 // Fill reference tensor
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100196 TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
197 std::vector<float> boxes_vec = generate_boxes(img_shape, boxes_shape[1], gen);
198 std::vector<float> deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
199 fill<T>(boxes, boxes_vec);
200 fill<TDeltas>(deltas, deltas_vec);
giuros01c04a0e82018-10-03 12:44:35 +0100201
202 return reference::bounding_box_transform(boxes, deltas, bbox_info);
203 }
204
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100205 TensorType _target{};
206 SimpleTensor<T> _reference{};
207 DataType _data_type_deltas{};
208 QuantizationInfo _boxes_qinfo{};
giuros01c04a0e82018-10-03 12:44:35 +0100209
210private:
giuros01c04a0e82018-10-03 12:44:35 +0100211};
212
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100213template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
214class BoundingBoxTransformFixture : public BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>
215{
216public:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100217 void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type)
218 {
219 BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(deltas_shape, info, data_type, QuantizationInfo());
220 }
221
222private:
223};
224
225template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
226class BoundingBoxTransformQuantizedFixture : public BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>
227{
228public:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100229 void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo)
230 {
231 BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(deltas_shape, info, data_type, deltas_qinfo);
232 }
233};
giuros01c04a0e82018-10-03 12:44:35 +0100234} // namespace validation
235} // namespace test
236} // namespace arm_compute
237#endif /* ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE */