File size: 2,653 Bytes
4db4d66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image

import matplotlib.pyplot as plt


def generate_gradcam(model, target_layers, images, labels, rgb_imgs):
    results = []
    cam = GradCAM(model=model, target_layers=target_layers, use_cuda=True)

    for image, label, np_image in zip(images, labels, rgb_imgs):
        targets = [ClassifierOutputTarget(label.item())]

        # You can also pass aug_smooth=True and eigen_smooth=True, to apply smoothing.
        grayscale_cam = cam(
            input_tensor=image.unsqueeze(0), targets=targets, aug_smooth=True
        )

        # In this example grayscale_cam has only one image in the batch:
        grayscale_cam = grayscale_cam[0, :]
        visualization = show_cam_on_image(
            np_image / np_image.max(), grayscale_cam, use_rgb=True
        )
        results.append(visualization)
    return results


def visualize_gradcam(misimgs, mistgts, mispreds, classes):
    fig, axes = plt.subplots(len(misimgs) // 2, 2)
    fig.tight_layout()
    for ax, img, tgt, pred in zip(axes.ravel(), misimgs, mistgts, mispreds):
        ax.imshow(img)
        ax.set_title(f"{classes[tgt]} | {classes[pred]}")
        ax.grid(False)
        ax.set_axis_off()
    plt.show()

def plot_gradcam(model, data, classes, target_layers, number_of_samples, inv_normalize=None, targets=None, transparency = 0.60, figsize=(10,10), rows=2, cols=5):
    
    fig = plt.figure(figsize=figsize)
    
    cam = GradCAM(model=model, target_layers=target_layers, use_cuda=True)
    for i in range(number_of_samples):
            plt.subplot(rows, cols, i + 1)
            input_tensor = data[i][0]
    
            # Get the activations of the layer for the images
            grayscale_cam = cam(input_tensor=input_tensor, targets=targets)
            grayscale_cam = grayscale_cam[0, :]
    
            # Get back the original image
            img = input_tensor.squeeze(0).to('cpu')
            if inv_normalize is not None:
                img = inv_normalize(img)
            rgb_img = np.transpose(img, (1, 2, 0))
            rgb_img = rgb_img.numpy()
    
            # Mix the activations on the original image
            visualization = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True, image_weight=transparency)
    
            # Display the images on the plot
            plt.imshow(visualization)
            plt.title(f"Label: {classes[data[i][1].item()]} \n Prediction: {classes[data[i][2].item()]}")
            plt.xticks([])
            plt.yticks([])