blob: 2abadd0f54ea12bbf1129983ed2fe115b6260de1 [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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#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
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010039template <typename T, typename TDeltas>
40SimpleTensor<T> bounding_box_transform(const SimpleTensor<T> &boxes, const SimpleTensor<TDeltas> &deltas, const BoundingBoxTransformInfo &info)
giuros01c04a0e82018-10-03 12:44:35 +010041{
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010042 const DataType boxes_data_type = boxes.data_type();
giuros01c04a0e82018-10-03 12:44:35 +010043 SimpleTensor<T> pred_boxes(deltas.shape(), boxes_data_type);
44
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010045 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();
giuros01c04a0e82018-10-03 12:44:35 +010049
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
giuros01d696cb62018-11-16 10:39:59 +000053 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));
giuros01c04a0e82018-10-03 12:44:35 +010057
58 const size_t box_fields = 4;
59 const size_t class_fields = 4;
Michalis Spyroud1d77222020-04-08 14:10:15 +010060#if defined(_OPENMP)
61 #pragma omp parallel for
62#endif /* _OPENMP */
giuros01c04a0e82018-10-03 12:44:35 +010063 for(size_t i = 0; i < num_boxes; ++i)
64 {
65 // Extract ROI information
66 const size_t start_box = box_fields * i;
giuros01d696cb62018-11-16 10:39:59 +000067 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;
giuros01c04a0e82018-10-03 12:44:35 +010071
72 for(size_t j = 0; j < num_classes; ++j)
73 {
74 // Extract deltas
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010075 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]);
giuros01c04a0e82018-10-03 12:44:35 +010080
81 // Clip dw and dh
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010082 dw = std::min(dw, TDeltas(info.bbox_xform_clip()));
83 dh = std::min(dh, TDeltas(info.bbox_xform_clip()));
giuros01c04a0e82018-10-03 12:44:35 +010084
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
giuros01d696cb62018-11-16 10:39:59 +000092 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));
giuros01c04a0e82018-10-03 12:44:35 +010096 }
97 }
98 return pred_boxes;
99}
100
101template SimpleTensor<float> bounding_box_transform(const SimpleTensor<float> &boxes, const SimpleTensor<float> &deltas, const BoundingBoxTransformInfo &info);
102template SimpleTensor<half> bounding_box_transform(const SimpleTensor<half> &boxes, const SimpleTensor<half> &deltas, const BoundingBoxTransformInfo &info);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100103
104template <>
105SimpleTensor<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}
giuros01c04a0e82018-10-03 12:44:35 +0100113} // namespace reference
114} // namespace validation
115} // namespace test
116} // namespace arm_compute