blob: 55dd165b510a6cecdefbd12e0f6e3879d0a96516 [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +01001/*
2 * Copyright (c) 2018 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
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
39template <typename T>
40SimpleTensor<T> bounding_box_transform(const SimpleTensor<T> &boxes, const SimpleTensor<T> &deltas, const BoundingBoxTransformInfo &info)
41{
42 const DataType boxes_data_type = deltas.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 T *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
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
73 const size_t start_delta = i * num_classes * class_fields + class_fields * j;
74 const T dx = deltas_ptr[start_delta] / T(info.weights()[0]);
75 const T dy = deltas_ptr[start_delta + 1] / T(info.weights()[1]);
76 T dw = deltas_ptr[start_delta + 2] / T(info.weights()[2]);
77 T dh = deltas_ptr[start_delta + 3] / T(info.weights()[3]);
78
79 // Clip dw and dh
80 dw = std::min(dw, T(info.bbox_xform_clip()));
81 dh = std::min(dh, T(info.bbox_xform_clip()));
82
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);
101} // namespace reference
102} // namespace validation
103} // namespace test
104} // namespace arm_compute