Why does CenterCrop allows me to save my images?

Hey guys, I’m trying to resize all my images to 256. I modified the code I was using to train my model as follow:

from torchvision import transforms

trans = transforms.Compose([
    transforms.Resize(256),
    transforms.ToTensor(),
])

And saving by using

 for img_batch, label_batch, path_batch in dataloader:
    for img, path in zip(img_batch, path_batch):
      save_image(img, filename)

But that doesn’t work, it says there’s an issue with the dimensions. If I add a CenterCrop to my transformations it works.

    transforms.Resize(256),
    transforms.CenterCrop(256),
    transforms.ToTensor(),

Can anyone explain this behavior?

Thanks

transforms.Resize uses a sequence of sizes or an int:

Desired output size. If size is a sequence like (h, w), output size will be matched to this. If size is an int, smaller edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to (size * height / width, size)

Since you are providing an int I would assume the smaller edge of the image will be matched. Could this be the issue?

1 Like

Maybe, so how can I resize and save without using crop?

Providing both numbers (Resize((256, 256))) will return this exact spatial size.