Hi,
I am trying to run inference on an image classification task for 4 images.
I am getting
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
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.
Below is my code for testing -
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
Could you check the shapes of data, output, pred and target?
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).
You are currently calculating the max in the batch dimension, which seems to be wrong as you would usually compute the predictions by taking the argmax in the class dimension.
This should work:
I don’t think you need to unsqueeze and squeeze the tensors to calculate the accuracy and for a multi-class classification your target should also have the shape [batch_size==12].
PS: don’t use the .data attribute as it might yield unwanted side effects.