blob: cd6ce99796a17476bd9494438c7a5df439ec5272 [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +01001/*
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +01002 * Copyright (c) 2018-2021 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
giuros01c04a0e82018-10-03 12:44:35 +0100105 template <typename...>
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100106 void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo)
giuros01c04a0e82018-10-03 12:44:35 +0100107 {
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100108 const bool is_qasymm16 = data_type == DataType::QASYMM16;
109 _data_type_deltas = (is_qasymm16) ? DataType::QASYMM8 : data_type;
110 _boxes_qinfo = (is_qasymm16) ? QuantizationInfo(.125f, 0) : QuantizationInfo();
111
giuros01c04a0e82018-10-03 12:44:35 +0100112 std::mt19937 gen_target(library->seed());
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100113 _target = compute_target(deltas_shape, data_type, info, gen_target, deltas_qinfo);
giuros01c04a0e82018-10-03 12:44:35 +0100114
115 std::mt19937 gen_reference(library->seed());
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100116 _reference = compute_reference(deltas_shape, data_type, info, gen_reference, deltas_qinfo);
giuros01c04a0e82018-10-03 12:44:35 +0100117 }
118
119protected:
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100120 template <typename data_type, typename U>
121 void fill(U &&tensor, std::vector<float> values)
122 {
123 data_type *data_ptr = reinterpret_cast<data_type *>(tensor.data());
124 switch(tensor.data_type())
125 {
126 case DataType::QASYMM8:
127 for(size_t i = 0; i < values.size(); ++i)
128 {
129 data_ptr[i] = quantize_qasymm8(values[i], tensor.quantization_info());
130 }
131 break;
132 case DataType::QASYMM16:
133 for(size_t i = 0; i < values.size(); ++i)
134 {
135 data_ptr[i] = quantize_qasymm16(values[i], tensor.quantization_info());
136 }
137 break;
138 default:
139 for(size_t i = 0; i < values.size(); ++i)
140 {
141 data_ptr[i] = static_cast<data_type>(values[i]);
142 }
143 }
144 }
145
giuros01c04a0e82018-10-03 12:44:35 +0100146 TensorType compute_target(const TensorShape &deltas_shape, DataType data_type,
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100147 const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen,
148 QuantizationInfo deltas_qinfo)
giuros01c04a0e82018-10-03 12:44:35 +0100149 {
150 // Create tensors
151 TensorShape boxes_shape(4, deltas_shape[1]);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100152 TensorType deltas = create_tensor<TensorType>(deltas_shape, _data_type_deltas, 1, deltas_qinfo);
153 TensorType boxes = create_tensor<TensorType>(boxes_shape, data_type, 1, _boxes_qinfo);
giuros01c04a0e82018-10-03 12:44:35 +0100154 TensorType pred_boxes;
155
156 // Create and configure function
157 FunctionType bbox_transform;
158 bbox_transform.configure(&boxes, &pred_boxes, &deltas, bbox_info);
159
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100160 ARM_COMPUTE_ASSERT(deltas.info()->is_resizable());
161 ARM_COMPUTE_ASSERT(boxes.info()->is_resizable());
162 ARM_COMPUTE_ASSERT(pred_boxes.info()->is_resizable());
giuros01c04a0e82018-10-03 12:44:35 +0100163
164 // Allocate tensors
165 deltas.allocator()->allocate();
166 boxes.allocator()->allocate();
167 pred_boxes.allocator()->allocate();
168
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100169 ARM_COMPUTE_ASSERT(!deltas.info()->is_resizable());
170 ARM_COMPUTE_ASSERT(!boxes.info()->is_resizable());
giuros01c04a0e82018-10-03 12:44:35 +0100171
172 // Fill tensors
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100173 TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
174 std::vector<float> boxes_vec = generate_boxes(img_shape, boxes_shape[1], gen);
175 std::vector<float> deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
176 fill<T>(AccessorType(boxes), boxes_vec);
177 fill<TDeltas>(AccessorType(deltas), deltas_vec);
giuros01c04a0e82018-10-03 12:44:35 +0100178
179 // Compute function
180 bbox_transform.run();
181
182 return pred_boxes;
183 }
184
185 SimpleTensor<T> compute_reference(const TensorShape &deltas_shape,
186 DataType data_type,
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100187 const BoundingBoxTransformInfo &bbox_info,
188 std::mt19937 &gen,
189 QuantizationInfo deltas_qinfo)
giuros01c04a0e82018-10-03 12:44:35 +0100190 {
191 // Create reference tensor
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100192 TensorShape boxes_shape(4, deltas_shape[1]);
193 SimpleTensor<T> boxes{ boxes_shape, data_type, 1, _boxes_qinfo };
194 SimpleTensor<TDeltas> deltas{ deltas_shape, _data_type_deltas, 1, deltas_qinfo };
giuros01c04a0e82018-10-03 12:44:35 +0100195
196 // Fill reference tensor
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100197 TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
198 std::vector<float> boxes_vec = generate_boxes(img_shape, boxes_shape[1], gen);
199 std::vector<float> deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
200 fill<T>(boxes, boxes_vec);
201 fill<TDeltas>(deltas, deltas_vec);
giuros01c04a0e82018-10-03 12:44:35 +0100202
203 return reference::bounding_box_transform(boxes, deltas, bbox_info);
204 }
205
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100206 TensorType _target{};
207 SimpleTensor<T> _reference{};
208 DataType _data_type_deltas{};
209 QuantizationInfo _boxes_qinfo{};
giuros01c04a0e82018-10-03 12:44:35 +0100210
211private:
giuros01c04a0e82018-10-03 12:44:35 +0100212};
213
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100214template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
215class BoundingBoxTransformFixture : public BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>
216{
217public:
218 template <typename...>
219 void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type)
220 {
221 BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(deltas_shape, info, data_type, QuantizationInfo());
222 }
223
224private:
225};
226
227template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
228class BoundingBoxTransformQuantizedFixture : public BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>
229{
230public:
231 template <typename...>
232 void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo)
233 {
234 BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(deltas_shape, info, data_type, deltas_qinfo);
235 }
236};
giuros01c04a0e82018-10-03 12:44:35 +0100237} // namespace validation
238} // namespace test
239} // namespace arm_compute
240#endif /* ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE */