nelbarman053
commited on
Commit
·
5388fd0
1
Parent(s):
1b52c9a
App on updating
Browse files- app.py +107 -21
- models/{recgonizer_model.pkl → recognizer_model.pkl} +0 -0
- previous_files/app.py +126 -0
app.py
CHANGED
@@ -1,30 +1,120 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
-
"""
|
3 |
|
4 |
Automatically generated by Colaboratory.
|
5 |
|
6 |
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/
|
8 |
"""
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
import gradio as gr
|
11 |
-
|
12 |
from fastai.vision.all import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
# temp = pathlib.PosixPath
|
16 |
-
# pathlib.PosixPath = pathlib.WindowsPath
|
17 |
|
18 |
-
|
|
|
19 |
|
20 |
labels = ['Ayre', 'Catla', 'Chital', 'Ilish', 'Kachki', 'Kajoli', 'Koi', 'Magur', 'Mola Dhela', 'Mrigal', 'Pabda', 'Pangash', 'Poa', 'Puti', 'Rui', 'Shing', 'Silver Carp', 'Taki', 'Telapia', 'Tengra']
|
21 |
|
22 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
label, _, probs = model.predict(image_path)
|
24 |
-
return dict(zip(labels, map(float, probs)))
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
examples = [
|
30 |
'test images/unknown_01.jpg',
|
@@ -46,15 +136,11 @@ examples = [
|
|
46 |
'test images/unknown_17.jpg'
|
47 |
]
|
48 |
|
49 |
-
interface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
interface.launch()
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
+
"""xai_app.ipynb
|
3 |
|
4 |
Automatically generated by Colaboratory.
|
5 |
|
6 |
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
|
35 |
+
temp = pathlib.PosixPath
|
36 |
+
pathlib.PosixPath = pathlib.WindowsPath
|
37 |
|
38 |
+
model = load_learner("models/recognizer_model.pkl")
|
|
|
|
|
39 |
|
40 |
+
# Transforming to pytorch model
|
41 |
+
pytorch_model = model.eval()
|
42 |
|
43 |
labels = ['Ayre', 'Catla', 'Chital', 'Ilish', 'Kachki', 'Kajoli', 'Koi', 'Magur', 'Mola Dhela', 'Mrigal', 'Pabda', 'Pangash', 'Poa', 'Puti', 'Rui', 'Shing', 'Silver Carp', 'Taki', 'Telapia', 'Tengra']
|
44 |
|
45 |
+
def xai_visualization(image, image_tensor, targeted_category, model, target_layers):
|
46 |
+
|
47 |
+
cam = GradCAM(model = model, target_layers = target_layers)
|
48 |
+
|
49 |
+
targets = [ClassifierOutputTarget(targeted_category)]
|
50 |
+
|
51 |
+
grayscale_cam = cam(input_tensor = image_tensor, targets = targets)
|
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 |
+
|
72 |
+
# Converting image to tensor
|
73 |
+
img_tensor = transforms.ToTensor()(image)
|
74 |
+
|
75 |
+
# Converting image to batch
|
76 |
+
img_tensor = img_tensor.reshape(1,3,224,224)
|
77 |
+
|
78 |
+
return image, img_tensor
|
79 |
+
|
80 |
+
def target_layers_finding(model):
|
81 |
+
# Available layers
|
82 |
+
layers = list(model.named_modules())
|
83 |
+
|
84 |
+
# For Resnet-50
|
85 |
+
target_layers = [layers[len(layers)-20][1]]
|
86 |
+
|
87 |
+
return target_layers
|
88 |
+
|
89 |
+
def classify_image(image_path):
|
90 |
+
|
91 |
+
# Model Prediction
|
92 |
label, _, probs = model.predict(image_path)
|
|
|
93 |
|
94 |
+
# Predicted Category
|
95 |
+
targeted_category = np.argmax(probs)
|
96 |
+
|
97 |
+
# Preprocessed image and image tensor
|
98 |
+
image, img_tensor = preprocess_image(image_path)
|
99 |
+
|
100 |
+
# Target layer
|
101 |
+
target_layer = target_layers_finding(pytorch_model)
|
102 |
+
|
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 = [
|
120 |
'test images/unknown_01.jpg',
|
|
|
136 |
'test images/unknown_17.jpg'
|
137 |
]
|
138 |
|
139 |
+
interface = gr.Interface(
|
140 |
+
fn = classify_image,
|
141 |
+
inputs = inputs,
|
142 |
+
outputs = outputs,
|
143 |
+
examples = examples
|
144 |
+
)
|
145 |
|
146 |
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models/{recgonizer_model.pkl → recognizer_model.pkl}
RENAMED
File without changes
|
previous_files/app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# # -*- coding: utf-8 -*-
|
2 |
+
# """app creation.ipynb
|
3 |
+
|
4 |
+
# Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
# Original file is located at
|
7 |
+
# https://colab.research.google.com/drive/1c8HIdMTAJxhNiPY7_kmzP78yFiwdhh8Q
|
8 |
+
# """
|
9 |
+
|
10 |
+
# import gradio as gr
|
11 |
+
# from fastai import *
|
12 |
+
# from fastai.vision.all import *
|
13 |
+
|
14 |
+
# # import pathlib
|
15 |
+
# # temp = pathlib.PosixPath
|
16 |
+
# # pathlib.PosixPath = pathlib.WindowsPath
|
17 |
+
|
18 |
+
# model = load_learner("models/recgonizer_model.pkl")
|
19 |
+
|
20 |
+
# labels = ['Ayre', 'Catla', 'Chital', 'Ilish', 'Kachki', 'Kajoli', 'Koi', 'Magur', 'Mola Dhela', 'Mrigal', 'Pabda', 'Pangash', 'Poa', 'Puti', 'Rui', 'Shing', 'Silver Carp', 'Taki', 'Telapia', 'Tengra']
|
21 |
+
|
22 |
+
# def recognize_image(image_path):
|
23 |
+
# label, _, probs = model.predict(image_path)
|
24 |
+
# return dict(zip(labels, map(float, probs)))
|
25 |
+
|
26 |
+
# inputs = gr.inputs.Image(shape=(224,224))
|
27 |
+
# outputs = gr.outputs.Label()
|
28 |
+
|
29 |
+
# examples = [
|
30 |
+
# 'test images/unknown_01.jpg',
|
31 |
+
# 'test images/unknown_02.png',
|
32 |
+
# 'test images/unknown_03.jpg',
|
33 |
+
# 'test images/unknown_04.jpg',
|
34 |
+
# 'test images/unknown_05.jpg',
|
35 |
+
# 'test images/unknown_06.jpg',
|
36 |
+
# 'test images/unknown_07.jpg',
|
37 |
+
# 'test images/unknown_08.jpg',
|
38 |
+
# 'test images/unknown_09.jpg',
|
39 |
+
# 'test images/unknown_10.jpg',
|
40 |
+
# 'test images/unknown_11.jpg',
|
41 |
+
# 'test images/unknown_12.png',
|
42 |
+
# 'test images/unknown_13.jpg',
|
43 |
+
# 'test images/unknown_14.png',
|
44 |
+
# 'test images/unknown_15.png',
|
45 |
+
# 'test images/unknown_16.png',
|
46 |
+
# 'test images/unknown_17.jpg'
|
47 |
+
# ]
|
48 |
+
|
49 |
+
# interface = gr.Interface(fn=recognize_image, inputs = inputs, outputs=outputs, examples = examples)
|
50 |
+
|
51 |
+
# interface.launch()
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
# -*- coding: utf-8 -*-
|
57 |
+
"""app creation.ipynb
|
58 |
+
|
59 |
+
Automatically generated by Colaboratory.
|
60 |
+
|
61 |
+
Original file is located at
|
62 |
+
https://colab.research.google.com/drive/1c8HIdMTAJxhNiPY7_kmzP78yFiwdhh8Q
|
63 |
+
"""
|
64 |
+
|
65 |
+
import numpy as np
|
66 |
+
import gradio as gr
|
67 |
+
from fastai import *
|
68 |
+
from fastai.vision.all import *
|
69 |
+
|
70 |
+
import pathlib
|
71 |
+
temp = pathlib.PosixPath
|
72 |
+
pathlib.PosixPath = pathlib.WindowsPath
|
73 |
+
|
74 |
+
model = load_learner("models/recgonizer_model.pkl")
|
75 |
+
|
76 |
+
labels = ['Ayre', 'Catla', 'Chital', 'Ilish', 'Kachki', 'Kajoli', 'Koi', 'Magur', 'Mola Dhela', 'Mrigal', 'Pabda', 'Pangash', 'Poa', 'Puti', 'Rui', 'Shing', 'Silver Carp', 'Taki', 'Telapia', 'Tengra']
|
77 |
+
|
78 |
+
def recognize_image(image_path):
|
79 |
+
label, _, probs = model.predict(image_path)
|
80 |
+
# return dict(zip(labels, map(float, probs)))
|
81 |
+
print(f"Category with most probability: {np.argmax(probs)}")
|
82 |
+
return image_path, dict(zip(labels, map(float, probs)))
|
83 |
+
|
84 |
+
# inputs = gr.inputs.Image(shape=(224,224))
|
85 |
+
# outputs = gr.outputs.Label()
|
86 |
+
|
87 |
+
examples = [
|
88 |
+
'test images/unknown_01.jpg',
|
89 |
+
'test images/unknown_02.png',
|
90 |
+
'test images/unknown_03.jpg',
|
91 |
+
'test images/unknown_04.jpg',
|
92 |
+
'test images/unknown_05.jpg',
|
93 |
+
'test images/unknown_06.jpg',
|
94 |
+
'test images/unknown_07.jpg',
|
95 |
+
'test images/unknown_08.jpg',
|
96 |
+
'test images/unknown_09.jpg',
|
97 |
+
'test images/unknown_10.jpg',
|
98 |
+
'test images/unknown_11.jpg',
|
99 |
+
'test images/unknown_12.png',
|
100 |
+
'test images/unknown_13.jpg',
|
101 |
+
'test images/unknown_14.png',
|
102 |
+
'test images/unknown_15.png',
|
103 |
+
'test images/unknown_16.png',
|
104 |
+
'test images/unknown_17.jpg'
|
105 |
+
]
|
106 |
+
|
107 |
+
interface = gr.Interface(fn=recognize_image, inputs = gr.Image(), outputs = [gr.Image(height=224, width=224), gr.Label(num_top_classes=5)] , examples = examples)
|
108 |
+
|
109 |
+
interface.launch()
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
|