Can't precise tuple size to Transforms.Scale with ImageFolder in torchvision 0.1.8

Hello everyone,
I’m working with ImageFolder. When I looked at the doc in my jupyter (Shift + Tab), I saw the following:

Init signature: T.Scale(size, interpolation=2)
Rescales the input PIL.Image to the given 'size'.
'size' will be the size of the smaller edge.
For example, if height > width, then image will be
rescaled to (size * height / width, size)
size: size of the smaller edge
interpolation: Default: PIL.Image.BILINEAR

So my question: Is there another way to set the new size with width=height like (224, 224) for example without using the github last version because I’ve noticed the online doc of Scale mentions the first argument, size can be a sequence.
Thank you.

Hi I found a short work around probably not elegant but it works.
It’s based on Data Loading and Processing Tutorial - Pytorch Tutorials 0.1.12_2 documentation.
I defined a custom transform function:

class Rescale(object):
Rescale the image in a sample to a given size.

    output_size (tuple or tuple): Desired output size. If tuple, output is
        matched to output_size.

def __init__(self, size=(224, 224)):
    assert isinstance(size, (int, tuple))
    self.output_size = size

def __call__(self, img):
    import PIL
    res = PIL.Image.Image.resize(img, self.output_size)

    return res

Then I called directly ImageFolder with Rescale:

train_data = dset.ImageFolder(root=path+'train', transform=T.Compose([Rescale(size=(256,256)), T.ToTensor()]), target_transform=None)
train_loader = DataLoader(train_data,batch_size=4)
for t, (x,y) in enumerate(train_loader):
    print(t, x.size())

Output(my dataset has 20 images so 5 batches of size 4):

0 torch.Size([4, 3, 256, 256])
1 torch.Size([4, 3, 256, 256])
2 torch.Size([4, 3, 256, 256])
3 torch.Size([4, 3, 256, 256])
4 torch.Size([4, 3, 256, 256])

Hopefully this going to be helpful for folks wanting such functionality.


the other convenient option probably is to install torchvision from source. Note that it has its own repository at

Torchvision is considerably easier to install from source than pytorch itself.

Best regards


1 Like