turbotimon
(Timon Erhart)
August 22, 2024, 9:05pm
1
I want to transform a PIL image or np.ndarray, but it in both cases, the transform does nothing to the image.
Minimal reproducable example: As you can see, the mean does not change
import torch
import numpy as np
import torchvision.transforms.v2 as v2
import matplotlib.pyplot as plt
from PIL import Image
## np.array (does nothing / fails silently)
img_np = np.ones((100,100,3))
img_np_transformed = v2.Pad(50)(img_np)
print(np.mean(img_np_transformed)) # still 1.0
plt.imshow(img_np_transformed)
## PIL image (does nothing / fails silently)
img_pil = Image.fromarray((img_np * 255).astype('uint8'))
img_pil_transformed = v2.Pad(50)(img_np) # still 1.0
print(np.mean(img_pil_transformed))
## Tensor (works)
tensor_img = torch.tensor(np.ones((1,3,100,100)))
tensor_img_transformed = v2.Pad(50)(tensor_img)
print(float(tensor_img_transformed.mean())) # correct 0.25
plt.figure()
plt.imshow(np.transpose(tensor_img_transformed.squeeze(), (2, 1, 0)))
What do I do wrong or is this a bug? According to the docs , PIL Images should work. And at least, I would expect an Exception if the input is invalid.
Gillian
(Milani)
August 23, 2024, 1:42pm
3
for PIL you have a typo:
img_pil_transformed = v2.Pad(50)(img_np )
1 Like
turbotimon
(Timon Erhart)
August 23, 2024, 2:02pm
4
Yes, thats true, thanks. What still bothers me, is that it fails silently with numpy.ndarray. Do you think that is word a issue github? Or is this intended?
Gillian
(Milani)
September 16, 2024, 1:17pm
5
Yes, you’re right. I also think the silent failure is a problem.
ptrblck
September 18, 2024, 8:40pm
6
Yes, I the transformation should not fail silently. CC @NicolasHug would you know if we are missing to raise an error or a warning here?
1 Like
turbotimon
(Timon Erhart)
September 27, 2024, 9:56am
7
ptrblck
September 27, 2024, 2:55pm
8
Thanks for creating this issue!
turbotimon
(Timon Erhart)
October 28, 2024, 2:46pm
9
Apparently this not a bug but a feature for passing arbitrary data structures. However, this is (at this time) not very well described in the documents and is only described here Getting started with transforms v2 — Torchvision 0.20 documentation but e.g. not here
See also #8662