Spaces:
Sleeping
Sleeping
keremoktay1
commited on
Commit
•
1cb76d4
1
Parent(s):
48568eb
Upload 10 files
Browse files- Gradio.py +118 -0
- cnn_1layer.h5 +3 -0
- cnn_2layer.h5 +3 -0
- cnn_3layer.h5 +3 -0
- dt2.pkl +3 -0
- dt3.pkl +3 -0
- dt5.pkl +3 -0
- knn1.pkl +3 -0
- knn3.pkl +3 -0
- knn5.pkl +3 -0
Gradio.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Update the path to the directory where your models are stored
|
8 |
+
model_directory = '/Users/keremoktay/Desktop/Gradio/'
|
9 |
+
|
10 |
+
# Load the decision tree and KNN models
|
11 |
+
dt_models = {
|
12 |
+
"2": pickle.load(open(model_directory + 'dt2.pkl', 'rb')),
|
13 |
+
"3": pickle.load(open(model_directory + 'dt3.pkl', 'rb')),
|
14 |
+
"5": pickle.load(open(model_directory + 'dt5.pkl', 'rb'))
|
15 |
+
}
|
16 |
+
knn_models = {
|
17 |
+
"1": pickle.load(open(model_directory + 'knn1.pkl', 'rb')),
|
18 |
+
"3": pickle.load(open(model_directory + 'knn3.pkl', 'rb')),
|
19 |
+
"5": pickle.load(open(model_directory + 'knn5.pkl', 'rb'))
|
20 |
+
}
|
21 |
+
|
22 |
+
# Load the neural network models
|
23 |
+
nn_models = {
|
24 |
+
"1": load_model(model_directory + 'cnn_1layer.h5'),
|
25 |
+
"2": load_model(model_directory + 'cnn_2layer.h5'),
|
26 |
+
"3": load_model(model_directory + 'cnn_3layer.h5')
|
27 |
+
}
|
28 |
+
|
29 |
+
def preprocess_image_for_ml(image_path):
|
30 |
+
try:
|
31 |
+
# For traditional ML models, just flatten the image without resizing
|
32 |
+
image = Image.open(image_path)
|
33 |
+
image_array = np.asarray(image).flatten().reshape(1, -1)
|
34 |
+
return image_array
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Error in preprocess_image_for_ml: {e}")
|
37 |
+
raise e
|
38 |
+
|
39 |
+
def preprocess_image_for_cnn(image_path, target_size=(128, 128)):
|
40 |
+
try:
|
41 |
+
# For CNN models, convert to RGB, resize to 128x128, normalize, and add a batch dimension
|
42 |
+
image = Image.open(image_path).convert('RGB')
|
43 |
+
image = image.resize(target_size)
|
44 |
+
image_array = np.asarray(image) / 255.0
|
45 |
+
image_array = np.expand_dims(image_array, axis=0)
|
46 |
+
return image_array
|
47 |
+
except Exception as e:
|
48 |
+
print(f"Error in preprocess_image_for_cnn: {e}")
|
49 |
+
raise e
|
50 |
+
|
51 |
+
class_names = {
|
52 |
+
0: "Angular Leaf Spot",
|
53 |
+
1: "Bean Rust",
|
54 |
+
2: "Healthy"
|
55 |
+
}
|
56 |
+
|
57 |
+
def classify_image_with_decision_tree(image, depth):
|
58 |
+
try:
|
59 |
+
image_array = preprocess_image_for_ml(image)
|
60 |
+
model = dt_models.get(depth)
|
61 |
+
prediction = model.predict(image_array)
|
62 |
+
# Sayısal tahmini hastalık ismine dönüştür
|
63 |
+
return class_names[int(prediction)]
|
64 |
+
except Exception as e:
|
65 |
+
print(f"Error in classify_image_with_decision_tree: {e}")
|
66 |
+
raise e
|
67 |
+
|
68 |
+
def classify_image_with_knn(image, k):
|
69 |
+
try:
|
70 |
+
image_array = preprocess_image_for_ml(image)
|
71 |
+
model = knn_models.get(k)
|
72 |
+
prediction = model.predict(image_array)
|
73 |
+
# Sayısal tahmini hastalık ismine dönüştür
|
74 |
+
return class_names[int(prediction)]
|
75 |
+
except Exception as e:
|
76 |
+
print(f"Error in classify_image_with_knn: {e}")
|
77 |
+
raise e
|
78 |
+
|
79 |
+
def classify_image_with_neural_network(image, layers):
|
80 |
+
try:
|
81 |
+
image_array = preprocess_image_for_cnn(image)
|
82 |
+
model = nn_models.get(layers)
|
83 |
+
prediction = model.predict(image_array)
|
84 |
+
# En yüksek tahmin değerine sahip indeksi bul ve hastalık ismine dönüştür
|
85 |
+
return class_names[np.argmax(prediction, axis=1)[0]]
|
86 |
+
except Exception as e:
|
87 |
+
print(f"Error in classify_image_with_neural_network: {e}")
|
88 |
+
raise e
|
89 |
+
|
90 |
+
# Create Gradio interface
|
91 |
+
with gr.Blocks() as demo:
|
92 |
+
gr.Markdown("Image Classification using Different Models")
|
93 |
+
|
94 |
+
with gr.Tab("Decision Tree"):
|
95 |
+
with gr.Row():
|
96 |
+
image_input_dt = gr.Image(type="filepath")
|
97 |
+
dropdown_depth = gr.Dropdown(label="Select Depth", choices=["2", "3", "5"])
|
98 |
+
output_dt = gr.Textbox(label="Decision Tree Output")
|
99 |
+
classify_button_dt = gr.Button("Classify with Decision Tree")
|
100 |
+
classify_button_dt.click(classify_image_with_decision_tree, inputs=[image_input_dt, dropdown_depth], outputs=output_dt)
|
101 |
+
|
102 |
+
with gr.Tab("K-Nearest Neighbors (KNN)"):
|
103 |
+
with gr.Row():
|
104 |
+
image_input_knn = gr.Image(type="filepath")
|
105 |
+
dropdown_k = gr.Dropdown(label="Select Number of Neighbors (k)", choices=["1", "3", "5"])
|
106 |
+
output_knn = gr.Textbox(label="KNN Output")
|
107 |
+
classify_button_knn = gr.Button("Classify with KNN")
|
108 |
+
classify_button_knn.click(classify_image_with_knn, inputs=[image_input_knn, dropdown_k], outputs=output_knn)
|
109 |
+
|
110 |
+
with gr.Tab("Neural Network"):
|
111 |
+
with gr.Row():
|
112 |
+
image_input_nn = gr.Image(type="filepath")
|
113 |
+
dropdown_layers = gr.Dropdown(label="Select Number of Layers", choices=["1", "2", "3"])
|
114 |
+
output_nn = gr.Textbox(label="Neural Network Output")
|
115 |
+
classify_button_nn = gr.Button("Classify with Neural Network")
|
116 |
+
classify_button_nn.click(classify_image_with_neural_network, inputs=[image_input_nn, dropdown_layers], outputs=output_nn)
|
117 |
+
|
118 |
+
demo.launch(share=True)
|
cnn_1layer.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f2831e5f6636a69d91b96a12afb011cb56da79cdab79114fd27037153c954616
|
3 |
+
size 390215648
|
cnn_2layer.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6d0708e873f5de90b2e262ab603f6d7ed5bba0097736e63560a418a233a674cd
|
3 |
+
size 183170752
|
cnn_3layer.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9bb1d8d52ce7d6039e7e0665c2c382e3971348255d760d77c0c7ff9581e519ca
|
3 |
+
size 39262856
|
dt2.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1f56fc087c0691a196b1d11ff680326c538b793e2c6ca9fe24a633da5b74974a
|
3 |
+
size 1744
|
dt3.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4faed4214fb0167d1c2bd364fa5fa2f5aa28ccbb4a3a773242c036d994aef870
|
3 |
+
size 2451
|
dt5.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:de9cfa01e769120d2607d8c0c3ccb6f0d01ba0a7f3a10128a9b4dc4660fdc1f2
|
3 |
+
size 5971
|
knn1.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6c7afbda0d258137e0b227661ae0c669feff134388b92506919f80cc008b1fd9
|
3 |
+
size 775508987
|
knn3.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2214a0b3ea409d39d703e5cdfc94278ce0f4220782bf53c2a7d47b98900cde06
|
3 |
+
size 775508987
|
knn5.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3adcdf9adb2e1b18f74e374bd5e0e85cf17d7b6ac0c6eccfbe3f754e92d5c462
|
3 |
+
size 775508987
|