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

Loading 16bit png images #2218

Closed
ssnl opened this issue May 15, 2020 · 3 comments · Fixed by #4657
Closed

Loading 16bit png images #2218

ssnl opened this issue May 15, 2020 · 3 comments · Fixed by #4657

Comments

@ssnl
Copy link
Contributor

ssnl commented May 15, 2020

ported here from pytorch/pytorch#32971

Original description:

When I was trying to load 16 bit .png grayscale image with torchvision.datasets.imagefolder ,it is loading every image as white only.
I solved this issue by doing transformation operations outside Compose function.

cc @pmeier @wanifarooq @choidongyeon

@shubhamsidhwa
Copy link

What was the solution for this issue ?

@pmeier
Copy link
Collaborator

pmeier commented May 6, 2021

@shubhamsidhwa If you don't set a loader for datasets.ImageFolder, internally

def default_loader(path: str) -> Any:
from torchvision import get_image_backend
if get_image_backend() == 'accimage':
return accimage_loader(path)
else:
return pil_loader(path)

is used. Since accimage is an extra installation and activation step, I'm guessing it is falling back to Pillow in your case:

def pil_loader(path: str) -> Image.Image:
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')

Their documentation states:

Pillow also provides limited support for a few special modes, including:

  • [...]
  • I;16 (16-bit unsigned integer pixels)
  • I;16L (16-bit little endian unsigned integer pixels)
  • I;16B (16-bit big endian unsigned integer pixels)
  • I;16N (16-bit native endian unsigned integer pixels)
  • [...]

My guess would be that you are hitting a case where Pillow does not have adequate support. Could you share an image so we can confirm hunch?


To overcome this you can set a custom loader that is able to handle your images. It should take a path as argument and should return the image in whatever format you need it to be.

@NicolasHug
Copy link
Member

Pillow is kinda bad at reading 16bits grayscale pngs (e.g. python-pillow/Pillow#3011), so I'm not sure this is a torchvision issue, more probably a PIL problem.

I marked this issue to be closed once #4657 is merged: from that point you'll be able to read 16 bits pngs natively in torchvision with read_image or decode_png.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants