mrdbourke / pytorch-deep-learning

Materials for the Learn PyTorch for Deep Learning: Zero to Mastery course.
https://learnpytorch.io
MIT License
11.06k stars 3.25k forks source link

Update torchvision transforms -> transforms.v2 #701

Open mrdbourke opened 1 year ago

mrdbourke commented 1 year ago

torchvision v0.15+ brings in updated image transformations.

Supposedly these are faster/better than the originals and should be drop-in replacements.

See the guide here: https://pytorch.org/vision/main/transforms.html#v1-or-v2-which-one-should-i-use

TODO

aagirre92 commented 1 year ago

Just for reference: Each time that ToTensor() is used with this torchvision.transforms.v2 module, following pops up:

/usr/local/lib/python3.10/dist-packages/torchvision/transforms/v2/_deprecated.py:43: UserWarning: The transform ToTensor() is deprecated and will be removed in a future release. Instead, please use v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)] warnings.warn(

Should we keep on using ToTensor()? What is the alternative? I have made the following test and it seems that output tensors are not the same:

# TEST: Check transforms.ToTensor() and v2.ToImage() + v2.ToDtype(torch.float32,scale=True)
# torch version 2.1.0+cu118 and torchvision version 0.16.0+cu118
from torchvision.transforms import v2 # new
from torchvision import transforms # old

transform1 = transforms.ToTensor()

transform2 = transforms.Compose([
    v2.ToImage(),
    v2.ToDtype(torch.float32, scale=True)
])

img_path = "/content/data/pizza_steak_sushi/test/pizza/1152100.jpg"
img = Image.open(img_path)

first_transform = transform1(img)
second_transform = transform2(img)

torch.equal(first_transform,second_transform)

This outputs False 😞

TwE3d commented 1 month ago

Just stumbled upon this issue in my research into this exact question! 😄 When using ToTensor or ToImage+ToDtype the values of the resulting tensors are the same. But the new method uses a torch.Tensor-Subclass "Image" for storing the tensor. Because of this torch.equal outputs False.

As it seems, both classes are mostly interchangeable with transforms etc, as described here.