Skip to main content

A fast 3D binary thinning implementation using CUDA and PyTorch.

Project description

Binary Thinning 3D CUDA

This package provides a blazing fast, memory-efficient GPU implementation of 3D Binary Thinning (skeletonization) using CUDA and PyTorch.

It is based on the 3D thinning algorithm by Lee, Kashyap and Chu (1994), which uses Euler characteristic invariance and 26-connectivity checks to safely erode a 3D binary volume down to a 1-pixel wide skeleton without altering its fundamental topology.

Features

This implementation provides two topologically safe operating modes to suit your needs:

  1. Mode 0: GPU Subgrid 8-Color Parallel (mode=0, Default)
    • Speed: Extremely Fast (~300x speedup over CPU)
    • Behavior: Operates entirely on the GPU. It avoids race conditions by partitioning the image into an 8-color 3D checkerboard. It re-checks and deletes pixels of the same color in parallel because they are mathematically guaranteed not to touch each other.
    • Topology: Topologically Safe. Produces a mathematically valid skeleton. Note: Because the deletion order differs slightly from a strict CPU raster-scan, the exact pixel placement may differ very slightly from ITK (e.g. 0.003% difference), but the overall global topology is preserved perfectly.
  2. Mode 1: Hybrid CPU-GPU Sequential (mode=1)
    • Speed: Fast (~100x speedup over CPU)
    • Behavior: Calculates Euler invariance on the GPU in parallel, but performs the final 26-connectivity re-checks strictly sequentially on the CPU (using zero-overhead memory compaction and host-side sorting).
    • Topology: 100% Identical to ITK. Guaranteed to produce the exact same pixel output as standard sequential CPU implementations like itk.BinaryThinningImageFilter3D.

Installation

Prerequisites

  • Python 3.10+
  • PyTorch (with CUDA support)
  • A CUDA-capable GPU

Install from PyPI (Recommended)

You can install the package directly from PyPI. Note that since this contains CUDA C++ extensions, it will be compiled on your machine during installation.

pip install binary-thinning-3d-cuda

Install from Source (Advanced Users)

For development or to run benchmarks, you can install from the source:

git clone https://github.com/sychen52/binary_thinning_3d_cuda.git
cd binary_thinning_3d_cuda

# Standard install
pip install -e . --no-build-isolation

# Install with development dependencies (for running benchmarks)
pip install -e ".[dev]" --no-build-isolation

(Note: itk-thickness3d and SimpleITK are not hard dependencies. They are only included in the [dev] extras for the purpose of benchmarking and validating against the CPU implementation).

Usage

The input can be a 3D PyTorch uint8 (Byte) tensor located on either a CPU or CUDA device.

  • If the tensor is on a CUDA device, the operation is performed in-place.
  • If the tensor is on the CPU, it is automatically moved to the GPU for processing and copied back to the original CPU tensor in-place.

All non-zero values are treated as foreground (0 for background, >0 for foreground).

import torch
from binary_thinning_3d import binary_thinning

# Create or load a 3D binary mask (CPU or GPU)
tensor = torch.zeros((100, 100, 100), dtype=torch.uint8)
tensor[25:75, 25:75, 25:75] = 1 # Solid block

# 1. GPU Subgrid (Default, Max Speed, Topologically Safe)
# Modifies the tensor in-place (handles CPU<->GPU transfer automatically)
binary_thinning(tensor, mode=0)

# 2. Hybrid CPU-GPU (Exact ITK Match)
binary_thinning(tensor, mode=1)

Benchmark

The following benchmark was run on a (767, 512, 512) NIfTI volume (CT Airways Label) containing 451,530 foreground voxels.

Hardware:

  • CPU: AMD Ryzen 7 2700 Eight-Core Processor
  • GPU: NVIDIA GeForce RTX 2070

The benchmark compares this CUDA implementation against itk.BinaryThinningImageFilter3D (which is run sequentially on the CPU). The CUDA timings include the time for CPU-to-GPU and GPU-to-CPU data transfers.

Method Output Voxel Count Time (Seconds) Speedup vs ITK Matches ITK CPU?
Mode 0 (GPU Subgrid) 4,286 0.38 s 331x Topologically equivalent
Mode 1 (Hybrid CPU) 4,281 1.22 s 101x Yes (100% Identical)
ITK (CPU Baseline) 4,281 139.90 s 1x Baseline

To reproduce these benchmarks yourself:

# Ensure you installed with dev dependencies: pip install -e ".[dev]"
python examples/process_nifti.py

(The script will cache the slow ITK result to disk on the first run, so subsequent runs finish instantly).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

binary_thinning_3d_cuda-1.2.3.tar.gz (9.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file binary_thinning_3d_cuda-1.2.3.tar.gz.

File metadata

  • Download URL: binary_thinning_3d_cuda-1.2.3.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for binary_thinning_3d_cuda-1.2.3.tar.gz
Algorithm Hash digest
SHA256 a53d88a0b57fc1c442dc29a1cbda59e89705466a1938b679d37ad66af0b229bd
MD5 26dca26fea4d67361212ec55bea09c51
BLAKE2b-256 55f975deebe0202a5f0cacb06f29ee5ded4a685324a39137cba0c36be1655ba6

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_thinning_3d_cuda-1.2.3.tar.gz:

Publisher: build_wheels.yml on sychen52/binary_thinning_3d_cuda

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file binary_thinning_3d_cuda-1.2.3-cp313-cp313-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for binary_thinning_3d_cuda-1.2.3-cp313-cp313-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 63e68462edb50fc3d9f7b80d783bc19ab84474b0484bce9e08c909ef98755abe
MD5 936bbd1e9886b6fe5ea3cf099f4b1716
BLAKE2b-256 204dc72739c0e0f2a079391e023a851b1f1a52f9b114cc3ea2dcb2a86a6d7109

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_thinning_3d_cuda-1.2.3-cp313-cp313-manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on sychen52/binary_thinning_3d_cuda

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file binary_thinning_3d_cuda-1.2.3-cp312-cp312-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for binary_thinning_3d_cuda-1.2.3-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c695bf8383dcc48c71faf5c4328a6c1e9587a4f61556126a5277f92c3ee87661
MD5 5a1bca3e715283a83fa3856da0b14ce0
BLAKE2b-256 4b3ad818b6f85602ab8c79c0da2cda5bf446816d31507484dc74e0bb898aa33c

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_thinning_3d_cuda-1.2.3-cp312-cp312-manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on sychen52/binary_thinning_3d_cuda

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file binary_thinning_3d_cuda-1.2.3-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for binary_thinning_3d_cuda-1.2.3-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 08fe33132acc724f68fff5c28a11ad7da00dab20e4962595195e48ddae6068ad
MD5 22c26600778e9c8b2aa3b5897ae7ccfb
BLAKE2b-256 d851073c6f9115aba394f495a9a9e7aa1fdb4a4cdb7a74366328fba52474e9a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_thinning_3d_cuda-1.2.3-cp311-cp311-manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on sychen52/binary_thinning_3d_cuda

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file binary_thinning_3d_cuda-1.2.3-cp310-cp310-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for binary_thinning_3d_cuda-1.2.3-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 436bf3ea455e00f35da775385144fa386f74ba453b726f092ee5c1ef404872ac
MD5 76441cc9369bda5c76b395c0d39927da
BLAKE2b-256 dee24d1e5897543ad79d777d4efd4eba6e89962e472cec8a9dd353539b7e4ae3

See more details on using hashes here.

Provenance

The following attestation bundles were made for binary_thinning_3d_cuda-1.2.3-cp310-cp310-manylinux2014_x86_64.whl:

Publisher: build_wheels.yml on sychen52/binary_thinning_3d_cuda

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page