Reshaping both the dimensions of images while loading dataset using dataloader

This is how I loaded my dataset. The shape of each batch while loading is (32, 3, 218, 178)

temp_dataset = ImageFolder('./rude_dir', transforms.Compose([ transforms.ToTensor() ]))
temp_loader = DataLoader(dataset=temp_dataset, batch_size=batch_size, shuffle=True, num_workers=8, drop_last=True)

temp_dataset = ImageFolder('./rude_dir', transforms.Compose([ transforms.ToTensor() ]))
temp_loader = DataLoader(dataset=temp_dataset, batch_size=batch_size, shuffle=True, num_workers=8, drop_last=True)

a[0].shape
torch.Size([32, 3, 218, 178])

How to reshape these images by just modifying the transforms function ?

transformations_exp = transforms.Compose([
    transforms.Resize(128),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
temp_dataset = ImageFolder('./rude_dir', transformations_exp)
temp_loader = DataLoader(dataset=temp_dataset, batch_size=batch_size, shuffle=True, num_workers=8, drop_last=True)
for a in temp_loader:
    break
a[0].shape
torch.Size([32, 3, 156, 128])

As you can see, when I specify transforms.Resize(128), only one dimension changes to 128 and another dimension changes to some other value.
Thank you so much in advance !

Hello,

Looking at the docs for transforms.Resize, you need to specify a sequence (h, w) if you want to reshape the image in both dimensions. If you only specify one number (like you did), it will resize the smaller edge and keep the aspect ratio for the other one.

Good luck!

Thanks, I tried it
But got the following error

ValueError: Caught ValueError in DataLoader worker process 0.
 ValueError: unknown resampling filter

I tried you transformations_exp on an image and everything works on my end. Could you post your image folder code? Also, make sure to give a tuple to transforms.Resize((128, 128)) or else the second value will be interpreted as the interpolation!

1 Like

It worked, thanks a lot :slight_smile:
I didn’t specify the input of transforms.Resize as a tuple !