Reputation: 31
My inputs are PIL images. Suppose I have the following transformation composition:
transforms.Compose([
transforms.RandomResizedCrop(size=224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()])
As most of the transforms in PyTorch can work on both PIL images and tensors, I wonder in which order I should use them. Shall I first use all the transformations possible to use on PIL images and then transform to tensor, or shall I first transform to tensor and then apply the other transformations on tensors? Is the one more effective than the other?
Upvotes: 2
Views: 2541
Reputation: 81
In the case of transformation you mentioned, I think order doesn't matter. But for a complete flow of work, the order helps a lot in understanding.
For example, in the given code, first, you are cropping the image, then using random horizontal flip for data augmentation after doing these two operations you are converting it into a PyTorch tensor which makes sense.
Upvotes: 0
Reputation: 16945
There's no real advantage in general to changing the order. However, there can be advantages to moving the ToTensor
out of the transforms chain. Specifically, you cannot JIT transformations operating on PIL images which may have optimization impact. For this reason, it may be better to convert PIL images to tensors in your data loading code and then transform as needed. I refer you to the documentation to read more about this.
Upvotes: 1