img should be PIL Image. Got <class 'torch.Tensor'>
Just add transforms.ToPILImage()
to convert into pil image and then it will work, example:
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize(255),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.RandomHorizontalFlip(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
transforms.RandomHorizontalFlip()
works on PIL.Images
, not torch.Tensor
. In your code above, you are applying transforms.ToTensor()
prior to transforms.RandomHorizontalFlip()
, which results in tensor.
But, as per the official pytorch documentation here,
transforms.RandomHorizontalFlip() horizontally flip the given PIL Image randomly with a given probability.
So, just change the order of your transformation in above code, like below:
train_transforms = transforms.Compose([transforms.Resize(255),
transforms.CenterCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])