Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from pytorch_grad_cam import GradCAM
|
| 8 |
+
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 9 |
+
from resnet import ResNet18
|
| 10 |
+
|
| 11 |
+
model = ResNet18()
|
| 12 |
+
model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu') ), strict=False)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
inv_normalize = transforms.Normalize(
|
| 16 |
+
mean=[-0.50/0.23, -0.50/0.23, -0.50/0.23],
|
| 17 |
+
std = [1/0.23, 1/0.23, 1/0.23]
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
|
| 21 |
+
|
| 22 |
+
def resize_image_pil(image, new_width, new_height):
|
| 23 |
+
|
| 24 |
+
# convert to PIL IMage
|
| 25 |
+
img = Image.fromarray(np.array(image))
|
| 26 |
+
# get original size
|
| 27 |
+
width, height = img.size
|
| 28 |
+
|
| 29 |
+
# calculate scale
|
| 30 |
+
width_scale = new_width/width
|
| 31 |
+
height_scale = new_height/height
|
| 32 |
+
|
| 33 |
+
scale = min(width_scale, height_scale)
|
| 34 |
+
|
| 35 |
+
# resize
|
| 36 |
+
resized = img.resize(size=(int(width*scale), int(height*scale)), resample=Image.NEAREST)
|
| 37 |
+
|
| 38 |
+
# crop resized image
|
| 39 |
+
resized = resized.crop((0, 0, new_width, new_height))
|
| 40 |
+
|
| 41 |
+
return resized
|
| 42 |
+
|
| 43 |
+
def inference(input_image, transparency=0.5, target_layer_number=-1):
|
| 44 |
+
input_image = resize_image_pil(input_image, 32, 32)
|
| 45 |
+
input_image = np.array(input_image)
|
| 46 |
+
org_img = input_image
|
| 47 |
+
input_image = input_image.reshape((32, 32, 3))
|
| 48 |
+
transforms = transforms.ToTensor()
|
| 49 |
+
input_image = transforms(input_image)
|
| 50 |
+
input_image = input_image.unsqueeze(0)
|
| 51 |
+
outputs = model(input_image)
|
| 52 |
+
softmax = torch.nn.Softmax(dim=0)
|
| 53 |
+
o = softmax(outputs.flatten())
|
| 54 |
+
confidences = {classes[i]: float(o[i]) for i in range(10)}
|
| 55 |
+
_, prediction = torch.max(outputs, 1)
|
| 56 |
+
target_layers = [model.layer2[target_layer_number]]
|
| 57 |
+
cam = GradCAM(model= model, target_layers = target_layers)
|
| 58 |
+
grayscale_cam = cam(input_tensor=input_image, target=None)
|
| 59 |
+
grayscale_cam = grayscale_cam[0, :]
|
| 60 |
+
visualization = show_cam_on_image(
|
| 61 |
+
org_img/255,
|
| 62 |
+
grayscale_cam,
|
| 63 |
+
use_rgb=True,
|
| 64 |
+
image_weight = transparency
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
return classes[prediction[0].item(), visualization, confidences]
|
| 68 |
+
|
| 69 |
+
demo = gr.Interface(
|
| 70 |
+
inference,
|
| 71 |
+
inputs = [
|
| 72 |
+
gr,Image(width=256, height=256, label="Input Image"),
|
| 73 |
+
gr.Slider(0, 1, value=0.5, label="Overall opacity fo the overlay"),
|
| 74 |
+
gr.Slider(-2, -1, value=-2, step=1, label="Which GradCAM layer?")
|
| 75 |
+
],
|
| 76 |
+
outputs = [
|
| 77 |
+
"text",
|
| 78 |
+
gr.Image(width=256, height=256, label="Output"),
|
| 79 |
+
gr.Label(num_top_classes=3)
|
| 80 |
+
],
|
| 81 |
+
title="CIFAR10 trained on ResNet18 with GradCAM feature",
|
| 82 |
+
description = "A simple Gradio app for checking GradCAM outputs from results of ResNet18 model."
|
| 83 |
+
examples = [["cat.jpg", 0.5, -1], ["dog.jpg", 0.7, -2]
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
demo.launch()
|