File size: 5,756 Bytes
011a617
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db44e50
011a617
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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 torch.utils.data import DataLoader
import itertools
import matplotlib.pyplot as plt

from custom_resnet import Custom_ResNet
import utils as utils

model = Custom_ResNet()
model.load_state_dict(torch.load("results/custom_resnet_trained.pth", map_location=torch.device('cpu')), strict=False)
model.eval()

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

cifar_valid = utils.Cifar10SearchDataset('.', train=False, download=True, transform=utils.augmentation_custom_resnet('Valid'))

inv_normalize = transforms.Normalize(
    mean=[-0.50/0.23, -0.50/0.23, -0.50/0.23],
    std=[1/0.23, 1/0.23, 1/0.23]
)

def inference(wants_gradcam, n_gradcam, target_layer_number, transparency, wants_misclassified, n_misclassified, input_img = None, n_top_classes=10):
    
    if wants_gradcam: 
      
      outputs_inference_gc = []
      cifar_valid_loader = DataLoader(cifar_valid, batch_size=1, shuffle = True)
      count_gradcam = 1

      for data, target in cifar_valid_loader:

        data, target = data.to('cpu'), target.to('cpu')
        target_layers = [model.layer2[target_layer_number]]

        cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
        grayscale_cam = cam(input_tensor=data, targets=None)
        grayscale_cam = grayscale_cam[0, :]

        org_img = inv_normalize(data).squeeze(0).numpy()
        org_img = np.transpose(org_img, (1, 2, 0))
        visualization = np.array(show_cam_on_image(org_img, grayscale_cam, use_rgb=True, image_weight=transparency))
        outputs_inference_gc.append(visualization)

        count_gradcam += 1
        if count_gradcam > n_gradcam:
          break 
    else:
      outputs_inference_gc = None

    if wants_misclassified: 
      outputs_inference_mis = []

      cifar_valid_loader = DataLoader(cifar_valid, batch_size=1, shuffle = True)
      count_mis = 1

      for data, target in cifar_valid_loader:

        data, target = data.to('cpu'), target.to('cpu')

        outputs = model(data)
        softmax = torch.nn.Softmax(dim=0)
        o = softmax(outputs.flatten())
        confidences = {classes[i]: float(o[i]) for i in range(10)}
        _, prediction = torch.max(outputs, 1)
        
        if target.numpy()[0] != prediction.numpy()[0]: 
          
            count_mis += 1

            org_img = inv_normalize(data).squeeze(0).numpy()
            org_img = np.transpose(org_img, (1, 2, 0))
            
            fig = plt.figure()
            fig.add_subplot(111)

            plt.imshow(org_img)
            plt.title(f'Target: {classes[target.numpy()[0]]}\nPred: {classes[prediction.numpy()[0]]}')
            plt.axis('off')
            
            fig.canvas.draw()

            fig_img = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
            fig_img = fig_img.reshape(fig.canvas.get_width_height()[::-1] + (3,))

            plt.close(fig)

            outputs_inference_mis.append(fig_img)

        if count_mis > n_misclassified:
            break 

    else:
      outputs_inference_mis = None

    if input_img is not None:
        transform=utils.augmentation_custom_resnet('Valid')
        org_img = input_img
        input_img = transform(image=input_img)
        input_img = input_img['image'].unsqueeze(0)
        outputs = model(input_img)
        softmax = torch.nn.Softmax(dim=0)
        o = softmax(outputs.flatten())
        confidences = {classes[i]: float(o[i]) for i in range(10)}
        _, prediction = torch.max(outputs, 1)

        confidences = {k: v for k, v in sorted(confidences.items(), key=lambda item: item[1], reverse=True)}
        confidences = dict(itertools.islice(confidences.items(), n_top_classes))
    else:
      confidences = None
    

    return outputs_inference_gc, outputs_inference_mis, confidences

    
title = "CIFAR10 trained on Custom ResNet Model with GradCAM"
description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
examples = [[None, None, None, None, None, None, 'examples/test_'+str(i)+'.jpg', None] for i in range(10)]

demo = gr.Interface(inference, 
                    inputs = [gr.Checkbox(False, label='Do you want to see GradCAM outputs?'),
                              gr.Slider(0, 10, value = 0, step=1, label="How many?"),
                              gr.Slider(-2, -1, value = -2, step=1, label="Which target layer?"), 
                              gr.Slider(0, 1, value = 0, label="Opacity of GradCAM"), 
                              gr.Checkbox(False, label='Do you want to see misclassified images?'),
                              gr.Slider(0, 10, value = 0, step=1, label="How many?"),
                              gr.Image(shape=(32, 32), label="Input image"), 
                              gr.Slider(0, 10, value = 0, step=1, label="How many top classes you want to see?")
                              ], 
                    outputs = [
                              gr.Gallery(label="GradCAM Outputs", show_label=True, elem_id="gallery").style(columns=[2], rows=[2], object_fit="contain", height="auto"), 
                              gr.Gallery(label="Misclassified Images", show_label=True, elem_id="gallery").style(columns=[2], rows=[2], object_fit="contain", height="auto"), 
                              gr.Label(num_top_classes=None)
                              ],
                    title = title, 
                    description = description, 
                    examples = examples
                    )
demo.launch()