Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 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_lightning import ResNet18Model
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
model = ResNet18Model.load_from_checkpoint("epoch=19-step=3920.ckpt")
|
| 13 |
+
|
| 14 |
+
inv_normalize = transforms.Normalize(
|
| 15 |
+
mean = [-0.50/0.23, -0.50/0.23, -0.50/0.23],
|
| 16 |
+
std= [1/0.23, 1/0.23,1/0.23]
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
classes = ('plane', 'car', 'bird', 'cat', 'deer',
|
| 20 |
+
'dog', 'frog', 'horse', 'ship', 'truck')
|
| 21 |
+
|
| 22 |
+
model_layer_names = ["1", "2", "3"]
|
| 23 |
+
|
| 24 |
+
def get_layer(layer_name):
|
| 25 |
+
print("layer name:", layer_name)
|
| 26 |
+
if layer_name == 1:
|
| 27 |
+
return [model.layer1[-1]]
|
| 28 |
+
elif layer_name == 2:
|
| 29 |
+
return [model.layer2[-1]]
|
| 30 |
+
elif layer_name == 3:
|
| 31 |
+
return [model.layer3[-1]]
|
| 32 |
+
else:
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
def resize_image_pil(image, new_width, new_height):
|
| 36 |
+
img = Image.fromarray(np.array(image))
|
| 37 |
+
width, height = img.size
|
| 38 |
+
|
| 39 |
+
width_scale = new_width/width
|
| 40 |
+
height_scale = new_height/height
|
| 41 |
+
scale = min(width_scale, height_scale)
|
| 42 |
+
resized = img.resize((int(width*scale), int(height*scale)), Image.NEAREST)
|
| 43 |
+
resized = resized.crop((0,0,new_width, new_height))
|
| 44 |
+
|
| 45 |
+
return resized
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def inference(input_img, show_gradcam, layer_name, num_classes, transparancy = 0.5):
|
| 49 |
+
print(show_gradcam, layer_name, num_classes, transparancy)
|
| 50 |
+
input_img = resize_image_pil(input_img,32,32)
|
| 51 |
+
input_img = np.array(input_img)
|
| 52 |
+
org_img = input_img
|
| 53 |
+
|
| 54 |
+
input_img= input_img.reshape((32,32,3))
|
| 55 |
+
transform = transforms.ToTensor()
|
| 56 |
+
input_img = transform(input_img)
|
| 57 |
+
input_img = input_img.unsqueeze(0)
|
| 58 |
+
outputs = model(input_img)
|
| 59 |
+
# print(outputs)
|
| 60 |
+
softmax = torch.nn.Softmax(dim=0)
|
| 61 |
+
o = softmax(outputs.flatten())
|
| 62 |
+
|
| 63 |
+
output_numpy = np.squeeze(np.asarray(outputs.detach().numpy()))
|
| 64 |
+
index_sort = np.argsort(output_numpy)[::-1]
|
| 65 |
+
|
| 66 |
+
confidences = {}
|
| 67 |
+
for i in range(int(num_classes)):
|
| 68 |
+
confidences[classes[index_sort[i]]] = float(o[index_sort[i]])
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
prediction= torch.max(outputs, 1)
|
| 72 |
+
|
| 73 |
+
if show_gradcam:
|
| 74 |
+
target_layers = get_layer(layer_name)
|
| 75 |
+
print("target layer",target_layers)
|
| 76 |
+
cam = GradCAM(model=model, target_layers=target_layers)
|
| 77 |
+
grayscale_cam = cam(input_tensor= input_img)
|
| 78 |
+
grayscale_cam = grayscale_cam[0, :]
|
| 79 |
+
visualization = show_cam_on_image(org_img/255,grayscale_cam,use_rgb=True,
|
| 80 |
+
image_weight=transparancy)
|
| 81 |
+
else:
|
| 82 |
+
visualization = org_img
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
return classes[int(prediction[0].item())], visualization, confidences
|
| 86 |
+
|
| 87 |
+
demo = gr.Interface(
|
| 88 |
+
inference,
|
| 89 |
+
inputs = [
|
| 90 |
+
gr.Image(width=256,height=256,label="Input image"),
|
| 91 |
+
gr.Number(value=3, maximum=10, minimum=1,step=1.0, precision=0,label="Number of classes to display"),
|
| 92 |
+
gr.Checkbox(True, label="Show GradCAM Image"),
|
| 93 |
+
gr.Dropdown(model_layer_names, value=3, label="Which layer for Gradcam"),
|
| 94 |
+
gr.Slider(0, 1, value=0.5,label="Overall opacity of the overlay"),
|
| 95 |
+
],
|
| 96 |
+
outputs = [
|
| 97 |
+
gr.Label(label="Class", container=True, show_label= True),
|
| 98 |
+
gr.Image(width= 256, height=256,label="Output Image"),
|
| 99 |
+
gr.Label(label="Confidences", container=True, show_label= True),
|
| 100 |
+
],
|
| 101 |
+
title = "CIFAR 10 trained on ResNet model in pytorch lightning with Gradcam",
|
| 102 |
+
description = " A simple gradio inference to infer on resnet18 model",
|
| 103 |
+
examples = [["cat.jpg",1, True, 10, 0.4]]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch()
|
| 108 |
+
|