Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply 2 different transformation on my dataset using pytorch

I am preparing my data for a downstream CNN application in pytorch. I wish to use the transform methods = ToTensor() and Resize() both on the same dataset. I am not able to apply them together.

from torchvision.datasets import ImageFolder
from torchvision.transforms import ToTensor # In order to convert the datat to pytorch tensors
from torchvision.transforms import Resize

Able to apply them separately, but need to apply both these transforms on the same dataset. The image files are stored in "/content/sub_folder" directory.

dataset = ImageFolder("/content/sub_folder" , transform = Resize([224,224])) # ToTensor is method

dataset = ImageFolder("/content/sub_folder" , transform = ToTensor()) # ToTensor is method

How to apply both these transformation on all the images contained in my folder.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Hi you can use compose to use multiple transforms

from torchvision import transforms as T
from torchvision.datasets import ImageFolder

trans_comp = T.Compose([
  T.Resize([224,224]),
  T.ToTensor()
])

dataset = ImageFolder("/content/sub_folder", transform=trans_comp) 

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading