Vvaann commited on
Commit
d31170d
·
verified ·
1 Parent(s): 6254023

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def resize_image_pil(image, new_width, new_height):
23
+ img = Image.fromarray(np.array(image))
24
+ width, height = img.size
25
+
26
+ width_scale = new_width/width
27
+ height_scale - new_height/height
28
+ scale = min(width_scale, height_scale)
29
+ resized = img.resize((int(width*Scale), int(height*Scale)), Image.NEAREST)
30
+ resized = resized.crop((0,0,new_width, new_height))
31
+
32
+ return resized
33
+
34
+
35
+ def inference(input_img, transparancy = 0.5, target_layer_number = -1):
36
+ input_img = resize_image_pil(input_img,32,32)
37
+ input_img = np.array(input_img)
38
+ org_img = input_img
39
+ input_img= input_img.reshape((32,32,3))
40
+ transform = transforms.ToTensor()
41
+ input_img = transform(input_img)
42
+ input_img = input_img.unsqueeze(0)
43
+ outputs = model(input_img)
44
+ softmax = torch.nn.Softmax(dim=0)
45
+ o = softmax(outputs.flattern())
46
+ confidences = {classes[i]:float(o[i] fir i in range(10))}
47
+ -, prediction= torch.max(outputs,1)
48
+ target_layers = [model.layer2[target_layer_number]]
49
+ cam = GradCAM(model=model, target_layers=target_layers)
50
+ grayscale_cam = cam(input_tensor= input_img,target=None)
51
+ grayscale_cam = grayscale_cam[0, :]
52
+ visualization = show_cam_on_image(org_img/255,grayscale_cam,use_rgb=True,
53
+ image_weight=transparancy)
54
+
55
+ return classes[prediction[0].item(),visualization,confidences]
56
+
57
+ demo = gr.Inference(
58
+ inference,
59
+ inputs = [
60
+ gr.Image(width=256,height=256,label="input image"),
61
+ gr.Slider(0,1,value=0.5,label="Overall opacity of the overelay"),
62
+ gr.Slider(-2,-1, value =-2, step=1, label= "Which layer for Gradcam")
63
+ ],
64
+ outputd = [
65
+ "text",
66
+ gr.IMage(width= 256, height=256,label="Output"),
67
+ gr.Label(num_top_classes=3)
68
+ ],
69
+ title = "CIFAR 10 trained on ResNet model in pytorch lightning with Gradcam"
70
+ description = " A simple gradio inference to infer on resnet18 model"
71
+ examples = [["cat.jpg", 0.5, -1],["dog.jpg",0.7,-2]]
72
+ )
73
+
74
+ demo.launch()