xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/BoundingBoxTransformFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE
25*c217d954SCole Faust #define ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/TensorShape.h"
28*c217d954SCole Faust #include "arm_compute/core/Types.h"
29*c217d954SCole Faust #include "tests/AssetsLibrary.h"
30*c217d954SCole Faust #include "tests/Globals.h"
31*c217d954SCole Faust #include "tests/IAccessor.h"
32*c217d954SCole Faust #include "tests/framework/Asserts.h"
33*c217d954SCole Faust #include "tests/framework/Fixture.h"
34*c217d954SCole Faust #include "tests/validation/Helpers.h"
35*c217d954SCole Faust #include "tests/validation/reference/BoundingBoxTransform.h"
36*c217d954SCole Faust 
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace test
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace validation
42*c217d954SCole Faust {
43*c217d954SCole Faust namespace
44*c217d954SCole Faust {
generate_deltas(std::vector<float> & boxes,const TensorShape & image_shape,size_t num_boxes,size_t num_classes,std::mt19937 & gen)45*c217d954SCole Faust std::vector<float> generate_deltas(std::vector<float> &boxes, const TensorShape &image_shape, size_t num_boxes, size_t num_classes, std::mt19937 &gen)
46*c217d954SCole Faust {
47*c217d954SCole Faust     std::vector<float> deltas(num_boxes * 4 * num_classes);
48*c217d954SCole Faust 
49*c217d954SCole Faust     std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1);
50*c217d954SCole Faust     std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1);
51*c217d954SCole Faust     std::uniform_int_distribution<> dist_w(1, image_shape[0]);
52*c217d954SCole Faust     std::uniform_int_distribution<> dist_h(1, image_shape[1]);
53*c217d954SCole Faust 
54*c217d954SCole Faust     for(size_t i = 0; i < num_boxes; ++i)
55*c217d954SCole Faust     {
56*c217d954SCole Faust         const float ex_width  = boxes[4 * i + 2] - boxes[4 * i] + 1.f;
57*c217d954SCole Faust         const float ex_height = boxes[4 * i + 3] - boxes[4 * i + 1] + 1.f;
58*c217d954SCole Faust         const float ex_ctr_x  = boxes[4 * i] + 0.5f * ex_width;
59*c217d954SCole Faust         const float ex_ctr_y  = boxes[4 * i + 1] + 0.5f * ex_height;
60*c217d954SCole Faust 
61*c217d954SCole Faust         for(size_t j = 0; j < num_classes; ++j)
62*c217d954SCole Faust         {
63*c217d954SCole Faust             const float x1     = dist_x1(gen);
64*c217d954SCole Faust             const float y1     = dist_y1(gen);
65*c217d954SCole Faust             const float width  = dist_w(gen);
66*c217d954SCole Faust             const float height = dist_h(gen);
67*c217d954SCole Faust             const float ctr_x  = x1 + 0.5f * width;
68*c217d954SCole Faust             const float ctr_y  = y1 + 0.5f * height;
69*c217d954SCole Faust 
70*c217d954SCole Faust             deltas[4 * num_classes * i + 4 * j]     = (ctr_x - ex_ctr_x) / ex_width;
71*c217d954SCole Faust             deltas[4 * num_classes * i + 4 * j + 1] = (ctr_y - ex_ctr_y) / ex_height;
72*c217d954SCole Faust             deltas[4 * num_classes * i + 4 * j + 2] = log(width / ex_width);
73*c217d954SCole Faust             deltas[4 * num_classes * i + 4 * j + 3] = log(height / ex_height);
74*c217d954SCole Faust         }
75*c217d954SCole Faust     }
76*c217d954SCole Faust     return deltas;
77*c217d954SCole Faust }
78*c217d954SCole Faust 
generate_boxes(const TensorShape & image_shape,size_t num_boxes,std::mt19937 & gen)79*c217d954SCole Faust std::vector<float> generate_boxes(const TensorShape &image_shape, size_t num_boxes, std::mt19937 &gen)
80*c217d954SCole Faust {
81*c217d954SCole Faust     std::vector<float> boxes(num_boxes * 4);
82*c217d954SCole Faust 
83*c217d954SCole Faust     std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1);
84*c217d954SCole Faust     std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1);
85*c217d954SCole Faust     std::uniform_int_distribution<> dist_w(1, image_shape[0]);
86*c217d954SCole Faust     std::uniform_int_distribution<> dist_h(1, image_shape[1]);
87*c217d954SCole Faust 
88*c217d954SCole Faust     for(size_t i = 0; i < num_boxes; ++i)
89*c217d954SCole Faust     {
90*c217d954SCole Faust         boxes[4 * i]     = dist_x1(gen);
91*c217d954SCole Faust         boxes[4 * i + 1] = dist_y1(gen);
92*c217d954SCole Faust         boxes[4 * i + 2] = boxes[4 * i] + dist_w(gen) - 1;
93*c217d954SCole Faust         boxes[4 * i + 3] = boxes[4 * i + 1] + dist_h(gen) - 1;
94*c217d954SCole Faust     }
95*c217d954SCole Faust     return boxes;
96*c217d954SCole Faust }
97*c217d954SCole Faust } // namespace
98*c217d954SCole Faust 
99*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
100*c217d954SCole Faust class BoundingBoxTransformGenericFixture : public framework::Fixture
101*c217d954SCole Faust {
102*c217d954SCole Faust public:
103*c217d954SCole Faust     using TDeltas = typename std::conditional<std::is_same<typename std::decay<T>::type, uint16_t>::value, uint8_t, T>::type;
104*c217d954SCole Faust 
105*c217d954SCole Faust     template <typename...>
setup(TensorShape deltas_shape,const BoundingBoxTransformInfo & info,DataType data_type,QuantizationInfo deltas_qinfo)106*c217d954SCole Faust     void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo)
107*c217d954SCole Faust     {
108*c217d954SCole Faust         const bool is_qasymm16 = data_type == DataType::QASYMM16;
109*c217d954SCole Faust         _data_type_deltas      = (is_qasymm16) ? DataType::QASYMM8 : data_type;
110*c217d954SCole Faust         _boxes_qinfo           = (is_qasymm16) ? QuantizationInfo(.125f, 0) : QuantizationInfo();
111*c217d954SCole Faust 
112*c217d954SCole Faust         std::mt19937 gen_target(library->seed());
113*c217d954SCole Faust         _target = compute_target(deltas_shape, data_type, info, gen_target, deltas_qinfo);
114*c217d954SCole Faust 
115*c217d954SCole Faust         std::mt19937 gen_reference(library->seed());
116*c217d954SCole Faust         _reference = compute_reference(deltas_shape, data_type, info, gen_reference, deltas_qinfo);
117*c217d954SCole Faust     }
118*c217d954SCole Faust 
119*c217d954SCole Faust protected:
120*c217d954SCole Faust     template <typename data_type, typename U>
fill(U && tensor,std::vector<float> values)121*c217d954SCole Faust     void fill(U &&tensor, std::vector<float> values)
122*c217d954SCole Faust     {
123*c217d954SCole Faust         data_type *data_ptr = reinterpret_cast<data_type *>(tensor.data());
124*c217d954SCole Faust         switch(tensor.data_type())
125*c217d954SCole Faust         {
126*c217d954SCole Faust             case DataType::QASYMM8:
127*c217d954SCole Faust                 for(size_t i = 0; i < values.size(); ++i)
128*c217d954SCole Faust                 {
129*c217d954SCole Faust                     data_ptr[i] = quantize_qasymm8(values[i], tensor.quantization_info());
130*c217d954SCole Faust                 }
131*c217d954SCole Faust                 break;
132*c217d954SCole Faust             case DataType::QASYMM16:
133*c217d954SCole Faust                 for(size_t i = 0; i < values.size(); ++i)
134*c217d954SCole Faust                 {
135*c217d954SCole Faust                     data_ptr[i] = quantize_qasymm16(values[i], tensor.quantization_info());
136*c217d954SCole Faust                 }
137*c217d954SCole Faust                 break;
138*c217d954SCole Faust             default:
139*c217d954SCole Faust                 for(size_t i = 0; i < values.size(); ++i)
140*c217d954SCole Faust                 {
141*c217d954SCole Faust                     data_ptr[i] = static_cast<data_type>(values[i]);
142*c217d954SCole Faust                 }
143*c217d954SCole Faust         }
144*c217d954SCole Faust     }
145*c217d954SCole Faust 
compute_target(const TensorShape & deltas_shape,DataType data_type,const BoundingBoxTransformInfo & bbox_info,std::mt19937 & gen,QuantizationInfo deltas_qinfo)146*c217d954SCole Faust     TensorType compute_target(const TensorShape &deltas_shape, DataType data_type,
147*c217d954SCole Faust                               const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen,
148*c217d954SCole Faust                               QuantizationInfo deltas_qinfo)
149*c217d954SCole Faust     {
150*c217d954SCole Faust         // Create tensors
151*c217d954SCole Faust         TensorShape boxes_shape(4, deltas_shape[1]);
152*c217d954SCole Faust         TensorType  deltas = create_tensor<TensorType>(deltas_shape, _data_type_deltas, 1, deltas_qinfo);
153*c217d954SCole Faust         TensorType  boxes  = create_tensor<TensorType>(boxes_shape, data_type, 1, _boxes_qinfo);
154*c217d954SCole Faust         TensorType  pred_boxes;
155*c217d954SCole Faust 
156*c217d954SCole Faust         // Create and configure function
157*c217d954SCole Faust         FunctionType bbox_transform;
158*c217d954SCole Faust         bbox_transform.configure(&boxes, &pred_boxes, &deltas, bbox_info);
159*c217d954SCole Faust 
160*c217d954SCole Faust         ARM_COMPUTE_ASSERT(deltas.info()->is_resizable());
161*c217d954SCole Faust         ARM_COMPUTE_ASSERT(boxes.info()->is_resizable());
162*c217d954SCole Faust         ARM_COMPUTE_ASSERT(pred_boxes.info()->is_resizable());
163*c217d954SCole Faust 
164*c217d954SCole Faust         // Allocate tensors
165*c217d954SCole Faust         deltas.allocator()->allocate();
166*c217d954SCole Faust         boxes.allocator()->allocate();
167*c217d954SCole Faust         pred_boxes.allocator()->allocate();
168*c217d954SCole Faust 
169*c217d954SCole Faust         ARM_COMPUTE_ASSERT(!deltas.info()->is_resizable());
170*c217d954SCole Faust         ARM_COMPUTE_ASSERT(!boxes.info()->is_resizable());
171*c217d954SCole Faust 
172*c217d954SCole Faust         // Fill tensors
173*c217d954SCole Faust         TensorShape        img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
174*c217d954SCole Faust         std::vector<float> boxes_vec  = generate_boxes(img_shape, boxes_shape[1], gen);
175*c217d954SCole Faust         std::vector<float> deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
176*c217d954SCole Faust         fill<T>(AccessorType(boxes), boxes_vec);
177*c217d954SCole Faust         fill<TDeltas>(AccessorType(deltas), deltas_vec);
178*c217d954SCole Faust 
179*c217d954SCole Faust         // Compute function
180*c217d954SCole Faust         bbox_transform.run();
181*c217d954SCole Faust 
182*c217d954SCole Faust         return pred_boxes;
183*c217d954SCole Faust     }
184*c217d954SCole Faust 
compute_reference(const TensorShape & deltas_shape,DataType data_type,const BoundingBoxTransformInfo & bbox_info,std::mt19937 & gen,QuantizationInfo deltas_qinfo)185*c217d954SCole Faust     SimpleTensor<T> compute_reference(const TensorShape              &deltas_shape,
186*c217d954SCole Faust                                       DataType                        data_type,
187*c217d954SCole Faust                                       const BoundingBoxTransformInfo &bbox_info,
188*c217d954SCole Faust                                       std::mt19937                   &gen,
189*c217d954SCole Faust                                       QuantizationInfo                deltas_qinfo)
190*c217d954SCole Faust     {
191*c217d954SCole Faust         // Create reference tensor
192*c217d954SCole Faust         TensorShape           boxes_shape(4, deltas_shape[1]);
193*c217d954SCole Faust         SimpleTensor<T>       boxes{ boxes_shape, data_type, 1, _boxes_qinfo };
194*c217d954SCole Faust         SimpleTensor<TDeltas> deltas{ deltas_shape, _data_type_deltas, 1, deltas_qinfo };
195*c217d954SCole Faust 
196*c217d954SCole Faust         // Fill reference tensor
197*c217d954SCole Faust         TensorShape        img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
198*c217d954SCole Faust         std::vector<float> boxes_vec  = generate_boxes(img_shape, boxes_shape[1], gen);
199*c217d954SCole Faust         std::vector<float> deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
200*c217d954SCole Faust         fill<T>(boxes, boxes_vec);
201*c217d954SCole Faust         fill<TDeltas>(deltas, deltas_vec);
202*c217d954SCole Faust 
203*c217d954SCole Faust         return reference::bounding_box_transform(boxes, deltas, bbox_info);
204*c217d954SCole Faust     }
205*c217d954SCole Faust 
206*c217d954SCole Faust     TensorType       _target{};
207*c217d954SCole Faust     SimpleTensor<T>  _reference{};
208*c217d954SCole Faust     DataType         _data_type_deltas{};
209*c217d954SCole Faust     QuantizationInfo _boxes_qinfo{};
210*c217d954SCole Faust 
211*c217d954SCole Faust private:
212*c217d954SCole Faust };
213*c217d954SCole Faust 
214*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
215*c217d954SCole Faust class BoundingBoxTransformFixture : public BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>
216*c217d954SCole Faust {
217*c217d954SCole Faust public:
218*c217d954SCole Faust     template <typename...>
setup(TensorShape deltas_shape,const BoundingBoxTransformInfo & info,DataType data_type)219*c217d954SCole Faust     void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type)
220*c217d954SCole Faust     {
221*c217d954SCole Faust         BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(deltas_shape, info, data_type, QuantizationInfo());
222*c217d954SCole Faust     }
223*c217d954SCole Faust 
224*c217d954SCole Faust private:
225*c217d954SCole Faust };
226*c217d954SCole Faust 
227*c217d954SCole Faust template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
228*c217d954SCole Faust class BoundingBoxTransformQuantizedFixture : public BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>
229*c217d954SCole Faust {
230*c217d954SCole Faust public:
231*c217d954SCole Faust     template <typename...>
setup(TensorShape deltas_shape,const BoundingBoxTransformInfo & info,DataType data_type,QuantizationInfo deltas_qinfo)232*c217d954SCole Faust     void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo)
233*c217d954SCole Faust     {
234*c217d954SCole Faust         BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(deltas_shape, info, data_type, deltas_qinfo);
235*c217d954SCole Faust     }
236*c217d954SCole Faust };
237*c217d954SCole Faust } // namespace validation
238*c217d954SCole Faust } // namespace test
239*c217d954SCole Faust } // namespace arm_compute
240*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE */
241