Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option for LoadImage to be compatible with torchvision save_image #6007

Closed
wyli opened this issue Feb 15, 2023 · 0 comments · Fixed by #6008
Closed

option for LoadImage to be compatible with torchvision save_image #6007

wyli opened this issue Feb 15, 2023 · 0 comments · Fixed by #6008
Assignees

Comments

@wyli
Copy link
Contributor

wyli commented Feb 15, 2023

Is your feature request related to a problem? Please describe.
for 2d natural images the current loader is not consistent with the common computer vision conversion:

import numpy as np
import torchvision

import monai
from monai.transforms import LoadImage

monai.apps.utils.download_url("https://monai.io/assets/img/MONAI-logo_color.png")

img = LoadImage(image_only=True, ensure_channel_first=True)("MONAI-logo_color.png")  # PILReader
torchvision.utils.save_image(img, "MONAI-logo_color_torchvision.png", normalize=True)

the output MONAI-logo_color_torchvision.png is transposed compared with MONAI-logo_color.png

would be great to add an option to the PIL-backend reader LoadImage(reader="PILReader") to support this use case.

see also

cc @holgerroth

@wyli wyli self-assigned this Feb 15, 2023
wyli added a commit to wyli/MONAI that referenced this issue Feb 15, 2023
Signed-off-by: Wenqi Li <[email protected]>
wyli added a commit that referenced this issue Feb 16, 2023
Fixes #6007

### Description
- reverse_indexing = False:
to support consistency with PIL/torchvision
```py
img = LoadImage(image_only=True, ensure_channel_first=True, reverse_indexing=False)("MONAI-logo_color.png")  # PILReader
torchvision.utils.save_image(img, "MONAI-logo_color_torchvision.png", normalize=True)
```
- reverse_indexing = True:
to support consistency with other backends in monai
```py
img = LoadImage(image_only=True, ensure_channel_first=True, reader="PILReader", reverse_indexing=True)(filename)  # PIL backend
img_1 = LoadImage(image_only=True, ensure_channel_first=True, reader="ITKReader")(filename)  # itk backend
np.testing.assert_allclose(img, img_1)  # true
```

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [x] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [x] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [x] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: Wenqi Li <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant