Am I using torchvision.Transforms the right way?

If you’re using any data augmentation in pytorch, such as RandomCrop or Random Flip, its input should be always PILImage.
Try to transform your train input data without ToPILImage method:

test_transform = transforms.Compose([
    transforms.RandomCrop(60),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor()
])

You will catch an exception, because input data should be PILImage type.
So, what does PILImage method do? It transforms each pixel of image to [0;1] representation. Imagine, you have rgb image and its shape is [3x60x60]. Each element has value from 0 to 255 and transforms to [0;1] representation after PILImage operation.
If you have applied PILImage operation to train data, you should do it for test data too.
Best regrads,
Alex

2 Likes