Pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>

import torch
import torchvision
from torch.utils.data import Dataset,DataLoader
from sklearn import preprocessing

class youtubeFacial(Dataset):

def __init__(self,transform=None):
    
    raw_data = pd.read_csv('../input/fpoints/training.csv')
    raw_data.fillna(0,inplace = True)
    self.n_samples = raw_data.shape[0]
    raw_data['Image'] = raw_data['Image'].apply(lambda img:  np.fromstring(img, sep = ' '))
    X = np.vstack(raw_data['Image'].values)
    X = X.reshape(-1, 1, 96, 96)
    X = X.astype('float64')
    self.X = torch.Tensor(X)
    Y = raw_data[raw_data.columns[:-1]].values
    Y = Y.astype('float64')
    self.Y = torch.Tensor(Y) 
    
    self.transform = transform
    
def __getitem__(self,index):
    return self.transform(self.X[index]),self.transform(self.X[index])

    

def __len__(self):
    return self.n_samples

and
from torchvision import transforms, utils
transform = transforms.Compose([
transforms.Normalize((0.5), (0.5)),
transforms.ToTensor()])
dataset = youtubeFacial(transform)
train_loader = DataLoader(dataset = dataset,batch_size = 4,shuffle = True)

but I am encountering this problem in the title during at the training time. how can i solve this problem ?

Could you swap the Normalize and ToTensor transformations and rerun the code?
I’m not sure, if the error message could be raised by this, so let me know, if this solves the issue.

Not working , i cant understand Could there be a problem with the data ?

Could you post the complete stack trace, please?

Sure , i uploaded to linkStack Trace and i have tried return value of getitem as a dict dtype like pytorch DataLoader documentation

Thanks for the stack trace. I would generally recommend to post it here directly as a code snippet instead of uploading pictures, as it would be easier to e.g. check it on a mobile device. :wink:

I missed that you are already transforming the data to tensors, which would throw this error:

self.X = torch.Tensor(X)

transforms.ToTensor() should be applied on PIL.Images or numpy arrays. Since you already are dealing with tensors, you could skip this transformation.

oh it was completely careless of me , thanks ptrblck :smile: .Although I’m feeding my model as CxHxW type, facing a problem now: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([30]).
The values ​​I give as pictures and labels are as follows; torch.Size([7049, 1, 96, 96])
torch.Size([7049, 30]) and My model output is 30 feature

Stack trace ;
ValueError Traceback (most recent call last)
in
----> 1 X,Y = dataset[0].item

in getitem(self, index)
25 def getitem(self,index):
26 X_transform = self.transform(self.X[index])
—> 27 Y_transform = self.transform(self.Y[index])
28 return self.X_transform,self.Y_transform
29

/opt/conda/lib/python3.7/site-packages/torchvision/transforms/transforms.py in call(self, img)
59 def call(self, img):
60 for t in self.transforms:
—> 61 img = t(img)
62 return img
63

/opt/conda/lib/python3.7/site-packages/torchvision/transforms/transforms.py in call(self, tensor)
164 Tensor: Normalized Tensor image.
165 “”"
–> 166 return F.normalize(tensor, self.mean, self.std, self.inplace)
167
168 def repr(self):

/opt/conda/lib/python3.7/site-packages/torchvision/transforms/functional.py in normalize(tensor, mean, std, inplace)
192 if tensor.ndimension() != 3:
193 raise ValueError('Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = ’
–> 194 ‘{}.’.format(tensor.size()))
195
196 if not inplace:

ValueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([30]).

As the error message explains, Normalize expects an image tensor in the shape [channels, height, width] as its input, while you are trying to pass the target tensor to it, which won’t work.
If you want to normalize the target, you could manually subtract the mean and divide by the standard deviation. However, make sure you really want and need to normalize the target, as it’s often not wanted.

You are right, thanks for all peter

I am having the same issue. I am new to Python and Pytorch.
My code has three parts:

  • first part is where I define the class function;
  • third and final part is using the googlenet model
  • following is the second part…followed by the error message:

import torch
import torchvision
import torch.nn as nn # All neural network modules, nn.Linear, nn.Conv2d, BatchNorm, Loss functions
import torch.optim as optim # for all optimization algorithms, SGD, Adam, etc.
import torchvision.transforms as transforms #transformations we can perform on our dataset
from torch.utils.data import DataLoader #Gives easier dataset management and creates mini batches

#Set device
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)

#hyperparameters
in_channel = 3
num_classes = 10
learning_rate = 1e-3
batch_size = 16
num_epochs = 1
img_size = 224

training_data = Abhidataset(
csv_file = ‘Book1.csv’,
root_dir = ‘dataset_1’,
transform = transforms.ToTensor()
)

#Load Data

train_set, test_set = torch.utils.data.random_split(training_data,[16,5])
train_loader = DataLoader(training_data, batch_size = batch_size, shuffle = True, collate_fn=batch_size)
test_loader = DataLoader(training_data, batch_size = batch_size, shuffle = True, collate_fn=batch_size)

ERROR MESSAGE:

TypeError Traceback (most recent call last)
in
11 losses=[]
12
—> 13 for batch_idx, (data, targets) in enumerate(train_loader):
14 #Get data to cuda if possible
15 data = data.to(device=device)

~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py in next(self)
519 if self._sampler_iter is None:
520 self._reset()
→ 521 data = self._next_data()
522 self._num_yielded += 1
523 if self._dataset_kind == _DatasetKind.Iterable and \

~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py in _next_data(self)
559 def _next_data(self):
560 index = self._next_index() # may raise StopIteration
→ 561 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
562 if self._pin_memory:
563 data = _utils.pin_memory.pin_memory(data)

~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
—> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]

~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py in (.0)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
—> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]

in getitem(self, idx)
19 label = self.img_labels.iloc[idx, 1]
20 if self.transform:
—> 21 image = self.transform(image)
22 # if self.target_transform:
23 # #label = self.target_transform(label)

~/opt/anaconda3/lib/python3.8/site-packages/torchvision/transforms/transforms.py in call(self, pic)
95 Tensor: Converted image.
96 “”"
—> 97 return F.to_tensor(pic)
98
99 def repr(self):

~/opt/anaconda3/lib/python3.8/site-packages/torchvision/transforms/functional.py in to_tensor(pic)
100 “”"
101 if not(F_pil._is_pil_image(pic) or _is_numpy(pic)):
→ 102 raise TypeError(‘pic should be PIL Image or ndarray. Got {}’.format(type(pic)))
103
104 if _is_numpy(pic) and not _is_numpy_image(pic):

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