Spaces:
Runtime error
Runtime error
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# gradioMisClassGradCAMimageInputter
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
from pytorch_grad_cam import GradCAM
|
6 |
+
from pytorch_grad_cam.utils.image import show_cam_on_image
|
7 |
+
from torchvision import datasets, transforms, utils
|
8 |
+
|
9 |
+
fileName = None
|
10 |
+
|
11 |
+
def hello(DoYouWantToShowMisClassifiedImages, HowManyImages):
|
12 |
+
if(DoYouWantToShowMisClassifiedImages.lower() == "yes"):
|
13 |
+
fileName = misclas_helper.display_cifar_misclassified_data(misclassified_data, classes, inv_normalize, number_of_samples=HowManyImages)
|
14 |
+
return Image.open(fileName)
|
15 |
+
else:
|
16 |
+
return None
|
17 |
+
misClass_demo = gr.Interface(
|
18 |
+
fn = hello,
|
19 |
+
inputs=['text', gr.Slider(0, 20, step=5)],
|
20 |
+
outputs=['image'],
|
21 |
+
title="Misclasseified Images",
|
22 |
+
description="If your answer to the question DoYouWantToShowMisClassifiedImages is yes, then only it works.",
|
23 |
+
)
|
24 |
+
|
25 |
+
|
26 |
+
############
|
27 |
+
|
28 |
+
targets = None
|
29 |
+
device = torch.device("cpu")
|
30 |
+
classes = ('plane', 'car', 'bird', 'cat', 'deer',
|
31 |
+
'dog', 'frog', 'horse', 'ship', 'truck')
|
32 |
+
|
33 |
+
|
34 |
+
def inference(DoYouWantToShowGradCAMMedImages, HowManyImages, WhichLayer, transparency):
|
35 |
+
if(DoYouWantToShowGradCAMMedImages.lower() == "yes"):
|
36 |
+
if(WhichLayer == -1):
|
37 |
+
target_layers = [model.model.resNetLayer2Part2[-1]]
|
38 |
+
elif(WhichLayer == -2):
|
39 |
+
target_layers = [model.model.resNetLayer2Part1[-1]]
|
40 |
+
elif(WhichLayer == -3):
|
41 |
+
target_layers = [model.model.Layer3[-1]]
|
42 |
+
fileName = gradcam_helper.display_gradcam_output(misclassified_data, classes, inv_normalize, model.model, target_layers, targets, number_of_samples=HowManyImages, transparency=0.70)
|
43 |
+
return Image.open(fileName)
|
44 |
+
|
45 |
+
gradCAM_demo = gr.Interface(
|
46 |
+
fn=inference,
|
47 |
+
#DoYouWantToShowGradCAMMedImages, HowManyImages, WhichLayer, transparency
|
48 |
+
inputs=['text', gr.Slider(0, 20, step=5), gr.Slider(-3, -1, value = -1, step=1), gr.Slider(0, 1, value = 0.7, label = "Overall Opacity of the Overlay")],
|
49 |
+
outputs=['image'],
|
50 |
+
title="GradCammd Images",
|
51 |
+
description="If your answer to the question DoYouWantToShowGradCAMMedImages is yes, then only it works.",
|
52 |
+
)
|
53 |
+
|
54 |
+
|
55 |
+
############
|
56 |
+
|
57 |
+
def ImageInputter(img1, img2, img3, img4, img5, img6, img7, img8, img9, img10):
|
58 |
+
return img1, img2, img3, img4, img5, img6, img7, img8, img9, img10
|
59 |
+
|
60 |
+
imageInputter_demo = gr.Interface(
|
61 |
+
ImageInputter,
|
62 |
+
[
|
63 |
+
"image","image","image","image","image","image","image","image","image","image"
|
64 |
+
],
|
65 |
+
[
|
66 |
+
"image","image","image","image","image","image","image","image","image","image"
|
67 |
+
],
|
68 |
+
examples=[
|
69 |
+
["bird.jpg", "car.jpg", "cat.jpg"],
|
70 |
+
["deer.jpg", "dog.jpg", "frog.jpg"],
|
71 |
+
["horse.jpg", "plane.jpg", "ship.jpg"],
|
72 |
+
[None, "truck.jpg", None],
|
73 |
+
],
|
74 |
+
title="Max 10 images input",
|
75 |
+
description="Here's a sample image inputter. Allows you to feed in 10 images and display them. You may drag and drop images from bottom examples to the input feeders",
|
76 |
+
)
|
77 |
+
|
78 |
+
|
79 |
+
############
|
80 |
+
|
81 |
+
|
82 |
+
demo = gr.TabbedInterface(
|
83 |
+
interface_list = [misClass_demo, gradCAM_demo, imageInputter_demo],
|
84 |
+
tab_names = ["MisClassified Images", "GradCAMMed Images", "10 images inputter"]
|
85 |
+
)
|
86 |
+
|
87 |
+
demo.launch(debug=True)
|