Spaces:
Runtime error
Runtime error
NORLIE JHON MALAGDAO
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -9,11 +9,15 @@ from tensorflow import keras
|
|
9 |
from tensorflow.keras import layers
|
10 |
from tensorflow.keras.models import Sequential
|
11 |
|
|
|
12 |
from PIL import Image
|
13 |
import gdown
|
14 |
import zipfile
|
|
|
15 |
import pathlib
|
16 |
|
|
|
|
|
17 |
# Define the Google Drive shareable link
|
18 |
gdrive_url = 'https://drive.google.com/file/d/1HjHYlQyRz5oWt8kehkt1TiOGRRlKFsv8/view?usp=drive_link'
|
19 |
|
@@ -53,69 +57,105 @@ for root, dirs, files in os.walk(extracted_path):
|
|
53 |
for f in files:
|
54 |
print(f"{subindent}{f}")
|
55 |
|
|
|
56 |
# Path to the dataset directory
|
57 |
data_dir = pathlib.Path('extracted_files/Pest_Dataset')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
img_height,
|
60 |
-
batch_size
|
61 |
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
)
|
69 |
|
70 |
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
|
79 |
class_names = train_ds.class_names
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
data_augmentation = keras.Sequential(
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
)
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
num_classes = len(class_names)
|
93 |
model = Sequential([
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
])
|
107 |
|
|
|
108 |
model.compile(optimizer='adam',
|
109 |
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
110 |
metrics=['accuracy'])
|
111 |
|
112 |
-
|
|
|
|
|
|
|
113 |
history = model.fit(
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
)
|
118 |
|
|
|
|
|
|
|
|
|
|
|
119 |
import gradio as gr
|
120 |
import numpy as np
|
121 |
import tensorflow as tf
|
@@ -125,16 +165,34 @@ def predict_image(img):
|
|
125 |
img_resized = tf.image.resize(img, (180, 180))
|
126 |
img_4d = tf.expand_dims(img_resized, axis=0)
|
127 |
prediction = model.predict(img_4d)[0]
|
128 |
-
|
129 |
-
|
|
|
|
|
130 |
|
131 |
image = gr.Image()
|
132 |
label = gr.Label(num_top_classes=12)
|
133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
gr.Interface(
|
135 |
fn=predict_image,
|
136 |
inputs=image,
|
137 |
outputs=label,
|
138 |
title="Pest Classification",
|
139 |
-
description="Upload an image of a pest to classify it into one of the predefined categories."
|
|
|
140 |
).launch(debug=True)
|
|
|
|
|
|
|
|
|
|
9 |
from tensorflow.keras import layers
|
10 |
from tensorflow.keras.models import Sequential
|
11 |
|
12 |
+
|
13 |
from PIL import Image
|
14 |
import gdown
|
15 |
import zipfile
|
16 |
+
|
17 |
import pathlib
|
18 |
|
19 |
+
|
20 |
+
|
21 |
# Define the Google Drive shareable link
|
22 |
gdrive_url = 'https://drive.google.com/file/d/1HjHYlQyRz5oWt8kehkt1TiOGRRlKFsv8/view?usp=drive_link'
|
23 |
|
|
|
57 |
for f in files:
|
58 |
print(f"{subindent}{f}")
|
59 |
|
60 |
+
import pathlib
|
61 |
# Path to the dataset directory
|
62 |
data_dir = pathlib.Path('extracted_files/Pest_Dataset')
|
63 |
+
data_dir = pathlib.Path(data_dir)
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
bees = list(data_dir.glob('bees/*'))
|
68 |
+
print(bees[0])
|
69 |
+
PIL.Image.open(str(bees[0]))
|
70 |
|
71 |
+
img_height,img_width=180,180
|
72 |
+
batch_size=32
|
73 |
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
74 |
+
data_dir,
|
75 |
+
validation_split=0.2,
|
76 |
+
subset="training",
|
77 |
+
seed=123,
|
78 |
+
image_size=(img_height, img_width),
|
79 |
+
batch_size=batch_size)
|
|
|
80 |
|
81 |
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
82 |
+
data_dir,
|
83 |
+
validation_split=0.2,
|
84 |
+
subset="validation",
|
85 |
+
seed=123,
|
86 |
+
image_size=(img_height, img_width),
|
87 |
+
batch_size=batch_size)
|
88 |
+
|
89 |
|
90 |
class_names = train_ds.class_names
|
91 |
+
print(class_names)
|
92 |
+
|
93 |
+
import matplotlib.pyplot as plt
|
94 |
+
|
95 |
+
plt.figure(figsize=(10, 10))
|
96 |
+
for images, labels in train_ds.take(1):
|
97 |
+
for i in range(9):
|
98 |
+
ax = plt.subplot(3, 3, i + 1)
|
99 |
+
plt.imshow(images[i].numpy().astype("uint8"))
|
100 |
+
plt.title(class_names[labels[i]])
|
101 |
+
plt.axis("off")
|
102 |
|
103 |
data_augmentation = keras.Sequential(
|
104 |
+
[
|
105 |
+
layers.RandomFlip("horizontal",
|
106 |
+
input_shape=(img_height,
|
107 |
+
img_width,
|
108 |
+
3)),
|
109 |
+
layers.RandomRotation(0.1),
|
110 |
+
layers.RandomZoom(0.1),
|
111 |
+
]
|
112 |
)
|
113 |
|
114 |
+
plt.figure(figsize=(10, 10))
|
115 |
+
for images, _ in train_ds.take(1):
|
116 |
+
for i in range(9):
|
117 |
+
augmented_images = data_augmentation(images)
|
118 |
+
ax = plt.subplot(3, 3, i + 1)
|
119 |
+
plt.imshow(augmented_images[0].numpy().astype("uint8"))
|
120 |
+
plt.axis("off")
|
121 |
+
|
122 |
+
|
123 |
num_classes = len(class_names)
|
124 |
model = Sequential([
|
125 |
+
data_augmentation,
|
126 |
+
layers.Rescaling(1./255),
|
127 |
+
layers.Conv2D(16, 3, padding='same', activation='relu'),
|
128 |
+
layers.MaxPooling2D(),
|
129 |
+
layers.Conv2D(32, 3, padding='same', activation='relu'),
|
130 |
+
layers.MaxPooling2D(),
|
131 |
+
layers.Conv2D(64, 3, padding='same', activation='relu'),
|
132 |
+
layers.MaxPooling2D(),
|
133 |
+
layers.Dropout(0.2),
|
134 |
+
layers.Flatten(),
|
135 |
+
layers.Dense(128, activation='relu'),
|
136 |
+
layers.Dense(num_classes, name="outputs")
|
137 |
])
|
138 |
|
139 |
+
|
140 |
model.compile(optimizer='adam',
|
141 |
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
|
142 |
metrics=['accuracy'])
|
143 |
|
144 |
+
|
145 |
+
model.summary()
|
146 |
+
|
147 |
+
epochs = 50
|
148 |
history = model.fit(
|
149 |
+
train_ds,
|
150 |
+
validation_data=val_ds,
|
151 |
+
epochs=epochs
|
152 |
)
|
153 |
|
154 |
+
results = model.evaluate(val_ds, verbose=0)
|
155 |
+
|
156 |
+
print("Validation Loss: {:.5f}".format(results[0]))
|
157 |
+
print("Validation Accuracy: {:.2f}%".format(results[1] * 100))
|
158 |
+
|
159 |
import gradio as gr
|
160 |
import numpy as np
|
161 |
import tensorflow as tf
|
|
|
165 |
img_resized = tf.image.resize(img, (180, 180))
|
166 |
img_4d = tf.expand_dims(img_resized, axis=0)
|
167 |
prediction = model.predict(img_4d)[0]
|
168 |
+
return {class_names[i]: float(prediction[i]) for i in range(len(class_names))}
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
|
173 |
image = gr.Image()
|
174 |
label = gr.Label(num_top_classes=12)
|
175 |
|
176 |
+
# Define custom CSS for background image
|
177 |
+
custom_css = """
|
178 |
+
body {
|
179 |
+
background-image: url('\extracted_files\Pest_Dataset\bees\bees (444).jpg');
|
180 |
+
background-size: cover;
|
181 |
+
background-repeat: no-repeat;
|
182 |
+
background-attachment: fixed;
|
183 |
+
color: white;
|
184 |
+
}
|
185 |
+
"""
|
186 |
+
|
187 |
gr.Interface(
|
188 |
fn=predict_image,
|
189 |
inputs=image,
|
190 |
outputs=label,
|
191 |
title="Pest Classification",
|
192 |
+
description="Upload an image of a pest to classify it into one of the predefined categories.",
|
193 |
+
css=custom_css
|
194 |
).launch(debug=True)
|
195 |
+
|
196 |
+
|
197 |
+
|
198 |
+
|