Skip to content

Spacingd does not work properly together with Transposed #5975

@TuanDTr

Description

@TuanDTr

Describe the bug
Spacingd results in a different size if it is used together with Transposed. This is the compose of transform that I want to apply on the image:

transforms = Compose([
    LoadImaged(keys=("image")),
    Transposed(keys=("image"), indices=None), # change to (z, y, x) dim
    EnsureChannelFirstd(keys=("image")),
    Spacingd(keys=("image"), pixdim=(0.7, 0.7, 0.7)),
])

If I remove Transposed from the list of transforms, I get the correct target shape and spacing. Please see details in below sections.

To Reproduce

from monai.transforms import LoadImaged, Transposed, Spacingd, Compose, EnsureChannelFirstd

image_path = "Medical_Decathlon_06_Lung_Segmentation/imagesTr/lung_003.nii.gz"

# This image has the shape of (512, 512, 288) and spacing of (0.9375, 0.9375, 1.2456597089767456)

transforms_with_transpose = Compose([
    LoadImaged(keys=("image")),
    Transposed(keys=("image"), indices=None),
    EnsureChannelFirstd(keys=("image")),
    Spacingd(keys=("image"), pixdim=(0.7, 0.7, 0.7)),
])

transforms_without_transpose = Compose([
    LoadImaged(keys=("image")),
    EnsureChannelFirstd(keys=("image")),
    Spacingd(keys=("image"), pixdim=(0.7, 0.7, 0.7)),
])

datum_dict = {"image": image_path}
datum_dict_with_transpose = transforms_with_transpose(datum_dict)
datum_dict_without_transpose = transforms_without_transpose(datum_dict)

print("Image size after transforming with transpose: ", datum_dict_with_transpose["image"].shape)
# returns: Image size after transforming with transpose:  torch.Size([1, 385, 685, 910])
print("Image size after transforming without transpose: ", datum_dict_without_transpose["image"].shape)
# returns: Image size after transforming without transpose:  torch.Size([1, 685, 685, 512])

Expected behavior
The transforms with Spacingd and Transposed should result in the shape of (1, 512, 685, 685)

Environment

Ensuring you use the relevant python executable, please paste the output of:

python -c 'import monai; monai.config.print_debug_info()'
================================
Printing MONAI config...
================================
MONAI version: 1.1.0
Numpy version: 1.23.4
Pytorch version: 1.12.1
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: a2ec3752f54bfc3b40e7952234fbeb5452ed63e3
MONAI __file__: /home/tuan_truong_bayer_com/miniconda3/envs/seg_clone/lib/python3.9/site-packages/monai/__init__.py

Optional dependencies:
Pytorch Ignite version: 0.4.8
Nibabel version: 4.0.2
scikit-image version: 0.19.3
Pillow version: 9.3.0
Tensorboard version: 2.10.1
gdown version: 4.5.3
TorchVision version: 0.13.1
tqdm version: 4.64.1
lmdb version: 1.3.0
psutil version: 5.9.3
pandas version: 1.4.4
einops version: 0.5.0
transformers version: 4.24.0
mlflow version: 1.30.0
pynrrd version: 1.0.0

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


================================
Printing system config...
================================
System: Linux
Linux version: Debian GNU/Linux 10 (buster)
Platform: Linux-4.19.0-12-cloud-amd64-x86_64-with-glibc2.28
Processor: 
Machine: x86_64
Python version: 3.9.13
Process name: python
Command: ['python', '-c', 'import monai; monai.config.print_debug_info()']
Open files: []
Num physical CPUs: 8
Num logical CPUs: 16
Num usable CPUs: 16
CPU usage (%): [5.1, 4.8, 4.8, 4.5, 99.7, 4.8, 4.8, 4.8, 4.1, 4.5, 5.1, 5.1, 4.5, 4.5, 4.8, 5.1]
CPU freq. (MHz): 2000
Load avg. in last 1, 5, 15 mins (%): [6.8, 5.8, 4.8]
Disk usage (%): 55.0
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 59.0
Available memory (GB): 55.8
Used memory (GB): 2.5

================================
Printing GPU config...
================================
Num GPUs: 1
Has CUDA: True
CUDA version: 11.6
cuDNN enabled: True
cuDNN version: 8302
Current device: 0
Library compiled for CUDA architectures: ['sm_37', 'sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'sm_86', 'compute_37']
GPU 0 Name: Tesla T4
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 40
GPU 0 Total memory (GB): 14.8
GPU 0 CUDA capability (maj.min): 7.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions