pytorch-deep-learning icon indicating copy to clipboard operation
pytorch-deep-learning copied to clipboard

Update torchvision transforms -> transforms.v2

Open mrdbourke opened this issue 2 years ago • 4 comments

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

  • [ ] Replicate the existing notebook with the new transforms and see what changes
  • [ ] Add the new notebook to the course with a note on the updates

mrdbourke avatar Oct 26 '23 05:10 mrdbourke

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 😞

aagirre92 avatar Nov 09 '23 12:11 aagirre92