keremoktay1 commited on
Commit
97eac4c
1 Parent(s): 61c039d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -63
app.py CHANGED
@@ -3,9 +3,6 @@ import pickle
3
  import numpy as np
4
  from tensorflow.keras.models import load_model
5
  from PIL import Image
6
- import torch
7
- import torchvision.transforms as transforms
8
- import torchvision.models as models
9
 
10
  # Update the path to the directory where your models are stored
11
  model_directory = '/Users/keremoktay/Desktop/Gradio/'
@@ -13,8 +10,8 @@ model_directory = '/Users/keremoktay/Desktop/Gradio/'
13
  # Load the decision tree and KNN models
14
  dt_models = {
15
  "2": pickle.load(open('dt2.pkl', 'rb')),
16
- "3": pickle.load(open('dt3.pkl', 'rb')),
17
- "5": pickle.load(open('dt5.pkl', 'rb'))
18
  }
19
  knn_models = {
20
  "1": pickle.load(open('knn1.pkl', 'rb')),
@@ -29,38 +26,27 @@ nn_models = {
29
  "3": load_model('cnn_3layer.h5')
30
  }
31
 
32
-
33
-
34
- model - models.mobilenet_v2()
35
- model.load_state_dict(torch.load('model_weights.pth'))
36
- model.eval()
37
-
38
  def preprocess_image_for_ml(image_path):
39
- image = Image.open(image_path)
40
- image_array = np.asarray(image).flatten().reshape(1, -1)
41
- return image_array
 
 
 
 
 
42
 
43
  def preprocess_image_for_cnn(image_path, target_size=(128, 128)):
44
- image = Image.open(image_path).convert('RGB')
45
- image = image.resize(target_size)
46
- image_array = np.asarray(image) / 255.0
47
- image_array = np.expand_dims(image_array, axis=0)
48
- return image_array
49
-
50
- def preprocess_image_for_pytorch(image_path):
51
- preprocess = transforms.compose([
52
- transforms.resize(256),
53
- transforms.CenterCrop(224),
54
- transforms.ToTensor(),
55
- transforms.Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225])
56
- ])
57
- img = Image.open(image_path)
58
- img_t = preprocess(img)
59
- batch_t = torch.unsqueeze(img_t, 0)
60
- return batch_t
61
-
62
-
63
-
64
 
65
  class_names = {
66
  0: "Angular Leaf Spot",
@@ -69,32 +55,37 @@ class_names = {
69
  }
70
 
71
  def classify_image_with_decision_tree(image, depth):
72
- image_array = preprocess_image_for_ml(image)
73
- model = dt_models.get(depth)
74
- prediction = model.predict(image_array)
75
- return class_names[int(prediction)]
 
 
 
 
 
76
 
77
  def classify_image_with_knn(image, k):
78
- image_array = preprocess_image_for_ml(image)
79
- model = knn_models.get(k)
80
- prediction = model.predict(image_array)
81
- return class_names[int(prediction)]
 
 
 
 
 
82
 
83
  def classify_image_with_neural_network(image, layers):
84
- image_array = preprocess_image_for_cnn(image)
85
- model = nn_models.get(layers)
86
- prediction = model.predict(image_array)
87
- return class_names[np.argmax(prediction, axis=1)[0]]
88
-
89
- def classify_image_with_pytorch(image):
90
- image_tensor = preprocess_image_for_pytorch(image)
91
- with torch.no_grad():
92
- out = model(image_tensor)
93
- _, indices = torch.topk(out, 1)
94
-
95
- return indices[0]
96
-
97
-
98
 
99
  # Create Gradio interface
100
  with gr.Blocks() as demo:
@@ -124,11 +115,4 @@ with gr.Blocks() as demo:
124
  classify_button_nn = gr.Button("Classify with Neural Network")
125
  classify_button_nn.click(classify_image_with_neural_network, inputs=[image_input_nn, dropdown_layers], outputs=output_nn)
126
 
127
- with gr.Tab("mobilenet Model"):
128
- with gr.Row():
129
- image_input_pytorch = gr.Image(type="filepath")
130
- output_pytorch = gr.Textbox(label="ResNet-18 Model Output")
131
- classify_button_pytorch = gr.Button("Classify with ResNet-18 Model")
132
- classify_button_pytorch.click(classify_image_with_pytorch, inputs=[image_input_pytorch], outputs=output_pytorch)
133
-
134
  demo.launch()
 
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/'
 
10
  # Load the decision tree and KNN models
11
  dt_models = {
12
  "2": pickle.load(open('dt2.pkl', 'rb')),
13
+ "3": pickle.load(open('dt3.pkl', 'rb')),
14
+ "5": pickle.load(open('dt5.pkl', 'rb'))
15
  }
16
  knn_models = {
17
  "1": pickle.load(open('knn1.pkl', 'rb')),
 
26
  "3": load_model('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",
 
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:
 
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()