Repository navigation
Closed
Description
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.5Metadata
Metadata
Assignees
Labels
No labels
Activity
KumoLiu commentedon Feb 10, 2023
Hi @beantr96, thanks for pointing this out. The reason is that in
Transposedit didn't update the affine. You could try to use "Orientationd" to change the orientation in medical images.Do we need to add the support for the update of affine in
Transposed? cc @wyli @Nic-MaTuanDTr commentedon Feb 10, 2023
Hi @KumoLiu, many thanks for the quick reply. As far as I am aware of,
Orientationddoes not really change the shape of the tensor. For implementation reason in our pipeline, we want the tensor shape to be (D, W, H) instead of (H, W, D) by default with Monai'sLoadImaged. We once tried Monai's ITK reader with thereverse_indexingflag but we observed the inconsistent behavior (but this is another story though). So we thought of using theTransposedas an option. Is there any alternative solution that I can use as a workaround to this problem?wyli commentedon Feb 10, 2023
I think you should use
Orientationdin this case.for this input:
you can get the image's current orientation using nibabel:
then set the output orientation to
SARor try other orientations:Add docstring note that Transpose/Transposed do not update the affine…