xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/reference/BoundingBoxTransform.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2020 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 #include "BoundingBoxTransform.h"
25 
26 #include "arm_compute/core/Types.h"
27 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28 #include "arm_compute/core/utils/misc/Utility.h"
29 #include "tests/validation/Helpers.h"
30 
31 namespace arm_compute
32 {
33 namespace test
34 {
35 namespace validation
36 {
37 namespace reference
38 {
39 template <typename T, typename TDeltas>
bounding_box_transform(const SimpleTensor<T> & boxes,const SimpleTensor<TDeltas> & deltas,const BoundingBoxTransformInfo & info)40 SimpleTensor<T> bounding_box_transform(const SimpleTensor<T> &boxes, const SimpleTensor<TDeltas> &deltas, const BoundingBoxTransformInfo &info)
41 {
42     const DataType  boxes_data_type = boxes.data_type();
43     SimpleTensor<T> pred_boxes(deltas.shape(), boxes_data_type);
44 
45     const size_t   num_classes    = deltas.shape()[0] / 4;
46     const size_t   num_boxes      = deltas.shape()[1];
47     const TDeltas *deltas_ptr     = deltas.data();
48     T             *pred_boxes_ptr = pred_boxes.data();
49 
50     const int img_h = floor(info.img_height() / info.scale() + 0.5f);
51     const int img_w = floor(info.img_width() / info.scale() + 0.5f);
52 
53     const auto scale_after  = (info.apply_scale() ? T(info.scale()) : T(1));
54     const auto scale_before = T(info.scale());
55     ARM_COMPUTE_ERROR_ON(scale_before <= 0);
56     const auto offset = (info.correct_transform_coords() ? T(1.f) : T(0.f));
57 
58     const size_t box_fields   = 4;
59     const size_t class_fields = 4;
60 #if defined(_OPENMP)
61     #pragma omp parallel for
62 #endif /* _OPENMP */
63     for(size_t i = 0; i < num_boxes; ++i)
64     {
65         // Extract ROI information
66         const size_t start_box = box_fields * i;
67         const T      width     = (boxes[start_box + 2] / scale_before) - (boxes[start_box] / scale_before) + T(1.f);
68         const T      height    = (boxes[start_box + 3] / scale_before) - (boxes[start_box + 1] / scale_before) + T(1.f);
69         const T      ctr_x     = (boxes[start_box] / scale_before) + T(0.5f) * width;
70         const T      ctr_y     = (boxes[start_box + 1] / scale_before) + T(0.5f) * height;
71 
72         for(size_t j = 0; j < num_classes; ++j)
73         {
74             // Extract deltas
75             const size_t  start_delta = i * num_classes * class_fields + class_fields * j;
76             const TDeltas dx          = deltas_ptr[start_delta] / TDeltas(info.weights()[0]);
77             const TDeltas dy          = deltas_ptr[start_delta + 1] / TDeltas(info.weights()[1]);
78             TDeltas       dw          = deltas_ptr[start_delta + 2] / TDeltas(info.weights()[2]);
79             TDeltas       dh          = deltas_ptr[start_delta + 3] / TDeltas(info.weights()[3]);
80 
81             // Clip dw and dh
82             dw = std::min(dw, TDeltas(info.bbox_xform_clip()));
83             dh = std::min(dh, TDeltas(info.bbox_xform_clip()));
84 
85             // Determine the predictions
86             const T pred_ctr_x = dx * width + ctr_x;
87             const T pred_ctr_y = dy * height + ctr_y;
88             const T pred_w     = T(std::exp(dw)) * width;
89             const T pred_h     = T(std::exp(dh)) * height;
90 
91             // Store the prediction into the output tensor
92             pred_boxes_ptr[start_delta]     = scale_after * utility::clamp<T>(pred_ctr_x - T(0.5f) * pred_w, T(0), T(img_w - 1));
93             pred_boxes_ptr[start_delta + 1] = scale_after * utility::clamp<T>(pred_ctr_y - T(0.5f) * pred_h, T(0), T(img_h - 1));
94             pred_boxes_ptr[start_delta + 2] = scale_after * utility::clamp<T>(pred_ctr_x + T(0.5f) * pred_w - offset, T(0), T(img_w - 1));
95             pred_boxes_ptr[start_delta + 3] = scale_after * utility::clamp<T>(pred_ctr_y + T(0.5f) * pred_h - offset, T(0), T(img_h - 1));
96         }
97     }
98     return pred_boxes;
99 }
100 
101 template SimpleTensor<float> bounding_box_transform(const SimpleTensor<float> &boxes, const SimpleTensor<float> &deltas, const BoundingBoxTransformInfo &info);
102 template SimpleTensor<half> bounding_box_transform(const SimpleTensor<half> &boxes, const SimpleTensor<half> &deltas, const BoundingBoxTransformInfo &info);
103 
104 template <>
bounding_box_transform(const SimpleTensor<uint16_t> & boxes,const SimpleTensor<uint8_t> & deltas,const BoundingBoxTransformInfo & info)105 SimpleTensor<uint16_t> bounding_box_transform(const SimpleTensor<uint16_t> &boxes, const SimpleTensor<uint8_t> &deltas, const BoundingBoxTransformInfo &info)
106 {
107     SimpleTensor<float>    boxes_tmp      = convert_from_asymmetric(boxes);
108     SimpleTensor<float>    deltas_tmp     = convert_from_asymmetric(deltas);
109     SimpleTensor<float>    pred_boxes_tmp = bounding_box_transform<float, float>(boxes_tmp, deltas_tmp, info);
110     SimpleTensor<uint16_t> pred_boxes     = convert_to_asymmetric<uint16_t>(pred_boxes_tmp, boxes.quantization_info());
111     return pred_boxes;
112 }
113 } // namespace reference
114 } // namespace validation
115 } // namespace test
116 } // namespace arm_compute
117