theschoolofai commited on
Commit
fd11437
·
verified ·
1 Parent(s): 09bae93

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from resnet import ResNet18
9
+ import gradio as gr
10
+
11
+ model = ResNet18()
12
+ model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')), strict=False)
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
+ classes = ('plane', 'car', 'bird', 'cat', 'deer',
19
+ 'dog', 'frog', 'horse', 'ship', 'truck')
20
+
21
+ def resize_image_pil(image, new_width, new_height):
22
+
23
+ # Convert to PIL image
24
+ img = Image.fromarray(np.array(image))
25
+
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
+ scale = min(width_scale, height_scale)
33
+
34
+ # Resize
35
+ resized = img.resize((int(width*scale), int(height*scale)), Image.NEAREST)
36
+
37
+ # Crop to exact size
38
+ resized = resized.crop((0, 0, new_width, new_height))
39
+
40
+ return resized
41
+
42
+ def inference(input_img, transparency = 0.5, target_layer_number = -1):
43
+ input_img = resize_image_pil(input_img, 32, 32)
44
+
45
+ input_img = np.array(input_img)
46
+ org_img = input_img
47
+ input_img = input_img.reshape((32, 32, 3))
48
+ transform = transforms.ToTensor()
49
+ input_img = transform(input_img)
50
+ input_img = input_img
51
+ input_img = input_img.unsqueeze(0)
52
+ outputs = model(input_img)
53
+ softmax = torch.nn.Softmax(dim=0)
54
+ o = softmax(outputs.flatten())
55
+ confidences = {classes[i]: float(o[i]) for i in range(10)}
56
+ _, prediction = torch.max(outputs, 1)
57
+ target_layers = [model.layer2[target_layer_number]]
58
+ cam = GradCAM(model=model, target_layers=target_layers)
59
+ grayscale_cam = cam(input_tensor=input_img, targets=None)
60
+ grayscale_cam = grayscale_cam[0, :]
61
+ # img = input_img.squeeze(0)
62
+ # img = inv_normalize(img)
63
+ # print(transparency)
64
+ visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
65
+ return classes[prediction[0].item()], visualization, confidences
66
+
67
+ title = "CIFAR10 trained on ResNet18 Model with GradCAM"
68
+ description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
69
+ examples = [["cat.jpg", 0.5, -1], ["dog.jpg", 0.5, -1]]
70
+ demo = gr.Interface(
71
+ inference,
72
+ inputs = [
73
+ gr.Image(width=256, height=256, label="Input Image"), gr.Slider
74
+ (0, 1, value = 0.5, label="Overall Opacity of Image"),
75
+ gr.Slider(-2, -1, value = -2, step=1, label="Which Layer?")
76
+ ],
77
+ outputs = [
78
+ "text",
79
+ gr.Image(width=256, height=256, label="Output"),
80
+ gr.Label(num_top_classes=3)
81
+ ],
82
+ title = title,
83
+ description = description,
84
+ examples = examples,
85
+ )
86
+ demo.launch()