0
$\begingroup$

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(), transforms.RandomHorizontalFlip(), 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?

$\endgroup$
3
  • $\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$ Commented Sep 19, 2023 at 14:16
  • $\begingroup$ Thank you. You are right. That was the problem. Such a childish mistake I made :/ $\endgroup$ Commented Sep 19, 2023 at 20:05
  • $\begingroup$ No worries, happy coding! $\endgroup$ Commented Sep 19, 2023 at 20:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.