Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,8 @@ from pytorch_grad_cam import GradCAM
|
|
7 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
8 |
from resnet import custom_ResNet
|
9 |
import gradio as gr
|
|
|
|
|
10 |
|
11 |
model = custom_ResNet()
|
12 |
model.load_state_dict(torch.load("custom_resnet_model.pth", map_location=torch.device('cpu')), strict=False)
|
@@ -19,16 +21,16 @@ inv_normalize = transforms.Normalize(
|
|
19 |
classes = ('plane', 'car', 'bird', 'cat', 'deer',
|
20 |
'dog', 'frog', 'horse', 'ship', 'truck')
|
21 |
|
22 |
-
|
|
|
23 |
transform = transforms.ToTensor()
|
24 |
org_img = input_img
|
25 |
input_img = transform(input_img)
|
26 |
-
input_img = input_img
|
27 |
input_img = input_img.unsqueeze(0)
|
28 |
outputs = model(input_img)
|
29 |
-
softmax = torch.nn.Softmax(dim=
|
30 |
-
o = softmax(outputs
|
31 |
-
confidences = {classes[i]: float(o[i]) for i in range(10)}
|
32 |
_, prediction = torch.max(outputs, 1)
|
33 |
target_layers = [model.convblock2_l1]
|
34 |
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
|
@@ -38,18 +40,67 @@ def inference(input_img, transparency = 0.5, target_layer_number = -1):
|
|
38 |
img = inv_normalize(img)
|
39 |
rgb_img = np.transpose(img, (1, 2, 0))
|
40 |
rgb_img = rgb_img.numpy()
|
41 |
-
visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
demo.launch()
|
|
|
7 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
8 |
from resnet import custom_ResNet
|
9 |
import gradio as gr
|
10 |
+
import os
|
11 |
+
|
12 |
|
13 |
model = custom_ResNet()
|
14 |
model.load_state_dict(torch.load("custom_resnet_model.pth", map_location=torch.device('cpu')), strict=False)
|
|
|
21 |
classes = ('plane', 'car', 'bird', 'cat', 'deer',
|
22 |
'dog', 'frog', 'horse', 'ship', 'truck')
|
23 |
|
24 |
+
|
25 |
+
def inference(input_img, transparency=0.5, target_layer_number=-1, top_classes=3):
|
26 |
transform = transforms.ToTensor()
|
27 |
org_img = input_img
|
28 |
input_img = transform(input_img)
|
|
|
29 |
input_img = input_img.unsqueeze(0)
|
30 |
outputs = model(input_img)
|
31 |
+
softmax = torch.nn.Softmax(dim=1) # Use dim=1 to compute softmax along the classes dimension
|
32 |
+
o = softmax(outputs)
|
33 |
+
confidences = {classes[i]: float(o[0, i]) for i in range(10)}
|
34 |
_, prediction = torch.max(outputs, 1)
|
35 |
target_layers = [model.convblock2_l1]
|
36 |
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
|
|
|
40 |
img = inv_normalize(img)
|
41 |
rgb_img = np.transpose(img, (1, 2, 0))
|
42 |
rgb_img = rgb_img.numpy()
|
43 |
+
visualization = show_cam_on_image(org_img / 255, grayscale_cam, use_rgb=True, image_weight=transparency)
|
44 |
+
|
45 |
+
# Sort the confidences dictionary by values in descending order
|
46 |
+
sorted_confidences = {k: v for k, v in sorted(confidences.items(), key=lambda item: item[1], reverse=True)}
|
47 |
+
# Take the top `top_classes` elements from the sorted_confidences
|
48 |
+
top_classes_confidences = {k: sorted_confidences[k] for k in list(sorted_confidences)[:top_classes]}
|
49 |
+
|
50 |
+
return top_classes_confidences, visualization
|
51 |
+
|
52 |
+
|
53 |
+
# Create a wrapper function for show_misclassified_images()
|
54 |
+
def show_misclassified_images_wrapper(num_images=10, use_gradcam=False, gradcam_layer=-1, transparency=0.5):
|
55 |
+
transparency = float(transparency)
|
56 |
+
num_images = int(num_images)
|
57 |
+
if use_gradcam == "Yes":
|
58 |
+
use_gradcam = True
|
59 |
+
else:
|
60 |
+
use_gradcam = False
|
61 |
+
|
62 |
+
return new_model.show_misclassified_images(num_images, use_gradcam, gradcam_layer, transparency)
|
63 |
+
|
64 |
+
description1 = "Input any image to test the models prediction"
|
65 |
+
|
66 |
+
|
67 |
+
# Define the full path to the images folder
|
68 |
+
images_folder = "examples"
|
69 |
+
|
70 |
+
# Define the examples list with full paths
|
71 |
+
examples = [[os.path.join(images_folder, "plane.jpeg"), 0.5, -1],
|
72 |
+
[os.path.join(images_folder, "car.jpg"), 0.5, -1],
|
73 |
+
[os.path.join(images_folder, "bird.jpeg"), 0.5, -1],
|
74 |
+
[os.path.join(images_folder, "cat.jpeg"), 0.5, -1],
|
75 |
+
[os.path.join(images_folder, "deer.jpeg"), 0.5, -1],
|
76 |
+
[os.path.join(images_folder, "dog.jpeg"), 0.5, -1],
|
77 |
+
[os.path.join(images_folder, "frog.jpeg"), 0.5, -1],
|
78 |
+
[os.path.join(images_folder, "horse.jpeg"), 0.5, -1],
|
79 |
+
[os.path.join(images_folder, "ship.jpeg"), 0.5, -1],
|
80 |
+
[os.path.join(images_folder, "truck.jpeg"), 0.5, -1]]
|
81 |
+
|
82 |
+
|
83 |
+
# Create a separate interface for the "Input an image" tab
|
84 |
+
input_interface = gr.Interface(inference,
|
85 |
+
inputs=[gr.Image(shape=(32, 32), label="Input Image"),
|
86 |
+
gr.Slider(0, 1, value=0.5, label="Opacity of GradCAM"),
|
87 |
+
gr.Slider(-2, -1, value=-2, step=1, label="Which Layer?"),
|
88 |
+
gr.Slider(1, 10, value=3, step=1, label="How many top confidence classes to be shown?")],
|
89 |
+
outputs=[gr.Label(),
|
90 |
+
gr.Image(shape=(32, 32), label="Model Prediction").style(width=128, height=128)],
|
91 |
+
description=description1,examples=examples)
|
92 |
+
|
93 |
+
description2 = "Displays misclassified image of the model"
|
94 |
+
|
95 |
+
# Create a separate interface for the "Misclassified Images" tab
|
96 |
+
misclassified_interface = gr.Interface(show_misclassified_images_wrapper,
|
97 |
+
inputs=[gr.Number(value=10, label="Number of images to display"),
|
98 |
+
gr.Radio(["Yes", "No"], value="No" , label="Show GradCAM outputs"),
|
99 |
+
gr.Slider(-2, -1, value=-1, step=1, label="Which layer for GradCAM?"),
|
100 |
+
gr.Slider(0, 1, value=0.5, label="Opacity of GradCAM")],
|
101 |
+
outputs=gr.Plot(), description=description2)
|
102 |
+
|
103 |
+
demo = gr.TabbedInterface([input_interface, misclassified_interface], tab_names=["Input an image", "Misclassified Images"],
|
104 |
+
title="Custom Resnet on CIFAR10 using GradCAM")
|
105 |
+
|
106 |
demo.launch()
|