You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.)
The text was updated successfully, but these errors were encountered: