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

akcaymert: <p>import torch<br>
import torchvision<br>
from torch.utils.data import Dataset,DataLoader<br>
from sklearn import preprocessing</p>
<p>class youtubeFacial(Dataset):</p>
<pre><code>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
</code></pre>
<p>and<br>
from torchvision import transforms, utils<br>
transform = transforms.Compose([<br>
transforms.Normalize((0.5), (0.5)),<br>
transforms.ToTensor()])<br>
dataset = youtubeFacial(transform)<br>
train_loader = DataLoader(dataset = dataset,batch_size = 4,shuffle = True)</p>
<p>but I am encountering this problem in the title during at the  training time. how can i solve this problem ?</p>

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

akcaymert: <p>Not working , i cant understand Could there be a problem with the data ?</p>

ptrblck: <p>Could you post the complete stack trace, please?</p>

akcaymert: <p>Sure , i uploaded to link<a href="https://prnt.sc/u992eh" rel="nofollow noopener">Stack Trace</a> and i have tried  return value of <strong>getitem</strong> as a dict dtype like pytorch DataLoader documentation</p>

ptrblck: <p>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. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<p>I missed that you are already transforming the data to tensors, which would throw this error:</p>
<pre><code class="lang-python">self.X = torch.Tensor(X)
</code></pre>
<p><code>transforms.ToTensor()</code> should be applied on <code>PIL.Image</code>s or numpy arrays. Since you already are dealing with tensors, you could skip this transformation.</p>

akcaymert: <p>oh it was completely careless of me , thanks ptrblck <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=9" title=":smile:" class="emoji" alt=":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]).<br>
The values ​​I give as pictures and labels are as follows; torch.Size([7049, 1, 96, 96])<br>
torch.Size([7049, 30]) and My model output is 30 feature</p>
<p><strong>Stack trace ;</strong><br>
ValueError                                Traceback (most recent call last)<br>
 in <br>
----&gt; 1 X,Y = dataset[0].item</p>
<p> in <strong>getitem</strong>(self, index)<br>
25     def <strong>getitem</strong>(self,index):<br>
26         X_transform = self.transform(self.X[index])<br>
—&gt; 27         Y_transform = self.transform(self.Y[index])<br>
28         return self.X_transform,self.Y_transform<br>
29</p>
<p>/opt/conda/lib/python3.7/site-packages/torchvision/transforms/transforms.py in <strong>call</strong>(self, img)<br>
59     def <strong>call</strong>(self, img):<br>
60         for t in self.transforms:<br>
—&gt; 61             img = t(img)<br>
62         return img<br>
63</p>
<p>/opt/conda/lib/python3.7/site-packages/torchvision/transforms/transforms.py in <strong>call</strong>(self, tensor)<br>
164             Tensor: Normalized Tensor image.<br>
165         “”"<br>
–&gt; 166         return F.normalize(tensor, self.mean, self.std, self.inplace)<br>
167<br>
168     def <strong>repr</strong>(self):</p>
<p>/opt/conda/lib/python3.7/site-packages/torchvision/transforms/functional.py in normalize(tensor, mean, std, inplace)<br>
192     if tensor.ndimension() != 3:<br>
193         raise ValueError('Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = ’<br>
–&gt; 194                          ‘{}.’.format(tensor.size()))<br>
195<br>
196     if not inplace:</p>
<p>ValueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([30]).</p>

ptrblck: <p>As the error message explains, <code>Normalize</code> expects an image tensor in the shape <code>[channels, height, width]</code> as its input, while you are trying to pass the target tensor to it, which won’t work.<br>
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.</p>

akcaymert: <p>You are right, thanks for  all peter</p>

Abhishek_Maheshwari: <p>I am having the same issue. I am new to Python and Pytorch.<br>
My code has three parts:</p>
<ul>
<li>first part is where I define the class function;</li>
<li>third and final part is using the googlenet model</li>
<li>following is the second part…followed by the error message:</li>
</ul>
<p>import torch<br>
import torchvision<br>
import torch.nn as nn # All neural network modules, nn.Linear, nn.Conv2d, BatchNorm, Loss functions<br>
import torch.optim as optim # for all optimization algorithms, SGD, Adam, etc.<br>
import torchvision.transforms as transforms <span class="hashtag">#transformations</span> we can perform on our dataset<br>
from torch.utils.data import DataLoader <span class="hashtag">#Gives</span> easier dataset management and creates mini batches</p>
<p><span class="hashtag">#Set</span> device<br>
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)</p>
<p><span class="hashtag">#hyperparameters</span><br>
in_channel = 3<br>
num_classes = 10<br>
learning_rate = 1e-3<br>
batch_size = 16<br>
num_epochs = 1<br>
img_size = 224</p>
<p>training_data = Abhidataset(<br>
csv_file = ‘Book1.csv’,<br>
root_dir = ‘dataset_1’,<br>
transform = transforms.ToTensor()<br>
)</p>
<p><span class="hashtag">#Load</span> Data</p>
<p>train_set, test_set = torch.utils.data.random_split(training_data,[16,5])<br>
train_loader = DataLoader(training_data, batch_size = batch_size, shuffle = True, collate_fn=batch_size)<br>
test_loader = DataLoader(training_data, batch_size = batch_size, shuffle = True, collate_fn=batch_size)</p>
<h2>
<a name="error-message-1" class="anchor" href="#error-message-1"></a>ERROR MESSAGE:</h2>
<p>TypeError                                 Traceback (most recent call last)<br>
 in <br>
11     losses=[]<br>
12<br>
—&gt; 13     for batch_idx, (data, targets) in enumerate(train_loader):<br>
14         <span class="hashtag">#Get</span> data to cuda if possible<br>
15         data = data.to(device=device)</p>
<p>~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py in <strong>next</strong>(self)<br>
519             if self._sampler_iter is None:<br>
520                 self._reset()<br>
 → 521             data = self._next_data()<br>
522             self._num_yielded += 1<br>
523             if self._dataset_kind == _DatasetKind.Iterable and \</p>
<p>~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py in _next_data(self)<br>
559     def _next_data(self):<br>
560         index = self._next_index()  # may raise StopIteration<br>
 → 561         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
562         if self._pin_memory:<br>
563             data = _utils.pin_memory.pin_memory(data)</p>
<p>~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)<br>
42     def fetch(self, possibly_batched_index):<br>
43         if self.auto_collation:<br>
—&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]<br>
45         else:<br>
46             data = self.dataset[possibly_batched_index]</p>
<p>~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py in (.0)<br>
42     def fetch(self, possibly_batched_index):<br>
43         if self.auto_collation:<br>
—&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]<br>
45         else:<br>
46             data = self.dataset[possibly_batched_index]</p>
<p> in <strong>getitem</strong>(self, idx)<br>
19         label = self.img_labels.iloc[idx, 1]<br>
20         if self.transform:<br>
—&gt; 21             image = self.transform(image)<br>
22 #        if self.target_transform:<br>
23 #       <span class="hashtag">#label</span> = self.target_transform(label)</p>
<p>~/opt/anaconda3/lib/python3.8/site-packages/torchvision/transforms/transforms.py in <strong>call</strong>(self, pic)<br>
95             Tensor: Converted image.<br>
96         “”"<br>
—&gt; 97         return F.to_tensor(pic)<br>
98<br>
99     def <strong>repr</strong>(self):</p>
<p>~/opt/anaconda3/lib/python3.8/site-packages/torchvision/transforms/functional.py in to_tensor(pic)<br>
100     “”"<br>
101     if not(F_pil._is_pil_image(pic) or _is_numpy(pic)):<br>
 → 102         raise TypeError(‘pic should be PIL Image or ndarray. Got {}’.format(type(pic)))<br>
103<br>
104     if _is_numpy(pic) and not _is_numpy_image(pic):</p>
<p>TypeError: pic should be PIL Image or ndarray. Got &lt;class ‘torch.Tensor’&gt;</p>