File size: 11,212 Bytes
1d30310
 
 
 
be5cc68
1d30310
be5cc68
 
9c7b15b
be5cc68
 
a6d636f
be5cc68
 
 
 
1d30310
be5cc68
1d30310
be5cc68
 
 
 
 
 
 
 
 
1d30310
 
 
be5cc68
 
 
 
 
1d30310
be5cc68
 
 
 
 
 
 
1d30310
 
 
be5cc68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa249b8
be5cc68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f348605
be5cc68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa249b8
be5cc68
 
fa249b8
be5cc68
 
 
 
fa249b8
 
be5cc68
 
 
 
fa249b8
be5cc68
 
 
 
 
fa249b8
 
be5cc68
 
 
 
 
fa249b8
 
 
be5cc68
1d30310
be5cc68
 
1d30310
be5cc68
fa249b8
be5cc68
 
 
 
 
fa249b8
be5cc68
 
 
 
fa249b8
 
 
be5cc68
 
 
 
 
 
 
 
 
 
1d30310
be5cc68
 
1d30310
be5cc68
fa249b8
be5cc68
 
 
 
 
 
 
 
fa249b8
 
 
 
be5cc68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0a31ac
4059fc9
be5cc68
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import numpy as np
import gradio as gr
from PIL import Image
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 torch
from torchvision import datasets, transforms
from custom_resnet import CustomResNet
import random


model = CustomResNet()
model.load_state_dict(torch.load('CustomResNet.pth', map_location=torch.device('cpu')), strict=False)
model.eval()

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


def inference(input_img, input_slider_grad_or_not, transparency = 0.5, target_layer_number = 3, topk = 3):
    mean=[0.49139968, 0.48215827, 0.44653124]
    std=[0.24703233, 0.24348505, 0.26158768]
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean, std)
    ])
    orginal_img = input_img
    input_img = transform(input_img)
    input_img = input_img.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)}
    if input_slider_grad_or_not == "No":
        return confidences, orginal_img
    _, prediction = torch.max(outputs, 1)
    target_layers = [model.layer_3[-1]]
    if target_layer_number == 1:
        target_layers = [model.layer_1[-1]]
    if target_layer_number == 2:
        target_layers = [model.layer_2[-1]]
    if target_layer_number == 3:
        target_layers = [model.layer_3[-1]]
    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, :]
    visualization = show_cam_on_image(orginal_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)

    return confidences, visualization


def show_gradcam_images(n, a, b):
    images = [
        ('examples/car.jpg', 'car'), 
        ('examples/cat.jpg', 'cat'), 
        ('examples/dog.jpg', 'dog'), 
        ('examples/horse.jpg', 'horse'), 
        ('examples/ship.jpg', 'ship'), 
        ('examples/bird.jpg', 'bird'),  
        ('examples/frog.jpg', 'frog'), 
        ('examples/plane.jpg', 'plane'), 
        ('examples/truck.jpg', 'truck'), 
        ('examples/deer.jpg', 'deer'), 
    ]
    images_with_gradcam = []
    for image_path, label in images:
        image = Image.open(image_path)
        image = image.resize((32, 32)) 
        image_array = np.asarray(image)
        visualization = inference(image_array, "Yes", a, b)[-1]
        images_with_gradcam.append((visualization, label))
        
    return {
            grad1_block: gr.update(visible=True),
            gallery3: images_with_gradcam[:n]
        }


def change_grad_view(choice):
    if choice == "Yes":
        return grad_block.update(visible=True)
    else:
        return grad_block.update(visible=False)


def show_misclassified_images(n, grad_cam, a, b):
    images = [
        ('misclassified_images/misclassified_0_GT_bird_Pred_cat.jpg', 'bird/cat'), 
        ('misclassified_images/misclassified_1_GT_car_Pred_truck.jpg', 'car/truck'), 
        ('misclassified_images/misclassified_2_GT_plane_Pred_truck.jpg', 'plane/truck'), 
        ('misclassified_images/misclassified_3_GT_deer_Pred_dog.jpg', 'deer/dog'), 
        ('misclassified_images/misclassified_4_GT_frog_Pred_cat.jpg', 'frog/cat'), 
        ('misclassified_images/misclassified_5_GT_cat_Pred_dog.jpg', 'cat/dog'),
        ('misclassified_images/misclassified_6_GT_cat_Pred_dog.jpg', 'cat/dog'), 
        ('misclassified_images/misclassified_7_GT_dog_Pred_horse.jpg', 'dog/horse'), 
        ('misclassified_images/misclassified_8_GT_bird_Pred_dog.jpg', 'bird/dog'), 
        ('misclassified_images/misclassified_9_GT_ship_Pred_plane.jpg', 'ship/plane')
    ]
    images_with_gradcam = []
    for image_path, label in images:
        image = Image.open(image_path)
        image_array = np.asarray(image)
        visualization = inference(image_array, "Yes", a, b)[-1]
        images_with_gradcam.append((visualization, label))
    if grad_cam == "Yes":
        return {
            miscls1_block: gr.update(visible=True),
            gallery: images_with_gradcam[:n]
        }
        
    return {
            miscls1_block: gr.update(visible=True),
            gallery: images[:n]
        }

def change_miscls_view(choice):
    if choice == "Yes":
        return miscls_block.update(visible=True)
    else:
        return miscls_block.update(visible=False)


def change_textbox(choice):
    if choice == "Yes":
        return [gr.Slider.update(visible=True), gr.Slider.update(visible=True)]
    else:
        return [gr.Slider.update(visible=False), gr.Slider.update(visible=False)]


def update_num_top_classes(input_img, input_slider_grad_or_not, transparency, target_layer_number, topk):
    output_classes.num_top_classes=topk
    return inference(input_img, input_slider_grad_or_not, transparency, target_layer_number, topk)[0]


def change_mygrad_view(choice):
    if choice == "Yes":
        return grad_or_not.update(visible=True)
    else:
        return grad_or_not.update(visible=False)


with gr.Blocks(theme='xiaobaiyuan/theme_brief') as demo:
    gr.Markdown("""
    
    # CustomResNet model with GradCAM 
    
    ### A simple Gradio interface to infer on CustomResNet model and get GradCAM results
    
    """)
    #gr.Markdown("# Model")
    gr.Markdown("## Grad-CAM Images")
    with gr.Row():
      grad_yes_no = gr.Radio(choices = ["Yes", "No"], value="No", label="Do you want to see GradCAM images")
    with gr.Row(visible=False) as grad_block:
        with gr.Column(scale=1):
            input_grad = gr.Slider(1, 10, value = 5, step=1, label="Number of GradCAM images to view")
            input_overlay = gr.Radio(choices = ["Yes", "No"], value="No", label="Do you want to configure gradcam")
            with gr.Row():
              clear_btn3 = gr.ClearButton()
              submit_btn3 = gr.Button("Submit")
        with gr.Column(scale=1):
            input_slider1 = gr.Slider(0, 1, value = 0.5, label="Opacity of GradCAM", interactive=True, visible=False)
            input_slider2 = gr.Slider(1, 3, value = 3, step=1, label="Which Layer?", interactive=True, visible=False)     
    with gr.Row(visible=False) as grad1_block:
        gallery3 = gr.Gallery(
                label="GradCAM images", show_label=True, elem_id="gallery3"
            ).style(columns=[4], rows=[3], object_fit="contain", height="auto")

    submit_btn3.click(fn=show_gradcam_images, inputs=[input_grad, input_slider1, input_slider2], outputs = [grad1_block, gallery3])
    clear_btn3.click(lambda: [None, None, None, None, None], outputs=[input_grad, input_grad, input_slider1, input_slider2,  gallery3])
    input_overlay.change(fn=change_textbox, inputs=input_overlay, outputs=[input_slider1, input_slider2])
    grad_yes_no.change(fn=change_grad_view, inputs=grad_yes_no, outputs=[grad_block])

    
    ###############################################

    
    gr.Markdown("## Misclassification Images")
    with gr.Row():
      miscls_yes_no = gr.Radio(choices = ["Yes", "No"], value="No", label="Do you want to see misclassified images")
    with gr.Row(visible=False) as miscls_block:
        with gr.Column(scale=1):
            input_miscn = gr.Slider(1, 10, value = 3, step=1, label="Number of misclassified images to view")

        with gr.Column(scale=1):
            input_grad2 = gr.Radio(choices = ["Yes", "No"], value="No", label="Do you want to overlay gradcam")
            input_slider21 = gr.Slider(0, 1, value = 0.5, label="Opacity of GradCAM", interactive=True, visible=False)
            input_slider22 = gr.Slider(1, 3, value = 3, step=1, label="Which Layer?", interactive=True, visible=False)    
            with gr.Row():
              clear_btn2 = gr.ClearButton()
              submit_btn2 = gr.Button("Submit")
    with gr.Column(visible=False) as miscls1_block:
        gallery = gr.Gallery(
                label="Misclassified images", show_label=True, elem_id="gallery"
        ).style(columns=[4], rows=[3], object_fit="contain", height="auto")


    submit_btn2.click(fn=show_misclassified_images, inputs=[input_miscn, input_grad2, input_slider21, input_slider22], outputs = [miscls1_block, gallery])
    clear_btn2.click(lambda: [None, None, None, None, None], outputs=[input_miscn, input_grad, input_slider21, input_slider22,  gallery])
    input_grad2.change(fn=change_textbox, inputs=input_grad2, outputs=[input_slider21, input_slider22])
    miscls_yes_no.change(fn=change_miscls_view, inputs=miscls_yes_no, outputs=[miscls_block])

    
    ###############################################

    
    gr.Markdown("## Input Interface ")
    with gr.Row():
        with gr.Column(scale=1):
            input_image = gr.Image(shape=(32, 32), label="Input Image")
            input_topk = gr.Slider(1, 10, value = 3, step=1, label="Top N Classes")
            input_slider_grad_or_not =  gr.Radio(choices = ["Yes", "No"], value="No", label="Do you want to overlay GradCAM output")
            with gr.Row():
              clear_btn = gr.ClearButton()
              submit_btn = gr.Button("Submit")
            with gr.Column(visible=False) as grad_or_not:
              input_slider1 = gr.Slider(0, 1, value = 0.5, label="Opacity of GradCAM")
              input_slider2 = gr.Slider(1, 3, value = 3, step=1, label="Which Layer?")

        with gr.Column(scale=1):
            output_classes = gr.Label(num_top_classes=3)
            output_image = gr.Image(shape=(32, 32), label="Output").style(width=128, height=128)


    gr.Markdown("## Examples")
    gr.Examples(
        examples=[["examples/car.jpg", "Yes", 0.5, 3, 3], 
                  ["examples/cat.jpg", "Yes", 0.7, 2, 5],
                  ["examples/dog.jpg", "Yes", 0.9, 1, 4],
                  ["examples/truck.jpg", "Yes", 0.3, 1, 7],
                  ["examples/horse.jpg", "Yes", 0.7, 3, 4],
                  ["examples/frog.jpg", "Yes", 0.8, 3, 6],
                  ["examples/bird.jpg", "Yes", 0.9, 1, 7],
                  ["examples/deer.jpg", "Yes", 0.3, 1, 3],
                  ["examples/plane.jpg", "Yes", 0.4, 3, 4],
                  ["examples/ship.jpg", "Yes", 0.5, 2, 5]
                 ],
        inputs=[input_image,input_slider_grad_or_not,input_slider1,input_slider2, input_topk],
        outputs=[output_classes,output_image],
        fn=inference,
        cache_examples=True,
    )
    
    submit_btn.click(fn=inference, inputs=[input_image, input_slider_grad_or_not, input_slider1, input_slider2, input_topk], outputs=[output_classes, output_image])
    clear_btn.click(lambda: [None, "No", 0.5, 3, None, None, 3], outputs=[input_image, input_slider_grad_or_not, input_slider1, input_slider2, output_classes, output_image])
    input_topk.change(update_num_top_classes, inputs=[input_image, input_slider_grad_or_not, input_slider1, input_slider2, input_topk], outputs=[output_classes])
    input_slider_grad_or_not.change(fn=change_mygrad_view, inputs=input_slider_grad_or_not, outputs=[grad_or_not])


if __name__ == "__main__":
  demo.launch(debug=True)