Project-MONAI / Project-MONAI/MONAI

Lazy DivisiblePadd converts uint8 to float32

Open
#6,713 4 comments 0 reactions 1 assignee View on GitHub

@atbenmurray is already working on this.

Since Feb 9, 2024.

Contribution wanted Feature request
Dominant language
Python
Stars
8.7k
Forks
1.6k
Avg merge
5d 1h
Merged PRs (30d)
20

Description

Describe the bug
When running DivisiblePadd in a lazy Compose pipeline, the padding implicitly converts a uint8 tensor to a float32 tensor.
This happens only with lazy=True, if I run the same pipeline with lazy=False, the tensor stays uint8 type.

To Reproduce

import torch
from monai.transforms import DivisiblePadd, Compose

# create a 3D tensor
input_batch = {"x": torch.arange(0, 27,dtype=torch.uint8).reshape(3, 3, 3)}
assert input_batch["x"].dtype == torch.uint8

print("non lazy transform")
non_lazy_transforms = Compose([DivisiblePadd(k=5,keys=["x"])],lazy=False)
non_lazy_batch = non_lazy_transforms(input_batch)
print(non_lazy_batch["x"].dtype)
assert non_lazy_batch["x"].dtype == torch.uint8

print("lazy transform")
lazy_transforms = Compose([DivisiblePadd(k=5,keys=["x"])],lazy=True)
lazy_batch = lazy_transforms(input_batch)
print(lazy_batch["x"].dtype)
assert lazy_batch["x"].dtype == torch.uint8

Output:

non lazy transform
torch.uint8
lazy transform
torch.float32
Traceback (most recent call last):
  File "/mnt/c/Projects/monai_bug/padd_bug.py", line 18, in <module>
    assert lazy_batch["x"].dtype == torch.uint8
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Expected behavior
The type does not get converted, as this causes problems later on, especially for label maps.

Environment

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

================================
Printing MONAI config...

MONAI version: 1.2.0
Numpy version: 1.25.0
Pytorch version: 2.0.1
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: c33f1ba588ee00229a309000e888f9817b4f1934

Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
ITK version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: NOT INSTALLED or UNKNOWN VERSION.
scikit-image version: NOT INSTALLED or UNKNOWN VERSION.
Pillow version: 9.4.0
Tensorboard version: NOT INSTALLED or UNKNOWN VERSION.
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: 0.15.2
tqdm version: NOT INSTALLED or UNKNOWN VERSION.
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: NOT INSTALLED or UNKNOWN VERSION.
pandas version: NOT INSTALLED or UNKNOWN VERSION.
einops version: NOT INSTALLED or UNKNOWN VERSION.
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow version: NOT INSTALLED or UNKNOWN VERSION.
pynrrd version: NOT INSTALLED or UNKNOWN VERSION.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.