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 = []
model.eval()
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)
incorrect_examples.append(data[idxs_mask].numpy())
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
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.
Code to view misclassified images and their corresponding label and predicted class
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