File size: 3,680 Bytes
7e93243
 
 
 
 
 
 
 
 
1b661c7
 
7e93243
 
 
 
432628d
cd6e155
 
 
 
 
 
 
 
 
 
 
 
67cda28
 
 
ca7dac7
67cda28
ca7dac7
998c6fe
 
 
 
72dc25c
 
 
 
 
998c6fe
5cd7415
998c6fe
 
 
 
 
 
 
76c88e2
 
 
 
76496e4
76c88e2
76496e4
7e93243
 
 
432628d
 
 
7e93243
 
 
7cf0aea
 
432628d
 
76c88e2
998c6fe
 
 
7e93243
150b753
ee170be
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
69
70
71
72
73
74
75
76
77
78
79
80
import torch, torchvision
from torchvision import transforms
import numpy as np
import gradio as gr
from PIL import Image
from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.image import show_cam_on_image
from custom_resnet import Net

model = Net('batch')
model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')), strict=False)

classes = ('plane', 'car', 'bird', 'cat', 'deer',
           'dog', 'frog', 'horse', 'ship', 'truck')

def inference(input_img, transparency = 0.5, target_layer_number = -1, num_top_classes = 5):
    """This function take input as an image and generate Grad Cam image of it.

    Args:
        input_img (_type_): Input image provided by user.
        transparency (float, optional): _description_. Defaults to 0.5.
        target_layer_number (int, optional): Output of layer which will be given to Grad Cam. Defaults to -1.
        num_top_classes (int, optional): To show number of classes to show in the output. Defaults to 5.

    Returns:
        top: Top Classes and Confidence level of the prediction
        visualization: Grad Cam output
    """
    # transform = transforms.ToTensor()
    transform = transforms.Compose([
                                    transforms.ToTensor(),
                                    transforms.Normalize((0.1307,), (0.3081,))
                                    ])
    org_img = input_img
    input_img = transform(input_img)
    # input_img = input_img
    input_img = input_img.unsqueeze(0)
    outputs = model(input_img)
    softmax = torch.nn.Softmax(dim=0)
    o = softmax(outputs.flatten())
    # exp_outputs = torch.exp(outputs.flatten())
    confidences = {classes[i]: float(o[i]) for i in range(10)}
    # confidences = {classes[i]: float(exp_outputs[i]) for i in range(10)}
    _, prediction = torch.max(outputs, 1)
    target_layers = [model.layer3_r3[target_layer_number]]
    cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
    grayscale_cam = cam(input_tensor=input_img, targets=None)
    grayscale_cam = grayscale_cam[0, :]
    img = input_img.squeeze(0)
    rgb_img = np.transpose(img, (1, 2, 0))
    rgb_img = rgb_img.numpy()
    visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)

    # Sort confidences dictionary in descending order of values and take top num_top_classes
    sorted_confidences = {k: v for k, v in sorted(confidences.items(), key=lambda item: item[1], reverse=True)}
    top_classes = list(sorted_confidences.keys())[:num_top_classes]
    top = dict((k,v) for k, v in sorted_confidences.items() if k in top_classes)

    return top, visualization

title = "CIFAR10 trained on ResNet18 Model with GradCAM"
description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
examples = [["airplane.png", 0.5, -1, 5],["bird.jpeg", 0.5, -1, 5], ["car.jpeg", 0.5, -1, 5], ["cat.png", 0.5, -1, 5],
            ["deer.jpeg", 0.5, -1, 6], ["dog.png", 0.5, -1, 7], ["frog.jpeg", 0.5, -1, 4], ["horse.png", 0.5, -1, 7],
            ["ship.png", 0.5, -1, 3], ["truck.jpeg", 0.5, -1, 8]]

demo = gr.Interface(
    inference, 
    inputs = [gr.Image(shape=(32, 32), label="Input Image"),
              gr.Slider(0, 1, value = 0.5, label="Opacity of GradCAM"), 
              gr.Slider(-2, -1, value = -2, step=1, label="Which Layer?"),
              gr.Slider(0, 10, value = 1, step=1, label="Number of Top Classes")], 
    outputs = [gr.Label(num_top_classes=10), gr.Image(shape=(32, 32), label="Output", style={"width": "128px", "height": "128px"})],
    title = title,
    description = description,
    examples = examples,
)

demo.launch()