TorchMorph:CUDA加速的形态学变换

·

观点分析 · 2026/08/26

TorchMorph 是一个轻量级的 PyTorch 扩展,提供 22 个公开算子,涵盖二值形态学、灰度形态学、精确和近似距离变换以及熵正则化最优传输,全部实现为融合 CUDA 内核,直接操作最多八维的 CUDA 张量。其 API 刻意模仿 scipy.ndimage,便于迁移。与单线程 CPU 参考相比,批量执行在灰度形态学上可达 1100 倍吞吐量,精确欧氏距离变换可达 350 倍,Sinkhorn 求解器比 POT 快 42 倍。二值和 chamfer 算子与 SciPy 完全一致,浮点算子与 CPU 参考的绝对误差在 1.8e-6 以内。

该论文解决了深度学习中形态学变换的 GPU 加速问题,提供了与 scipy 兼容的高性能实现,对图像处理和计算机视觉领域的研究者具有实用价值。

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.

阅读原文

参与讨论