Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -40,6 +40,40 @@ def inference(input_img, transparency = 0.5, target_layer_number = -1):
|
|
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 = "Gradio interface to infer on ResNet18 model, and get GradCAM results"
|
45 |
examples = [["cat.jpg", 0.5, -1], ["dog.jpg", 0.5, -1]]
|
@@ -62,7 +96,20 @@ demo = gr.Interface(
|
|
62 |
gr.Image(label="Uploaded image" if "Interface" in gr.__file__ else "Image"),
|
63 |
gr.Button(label="Submit", type="boolean")
|
64 |
],
|
65 |
-
outputs=gr.
|
|
|
66 |
live=True
|
67 |
)
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
|
41 |
return confidences, visualization
|
42 |
|
43 |
+
def inference_confidences(input_img, transparency = 0.5, target_layer_number = -1):
|
44 |
+
transform = transforms.ToTensor()
|
45 |
+
org_img = input_img
|
46 |
+
input_img = transform(input_img)
|
47 |
+
input_img = input_img
|
48 |
+
input_img = input_img.unsqueeze(0)
|
49 |
+
outputs = model(input_img)
|
50 |
+
softmax = torch.nn.Softmax(dim=0)
|
51 |
+
o = softmax(outputs.flatten())
|
52 |
+
confidences = {classes[i]: float(o[i]) for i in range(10)}
|
53 |
+
return confidences
|
54 |
+
|
55 |
+
def inference_visualization(input_img, transparency = 0.5, target_layer_number = -1):
|
56 |
+
transform = transforms.ToTensor()
|
57 |
+
org_img = input_img
|
58 |
+
input_img = transform(input_img)
|
59 |
+
input_img = input_img
|
60 |
+
input_img = input_img.unsqueeze(0)
|
61 |
+
outputs = model(input_img)
|
62 |
+
softmax = torch.nn.Softmax(dim=0)
|
63 |
+
o = softmax(outputs.flatten())
|
64 |
+
confidences = {classes[i]: float(o[i]) for i in range(10)}
|
65 |
+
_, prediction = torch.max(outputs, 1)
|
66 |
+
target_layers = [model.layer2[target_layer_number]]
|
67 |
+
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
|
68 |
+
grayscale_cam = cam(input_tensor=input_img, targets=None)
|
69 |
+
grayscale_cam = grayscale_cam[0, :]
|
70 |
+
img = input_img.squeeze(0)
|
71 |
+
img = inv_normalize(img)
|
72 |
+
rgb_img = np.transpose(img, (1, 2, 0))
|
73 |
+
rgb_img = rgb_img.numpy()
|
74 |
+
visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
|
75 |
+
return visualization
|
76 |
+
|
77 |
title = "CIFAR10 trained on ResNet18 Model with GradCAM"
|
78 |
description = "Gradio interface to infer on ResNet18 model, and get GradCAM results"
|
79 |
examples = [["cat.jpg", 0.5, -1], ["dog.jpg", 0.5, -1]]
|
|
|
96 |
gr.Image(label="Uploaded image" if "Interface" in gr.__file__ else "Image"),
|
97 |
gr.Button(label="Submit", type="boolean")
|
98 |
],
|
99 |
+
outputs = [gr.Label(num_top_classes=3), gr.Image(shape=(32, 32), label="Output").style(width=128, height=128)],
|
100 |
+
examples = examples,
|
101 |
live=True
|
102 |
)
|
103 |
+
|
104 |
+
# Callback function for the Gradio interface
|
105 |
+
def gradio_callback(view_gradcam, num_gradcam_images, layer_name, opacity,
|
106 |
+
view_misclassified, num_misclassified_images,
|
107 |
+
image_source, uploaded_image, submit,input_img, transparency = 0.5, target_layer_number = -1):
|
108 |
+
confidence = inference_confidences(input_img, transparency = 0.5, target_layer_number = -1)
|
109 |
+
visualization = inference_visualization(input_img, transparency = 0.5, target_layer_number = -1)
|
110 |
+
return confidence, visualization
|
111 |
+
|
112 |
+
|
113 |
+
# Set the callback function to the Gradio interface
|
114 |
+
demo.fn = gradio_callback
|
115 |
+
demo.launch()
|