v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]) return Image instead of Tensor?

[ToTensor — Torchvision main documentation]([v2.ToTensor()] [DEPRECATED] Use v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]) instead.)

v2.Compose([v2.ToTensor()]) is return a Tensor
But v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]) is return a Image

Are there steps missing?

The second transformation will return a torchvision.tv_tensors._image.Image as seen here:

transform = v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])
image = torchvision.transforms.ToPILImage()(torch.randn(3, 224, 224))
out = transform(image)
print(type(out))
# <class 'torchvision.tv_tensors._image.Image'>

If you want to access the internal tensor use the .data attribute as shown in the docs.

1 Like