Open3D (C++ API)  0.20.0
Loading...
Searching...
No Matches
Nms.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// Copyright (c) 2018-2026 www.open3d.org
5// SPDX-License-Identifier: MIT
6// ----------------------------------------------------------------------------
7//
8// Reference:
9// https://github.com/open-mmlab/OpenPCDet/blob/master/pcdet/ops/iou3d_nms/src/iou3d_nms_kernel.cu
10//
11// Reference:
12// https://github.com/open-mmlab/mmdetection3d/blob/master/mmdet3d/ops/iou3d/src/iou3d_kernel.cu
13// 3D IoU Calculation and Rotated NMS(modified from 2D NMS written by others)
14// Written by Shaoshuai Shi
15// All Rights Reserved 2019-2020.
16
17#pragma once
18
19#include <algorithm>
20#include <cstdint>
21#include <numeric>
22#include <vector>
23
25
26#ifdef BUILD_SYCL_MODULE
27// NmsSYCLKernel takes a real sycl::queue&, so any TU that sees this
28// declaration needs the full SYCL runtime type (not -fsycl compilation --
29// NmsOps.cpp already gets this transitively via <c10/xpu/XPUDeviceProp.h>,
30// but Nms.h is included before that, so it must include the header directly
31// to avoid an incomplete-type error). NmsSYCL.cpp (built with -fsycl) is
32// where the actual device kernels are compiled.
33#include <sycl/sycl.hpp>
34#endif
35
38
39namespace open3d {
40namespace ml {
41namespace contrib {
42
43#ifdef BUILD_CUDA_MODULE
44
60int NmsCUDAKernel(const float *boxes,
61 const float *scores,
62 int n,
63 double nms_overlap_thresh,
64 int64_t *keep_indices_out);
65#endif
66
67#ifdef BUILD_SYCL_MODULE
68
82int NmsSYCLKernel(sycl::queue &queue,
83 const float *boxes,
84 const float *scores,
85 int n,
86 double nms_overlap_thresh,
87 int64_t *keep_indices_out);
88#endif
89
96std::vector<int64_t> NmsCPUKernel(const float *boxes,
97 const float *scores,
98 int n,
99 double nms_overlap_thresh);
100
105template <typename T>
106inline std::vector<int64_t> SortIndexesDescending(const T *values,
107 int64_t num) {
108 std::vector<int64_t> indices(num);
109 std::iota(indices.begin(), indices.end(), 0);
110 std::stable_sort(
111 indices.begin(), indices.end(),
112 [&values](int64_t i, int64_t j) { return values[i] > values[j]; });
113 return indices;
114}
115
137OPEN3D_HOST_DEVICE inline int NmsGreedyKeepCore(const uint64_t *mask,
138 const int64_t *sort_indices,
139 int n,
140 uint64_t *remv,
141 int64_t *keep_indices) {
142 // Avoid utility::DivUp() (host-only, uses std::div) so this function
143 // stays callable from CUDA/SYCL device code.
144 const int num_block_cols = (n + NMS_BLOCK_SIZE - 1) / NMS_BLOCK_SIZE;
145 for (int j = 0; j < num_block_cols; j++) {
146 remv[j] = 0;
147 }
148 int count = 0;
149 for (int i = 0; i < n; i++) {
150 int block_col_idx = i / NMS_BLOCK_SIZE;
151 int inner_block_col_idx = i % NMS_BLOCK_SIZE;
152
153 if (!(remv[block_col_idx] & (1ULL << inner_block_col_idx))) {
154 keep_indices[count++] = sort_indices[i];
155
156 const uint64_t *p = mask + i * num_block_cols;
157 for (int j = block_col_idx; j < num_block_cols; j++) {
158 remv[j] |= p[j];
159 }
160 }
161 }
162 return count;
163}
164
167inline std::vector<int64_t> NmsGreedyKeep(const uint64_t *mask,
168 const int64_t *sort_indices,
169 int n) {
170 const int num_block_cols = utility::DivUp(n, NMS_BLOCK_SIZE);
171 std::vector<uint64_t> remv(num_block_cols);
172 std::vector<int64_t> keep_indices(n);
173 int count = NmsGreedyKeepCore(mask, sort_indices, n, remv.data(),
174 keep_indices.data());
175 keep_indices.resize(count);
176 return keep_indices;
177}
178
179} // namespace contrib
180} // namespace ml
181} // namespace open3d
Common CUDA utilities.
#define OPEN3D_HOST_DEVICE
Definition CUDAUtils.h:43
std::vector< int > indices
Definition PointCloudSmoothing.cpp:133
sycl::queue queue
Definition SYCLContext.cpp:88
int count
Definition FilePCD.cpp:43
constexpr int NMS_BLOCK_SIZE
Definition IoUImpl.h:19
int NmsSYCLKernel(sycl::queue &queue, const float *boxes, const float *scores, int n, double nms_overlap_thresh, int64_t *keep_indices_out)
Definition NmsSYCL.cpp:44
OPEN3D_HOST_DEVICE int NmsGreedyKeepCore(const uint64_t *mask, const int64_t *sort_indices, int n, uint64_t *remv, int64_t *keep_indices)
Definition Nms.h:137
std::vector< int64_t > NmsCPUKernel(const float *boxes, const float *scores, int n, double nms_overlap_thresh)
Definition Nms.cpp:96
std::vector< int64_t > SortIndexesDescending(const T *values, int64_t num)
Definition Nms.h:106
std::vector< int64_t > NmsGreedyKeep(const uint64_t *mask, const int64_t *sort_indices, int n)
Definition Nms.h:167
int DivUp(int x, int y)
Computes the quotient of x/y with rounding up.
Definition Helper.h:176
Definition PinholeCameraIntrinsic.cpp:16