nelbarman053
commited on
Commit
·
3548420
1
Parent(s):
9fca8bc
new updates
Browse files- app.py +124 -4
- xai/xai_visualization.png +0 -0
app.py
CHANGED
@@ -17,9 +17,9 @@ from torchvision import transforms
|
|
17 |
from pytorch_grad_cam import GradCAM
|
18 |
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
model = load_learner("models/recognizer_model.pkl")
|
25 |
|
@@ -28,6 +28,111 @@ pytorch_model = model.eval()
|
|
28 |
|
29 |
labels = ['Ayre', 'Catla', 'Chital', 'Ilish', 'Kachki', 'Kajoli', 'Koi', 'Magur', 'Mola Dhela', 'Mrigal', 'Pabda', 'Pangash', 'Poa', 'Puti', 'Rui', 'Shing', 'Silver Carp', 'Taki', 'Telapia', 'Tengra']
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
def xai_visualization(image, image_tensor, targeted_category, model, target_layers):
|
32 |
|
33 |
cam = GradCAM(model = model, target_layers = target_layers)
|
@@ -93,7 +198,19 @@ def classify_image(image_path):
|
|
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 |
|
@@ -107,6 +224,9 @@ outputs = [
|
|
107 |
label = "GradCAM visualization",
|
108 |
show_label = True
|
109 |
),
|
|
|
|
|
|
|
110 |
gr.Label(
|
111 |
num_top_classes=5,
|
112 |
label="Predicted Category"
|
|
|
17 |
from pytorch_grad_cam import GradCAM
|
18 |
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
19 |
|
20 |
+
import pathlib
|
21 |
+
temp = pathlib.PosixPath
|
22 |
+
pathlib.PosixPath = pathlib.WindowsPath
|
23 |
|
24 |
model = load_learner("models/recognizer_model.pkl")
|
25 |
|
|
|
28 |
|
29 |
labels = ['Ayre', 'Catla', 'Chital', 'Ilish', 'Kachki', 'Kajoli', 'Koi', 'Magur', 'Mola Dhela', 'Mrigal', 'Pabda', 'Pangash', 'Poa', 'Puti', 'Rui', 'Shing', 'Silver Carp', 'Taki', 'Telapia', 'Tengra']
|
30 |
|
31 |
+
|
32 |
+
found_and_test = {
|
33 |
+
'Ayre' : {
|
34 |
+
'found': 'Found in South and Southeast Asia',
|
35 |
+
'taste': 'Sweet taste',
|
36 |
+
'scientific name': 'Sperata aor',
|
37 |
+
},
|
38 |
+
'Catla' : {
|
39 |
+
'found': 'Native to South Asia',
|
40 |
+
'taste': 'sweet and nutty flavors',
|
41 |
+
'scientific name': 'Catla catla',
|
42 |
+
},
|
43 |
+
'Chital' : {
|
44 |
+
'found': 'Native to South and Southeast Asia',
|
45 |
+
'taste': 'sweet taste',
|
46 |
+
'scientific name': 'Chitala chitala',
|
47 |
+
},
|
48 |
+
'Ilish' : {
|
49 |
+
'found': 'Found in South Asia',
|
50 |
+
'taste': 'sweet and salty taste',
|
51 |
+
'scientific name': 'Tenualosa ilisha',
|
52 |
+
},
|
53 |
+
'Kachki' : {
|
54 |
+
'found': 'Found in South and Southeast Asia',
|
55 |
+
'taste': 'great when fresh',
|
56 |
+
'scientific name': 'Corica soborna Hamilo',
|
57 |
+
},
|
58 |
+
'Kajoli' : {
|
59 |
+
'found': 'Native to Asia',
|
60 |
+
'taste': 'delicate and tastes divine',
|
61 |
+
'scientific name': 'Ailia coila',
|
62 |
+
},
|
63 |
+
'Koi' : {
|
64 |
+
'found': 'Native to Asia and Europe',
|
65 |
+
'taste': 'mild flavor and firm flesh that tastes slightly sweet',
|
66 |
+
'scientific name': 'Cyprinus rubrofuscus',
|
67 |
+
},
|
68 |
+
'Magur' : {
|
69 |
+
'found': 'Native to Southeast Asia',
|
70 |
+
'taste': 'delicate flavor',
|
71 |
+
'scientific name': 'Clarias batrachus',
|
72 |
+
},
|
73 |
+
'Mola Dhela' : {
|
74 |
+
'found': 'Found in South Asia',
|
75 |
+
'taste': 'black cod or lobster',
|
76 |
+
'scientific name': 'Osteobrama cotio',
|
77 |
+
},
|
78 |
+
'Mrigal' : {
|
79 |
+
'found': 'Native to the Indo-Gangetic riverine systems of South Asia.',
|
80 |
+
'taste': 'sweet, mild taste',
|
81 |
+
'scientific name': 'Cirrhinus cirrhosus',
|
82 |
+
},
|
83 |
+
'Pabda' : {
|
84 |
+
'found': 'freshwater',
|
85 |
+
'taste': 'mild taste',
|
86 |
+
'scientific name': 'Ompok bimaculatus',
|
87 |
+
},
|
88 |
+
'Pangash' : {
|
89 |
+
'found': 'Native to South and Southeast Asia',
|
90 |
+
'taste': 'Mild, sweet, and moist taste',
|
91 |
+
'scientific name': 'Pangasius pangasius',
|
92 |
+
},
|
93 |
+
'Poa' : {
|
94 |
+
'found': 'Found in South Asia',
|
95 |
+
'taste': 'Mild and flaky to bold and robust',
|
96 |
+
'scientific name': 'Otolithoides pama',
|
97 |
+
},
|
98 |
+
'Puti' : {
|
99 |
+
'found': 'native to Asia',
|
100 |
+
'taste': 'does not taste so good but bitter',
|
101 |
+
'scientific name': 'Puntius sophore',
|
102 |
+
},
|
103 |
+
'Rui' : {
|
104 |
+
'found': 'Found in South Asia',
|
105 |
+
'taste': 'almost free of a "fishy" taste',
|
106 |
+
'scientific name': 'Labeo rohita',
|
107 |
+
},
|
108 |
+
'Shing' : {
|
109 |
+
'found': 'native to South Asia',
|
110 |
+
'taste': 'sweet, mild taste',
|
111 |
+
'scientific name': 'Heteropneustes fossilis',
|
112 |
+
},
|
113 |
+
'Silver Carp' : {
|
114 |
+
'found': 'native to eastern Asia',
|
115 |
+
'taste': 'similar in flavor and texture to tilapia and catfish',
|
116 |
+
'scientific name': 'Hypophthalmichthys molitrix',
|
117 |
+
},
|
118 |
+
'Taki' : {
|
119 |
+
'found': 'found in South and Southeast Asia',
|
120 |
+
'taste': 'mild flavor',
|
121 |
+
'scientific name': 'Channa striata',
|
122 |
+
},
|
123 |
+
'Telapia' : {
|
124 |
+
'found': 'native to Africa and the Levant',
|
125 |
+
'taste': 'sweet, mild taste',
|
126 |
+
'scientific name': 'Oreochromis niloticus',
|
127 |
+
},
|
128 |
+
'Tengra' : {
|
129 |
+
'found': 'found in South and Southeast Asia',
|
130 |
+
'taste': 'delicate, slightly sweet taste',
|
131 |
+
'scientific name': 'Mystus tengara',
|
132 |
+
},
|
133 |
+
}
|
134 |
+
|
135 |
+
|
136 |
def xai_visualization(image, image_tensor, targeted_category, model, target_layers):
|
137 |
|
138 |
cam = GradCAM(model = model, target_layers = target_layers)
|
|
|
198 |
# print(f"Category with most probability: {np.argmax(probs)}")
|
199 |
xai_image = "xai/xai_visualization.png"
|
200 |
|
201 |
+
# return xai_image, dict(zip(labels, map(float, probs)))
|
202 |
+
|
203 |
+
predicted_category = labels[targeted_category]
|
204 |
+
|
205 |
+
information = ""
|
206 |
+
|
207 |
+
if predicted_category in found_and_test.keys():
|
208 |
+
information = found_and_test[predicted_category]
|
209 |
+
|
210 |
+
# return image_path, information, dict(zip(labels, map(float, probs)))
|
211 |
+
# print(information)
|
212 |
+
|
213 |
+
return xai_image, information, dict(zip(labels, map(float, probs)))
|
214 |
|
215 |
# classify_image('test images/unknown_01.jpg')
|
216 |
|
|
|
224 |
label = "GradCAM visualization",
|
225 |
show_label = True
|
226 |
),
|
227 |
+
gr.Label(
|
228 |
+
label = "Information"
|
229 |
+
),
|
230 |
gr.Label(
|
231 |
num_top_classes=5,
|
232 |
label="Predicted Category"
|
xai/xai_visualization.png
CHANGED