Skip to content

Monai distance_transform_edt fails with “indices array has wrong shape” for channel-first inputs when return_indices=True #8656

@alexanderjaus

Description

@alexanderjaus

Bug Description
Monai's distance_transform_edt allocates the feature-transform indices buffer with layout (ndim, C, …) instead of channel-first (C, spatial_dims, …), so SciPy/cuCIM reject the provided array and raise RuntimeError: indices array has wrong shape whenever
return_indices=True on channel-first inputs.

To Reproduce
Steps to reproduce the behavior:

import torch
from monai.transforms.utils import distance_transform_edt

img = torch.tensor([[[0, 0, 1],
                     [0, 1, 1],
                     [1, 1, 1]]], dtype=torch.float32)  # shape (1, 3, 3)

# Raises: RuntimeError: indices array has wrong shape
indices = distance_transform_edt(img, return_distances=False, return_indices=True)
print(indices.shape)  # should be: (1, 2, 3, 3)

Expected behavior
torch.Size([1, 2, 3, 3])

Monai output

user@host:~/project/MONAI$ python testrun_monai_edt.py
Traceback (most recent call last):
  File "/home/user/project/MONAI/testrun_monai_edt.py", line 9, in <module>
    indices = distance_transform_edt(img, return_distances=False, return_indices=True)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/project/MONAI/monai/transforms/utils.py", line 2543, in distance_transform_edt
    ndimage.distance_transform_edt(
  File "/home/user/miniconda3/envs/cc_metrics_monai/lib/python3.11/site-packages/scipy/ndimage/_morphology.py", line 2579, in distance_transform_edt
    raise RuntimeError('indices array has wrong shape')
RuntimeError: indices array has wrong shape

Environment

================================
Printing MONAI config...
================================
MONAI version: 1.5.1+26.g15fd428c
Numpy version: 2.3.5
Pytorch version: 2.9.1+cu128
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: 15fd428cfd9965e9d9875c1f548ee5576adf8a38
MONAI __file__: /home/<username>/CC_Metris_Monai/MONAI/monai/__init__.py

Optional dependencies:
Pytorch Ignite version: 0.4.11
ITK version: 5.4.5
Nibabel version: 5.3.2
scikit-image version: 0.25.2
scipy version: 1.16.3
Pillow version: 12.0.0
Tensorboard version: 2.20.0
gdown version: 5.2.0
TorchVision version: 0.24.1+cu128
tqdm version: 4.67.1
lmdb version: 1.7.5
psutil version: 7.1.3
pandas version: 2.3.3
einops version: 0.8.1
transformers version: 4.57.1
mlflow version: 3.6.0
pynrrd version: 1.1.3
clearml version: NOT INSTALLED or UNKNOWN VERSION.

For details about installing the optional dependencies, please visit:
    https://monai.readthedocs.io/en/latest/installation.html#installing-the-recommended-dependencies


================================
Printing system config...
================================
System: Linux
Linux version: Ubuntu 22.04.5 LTS
Platform: Linux-6.8.0-79-generic-x86_64-with-glibc2.35
Processor: x86_64
Machine: x86_64
Python version: 3.11.14
Process name: python
Command: ['python', '-c', 'import monai; monai.config.print_debug_info()']
Open files: [popenfile(path='/home/ajaus/monai_info.txt', fd=1, position=1128, mode='w', flags=32769)]
Num physical CPUs: 12
Num logical CPUs: 20
Num usable CPUs: 20
CPU usage (%): [2.1, 1.7, 2.2, 1.7, 2.4, 1.7, 2.7, 1.8, 33.2, 15.6, 4.6, 4.3, 21.2, 1.8, 5.1, 1.8, 3.1, 2.4, 2.2, 1.7]
CPU freq. (MHz): 1385
Load avg. in last 1, 5, 15 mins (%): [2.6, 1.6, 2.1]
Disk usage (%): 99.7
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 62.7
Available memory (GB): 42.0
Used memory (GB): 20.7

================================
Printing GPU config...
================================
Num GPUs: 1
Has CUDA: True
CUDA version: 12.8
cuDNN enabled: True
NVIDIA_TF32_OVERRIDE: None
TORCH_ALLOW_TF32_CUBLAS_OVERRIDE: None
cuDNN version: 91002
Current device: 0
Library compiled for CUDA architectures: ['sm_70', 'sm_75', 'sm_80', 'sm_86', 'sm_90', 'sm_100', 'sm_120']
GPU 0 Name: NVIDIA GeForce RTX 4080
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 76
GPU 0 Total memory (GB): 15.6
GPU 0 CUDA capability (maj.min): 8.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions