File size: 9,823 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import gradio as gr
import random
import numpy as np
from PIL import Image
import torch
import torchvision

from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.image import show_cam_on_image

from models.resnet_lightning import ResNet
from utils.data import CIFARDataModule
from utils.transforms import test_transform
from utils.common import get_misclassified_data

inv_normalize = torchvision.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]
)

datamodule = CIFARDataModule()
datamodule.setup()
classes = datamodule.train_dataset.classes

model = ResNet.load_from_checkpoint("model.ckpt")
model = model.to("cpu")

prediction_image = None


def upload_file(files):
    file_paths = [file.name for file in files]
    return file_paths


def read_image(path):
    img = Image.open(path)
    img.load()
    data = np.asarray(img, dtype="uint8")
    return data


def sample_images():
    images = []
    length = len(datamodule.test_dataset)
    classes = datamodule.train_dataset.classes
    for i in range(10):
        idx = random.randint(0, length - 1)
        image, label = datamodule.test_dataset[idx]
        image = inv_normalize(image).permute(1, 2, 0).numpy()
        images.append((image, classes[label]))
    return images


def get_misclassified_images(misclassified_count):
    misclassified_images = []
    misclassified_data = get_misclassified_data(
        model=model,
        device="cpu",
        test_loader=datamodule.test_dataloader(),
        count=misclassified_count,
    )
    for i in range(misclassified_count):
        img = misclassified_data[i][0].squeeze().to("cpu")
        img = inv_normalize(img)
        img = np.transpose(img.numpy(), (1, 2, 0))
        label = f"Label: {classes[misclassified_data[i][1].item()]} | Prediction: {classes[misclassified_data[i][2].item()]}"
        misclassified_images.append((img, label))
    return misclassified_images


def get_gradcam_images(gradcam_layer, gradcam_count, gradcam_opacity):
    gradcam_images = []
    if gradcam_layer == "Layer1":
        target_layers = [model.layer1[-1]]
    elif gradcam_layer == "Layer2":
        target_layers = [model.layer2[-1]]
    else:
        target_layers = [model.layer3[-1]]

    cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
    data = get_misclassified_data(
        model=model,
        device="cpu",
        test_loader=datamodule.test_dataloader(),
        count=gradcam_count,
    )
    for i in range(gradcam_count):
        input_tensor = data[i][0]

        # Get the activations of the layer for the images
        grayscale_cam = cam(input_tensor=input_tensor, targets=None)
        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=gradcam_opacity
        )
        label = f"Label: {classes[data[i][1].item()]} | Prediction: {classes[data[i][2].item()]}"
        gradcam_images.append((visualization, label))
    return gradcam_images


def show_hide_misclassified(status):
    if not status:
        return {misclassified_count: gr.update(visible=False)}
    return {misclassified_count: gr.update(visible=True)}


def show_hide_gradcam(status):
    if not status:
        return [gr.update(visible=False) for i in range(3)]
    return [gr.update(visible=True) for i in range(3)]


def set_prediction_image(evt: gr.SelectData, gallery):
    global prediction_image
    if isinstance(gallery[evt.index], dict):
        prediction_image = gallery[evt.index]["name"]
    else:
        prediction_image = gallery[evt.index][0]["name"]


def predict(
    is_misclassified,
    misclassified_count,
    is_gradcam,
    gradcam_count,
    gradcam_layer,
    gradcam_opacity,
    num_classes,
):
    misclassified_images = None
    if is_misclassified:
        misclassified_images = get_misclassified_images(int(misclassified_count))

    gradcam_images = None
    if is_gradcam:
        gradcam_images = get_gradcam_images(
            gradcam_layer, int(gradcam_count), gradcam_opacity
        )

    img = read_image(prediction_image)
    image_transformed = test_transform(image=img)["image"]
    output = model(image_transformed.unsqueeze(0))
    preds = torch.softmax(output, dim=1).squeeze().detach().numpy()
    indices = (
        output.argsort(descending=True).squeeze().detach().numpy()[: int(num_classes)]
    )
    predictions = {classes[i]: round(float(preds[i]), 2) for i in indices}

    return {
        miscalssfied_output: gr.update(value=misclassified_images),
        gradcam_output: gr.update(value=gradcam_images),
        prediction_label: gr.update(value=predictions),
    }


with gr.Blocks() as app:
    gr.Markdown("## ERA Session12 - CIFAR10 Classification with ResNet")
    with gr.Row():
        with gr.Column():
            with gr.Box():
                is_misclassified = gr.Checkbox(
                    label="Misclassified Images", info="Display misclassified images?"
                )
                misclassified_count = gr.Dropdown(
                    choices=["10", "20"],
                    label="Select Number of Images",
                    info="Number of Misclassified images",
                    visible=False,
                    interactive=True,
                )
                is_misclassified.input(
                    show_hide_misclassified,
                    inputs=[is_misclassified],
                    outputs=[misclassified_count],
                )
            with gr.Box():
                is_gradcam = gr.Checkbox(
                    label="GradCAM Images",
                    info="Display GradCAM images?",
                )
                gradcam_count = gr.Dropdown(
                    choices=["10", "20"],
                    label="Select Number of Images",
                    info="Number of GradCAM images",
                    interactive=True,
                    visible=False,
                )
                gradcam_layer = gr.Dropdown(
                    choices=["Layer1", "Layer2", "Layer3"],
                    label="Select the layer",
                    info="Please select the layer for which the GradCAM is required",
                    interactive=True,
                    visible=False,
                )
                gradcam_opacity = gr.Slider(
                    minimum=0,
                    maximum=1,
                    value=0.6,
                    label="Opacity",
                    info="Opacity of GradCAM output",
                    interactive=True,
                    visible=False,
                )

                is_gradcam.input(
                    show_hide_gradcam,
                    inputs=[is_gradcam],
                    outputs=[gradcam_count, gradcam_layer, gradcam_opacity],
                )
            with gr.Box():
                # file_output = gr.File(file_types=["image"])
                with gr.Group():
                    upload_gallery = gr.Gallery(
                        value=None,
                        label="Uploaded images",
                        show_label=False,
                        elem_id="gallery_upload",
                        columns=5,
                        rows=2,
                        height="auto",
                        object_fit="contain",
                    )
                    upload_button = gr.UploadButton(
                        "Click to Upload images",
                        file_types=["image"],
                        file_count="multiple",
                    )
                    upload_button.upload(upload_file, upload_button, upload_gallery)

                with gr.Group():
                    sample_gallery = gr.Gallery(
                        value=sample_images,
                        label="Sample images",
                        show_label=True,
                        elem_id="gallery_sample",
                        columns=5,
                        rows=2,
                        height="auto",
                        object_fit="contain",
                    )

                upload_gallery.select(set_prediction_image, inputs=[upload_gallery])
                sample_gallery.select(set_prediction_image, inputs=[sample_gallery])

            with gr.Box():
                num_classes = gr.Dropdown(
                    choices=[str(i + 1) for i in range(10)],
                    label="Select Number of Top Classes",
                    info="Number of Top target classes to be shown",
                )
            run_btn = gr.Button()
        with gr.Column():
            with gr.Box():
                miscalssfied_output = gr.Gallery(
                    value=None, label="Misclassified Images", show_label=True
                )
            with gr.Box():
                gradcam_output = gr.Gallery(
                    value=None, label="GradCAM Images", show_label=True
                )
            with gr.Box():
                prediction_label = gr.Label(value=None, label="Predictions")

        run_btn.click(
            predict,
            inputs=[
                is_misclassified,
                misclassified_count,
                is_gradcam,
                gradcam_count,
                gradcam_layer,
                gradcam_opacity,
                num_classes,
            ],
            outputs=[miscalssfied_output, gradcam_output, prediction_label],
        )


app.launch(server_name="0.0.0.0", server_port=9998)