Skip to main content

3D Non-Maximum Suppression

PyPI version Downloads Python versions License

Implementation of 3D non-maximum suppression (NMS-3D) for axis-aligned bounding boxes in PyTorch, with optional Plotly visualisation.

⬇️ Installation and Import

Now, this code is available with PyPI here. The package can be installed with:

pip install nms-3d          # NMS only, depends on torch
pip install "nms-3d[plot]"  # also plot_3d_boxes (plotly)

and can be imported as:

import nms_3d

Quick use:

from nms_3d import nms_3d

# boxes: (N, 7) tensor with columns SCORE, X MIN, Y MIN, Z MIN, X MAX, Y MAX, Z MAX
kept_boxes, kept_indices = nms_3d(boxes, iou_threshold=0.5, return_indices=True)

⏱️ Benchmark

nms_3d against MONAI's non_max_suppression on synthetic clustered boxes (float32, about 20 detections per object in a 512³ volume, seed 0). Times are the median of 5 runs after one warm-up run. The script fails if the kept indices, or their order, differ from MONAI's.

Device N IoU thr Kept nms_3d (ms) MONAI (ms) MONAI / nms_3d
CPU 1,000 0.3 50 2.9 8.6 3.01x
CPU 1,000 0.5 51 2.8 8.6 3.08x
CPU 1,000 0.7 174 6.9 26.1 3.77x
CPU 5,000 0.3 250 31.9 62.9 1.97x
CPU 5,000 0.5 251 32.0 63.0 1.97x
CPU 5,000 0.7 905 78.9 186.0 2.36x
CPU 20,000 0.3 997 326.3 492.6 1.51x
CPU 20,000 0.5 1,010 330.6 497.1 1.50x
CPU 20,000 0.7 3,482 743.3 1,235.0 1.66x
CPU 50,000 0.3 2,486 1,403.3 2,297.9 1.64x
CPU 50,000 0.5 2,525 1,418.1 2,348.7 1.66x
CPU 50,000 0.7 8,696 3,299.8 5,455.1 1.65x

Measured on an Apple M5 (4 torch threads, macOS 27.2.1, Low Power Mode on) with Python 3.12.12, torch 2.15.0 and MONAI 1.6.2. No CUDA device was available, so there are no GPU numbers; on a CUDA machine the script adds those rows. Equality with MONAI is checked on these inputs only: results can differ for boxes much smaller than one unit (for example normalized coordinates), where MONAI's IoU epsilon lowers the IoU and nms_3d uses none.

To reproduce:

git clone https://github.com/GiulioRusso/NMS-3D.git
cd NMS-3D
pip install -e ".[dev]" torch==2.15.0 monai==1.6.2
python benchmarks/bench_nms.py

This prints the table and overwrites benchmarks/results.csv, which also records the versions and the CPU/GPU model. MONAI is only part of the dev extra, not a dependency of the package.

📦 Package documentation

Package documentation is available here.

📂 Project Organization

The project consists of the following Python modules:

.
├── nms_3d/                # The NMS 3D package folder
│   ├── nms_3d.py          # Function that applies the NMS 3D algorithm.
│   └── plot_3d_boxes.py   # Function for creating 3D plots of bounding boxes using Plotly.
│
├── tests/                 # pytest suite (edge cases, torchvision and MONAI equivalence).
│
├── example.py             # The script that contains the 3D NMS application example.
│
├── bbox-coords/           # The folder that contains the bounding box .csv files before and after the 3D NMS.
│   ├── bbox-coords-before-nms-3d.csv  # Example file of bounding boxes to suppress.
│   └── bbox-coords-after-nms-3d.csv   # Output file after applying the 3D NMS on bbox-coords-before-nms-3d.csv.
│
└── output/                # The folder that contains the .html visualization of the boxes before and after the 3D NMS.
    ├── best_boxes_after_nms.html   # .html view of the boxes after the 3D NMS.
    ├── best_boxes_before_nms.html  # .html view of the boxes before the 3D NMS.
    └── NMS-image-example.png       # Example image used in this README.md file.

Run the application example with:

python3 example.py

This code will read the bounding box coordinates from bbox-coords-before-nms-3d.csv, perform 3D NMS, and save the result to bbox-coords-after-nms-3d.csv. Also, two 3D plots are saved as HTML files to show the boxes before and after NMS.

🚨 Requirements

Python>=3.9.5
torch>=2.2.2
plotly>=5.13.1  # optional, only for plot_3d_boxes

Install the requirements of example.py with:

pip3 install -r requirements.txt pandas

Run the tests with:

pip install -e ".[dev,plot]"
pytest

🤝 Contribution

👨‍💻 Ciro Russo, PhD

⚖️ License

MIT License

Metadata

Release files for nms-3d 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nms-3d 0.2.1
File Size Uploaded Provenance Actions
nms_3d-0.2.1.tar.gz 381.2 kB Without provenance Details for nms_3d-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nms-3d 0.2.1
File Interpreter ABI Platform Actions
nms_3d-0.2.1-py3-none-any.whl Python 3 none any Details for nms_3d-0.2.1-py3-none-any.whl

Total release size: 390.3 kB

Release files / nms_3d-0.2.1.tar.gz

Download URL nms_3d-0.2.1.tar.gz
Size 381.2 kB
Tags Source
SHA-256 checksum
How to use checksums
30049493b450abf7faaffa187024370f80dce3dc0e1fe797bdd877fe035482d5
BLAKE2b-256 checksum
How to use checksums
dfdbc9759eac1bf4f117cb46b22160ea939ec00a50c432bffadde74219cde15b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Release files / nms_3d-0.2.1-py3-none-any.whl

Download URL nms_3d-0.2.1-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
44f3585deadb1527ef26853243d5ed5afecfae52569b94f773b003524ef0a127
BLAKE2b-256 checksum
How to use checksums
bef3ac08860f5eac7180f1cb786c7c8cda8a3b2602dfa328fe40bdddf792ec62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.12

Security for nms-3d 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page