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

torchvision.transforms.functional.to_pil_image #1099

Closed
liangsizhuang opened this issue Jul 7, 2019 · 0 comments · Fixed by #1100
Closed

torchvision.transforms.functional.to_pil_image #1099

liangsizhuang opened this issue Jul 7, 2019 · 0 comments · Fixed by #1100

Comments

@liangsizhuang
Copy link

The function, torchvision.transforms.functional.to_pil_image, transforms FloatTensor to uint8 internally. Hence, it is impossible to use mode = 'F'. This can be frustrating when dealing with high bit-depth images.

I was working with spectrograms and the spectrograms had high resolution (more than 8 bits per channel).

Problem Demonstration 1

Here is a demonstration of what was happening.

import torch
import torchvision.transforms
X1 = torch.Tensor(10, 10)
X1.normal_(0.5, 0.5)
print(X1.dtype)
X2 = torchvision.transforms.functional.to_pil_image(X1, mode = 'F') # This is going to be error.
Output: ValueError: Incorrect mode (F) supplied for input type <class 'numpy.dtype'>. Should be L

Problem Demonstration 2

If we use mode L, we have

X2 = torchvision.transforms.functional.to_pil_image(X1)
X3 = torchvision.transforms.functional.to_tensor(X2)
print((X1 - X3).abs().max())
Output: tensor(2.0097)

This means that X3 is significantly different from X1.

Expected Behavior

This is what it should be

from PIL import Image
X4 = Image.fromarray(X1.numpy(), mode = 'F')
X5 = torchvision.transforms.functional.to_tensor(X4)
print((X1 - X5).abs().max())
Output: tensor(0.)

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.

2 participants