Getting runtime error: Shape [1,1,4] is invalid for input of size 12

bing: <p>Hi,<br>
I am trying to run inference on an image classification task for 4 images.<br>
I am getting</p>
<p><code>  File "inference.py", line 88, in accuracy     test_correct += np.sum(np.squeeze(pred.eq(target.data.view_as(pred)).cpu().numpy())) # noqa RuntimeError: shape '[1, 1, 4]' is invalid for input of size 12</code><br>
I am quite not sure where I am doing wrong,as the same code is working fine when I am running along with training loop for training set.<br>
Below is my code for testing -</p>
<pre><code class="lang-auto">def accuracy(model, loader, criterion, use_cuda=False):
    """Calculate Accuracy."""
    test_accuracy = 0.0
    test_loss = 0.0
    test_correct = 0.0
    test_total = 0.0
    model.eval()
    for batch_idx, (data, target) in enumerate(loader):
        # move to GPU
        if use_cuda:
            data, target = data.cuda(), target.cuda()
        # update the average validation loss
        output = model(data).squeeze()
        output = torch.unsqueeze(output, 0)
        pred = output.data.max(1, keepdim=True)[1]
        test_correct += np.sum(np.squeeze(pred.eq(target.data.view_as(pred)).cpu().numpy())) # noqa
        test_total += data.size(0)
    test_accuracy = 100. * (test_correct/test_total)
    print(test_accuracy)
    return None
</code></pre>

ptrblck: <p>Could you check the shapes of <code>data</code>, <code>output</code>, <code>pred</code> and <code>target</code>?<br>
Based on the error message I guess you might accidentally view or reshape the data or an activation in a wrong way, which could result in such a shape mismatch (e.g. by flattening the activation and removing the batch dimension).</p>

bing: <p>Thank You.<br>
Below are the shapes of <code>data, output, pred, target</code></p>
<pre><code class="lang-auto">data shape: torch.Size([12, 3, 600, 600])
target shape: torch.Size([12])
output1:  torch.Size([12, 4]) # Before and after squeezing
output2:  torch.Size([1, 12, 4])
Prediction:  torch.Size([1, 1, 4])
</code></pre>
<p>I think, I have removed the batch dimension somewhere, I am now running with using the batch size 1 but how to make it work for any batch size</p>

praveen_91: <aside class="quote no-group" data-username="bing" data-post="1" data-topic="101629">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/839c29/48.png" class="avatar"> bing:</div>
<blockquote>
<p><code>pred = output.data.max(1, keepdim=True)[1]</code></p>
</blockquote>
</aside>
<p>Could you try and see if this works, considering the batch size to be 18</p>
<pre><code class="lang-auto">target = torch.ones((16,12))
output1 = torch.ones((16,12,4))
output2 = torch.ones((16,1,12,4))
pred = output1.data.max(-1, keepdim=False)[1]
</code></pre>
<p>The shapes after doing the above is</p>
<pre><code class="lang-auto">target:  torch.Size([16, 12])
output1 : torch.Size([16, 12, 4])
output2:  torch.Size([16, 1, 12, 4])
Predicion:  torch.Size([16, 12])
</code></pre>
<p>Now your targets and preds are in the same shape. Does this help?</p>

ptrblck: <p>You are currently calculating the <code>max</code> in the batch dimension, which seems to be wrong as you would usually compute the predictions by taking the <code>argmax</code> in the class dimension.<br>
This should work:</p>
<pre><code class="lang-python">output = model(data).squeeze() # output.shape == [12, 4]
preds = torch.argmax(output, dim=1) # preds.shape == [12]
</code></pre>
<p>I don’t think you need to <code>unsqueeze</code> and <code>squeeze</code> the tensors to calculate the accuracy and for a multi-class classification your target should also have the shape <code>[batch_size==12]</code>.</p>
<p>PS: don’t use the <code>.data</code> attribute as it might yield unwanted side effects.</p>