From d85a77a546783ab28df9eb6347f855cc54f6192d Mon Sep 17 00:00:00 2001 From: Pablo Tello Date: Fri, 21 Dec 2018 16:47:23 +0000 Subject: COMPMID-1766: Implemented CPP Non Max Suppression Change-Id: I2d2b684d464f7b3bb1f91cfd29952f612d65f11f Signed-off-by: Pablo Tello Reviewed-on: https://review.mlplatform.org/708 Reviewed-by: VidhyaSudhan Loganathan Tested-by: Arm Jenkins --- tests/validation/reference/NonMaxSuppression.cpp | 157 +++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 tests/validation/reference/NonMaxSuppression.cpp (limited to 'tests/validation/reference/NonMaxSuppression.cpp') diff --git a/tests/validation/reference/NonMaxSuppression.cpp b/tests/validation/reference/NonMaxSuppression.cpp new file mode 100644 index 0000000000..75929085b3 --- /dev/null +++ b/tests/validation/reference/NonMaxSuppression.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2019 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "NonMaxSuppression.h" + +#include "arm_compute/core/Types.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +namespace +{ +using CandidateBox = std::pair; +using Box = std::tuple; + +inline float get_elem_by_coordinate(const SimpleTensor &tensor, Coordinates coord) +{ + return *static_cast(tensor(coord)); +} + +inline Box get_box(const SimpleTensor &boxes, size_t id) +{ + return std::make_tuple( + get_elem_by_coordinate(boxes, Coordinates(0, id)), + get_elem_by_coordinate(boxes, Coordinates(1, id)), + get_elem_by_coordinate(boxes, Coordinates(2, id)), + get_elem_by_coordinate(boxes, Coordinates(3, id))); +} + +inline std::pair get_min_yx(Box b) +{ + return std::make_pair( + std::min(std::get<0>(b), std::get<2>(b)), + std::min(std::get<1>(b), std::get<3>(b))); +} + +inline std::pair get_max_yx(Box b) +{ + return std::make_pair( + std::max(std::get<0>(b), std::get<2>(b)), + std::max(std::get<1>(b), std::get<3>(b))); +} + +inline float compute_size(const std::pair &min, const std::pair &max) +{ + return (max.first - min.first) * (max.second - min.second); +} + +inline float compute_intersection(const std::pair &b0_min, const std::pair &b0_max, + const std::pair &b1_min, const std::pair &b1_max, float b0_size, float b1_size) +{ + const float inter = std::max(std::min(b0_max.first, b1_max.first) - std::max(b0_min.first, b1_min.first), 0.0) * std::max(std::min(b0_max.second, + b1_max.second) + - std::max(b0_min.second, b1_min.second), + 0.0); + return inter / (b0_size + b1_size - inter); +} + +inline bool reject_box(Box b0, Box b1, float threshold) +{ + const auto b0_min = get_min_yx(b0); + const auto b0_max = get_max_yx(b0); + const auto b1_min = get_min_yx(b1); + const auto b1_max = get_max_yx(b1); + const float b0_size = compute_size(b0_min, b0_max); + const float b1_size = compute_size(b1_min, b1_max); + if(b0_size <= 0.f || b1_size <= 0.f) + { + return false; + } + else + { + return compute_intersection(b0_min, b0_max, b1_min, b1_max, b0_size, b1_size) > threshold; + } +} + +inline std::vector get_candidates(const SimpleTensor &scores, float threshold) +{ + std::vector candidates_vector; + for(int i = 0; i < scores.num_elements(); ++i) + { + if(scores[i] > threshold) + { + const auto cb = CandidateBox({ i, scores[i] }); + candidates_vector.push_back(cb); + } + } + std::sort(candidates_vector.begin(), candidates_vector.end(), [](const CandidateBox bb0, const CandidateBox bb1) + { + return bb0.second >= bb1.second; + }); + return candidates_vector; +} + +inline bool is_box_selected(const CandidateBox &cb, const SimpleTensor &bboxes, std::vector &selected_boxes, float threshold) +{ + for(int j = selected_boxes.size() - 1; j >= 0; --j) + { + if(reject_box(get_box(bboxes, cb.first), get_box(bboxes, selected_boxes[j]), threshold)) + { + return false; + } + } + return true; +} +} // namespace + +SimpleTensor non_max_suppression(const SimpleTensor &bboxes, const SimpleTensor &scores, SimpleTensor &indices, + unsigned int max_output_size, float score_threshold, float nms_threshold) +{ + const size_t num_boxes = bboxes.shape().y(); + const size_t output_size = std::min(static_cast(max_output_size), num_boxes); + const std::vector candidates_vector = get_candidates(scores, score_threshold); + std::vector selected; + size_t p(0); + while(selected.size() < output_size && p < candidates_vector.size() && selected.size() < candidates_vector.size()) + { + const auto nc = candidates_vector[p++]; + if(is_box_selected(nc, bboxes, selected, nms_threshold)) + { + selected.push_back(nc.first); + } + } + std::copy_n(selected.begin(), selected.size(), indices.data()); + return indices; +} + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute -- cgit v1.2.1