Spaces:
Sleeping
Sleeping
adding main, model.pth and input files
Browse files
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.39.0
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
+
title: Demo1
|
3 |
+
emoji: π
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: gray
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.39.0
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 inference(input_img, transparency = 0.5, target_layer_number = -1):
|
22 |
+
transform = transforms.ToTensor()
|
23 |
+
org_img = input_img
|
24 |
+
input_img = transform(input_img)
|
25 |
+
input_img = input_img
|
26 |
+
input_img = input_img.unsqueeze(0)
|
27 |
+
outputs = model(input_img)
|
28 |
+
softmax = torch.nn.Softmax(dim=0)
|
29 |
+
o = softmax(outputs.flatten())
|
30 |
+
confidences = {classes[i]: float(o[i]) for i in range(10)}
|
31 |
+
_, prediction = torch.max(outputs, 1)
|
32 |
+
target_layers = [model.layer2[target_layer_number]]
|
33 |
+
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
|
34 |
+
grayscale_cam = cam(input_tensor=input_img, targets=None)
|
35 |
+
grayscale_cam = grayscale_cam[0, :]
|
36 |
+
img = input_img.squeeze(0)
|
37 |
+
img = inv_normalize(img)
|
38 |
+
rgb_img = np.transpose(img, (1, 2, 0))
|
39 |
+
rgb_img = rgb_img.numpy()
|
40 |
+
visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
|
41 |
+
return confidences, visualization
|
42 |
+
|
43 |
+
title = "CIFAR10 trained on ResNet18 Model with GradCAM"
|
44 |
+
description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
|
45 |
+
examples = [["cat.jpg", 0.5, -1], ["dog.jpg", 0.5, -1]]
|
46 |
+
demo = gr.Interface(
|
47 |
+
inference,
|
48 |
+
inputs = [gr.Image(shape=(32, 32), label="Input Image"), gr.Slider(0, 1, value = 0.5, label="Opacity of GradCAM"), gr.Slider(-2, -1, value = -2, step=1, label="Which Layer?")],
|
49 |
+
outputs = [gr.Label(num_top_classes=3), gr.Image(shape=(32, 32), label="Output").style(width=128, height=128)],
|
50 |
+
title = title,
|
51 |
+
description = description,
|
52 |
+
examples = examples,
|
53 |
+
)
|
54 |
+
demo.launch()
|
cat.jpg
ADDED
![]() |
dog.jpg
ADDED
![]() |
model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5cf9335b863d513421b678d5b93078c44eca26d4d1a7afdd7411cc27d4b907b9
|
3 |
+
size 133
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
torch-lr-finder
|
4 |
+
grad-cam
|
5 |
+
pillow
|
6 |
+
numpy
|
resnet.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
ResNet in PyTorch.
|
3 |
+
For Pre-activation ResNet, see 'preact_resnet.py'.
|
4 |
+
|
5 |
+
Reference:
|
6 |
+
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
|
7 |
+
Deep Residual Learning for Image Recognition. arXiv:1512.03385
|
8 |
+
"""
|
9 |
+
import torch.nn as nn
|
10 |
+
import torch.nn.functional as F
|
11 |
+
|
12 |
+
|
13 |
+
class BasicBlock(nn.Module):
|
14 |
+
expansion = 1
|
15 |
+
|
16 |
+
def __init__(self, in_planes, planes, stride=1):
|
17 |
+
super(BasicBlock, self).__init__()
|
18 |
+
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
19 |
+
self.bn1 = nn.BatchNorm2d(planes)
|
20 |
+
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
|
21 |
+
self.bn2 = nn.BatchNorm2d(planes)
|
22 |
+
|
23 |
+
self.shortcut = nn.Sequential()
|
24 |
+
if stride != 1 or in_planes != self.expansion*planes:
|
25 |
+
self.shortcut = nn.Sequential(
|
26 |
+
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
|
27 |
+
nn.BatchNorm2d(self.expansion*planes)
|
28 |
+
)
|
29 |
+
|
30 |
+
def forward(self, x):
|
31 |
+
out = F.relu(self.bn1(self.conv1(x)))
|
32 |
+
out = self.bn2(self.conv2(out))
|
33 |
+
out += self.shortcut(x)
|
34 |
+
out = F.relu(out)
|
35 |
+
return out
|
36 |
+
|
37 |
+
|
38 |
+
class ResNet(nn.Module):
|
39 |
+
def __init__(self, block, num_blocks, num_classes=10):
|
40 |
+
super(ResNet, self).__init__()
|
41 |
+
self.in_planes = 64
|
42 |
+
|
43 |
+
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
|
44 |
+
self.bn1 = nn.BatchNorm2d(64)
|
45 |
+
self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
|
46 |
+
self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
|
47 |
+
self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
|
48 |
+
self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
|
49 |
+
self.linear = nn.Linear(512*block.expansion, num_classes)
|
50 |
+
|
51 |
+
def _make_layer(self, block, planes, num_blocks, stride):
|
52 |
+
strides = [stride] + [1]*(num_blocks-1)
|
53 |
+
layers = []
|
54 |
+
for stride in strides:
|
55 |
+
layers.append(block(self.in_planes, planes, stride))
|
56 |
+
self.in_planes = planes * block.expansion
|
57 |
+
return nn.Sequential(*layers)
|
58 |
+
|
59 |
+
def forward(self, x):
|
60 |
+
out = F.relu(self.bn1(self.conv1(x)))
|
61 |
+
out = self.layer1(out)
|
62 |
+
out = self.layer2(out)
|
63 |
+
out = self.layer3(out)
|
64 |
+
out = self.layer4(out)
|
65 |
+
out = F.avg_pool2d(out, 4)
|
66 |
+
out = out.view(out.size(0), -1)
|
67 |
+
out = self.linear(out)
|
68 |
+
return out
|
69 |
+
|
70 |
+
|
71 |
+
def ResNet18():
|
72 |
+
return ResNet(BasicBlock, [2, 2, 2, 2])
|
73 |
+
|
74 |
+
|
75 |
+
def ResNet34():
|
76 |
+
return ResNet(BasicBlock, [3, 4, 6, 3])
|