TorchMorph: CUDA-accelerated Morphological Transforms
Kai Zhao
Key points
From the paper's conclusion- TorchMorph exposes 22 public operators covering binary morphology, greyscale morphology, exact and approximate distance transforms, and entropy-regularised optimal transport, all implemented as fused CUDA kernels that operate directly on (B, C, Spatial...) CUDA tensors with up to eight spatial dimensions.
Abstract
Morphological transforms are long-standing tools for shape and mask processing, but the de facto reference implementation in the Python ecosystem, i.e. scipy.ndimage, is CPU-only, single-array, and therefore unusable inside a GPU training loop without an expensive device-to-host round trip. GPU vision libraries built on PyTorch cover a narrow subset of these operators, typically restricted to two spatial dimensions and flat structuring elements. We present TorchMorph, a lightweight PyTorch extension that closes this gap. TorchMorph exposes 22 public operators covering binary morphology, greyscale morphology, exact and approximate distance transforms, and entropy-regularised optimal transport, all implemented as fused CUDA kernels that operate directly on (B, C, Spatial...) CUDA tensors with up to eight spatial dimensions. The API deliberately mirrors scipy.ndimage argument-for-argument, including border modes, structuring-element origins and pre-allocated outputs, so that existing pipelines port with a change of import. We describe the layered architecture and the kernel designs behind each operator family. Against single-threaded CPU references, batched execution reaches up to 1.1e3 times the throughput of scipy.ndimage on greyscale morphology and up to 350x on exact Euclidean distance transforms, while the Sinkhorn solver runs up to 42x faster than POT. Binary and chamfer operators reproduce their SciPy counterparts exactly, and every float-valued operator agrees with the CPU reference to within 1.8e-6 absolute error. TorchMorph is released under the MIT licence at https://intcomp.github.io/tm.
Read the paper
Discussion
Science Skim's AI key points, plus readers' public thoughts
Readers' thoughts
No reader thoughts yet.
Related papers
- TinyFlow: Breaking the Warping Bottleneck for Edge Optical FlowarXiv · 2026
- SoftMorph: Differentiable probabilistic morphological operators for image analysis.Medical image analysis · 2026
- TrimSO(2): Fast and Memory-Efficient Training of Equivariant Interatomic PotentialsarXiv · 2026
- GPU-Native Real-Time Textured Mesh Reconstruction with Seam-Aware Multi-View Radiometric CorrectionarXiv · 2026
- Phantom: Temporal Caching for Diffusion ModelsarXiv · 2026
- KernelMosaic: An Open Dataset and Agentic Framework for CUDA Kernel GenerationarXiv · 2026