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

Mona_Jalal: <p>Here’s my evaluation cell:</p>
<pre><code>start_time = time.time()

with torch.no_grad():

    best_network = Network()
    best_network.cuda()
    best_network.load_state_dict(torch.load('../moth_landmarks.pth')) 
    best_network.eval()
    
    batch = next(iter(train_loader))
    images, landmarks = batch['image'], batch['landmarks']
    #images = images.unsqueeze_(1)

    images = torch.cat((images,images,images),1)
    images = images.cuda()

    norm_image = transforms.Normalize(0.3812, 0.1123) 
    for image in images:
        image = image.float()
        ##image = to_tensor(image) #TypeError: pic should be PIL Image or ndarray. Got &lt;class 'torch.Tensor'&gt;
        image = norm_image(image)
    landmarks = (landmarks + 0.5) * 224
    
    ##[8, 600, 800] --&gt; [8,3,600,800]
    images = images.unsqueeze(1)
    images = torch.cat((images, images, images), 1)

    predictions = (best_network(images).cpu() + 0.5) * 224
    predictions = predictions.view(-1,4,2)
    
    plt.figure(figsize=(10,40))
    
    for img_num in range(8):
        plt.subplot(8,1,img_num+1)
        plt.imshow(images[img_num].cpu().numpy().transpose(1,2,0).squeeze(), cmap='gray')
        plt.scatter(predictions[img_num,:,0], predictions[img_num,:,1], c = 'r')
        plt.scatter(landmarks[img_num,:,0], landmarks[img_num,:,1], c = 'g')

print('Total number of test images: {}'.format(len(test_dataset)))

end_time = time.time()
print("Elapsed Time : {}".format(end_time - start_time)) 
</code></pre>
<p>How should I fix the following error?</p>
<pre><code>---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-59-e4aa0ace8c75&gt; in &lt;module&gt;
     19         image = image.float()
     20         ##image = to_tensor(image) #TypeError: pic should be PIL Image or ndarray. Got &lt;class 'torch.Tensor'&gt;
---&gt; 21         image = norm_image(image)
     22     landmarks = (landmarks + 0.5) * 224
     23 

~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py in __call__(self, tensor)
    210             Tensor: Normalized Tensor image.
    211         """
--&gt; 212         return F.normalize(tensor, self.mean, self.std, self.inplace)
    213 
    214     def __repr__(self):

~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/functional.py in normalize(tensor, mean, std, inplace)
    282     if tensor.ndimension() != 3:
    283         raise ValueError('Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = '
--&gt; 284                          '{}.'.format(tensor.size()))
    285 
    286     if not inplace:

ValueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([1800, 800]).
</code></pre>
<p>If I remove the following line:</p>
<pre><code>images = torch.cat((images,images,images),1)
</code></pre>
<p>I will get this new error:</p>
<pre><code>---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-61-3e892b69015c&gt; in &lt;module&gt;
     19         image = image.float()
     20         ##image = to_tensor(image) #TypeError: pic should be PIL Image or ndarray. Got &lt;class 'torch.Tensor'&gt;
---&gt; 21         image = norm_image(image)
     22     landmarks = (landmarks + 0.5) * 224
     23 

~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/transforms.py in __call__(self, tensor)
    210             Tensor: Normalized Tensor image.
    211         """
--&gt; 212         return F.normalize(tensor, self.mean, self.std, self.inplace)
    213 
    214     def __repr__(self):

~/anaconda3/lib/python3.7/site-packages/torchvision/transforms/functional.py in normalize(tensor, mean, std, inplace)
    282     if tensor.ndimension() != 3:
    283         raise ValueError('Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = '
--&gt; 284                          '{}.'.format(tensor.size()))
    285 
    286     if not inplace:

ValueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([600, 800]).</code></pre>

Mona_Jalal: <p>The following is correct:</p>
<pre><code class="lang-auto">start_time = time.time()

with torch.no_grad():

    best_network = Network()
    best_network.cuda()
    best_network.load_state_dict(torch.load('../moth_landmarks.pth')) 
    best_network.eval()
    
    batch = next(iter(train_loader))
    images, landmarks = batch['image'], batch['landmarks']
    landmarks = landmarks.view(landmarks.size(0),-1).cuda()

    print(landmarks.shape)
    for i in range(8):
        if(i%2==0):
            landmarks[:,i] = landmarks[:,i]/800
        else:
            landmarks[:,i] = landmarks[:,i]/600
    landmarks [landmarks != landmarks] = 0
    #landmarks = landmarks.unsqueeze_(0)

    images = images.cuda()
    
    print('*, ', landmarks.shape)

    norm_image = transforms.Normalize(0.3812, 0.1123) 
    print('images shape: ', images.shape)
    for image in images:
        
        image = image.unsqueeze_(1)

        #images = torch.cat((images,images,images),1)
        image = image.float()
        ##image = to_tensor(image) #TypeError: pic should be PIL Image or ndarray. Got &lt;class 'torch.Tensor'&gt;
        image = norm_image(image)
    
    print('max: ', torch.max(landmarks))
    print('min: ', torch.min(landmarks))

    ##landmarks = (landmarks + 0.5) * 224 #?? chera?
    print('**')
    print(images.shape, landmarks.shape)
    ##[8, 600, 800] --&gt; [8,3,600,800]
    images = images.unsqueeze(1)
    images = torch.cat((images, images, images), 1)

    #predictions = (best_network(images).cpu() + 0.5) * 224
    predictions = best_network(images).cpu()  

    print('****', predictions.shape)
    for i in range(8):
        if(i%2==0):
            predictions[:,i] = predictions[:,i]*800
        else:
            predictions[:,i] = predictions[:,i]*600

    predictions = predictions.view(-1,4,2)
    print('****', predictions.shape)
    
  
    
    for i in range(8):
        if(i%2==0):
            landmarks[:,i] = landmarks[:,i]*800
        else:
            landmarks[:,i] = landmarks[:,i]*600

    landmarks = landmarks.view(-1,4,2)
    plt.figure(figsize=(10,40))
    landmarks = landmarks.cpu()
    print(type(landmarks), landmarks.shape)
    for img_num in range(8):
        plt.subplot(8,1,img_num+1)
        plt.imshow(images[img_num].cpu().numpy().transpose(1,2,0).squeeze(), cmap='gray')
        plt.scatter(predictions[img_num,:,0], predictions[img_num,:,1], c = 'r')
        plt.scatter(landmarks[img_num,:,0], landmarks[img_num,:,1], c = 'g')

print('Total number of test images: {}'.format(len(test_dataset)))

end_time = time.time()
print("Elapsed Time : {}".format(end_time - start_time))
</code></pre>