blob: e09bcff1c6a8c19acdf785e65b0bbe014aeef99c [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +01001/*
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +01002 * Copyright (c) 2018-2019 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;
60
61 for(size_t i = 0; i < num_boxes; ++i)
62 {
63 // Extract ROI information
64 const size_t start_box = box_fields * i;
giuros01d696cb62018-11-16 10:39:59 +000065 const T width = (boxes[start_box + 2] / scale_before) - (boxes[start_box] / scale_before) + T(1.f);
66 const T height = (boxes[start_box + 3] / scale_before) - (boxes[start_box + 1] / scale_before) + T(1.f);
67 const T ctr_x = (boxes[start_box] / scale_before) + T(0.5f) * width;
68 const T ctr_y = (boxes[start_box + 1] / scale_before) + T(0.5f) * height;
giuros01c04a0e82018-10-03 12:44:35 +010069
70 for(size_t j = 0; j < num_classes; ++j)
71 {
72 // Extract deltas
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010073 const size_t start_delta = i * num_classes * class_fields + class_fields * j;
74 const TDeltas dx = deltas_ptr[start_delta] / TDeltas(info.weights()[0]);
75 const TDeltas dy = deltas_ptr[start_delta + 1] / TDeltas(info.weights()[1]);
76 TDeltas dw = deltas_ptr[start_delta + 2] / TDeltas(info.weights()[2]);
77 TDeltas dh = deltas_ptr[start_delta + 3] / TDeltas(info.weights()[3]);
giuros01c04a0e82018-10-03 12:44:35 +010078
79 // Clip dw and dh
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010080 dw = std::min(dw, TDeltas(info.bbox_xform_clip()));
81 dh = std::min(dh, TDeltas(info.bbox_xform_clip()));
giuros01c04a0e82018-10-03 12:44:35 +010082
83 // Determine the predictions
84 const T pred_ctr_x = dx * width + ctr_x;
85 const T pred_ctr_y = dy * height + ctr_y;
86 const T pred_w = T(std::exp(dw)) * width;
87 const T pred_h = T(std::exp(dh)) * height;
88
89 // Store the prediction into the output tensor
giuros01d696cb62018-11-16 10:39:59 +000090 pred_boxes_ptr[start_delta] = scale_after * utility::clamp<T>(pred_ctr_x - T(0.5f) * pred_w, T(0), T(img_w - 1));
91 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));
92 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));
93 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 +010094 }
95 }
96 return pred_boxes;
97}
98
99template SimpleTensor<float> bounding_box_transform(const SimpleTensor<float> &boxes, const SimpleTensor<float> &deltas, const BoundingBoxTransformInfo &info);
100template SimpleTensor<half> bounding_box_transform(const SimpleTensor<half> &boxes, const SimpleTensor<half> &deltas, const BoundingBoxTransformInfo &info);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100101
102template <>
103SimpleTensor<uint16_t> bounding_box_transform(const SimpleTensor<uint16_t> &boxes, const SimpleTensor<uint8_t> &deltas, const BoundingBoxTransformInfo &info)
104{
105 SimpleTensor<float> boxes_tmp = convert_from_asymmetric(boxes);
106 SimpleTensor<float> deltas_tmp = convert_from_asymmetric(deltas);
107 SimpleTensor<float> pred_boxes_tmp = bounding_box_transform<float, float>(boxes_tmp, deltas_tmp, info);
108 SimpleTensor<uint16_t> pred_boxes = convert_to_asymmetric<uint16_t>(pred_boxes_tmp, boxes.quantization_info());
109 return pred_boxes;
110}
giuros01c04a0e82018-10-03 12:44:35 +0100111} // namespace reference
112} // namespace validation
113} // namespace test
114} // namespace arm_compute