Application of transforms to a variable type image

Hi all,

I have a network “net” which is producing an image as output:
b = net(a)

The type of “b” is “torch.cuda.FloatTensor of size 1x3x256x256”. Now I want to resize the image “b” into 1x3x224x224.

To do that I am defining my transforms as follows:

transform_list_classifier = [transforms.ToPILImage(), transforms.Resize((224,224)),transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]

transform_classifier = transforms.Compose(transform_list_classifier)

and then I am applying my transform as follows:

input_classifier = transform_classifier(torch.Tensor((

this input classifier is supposed to be torch.cuda.FloatTensor of size 1x3x224x224

I am getting an error pic should be Tensor or ndarray. Got <class ‘torch.FloatTensor’>.

Is there any way to directly resize a torch.cuda.FloatTensor of size 1x3x256x256 to torch.cuda.FloatTensor of size 1x3x224x224 ???


The error originates from the ToPILImage() transform. The input should have three dimensions, i.e.: 3x256x256, for it to work. Use .squeeze() to remove the dummy dimension:

a = torch.randn((1, 3, 256, 256))

transform = transforms.Compose([
    transforms.Resize((224, 224)),

b = transform(a.squeeze())

print(b.size()) # torch.Size([3, 224, 224])

If you want to have shape 1x3x224x224 again, simply use .unsqueeze() on the desired dimension:

b = b.unsqueeze(0)
print(b.size()) # torch.Size([1, 3, 224, 224])

Thanks @BartolomeD.

It works like a charm.