File size: 3,121 Bytes
8d26f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# gradioMisClassGradCAMimageInputter
import numpy as np
import gradio as gr
from PIL import Image
from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.image import show_cam_on_image
from torchvision import datasets, transforms, utils

fileName = None

def hello(DoYouWantToShowMisClassifiedImages, HowManyImages):
  if(DoYouWantToShowMisClassifiedImages.lower() == "yes"):
    fileName = misclas_helper.display_cifar_misclassified_data(misclassified_data, classes, inv_normalize, number_of_samples=HowManyImages)
    return Image.open(fileName)
  else:
    return None
misClass_demo = gr.Interface(
    fn = hello,
    inputs=['text', gr.Slider(0, 20, step=5)],
    outputs=['image'],
    title="Misclasseified Images",
    description="If your answer to the question DoYouWantToShowMisClassifiedImages is yes, then only it works.",
)


############

targets = None
device = torch.device("cpu")
classes = ('plane', 'car', 'bird', 'cat', 'deer',
           'dog', 'frog', 'horse', 'ship', 'truck')


def inference(DoYouWantToShowGradCAMMedImages, HowManyImages, WhichLayer, transparency):
  if(DoYouWantToShowGradCAMMedImages.lower() == "yes"):
    if(WhichLayer == -1):
      target_layers = [model.model.resNetLayer2Part2[-1]]
    elif(WhichLayer == -2):
      target_layers = [model.model.resNetLayer2Part1[-1]]
    elif(WhichLayer == -3):
      target_layers = [model.model.Layer3[-1]]
    fileName = gradcam_helper.display_gradcam_output(misclassified_data, classes, inv_normalize, model.model, target_layers, targets, number_of_samples=HowManyImages, transparency=0.70)
    return Image.open(fileName)

gradCAM_demo = gr.Interface(
    fn=inference,
        #DoYouWantToShowGradCAMMedImages, HowManyImages, WhichLayer, transparency
    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")],
    outputs=['image'],
    title="GradCammd Images",
    description="If your answer to the question DoYouWantToShowGradCAMMedImages is yes, then only it works.",
)


############

def ImageInputter(img1, img2, img3, img4, img5, img6, img7, img8, img9, img10):
  return img1, img2, img3, img4, img5, img6, img7, img8, img9, img10

imageInputter_demo = gr.Interface(
    ImageInputter,
    [
        "image","image","image","image","image","image","image","image","image","image"
    ],
    [
        "image","image","image","image","image","image","image","image","image","image"
    ],
    examples=[
        ["bird.jpg", "car.jpg", "cat.jpg"],
        ["deer.jpg", "dog.jpg", "frog.jpg"],
        ["horse.jpg", "plane.jpg", "ship.jpg"],
        [None, "truck.jpg", None],
    ],
    title="Max 10 images input",
    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",
)


############


demo = gr.TabbedInterface(
    interface_list = [misClass_demo, gradCAM_demo, imageInputter_demo],
    tab_names = ["MisClassified Images", "GradCAMMed Images", "10 images inputter"]
)

demo.launch(debug=True)