padmanabhbosamia commited on
Commit
8e487ef
·
1 Parent(s): 9d5e967

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -40
app.py CHANGED
@@ -5,12 +5,14 @@ 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 model import CustomResNet
 
 
 
9
 
10
- PATH = "model.pth"
11
- model= CustomResNet()
12
- model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')), strict=False)
13
- #model.to(device)
14
 
15
  inv_normalize = transforms.Normalize(
16
  mean=[-0.50/0.23, -0.50/0.23, -0.50/0.23],
@@ -19,18 +21,18 @@ inv_normalize = transforms.Normalize(
19
  classes = ('plane', 'car', 'bird', 'cat', 'deer',
20
  'dog', 'frog', 'horse', 'ship', 'truck')
21
 
22
- def inference(input_img, transparency = 0.5, target_layer_number = -1):
 
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=0)
30
- o = softmax(outputs.flatten())
31
- confidences = {classes[i]: float(o[i]) for i in range(10)}
32
  _, prediction = torch.max(outputs, 1)
33
- target_layers = [model.layer2[target_layer_number]]
34
  cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
35
  grayscale_cam = cam(input_tensor=input_img, targets=None)
36
  grayscale_cam = grayscale_cam[0, :]
@@ -38,33 +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
- return confidences, visualization
43
-
44
- title = "CIFAR10 trained on CustomBlock Model with GradCAM"
45
- description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
46
- examples = [["cat.jpg", 0.5, -1],
47
- ["dog.jpg", 0.5, -1],
48
- ["plane.jpg", 0.5, -1],
49
- ["car.jpg", 0.5, -1],
50
- ["bird.jpg", 0.5, -1],
51
- ["deer.jpg", 0.5, -1],
52
- ["frog.jpg", 0.5, -1],
53
- ["horse.jpg", 0.5, -1],
54
- ["ship.jpg", 0.5, -1],
55
- ["truck.jpg", 0.5, -1]
56
- ]
57
- demo = gr.Interface(
58
- inference,
59
- inputs = [gr.Image(shape=(32, 32), label="Input Image"),
60
- gr.Slider(0, 1, value = 0.5, label="Opacity of GradCAM"),
61
- gr.Slider(-2, -1, value = -2, step=1, label="Which Layer?"),
62
- gr.Slider(1, 10,step=1, label="How many missclassifed images"),
63
- gr.Slider(1, 10,step=1, label="How many Top Class"),
64
- ],
65
- outputs = [gr.Label(num_top_classes=3), gr.Image(shape=(32, 32), label="Output").style(width=128, height=128)],
66
- title = title,
67
- description = description,
68
- examples = examples,
69
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  demo.launch()
 
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 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)
15
+ model.setup(stage="test")
 
16
 
17
  inv_normalize = transforms.Normalize(
18
  mean=[-0.50/0.23, -0.50/0.23, -0.50/0.23],
 
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)
37
  grayscale_cam = cam(input_tensor=input_img, targets=None)
38
  grayscale_cam = grayscale_cam[0, :]
 
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 model.show_misclassified_images(num_images, use_gradcam, gradcam_layer, transparency)
63
+
64
+ description1 = "Test the model's prediction. Currently the model only supports the following classes - plane, car, bird, cat, deer, dog, frog, horse, ship, truck."
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.jpg"), 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=300, height=300)],
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()