File size: 6,124 Bytes
9022436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee175b7
c922790
9022436
 
 
 
 
 
a9c8702
9022436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
import torch
from torchvision import transforms 
from model import CustomResNet
from utils.utils import wrong_predictions
from utils.dataloader import get_dataloader
import random 
from collections import OrderedDict
import os

test_o = get_dataloader()
# test_o=next(iter(test_o))   


examples_dir = os.path.join(os.getcwd(), 'examples')
examples = [[os.path.join(examples_dir, img), 0.5] for img in os.listdir(examples_dir)]


model = CustomResNet()
model.load_state_dict(torch.load('modelp.ckpt', map_location='cpu')['state_dict']) #, strict = False)
# model = model.cpu()

classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
norm_mean=(0.4914, 0.4822, 0.4465) 
norm_std=(0.2023, 0.1994, 0.2010)
misclassified_images, all_predictions = wrong_predictions(model,test_o, norm_mean, norm_std, classes, 'cpu')

# layers = ['layer_1', 'layer_3']
# layers = [model.layer_1, model.layer_2, model.layer_3]
def inference(input_img, transparency, layer_num, top_classes):
    input_img_ori = input_img.copy()
    transform = transforms.ToTensor()
    # transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize(
    #     mean=[0.485,0.456,0.406],
    #     std=[0.229, 0.224, 0.255]
    # )])
    inv_normalize = transforms.Normalize(
        mean=[-0.485/0.229, -0.456/0.224, -0.406/0.255],
        std=[1/0.229, 1/0.224, 1/0.255]
    )
    input_img = transform(input_img)
    # input_img = input_img.to(device)
    input_img = input_img.unsqueeze(0)
    outputs = model(input_img)
    _, prediction = torch.max(outputs, 1)
    softmax = torch.nn.Softmax(dim=0)
    outputs = softmax(outputs.flatten())
    # print(outputs)
    confidences = {classes[i]: float(outputs[i]) for i in range(10)}
    confidences = OrderedDict(sorted(confidences.items(), key=lambda x:x[1], reverse=True))
    # print(confidences)
    filtered_confidences ={}# OrderedDict()
    for i, (key, val) in enumerate(confidences.items()):
        if i ==  top_classes:
            break
        filtered_confidences[key] = val


    if layer_num == 1:
        target_layers = [model.layer_1]
    elif layer_num == 2:
        target_layers = [model.layer_2]
    else:
        target_layers = [model.layer_3]
    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)
    img = inv_normalize(img)
    rgb_img = np.transpose(img, (1, 2, 0))
    rgb_img = np.array(np.clip(rgb_img,0,1), np.float32)
    visualization = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True, image_weight=transparency)

    # visualization = input_img_ori
    return filtered_confidences, visualization
    # return filtered_confidences, superimposed_img

def get_misclassified_images(num):
    outputimgs = []
    # misclassified_images = wrong_predictions(model,test_o, norm_mean, norm_std, classes, 'cpu')
    for i in range(int(num)):
        # misclassified_images[0][0].cpu().numpy()
        inv_normalize = transforms.Normalize(
            mean=[-0.485/0.229, -0.456/0.224, -0.406/0.255],
            std=[1/0.229, 1/0.224, 1/0.255]
        )
        inv_tensor = np.array(inv_normalize(misclassified_images[random.randint(2,98)][0]).cpu().permute(1,2,0)*255, dtype='uint8')
        outputimgs.append(inv_tensor)
    return outputimgs


def get_gradcam_images(num, transparency, layer_num):
    outcoms=[]
    for i in range(int(num)):
        input_img = all_predictions[random.randint(2,98)][0]
        inv_normalize = transforms.Normalize(
            mean=[-0.485/0.229, -0.456/0.224, -0.406/0.255],
            std=[1/0.229, 1/0.224, 1/0.255]
        )
        input_img = input_img.unsqueeze(0)
        if layer_num == 1:
            target_layers = [model.layer_1]
        elif layer_num == 2:
            target_layers = [model.layer_2]
        else:
            target_layers = [model.layer_3]

        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)
        img = inv_normalize(img)
        rgb_img = np.transpose(img, (1, 2, 0))
        rgb_img = np.array(np.clip(rgb_img,0,1), np.float32)
        visualization = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True, image_weight=transparency)
        outcoms.append(visualization)
    return outcoms

# demo = gr.Interface(inference, [gr.Image(shape=(32, 32)), gr.Slider(0, 1)], ["text", gr.Image(shape=(32, 32)).style(width=128, height=128)])
inference_new_image = gr.Interface(
    inference, 
    inputs = [gr.Image(shape=(32, 32), label="Input Image"), gr.Slider(0, 1, value = 0.3, label="transparency?"), gr.Slider(1, 3, value = 1,step=1, label="layer?"),
              gr.Slider(1, 10, value = 3, step=1, label="top classes?")], 
    
    outputs = [gr.Label(),gr.Image(shape=(32, 32), label="Model Prediction").style(width=300, height=300)],
    title = 'gradio app',
    description = 'for dl purposes',
    examples = examples,
)

misclassified_interface = gr.Interface(
    get_misclassified_images, 
    inputs = [gr.Number(value=10, label="images number")], 
    
    outputs = [gr.Gallery(label="misclassified images")],
    title = 'gradio app',
    description = 'for dl purposes'
)

gradcam_images = gr.Interface(
    get_gradcam_images, 
    inputs = [gr.Number(value=10, label="images number"), gr.Slider(0, 1, value = 0.3, label="transparency?"), gr.Slider(1, 3, value = 1,step=1, label="layer?")], 
    
    outputs = [gr.Gallery(label="gradcam images")],
    title = 'gradio app',
    description = 'for dl purposes'
)


demo = gr.TabbedInterface([inference_new_image, misclassified_interface, gradcam_images], tab_names=["Input image", "Misclassified Images", "grad cam images"], 
                          title="customresnet gradcam")
demo.launch()