when i ues "torchvision.transforms.Totensor()"to conver a numpy.array to tensor.But numpy.array is not ndarray?
Could you print the shape and type or your input?
maybe your input dimension is not a accept shape, can not deal with like a image
@luodahei Hi, have you fixed the problem?
I have encountered the same problem, but no idea to fix it.
Could you please help me?
torchvision.transforms.ToTensor() does accept a ndarray. What doesn’t is the torchvision.transforms.Resize() or CenterCrop().
Resize() takes a PIL image, so you need to convert the ndarray like this:
from PIL import Image PIL_image = Image.fromarray(ndarray_image)
Hope this does the trick for you!
I have the same error, and the reason in my case is the array is None, i.e, obtained from
x = None
I have the same issue, being that my data is of size
torch.Size([32, 3, 64, 64]), and I’m using the transformations:
transform = transforms.Compose( [transforms.RandomHorizontalFlip(p=0.2), transforms.ColorJitter(brightness=.5, hue=.1), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
I am calling as this:
According to the docs, this composition should accept tensor. My PyTorch version is
Why is there even such difference between accepting PIL or tensor types? This makes everything so much harder
This code works for me, so you might need to update PyTorch and
transform = transforms.Compose( [transforms.RandomHorizontalFlip(p=0.2), transforms.ColorJitter(brightness=.5, hue=.1), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) x = torch.randn(32, 3, 64, 64) out = transform(x) print(out.shape) > torch.Size([32, 3, 64, 64])
The ability to apply
torchvision.transforms on tensors is relatively new and makes it easier now.
Hi I’m also getting this error with the most updated pytorch and torchvision. My transforms are
transform = transforms.Compose([torchvision.transforms.RandomCrop(size=(16, 16)), torchvision.transforms.RandomHorizontalFlip(p=0.5), torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])
If I comment the ToTensor() transform out I instead get an “Unexpected type <class ‘numpy.ndarray’>” error.
The error shows up when I’m calling this line in my training function.
for i, (x, y) in enumerate(loader):
It works though when I just have the ToTensor and Normalise transform, but not the first 2
The transformations would work on
PIL.Images as well as PyTorch tensors (in newer
torchvision releases), while it seems that you are trying to use a numpy array based on the error message.
Pass on of the supported types to
transform and it should work.