giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 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 | { |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 39 | template <typename T, typename TDeltas> |
| 40 | SimpleTensor<T> bounding_box_transform(const SimpleTensor<T> &boxes, const SimpleTensor<TDeltas> &deltas, const BoundingBoxTransformInfo &info) |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 41 | { |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 42 | const DataType boxes_data_type = boxes.data_type(); |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 43 | SimpleTensor<T> pred_boxes(deltas.shape(), boxes_data_type); |
| 44 | |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 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(); |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 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 | |
giuros01 | d696cb6 | 2018-11-16 10:39:59 +0000 | [diff] [blame] | 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)); |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 57 | |
| 58 | const size_t box_fields = 4; |
| 59 | const size_t class_fields = 4; |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 60 | #if defined(_OPENMP) |
| 61 | #pragma omp parallel for |
| 62 | #endif /* _OPENMP */ |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 63 | for(size_t i = 0; i < num_boxes; ++i) |
| 64 | { |
| 65 | // Extract ROI information |
| 66 | const size_t start_box = box_fields * i; |
giuros01 | d696cb6 | 2018-11-16 10:39:59 +0000 | [diff] [blame] | 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; |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 71 | |
| 72 | for(size_t j = 0; j < num_classes; ++j) |
| 73 | { |
| 74 | // Extract deltas |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 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]); |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 80 | |
| 81 | // Clip dw and dh |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 82 | dw = std::min(dw, TDeltas(info.bbox_xform_clip())); |
| 83 | dh = std::min(dh, TDeltas(info.bbox_xform_clip())); |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 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 |
giuros01 | d696cb6 | 2018-11-16 10:39:59 +0000 | [diff] [blame] | 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)); |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 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); |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 103 | |
| 104 | template <> |
| 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 | } |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 113 | } // namespace reference |
| 114 | } // namespace validation |
| 115 | } // namespace test |
| 116 | } // namespace arm_compute |