nelbarman053 commited on
Commit
18b304b
·
1 Parent(s): 5388fd0

XAI implementation added

Browse files
Files changed (3) hide show
  1. app.py +21 -24
  2. requirements.txt +0 -0
  3. xai/xai_visualization.png +0 -0
app.py CHANGED
@@ -7,28 +7,14 @@ Original file is located at
7
  https://colab.research.google.com/drive/1XRB_m0JRoi0KugiHw5WSPJzV0udfU69O
8
  """
9
 
10
- # !nvidia-smi
11
-
12
- # Commented out IPython magic to ensure Python compatibility.
13
- # %reload_ext autoreload
14
- # %autoreload 2
15
- # %matplotlib inline
16
-
17
-
18
- # Commented out IPython magic to ensure Python compatibility.
19
- # %cd /content/drive/MyDrive/Bengali Fish Recognizer/
20
-
21
 
22
  import cv2
23
- import torch
24
  import numpy as np
25
  import gradio as gr
26
  import matplotlib as plt
27
  from fastai.vision.all import *
28
  from torchvision import transforms
29
- from torchvision.io import read_image
30
  from pytorch_grad_cam import GradCAM
31
- from pytorch_grad_cam.utils.image import show_cam_on_image
32
  from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
33
 
34
  import pathlib
@@ -52,20 +38,22 @@ def xai_visualization(image, image_tensor, targeted_category, model, target_laye
52
 
53
  mask = grayscale_cam[0, :]
54
 
 
 
 
 
55
  plt.imshow(image)
56
 
57
- plt.imshow(mask*255, cmap="plasma", alpha=0.6)
 
 
58
 
59
- plt.show()
60
 
61
  def preprocess_image(image_path):
62
- # Reading image
63
- # image = cv2.imread(image_path)
64
- image = torchvision.io.read_image(image_path)
65
 
66
  # Resizing an image
67
  image = cv2.resize(
68
- image,
69
  dsize=(224, 224),
70
  interpolation=cv2.INTER_CUBIC)
71
 
@@ -103,17 +91,26 @@ def classify_image(image_path):
103
  xai_visualization(image, img_tensor, targeted_category, pytorch_model, target_layer)
104
 
105
  # print(f"Category with most probability: {np.argmax(probs)}")
 
106
 
107
- return image_path, dict(zip(labels, map(float, probs)))
108
 
109
  # classify_image('test images/unknown_01.jpg')
110
 
111
 
112
- inputs = gr.Image()
 
 
113
 
114
  outputs = [
115
- gr.Image(),
116
- gr.Label(num_top_classes=5)
 
 
 
 
 
 
117
  ]
118
 
119
  examples = [
 
7
  https://colab.research.google.com/drive/1XRB_m0JRoi0KugiHw5WSPJzV0udfU69O
8
  """
9
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  import cv2
 
12
  import numpy as np
13
  import gradio as gr
14
  import matplotlib as plt
15
  from fastai.vision.all import *
16
  from torchvision import transforms
 
17
  from pytorch_grad_cam import GradCAM
 
18
  from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
19
 
20
  import pathlib
 
38
 
39
  mask = grayscale_cam[0, :]
40
 
41
+ plt.figure(figsize=(5,5))
42
+
43
+ plt.axis('off')
44
+
45
  plt.imshow(image)
46
 
47
+ plt.imshow(mask*255, cmap="plasma", alpha=0.7)
48
+
49
+ plt.savefig("xai/xai_visualization.png", dpi=150)
50
 
 
51
 
52
  def preprocess_image(image_path):
 
 
 
53
 
54
  # Resizing an image
55
  image = cv2.resize(
56
+ image_path,
57
  dsize=(224, 224),
58
  interpolation=cv2.INTER_CUBIC)
59
 
 
91
  xai_visualization(image, img_tensor, targeted_category, pytorch_model, target_layer)
92
 
93
  # print(f"Category with most probability: {np.argmax(probs)}")
94
+ xai_image = "xai/xai_visualization.png"
95
 
96
+ return xai_image, dict(zip(labels, map(float, probs)))
97
 
98
  # classify_image('test images/unknown_01.jpg')
99
 
100
 
101
+ inputs = gr.Image(
102
+ label = "Input Image"
103
+ )
104
 
105
  outputs = [
106
+ gr.Image(
107
+ label = "GradCAM visualization",
108
+ show_label = True
109
+ ),
110
+ gr.Label(
111
+ num_top_classes=5,
112
+ label="Predicted Category"
113
+ )
114
  ]
115
 
116
  examples = [
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
 
xai/xai_visualization.png ADDED