Viewing misclassified image predictions

I am trying to view the images that my model misclassified without using tensorboard. So far, I have been unable to just get only the misclassified images. Currently this is what I have ;

incorrect_examples = []

for data,target in val_loader:
if train_on_gpu:
data , target = data.cuda(), target.cuda()
output = model(data)
_, pred = torch.max(output,1)
idxs_mask = (pred == target).view(-1)

display 10 images

fig = plt.figure(figsize=(20, 8))

for idx in np.arange(10):
ax = fig.add_subplot(2, 10/2, idx+1, xticks=[], yticks=[])
#std = np.array([0.229, 0.224, 0.225])
#mean = np.array([0.485, 0.456, 0.406])
img = incorrect_examples[idx][idx]
img = img/2 + 0.5
img = np.clip(img, 0, 1)
ax.set_title(f"{classes[pred[idx]]}: x%\n (label: {classes[target[idx]]})",
color=(“green” if pred[idx]==target[idx].item() else “red”))

and these just give me a batch of the predicted images and not the misclassified ones. I am new to pytorch so it is possible that I am not doing the right thing. Please help

Indices of incorrect examples are wrong.

idxs_mask = (pred == target).view(-1)

Replace this line with the following code.

idxs_mask = ((pred == target) == False).nonzero()


Thank you for your reply.

When I do your suggestion I get this

tensor([], size=(0, 1), dtype=torch.int64)

it’s empty.


Yes. This is an empty tensor. Could you please tell me which tensor is this? I think that you are printing idxs_mask. In some iterations of the loop

for data,target in val_loader:

You may not get any misclassified images. For your list, you have to append the tensors which are non -empty.



Agh Yes!
I printed the idxs_mask to check and see if it was capturing the misclassified images. And yes you’re right, it was empty because I was not getting misclassified images in the last loop but appending the results from all the loops captured what I wanted.

Thank you for your help!

Code to view misclassified images and their corresponding label and predicted class

incorrect_examples = []
incorrect_labels = []
incorrect_pred = []
for data,target in test_loader:

  data , target =,
  output = model(data) # shape = torch.Size([batch_size, 10])
  pred = output.argmax(dim=1, keepdim=True) #pred will be a 2d tensor of shape [batch_size,1]
  idxs_mask = ((pred == target.view_as(pred))==False).view(-1)
  if idxs_mask.numel(): #if index masks is non-empty append the correspoding data value in incorrect examples
    incorrect_labels.append(target[idxs_mask].cpu().numpy()) #the corresponding target to the misclassified image
    incorrect_pred.append(pred[idxs_mask].squeeze().cpu().numpy()) #the corresponiding predicted class of the misclassified image