Anthony-Ml commited on
Commit
311d8c3
1 Parent(s): 94d2d6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -1
app.py CHANGED
@@ -2,6 +2,11 @@ import gradio as gr
2
  from fastai.vision.all import *
3
  from efficientnet_pytorch import EfficientNet
4
 
 
 
 
 
 
5
 
6
  title = "COVID_19 Infection Detectation App!"
7
  head = (
@@ -56,7 +61,42 @@ def predict_image(get_image):
56
  pred, idx, probs = learn.predict(get_image)
57
  return dict(zip(categories, map(float, probs)))
58
 
59
- interpretation="default"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  enable_queue=True
61
 
62
  gr.Interface(fn=predict_image, inputs=gr.Image(shape=(224,224)),
 
2
  from fastai.vision.all import *
3
  from efficientnet_pytorch import EfficientNet
4
 
5
+ import torch, torchvision
6
+ from torchvision import transforms
7
+ from pytorch_grad_cam import GradCAM
8
+ from pytorch_grad_cam.utils.image import show_cam_on_image
9
+ from PIL import Image
10
 
11
  title = "COVID_19 Infection Detectation App!"
12
  head = (
 
61
  pred, idx, probs = learn.predict(get_image)
62
  return dict(zip(categories, map(float, probs)))
63
 
64
+ def interpretation_function(get_image):
65
+ # Create or load your PyTorch model
66
+ target_layer = learn.layer4[-1]
67
+ # Create an instance of GradCAM
68
+ cam = GradCAM(model=learn, target_layer=target_layer)
69
+ # Load and preprocess your image
70
+ image_path = get_image #'your_image.jpg'
71
+ image = Image.open(image_path)
72
+ preprocess = transforms.Compose([
73
+ transforms.Resize((224, 224)),
74
+ transforms.ToTensor(),
75
+ ])
76
+ input_image = preprocess(image).unsqueeze(0) # Add a batch dimension
77
+ input_image = input_image.to('cuda' if torch.cuda.is_available() else 'cpu')
78
+ # Compute the CAM
79
+ cam_image = cam(input_tensor=input_image, target_category=None)
80
+ # Compute the CAM
81
+ cam_image = cam(input_tensor=input_image, target_category=None)
82
+ # Show the CAM on the original image
83
+ visualization = show_cam_on_image(input_image, cam_image)
84
+ return visualization
85
+
86
+ with gr.Blocks() as demo:
87
+ with gr.Row():
88
+ with gr.Column():
89
+ input_img = gr.Image(label="Input Image", shape=(224, 224))
90
+ with gr.Row():
91
+ interpret = gr.Button("Interpret")
92
+ with gr.Column():
93
+ label = gr.Label(label="Predicted Class")
94
+ with gr.Column():
95
+ interpretation = gr.components.Interpretation(input_img)
96
+ interpret.click(interpretation_function, input_img, interpretation)
97
+
98
+
99
+ #interpretation="default"
100
  enable_queue=True
101
 
102
  gr.Interface(fn=predict_image, inputs=gr.Image(shape=(224,224)),