This error is about a tensor to PIL image

TypeError: pic should be Tensor or ndarray. Got <class ‘torch.FloatTensor’>.
This is my error when I try to change a ‘torch.FloatTensor’ to a picture ,this error will be occur. but i discorver the question is that a image was open ,then transform it to a tensor which a ’ torch.FloatTensor’. so why the error appear

1 Like

Could you post a small code snippet reproducing this error.
FloatTensors can usually be transformed to a PIL image:

a = torch.FloatTensor(3, 128, 128)
b = transforms.ToPILImage()(a)

The error message you get is also given when you pass something not channel x height x width. So eg if you have gray as h x w you need to use .unsqueeze(0) or so to add the channel axis.

Best regards

Thomas

2 Likes

thank for you help.
This error occur because two reasons.
one , dimension is incorrect
two ,I i have not change a variable to data