Torchvision.transforms.v2 does nothing / fails silently?

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.

for PIL you have a typo:
img_pil_transformed = v2.Pad(50)(img_np)

1 Like

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?

Yes, you’re right. I also think the silent failure is a problem.

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

@ptrblck thanks for picking this up!

I made an issue in the mean time Torchvision.transforms.v2 does nothing / fails silently with numpy array · Issue #8662 · pytorch/vision · GitHub

Let me know if i can help something

Thanks for creating this issue!

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