Viewing misclassified image predictions

SSI: <p>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 ;</p>
<p>incorrect_examples = []</p>
<p>model.eval()<br>
for data,target in val_loader:<br>
if train_on_gpu:<br>
data , target = data.cuda(), target.cuda()<br>
output = model(data)<br>
_, pred = torch.max(output,1)<br>
idxs_mask = (pred == target).view(-1)<br>
incorrect_examples.append(data[idxs_mask].numpy())</p>
<h1>display 10 images</h1>
<p>fig = plt.figure(figsize=(20, 8))</p>
<p>for idx in np.arange(10):<br>
ax = fig.add_subplot(2, 10/2, idx+1, xticks=[], yticks=[])<br>
<span class="hashtag">#std</span> = np.array([0.229, 0.224, 0.225])<br>
<span class="hashtag">#mean</span> = np.array([0.485, 0.456, 0.406])<br>
img = incorrect_examples[idx][idx]<br>
img = img/2 + 0.5<br>
img = np.clip(img, 0, 1)<br>
imshow(img)<br>
ax.set_title(f"{classes[pred[idx]]}: x%\n (label: {classes[target[idx]]})",<br>
color=(“green” if pred[idx]==target[idx].item() else “red”))</p>
<p>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</p>

Pranavan_Theivendira: <p>Hi,<br>
Indices of incorrect examples are wrong.</p>
<pre><code class="lang-auto">idxs_mask = (pred == target).view(-1)
</code></pre>
<p>Replace this line with the following code.</p>
<pre><code class="lang-auto">idxs_mask = ((pred == target) == False).nonzero()
</code></pre>
<p>Thanks<br>
regards<br>
Pranavan</p>

SSI: <p>Thank you for your reply.</p>
<p>When I do your suggestion I get this</p>
<p>tensor([], size=(0, 1), dtype=torch.int64)</p>
<p>it’s empty.</p>

Pranavan_Theivendira: <p>Hi,</p>
<p>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</p>
<pre><code class="lang-auto">for data,target in val_loader:
</code></pre>
<p>You may not get any misclassified images. For your list, you have to append the tensors which are non -empty.</p>
<p>Thanks</p>

SSI: <p>Agh Yes!<br>
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.</p>
<p>Thank you for your help!</p>

Nishant_Bhansali: <h3>
<a name="code-to-view-misclassified-images-and-their-corresponding-label-and-predicted-class-1" class="anchor" href="#code-to-view-misclassified-images-and-their-corresponding-label-and-predicted-class-1"></a>Code to view misclassified images and their corresponding label and predicted class</h3>
<pre><code class="lang-auto">incorrect_examples = []
incorrect_labels = []
incorrect_pred = []
model.eval()
for data,target in test_loader:

  data , target = data.to(device), target.to(device)
  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_examples.append(data[idxs_mask].squeeze().cpu().numpy())
    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
</code></pre>