pcod-common v1.0.0
Shared preprocessing and postprocessing for point-cloud object detection
Loading...
Searching...
No Matches
nms.cpp
Go to the documentation of this file.
1// Copyright Institute for Automotive Engineering (ika), RWTH Aachen University
2// SPDX-License-Identifier: Apache-2.0
3
4#include "pcod_common/nms.hpp"
5
6#include <algorithm>
7
9
10namespace pcod_common {
11
12void ApplyRotatedNms(std::vector<BoundingBox>& bboxes, const NmsConfig& config) {
13 if (bboxes.empty()) {
14 return;
15 }
16
17 std::vector<std::pair<float, BoundingBox*>> scored;
18 scored.reserve(bboxes.size());
19
20 for (auto& bbox : bboxes) {
21 float max_class_score = 0.0F;
22 std::size_t max_class_idx = 0;
23 for (const auto& entry : bbox.classification) {
24 if (entry.score > max_class_score) {
25 max_class_score = entry.score;
26 max_class_idx = entry.class_idx;
27 }
28 }
29 float class_thresh = config.internal_score_threshold;
30 if (max_class_idx < config.score_thresholds.size()) {
31 class_thresh = config.score_thresholds[max_class_idx];
32 }
33 float score = scale_score(bbox.existence_probability, class_thresh, config.internal_score_threshold);
34 scored.emplace_back(score, &bbox);
35 }
36
37 std::sort(scored.begin(), scored.end(), [](const auto& a, const auto& b) { return a.first > b.first; });
38
39 std::vector<BoundingBox> kept;
40 kept.reserve(bboxes.size());
41
42 for (const auto& candidate : scored) {
43 if (candidate.first < config.internal_score_threshold) {
44 break;
45 }
46 bool keep = true;
47 for (const auto& kept_box : kept) {
48 if (candidate.second->overlaps(kept_box, config.iou_threshold)) {
49 keep = false;
50 break;
51 }
52 }
53 if (keep) {
54 kept.push_back(*candidate.second);
55 if (static_cast<int>(kept.size()) >= config.max_detections) {
56 break;
57 }
58 }
59 }
60
61 bboxes = std::move(kept);
62}
63
64} // namespace pcod_common
void ApplyRotatedNms(std::vector< BoundingBox > &bboxes, const NmsConfig &config)
Definition nms.cpp:12
T scale_score(T score, T old_thresh, T new_thresh)
Definition math.hpp:23
float iou_threshold
IoU above which a lower-scored box is suppressed.
Definition nms.hpp:15
std::vector< float > score_thresholds
Per-class thresholds, or one threshold shared by all classes.
Definition nms.hpp:14
int max_detections
Maximum number of boxes retained.
Definition nms.hpp:16
float internal_score_threshold
Score pivot used when rescaling class thresholds.
Definition nms.hpp:17