GradCAM visualization and error

Maryam_Darei: <p>I want to compare the predicted label with true label and if the predicted label was not mach, plot GradCAM image and original image in beside each other. I developed a pice of code but I received an error.</p>
<p>It is my code:</p>
<p>for images, labels in test_loader:<br>
images = images.to(device)<br>
labels = labels.to(device)<br>
outputs = model(images)<br>
_, predicted = torch.max(outputs.data, 1)<br>
print(labels)<br>
print(predicted)<br>
for i in range(len(predicted)):<br>
if predicted[i] != labels[i]:<br>
resize_size =(227,227)<br>
directory = ‘/content/drive/MyDrive/Herbarium Data-2019/Data/sample/val’<br>
files = os.listdir(directory)<br>
for foldername in files:<br>
img_files = os.listdir(‘/content/drive/MyDrive/Herbarium Data-2019/Data/sample/val/’+foldername)<br>
for filename in img_files:<br>
input_image = Image.open(‘/content/drive/MyDrive/Herbarium Data-2019/Data/sample/val/’+foldername +‘/’ +filename)<br>
preprocess = transforms.Compose(<br>
[<br>
resize_to,<br>
transforms.ToTensor(),<br>
transforms.Normalize(<br>
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]<br>
),  # IMPORTANT: normalize for pre-trained models<br>
]<br>
)<br>
input_tensor = preprocess(input_image)<br>
print(“Input Tensor Shape:”, input_tensor.shape)</p>
<pre><code>            input_batch = input_tensor.unsqueeze(0)

            targets = [ClassifierOutputTarget(6)]

            grayscale_cam = cam(input_batch, targets=None,aug_smooth=True)

            grayscale_cam = grayscale_cam[0, :]


            img=np.array(input_image.resize(resize_size),np.float32)
            img = img.reshape(img.shape[1],img.shape[0],img.shape[2])
            #print("img shape",img.shape,img.max())
            img = img/255
            visualization = show_cam_on_image(img, grayscale_cam, use_rgb=True)
            #cam_images = [show_cam_on_image(img, grayscale, use_rgb=True) for img, grayscale in zip(input_image, grayscale_cam)]
            visualization = Image.fromarray(visualization)
            out_file_name ="/content/drive/MyDrive/Herbarium Data-2019/test222/" +modelname+ "_"+gradvariantname+ "_" + foldername_to_class[os.path.basename(foldername)] + "_" +os.path.basename(filename)
            visualization.save(out_file_name)
            #print("Visualization saved- now trying to show (GUI mode)")
            im = Image.open(out_file_name)
            im.show()

            image = np.transpose(images.cpu().squeeze().numpy(), (1, 2, 0))
            plt.subplot(1, 2, 1)
            plt.imshow(image)
            plt.title(f'True label: {labels}, Pred label: {predicted}')
            plt.axis('off')
            plt.subplot(1, 2, 2)
            plt.imshow(cam, cmap='jet')
            plt.axis('off')
            plt.show()
</code></pre>
<p>And it is an error:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/7/e7e60ab475af67f23f649d1d9d21b686c45b3cc1.png" data-download-href="https://discuss.pytorch.org/uploads/default/e7e60ab475af67f23f649d1d9d21b686c45b3cc1" title="Screen Shot 2023-02-21 at 6.55.25 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/7/e7e60ab475af67f23f649d1d9d21b686c45b3cc1_2_690x347.png" alt="Screen Shot 2023-02-21 at 6.55.25 PM" data-base62-sha1="x5t9NjPSmGbRmfIrmk7cygQVBlv" width="690" height="347" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/7/e7e60ab475af67f23f649d1d9d21b686c45b3cc1_2_690x347.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/7/e7e60ab475af67f23f649d1d9d21b686c45b3cc1_2_1035x520.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/7/e7e60ab475af67f23f649d1d9d21b686c45b3cc1_2_1380x694.png 2x" data-dominant-color="F6F6F8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2023-02-21 at 6.55.25 PM</span><span class="informations">1718×866 105 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Based on the error the <code>np.transpose</code> call fails as the specified <code>axes</code> do not match the number of dimensions in the numpy array as seen here:</p>
<pre><code class="lang-python"># works
images = torch.randn(2, 3, 4)
image = np.transpose(images.cpu().squeeze().numpy(), (1, 2, 0))

# fails
images = torch.randn(2, 3, 4, 5)
image = np.transpose(images.cpu().squeeze().numpy(), (1, 2, 0))
# ValueError: axes don't match array
</code></pre>
<p>Check the shape of <code>images</code> and make sure it has 3 dimensions or fix the <code>np.transpose</code> call.</p>