I am trying to write a NN. However I am getting error. Here is my Code:

tfms= transforms.Compose([
    transforms.Resize((IMG_SIZE, IMG_SIZE)),
    transforms.ToTensor(),  #scale data to [0,1]
    transforms.Lambda(lambda t: (t*2)-1) #scale data to [-1,1]

datas= datasets.ImageFolder(root='D:\\Ali\\python files\\mycats\\', transform= tfms)

Then, when defining dataloader as

dataloader= torch.utils.data.DataLoader(datas, batch_size= BATCH_SIZE, drop_last= True)

writing image= next(iter(dataloader))[0], I get error:

TypeError: pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>.

What is wrong here and what should I do?

  • $\begingroup$ Please include the complete error traceback. It seems like in your transforms.Compose(), you are applying transforms.ToTensor() twice which might cause the issue. Remove the first transforms.ToTensor() and keep it as the last argument in transforms.Compose(). $\endgroup$
    – Ro.oT
    Sep 19, 2023 at 14:16
  • $\begingroup$ Thank you. You are right. That was the problem. Such a childish mistake I made :/ $\endgroup$
    – Ali.A
    Sep 19, 2023 at 20:05
  • $\begingroup$ No worries, happy coding! $\endgroup$
    – Ro.oT
    Sep 19, 2023 at 20:09


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.