From a0a0e29f635de08092c2325f8f049ffb286aabaf 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: I1dcd5fb3d9ad6c6c750415bf8074698b800dfbc1 Reviewed-on: https://review.mlplatform.org/494 Tested-by: Arm Jenkins Reviewed-by: Giuseppe Rossini Reviewed-by: Georgios Pinitas --- tests/validation/reference/NonMaxSuppression.cpp | 126 +++++++++++++++++++++++ 1 file changed, 126 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..013a26f645 --- /dev/null +++ b/tests/validation/reference/NonMaxSuppression.cpp @@ -0,0 +1,126 @@ +/* + * 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 "Permute.h" + +#include "arm_compute/core/Types.h" +#include "tests/validation/Helpers.h" +#include + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +namespace +{ +inline float get_elem_by_coordinate(const SimpleTensor &tensor, Coordinates coord) +{ + return *static_cast(tensor(coord)); +} + +// Return intersection-over-union overlap between boxes i and j +inline bool iou_greater_than_threshold(const SimpleTensor &boxes, size_t i, size_t j, float iou_threshold) +{ + const float ymin_i = std::min(get_elem_by_coordinate(boxes, Coordinates(0, i)), get_elem_by_coordinate(boxes, Coordinates(2, i))); + const float xmin_i = std::min(get_elem_by_coordinate(boxes, Coordinates(1, i)), get_elem_by_coordinate(boxes, Coordinates(3, i))); + const float ymax_i = std::max(get_elem_by_coordinate(boxes, Coordinates(0, i)), get_elem_by_coordinate(boxes, Coordinates(2, i))); + const float xmax_i = std::max(get_elem_by_coordinate(boxes, Coordinates(1, i)), get_elem_by_coordinate(boxes, Coordinates(3, i))); + const float ymin_j = std::min(get_elem_by_coordinate(boxes, Coordinates(0, j)), get_elem_by_coordinate(boxes, Coordinates(2, j))); + const float xmin_j = std::min(get_elem_by_coordinate(boxes, Coordinates(1, j)), get_elem_by_coordinate(boxes, Coordinates(3, j))); + const float ymax_j = std::max(get_elem_by_coordinate(boxes, Coordinates(0, j)), get_elem_by_coordinate(boxes, Coordinates(2, j))); + const float xmax_j = std::max(get_elem_by_coordinate(boxes, Coordinates(1, j)), get_elem_by_coordinate(boxes, Coordinates(3, j))); + const float area_i = (ymax_i - ymin_i) * (xmax_i - xmin_i); + const float area_j = (ymax_j - ymin_j) * (xmax_j - xmin_j); + if(area_i <= 0 || area_j <= 0) + { + return false; + } + const float intersection_ymin = std::max(ymin_i, ymin_j); + const float intersection_xmin = std::max(xmin_i, xmin_j); + const float intersection_ymax = std::min(ymax_i, ymax_j); + const float intersection_xmax = std::min(xmax_i, xmax_j); + const float intersection_area = std::max(intersection_ymax - intersection_ymin, 0.0) * std::max(intersection_xmax - intersection_xmin, 0.0); + const float iou = intersection_area / (area_i + area_j - intersection_area); + return iou > iou_threshold; +} + +} // 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); + std::vector scores_data(num_boxes); + std::copy_n(scores.data(), num_boxes, scores_data.begin()); + + using CandidateBox = std::pair; + auto cmp = [](const CandidateBox bb0, const CandidateBox bb1) + { + return bb0.second < bb1.second; + }; + + std::priority_queue, decltype(cmp)> candidate_priority_queue(cmp); + for(size_t i = 0; i < scores_data.size(); ++i) + { + if(scores_data[i] > score_threshold) + { + candidate_priority_queue.emplace(CandidateBox({ i, scores_data[i] })); + } + } + + std::vector selected; + std::vector selected_scores; + CandidateBox next_candidate; + + while(selected.size() < output_size && !candidate_priority_queue.empty()) + { + next_candidate = candidate_priority_queue.top(); + candidate_priority_queue.pop(); + bool should_select = true; + for(int j = selected.size() - 1; j >= 0; --j) + { + if(iou_greater_than_threshold(bboxes, next_candidate.first, selected[j], nms_threshold)) + { + should_select = false; + break; + } + } + if(should_select) + { + selected.push_back(next_candidate.first); + selected_scores.push_back(next_candidate.second); + } + } + std::copy_n(selected.begin(), selected.size(), indices.data()); + return indices; +} + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute -- cgit v1.2.1