title: Pytorch problem testing CNN: RuntimeError: shape '[64, 1]' is invalid for input of size 1920
body: <p>Testing network:</p>
  
  <pre><code> def test(args, model, device, test_loader):
             model.eval()
             total = 0
             test_loss = 0
             correct = 0
             with torch.no_grad():
             for batch_idx, batch in enumerate(test_loader):
                 data = batch['image']
                 target = batch['key_points']
                 data, target = data.to(device), target.to(device)
                 # data, target = Variable(data), Variable(target)
             # for data, target in test_loader:
             #     data, target = data.to(device), target.to(device)
             #     data, target = Variable(data), Variable(target)
                   data = data.unsqueeze(1).float() 
  
                   print('TESTING 1: Data and target shape: ', data.shape, ' 
                         ', target.shape)
                   output = model(data)
                   target = target.view(target.shape[0], -1)  
  
                   print('TESTING 2: Data and target shape: ', data.shape, ' 
                         ', target.shape)
                   # test_loss += F.nll_loss(output, target, 
                   reduction='sum').item()  # sum up batch loss orginal
                   test_loss = F.nll_loss(output, torch.max(target, 1)[1])
                    # test_loss += F.nll_loss(output, 
                    torch.max(target.float(), 1)[1], reduction='sum').item()
  
                     print('TESTING 3: Data and target shape: ', data.shape, 
                         ' ', target.shape)
                     # pred = output.max(1, keepdim=True)[1]  
                     pred = output.argmax(dim=1, keepdim=True)  # original
  
                     print('TESTING 4: Data and target shape: ', data.shape, 
                         ' ', target.shape)
  
                     correct += pred.eq(target.view_as(pred)).sum().item()   
                     print('TESTING last: Data and target shape: ', 
                     data.shape, ' ', target.shape)
  
                     test_loss /= len(test_loader.dataset)
                     print('\nTest set: Average loss: {:.4f}, Accuracy: 
                          {}/{} ({:.0f}%)\n'.format(
                           test_loss , correct, len(test_loader.dataset),
                           100. * correct / len(test_loader.dataset)))
  </code></pre>
  
  <p>I can run the training loop which is fine however when I try to run testing loop as well the training will only run 1 epoch and I get the following message.</p>
  
  <blockquote>
  <pre><code>TESTING 1: Data and target shape:  torch.Size([64, 1, 96, 96])  
  torch.Size([64, 15, 2]) TESTING 2: Data and target shape: 
  torch.Size([64, 1, 96, 96])   torch.Size([64, 30]) 
  TESTING 3: Data and target shape:  torch.Size([64, 1, 96, 96])  
  torch.Size([64, 30])
  TESTING 4: Data and target shape:  torch.Size([64, 1, 96, 96])  
   torch.Size([64, 30]) Traceback (most recent call last):   File
   "/home/keith/PycharmProjects/FacialLandMarks/WorkOut.py", line 468, in &lt;module&gt;
      main()   File "/home/keith/PycharmProjects/FacialLandMarks/WorkOut.py", line 463, in main
      test(args, model, device, test_loader)   File "/home/keith/PycharmProjects/FacialLandMarks/WorkOut.py", line 380, in test
      correct += pred.eq(target.view_as(pred)).sum().item()   
   RuntimeError: shape '[64, 1]' is invalid for input of size 1920
   Process finished with exit code 1
  </code></pre>
  </blockquote>
  
  <p>I have not been able to come across any helpful resources on-line or in a number of books I've purchased, on this specific problem, facial key point detection testing phase. I think problem is shown in error message(marked with comment) or with my data.  I am using a separate set of test images with training key points csv file.  As you can see I have printed out shape of data throughout the testing phase.
  Any help or useful links always appreciated. Thank you </p>
  
  <p>When I use pycharm debugger and view breakpoints when I get to the line of code below:</p>
  
  <pre><code>pred = output.max(1, keepdim=True)[1]
  </code></pre>
  
  <p>Then pred tensor changes to this:</p>
  
  <pre><code>tensor([[29],
          [29],
          [29],
          [29],
          [29],
          [29],
          [29],
          [29],
  
           ...
          [29],
          [29],
          [29],
          [29],
          [29],
          [29],
          [29]], device='cuda:0')
  </code></pre>