Commit
·
3f38b4a
1
Parent(s):
d9eb37b
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch, torchvision
|
| 2 |
+
from torchvision import transforms
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from pytorch_grad_cam import GradCAM
|
| 7 |
+
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 8 |
+
|
| 9 |
+
model = CustomResNet()
|
| 10 |
+
model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')), strict=False)
|
| 11 |
+
|
| 12 |
+
inv_normalize = transforms.Normalize(
|
| 13 |
+
mean=[-0.50/0.23, -0.50/0.23, -0.50/0.23],
|
| 14 |
+
std=[1/0.23, 1/0.23, 1/0.23]
|
| 15 |
+
)
|
| 16 |
+
classes = ('plane', 'car', 'bird', 'cat', 'deer',
|
| 17 |
+
'dog', 'frog', 'horse', 'ship', 'truck')
|
| 18 |
+
|
| 19 |
+
def inference(input_img, transparency = 0.5, target_layer_number = -1):
|
| 20 |
+
transform = transforms.ToTensor()
|
| 21 |
+
org_img = input_img
|
| 22 |
+
input_img = transform(input_img)
|
| 23 |
+
input_img = input_img
|
| 24 |
+
input_img = input_img.unsqueeze(0)
|
| 25 |
+
outputs = model(input_img)
|
| 26 |
+
softmax = torch.nn.Softmax(dim=0)
|
| 27 |
+
o = softmax(outputs.flatten())
|
| 28 |
+
confidences = {classes[i]: float(o[i]) for i in range(10)}
|
| 29 |
+
_, prediction = torch.max(outputs, 1)
|
| 30 |
+
target_layers = [model.layer2[target_layer_number]]
|
| 31 |
+
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
|
| 32 |
+
grayscale_cam = cam(input_tensor=input_img, targets=None)
|
| 33 |
+
grayscale_cam = grayscale_cam[0, :]
|
| 34 |
+
img = input_img.squeeze(0)
|
| 35 |
+
img = inv_normalize(img)
|
| 36 |
+
rgb_img = np.transpose(img, (1, 2, 0))
|
| 37 |
+
rgb_img = rgb_img.numpy()
|
| 38 |
+
visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
|
| 39 |
+
return confidences, visualization
|
| 40 |
+
|
| 41 |
+
title = "CIFAR10 trained on CustomBlock Model with GradCAM"
|
| 42 |
+
description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
|
| 43 |
+
examples = [["cat.jpg", 0.5, -1],
|
| 44 |
+
["dog.jpg", 0.5, -1],
|
| 45 |
+
["plane.jpg", 0.5, -1],
|
| 46 |
+
["car.jpg", 0.5, -1],
|
| 47 |
+
["bird.jpg", 0.5, -1],
|
| 48 |
+
["deer.jpg", 0.5, -1],
|
| 49 |
+
["frog.jpg", 0.5, -1],
|
| 50 |
+
["horse.jpg", 0.5, -1],
|
| 51 |
+
["ship.jpg", 0.5, -1],
|
| 52 |
+
["truck.jpg", 0.5, -1]
|
| 53 |
+
]
|
| 54 |
+
demo = gr.Interface(
|
| 55 |
+
inference,
|
| 56 |
+
inputs = [gr.Image(shape=(32, 32), label="Input Image"),
|
| 57 |
+
gr.Slider(0, 1, value = 0.5, label="Opacity of GradCAM"),
|
| 58 |
+
gr.Slider(-2, -1, value = -2, step=1, label="Which Layer?"),
|
| 59 |
+
gr.Slider(1, 10,step=1, label="How many missclassifed images"),
|
| 60 |
+
gr.Slider(1, 10,step=1, label="How many Top Class"),
|
| 61 |
+
],
|
| 62 |
+
outputs = [gr.Label(num_top_classes=3), gr.Image(shape=(32, 32), label="Output").style(width=128, height=128)],
|
| 63 |
+
title = title,
|
| 64 |
+
description = description,
|
| 65 |
+
examples = examples,
|
| 66 |
+
)
|
| 67 |
+
demo.launch()
|