Spaces:
Runtime error
Runtime error
NORLIE JHON MALAGDAO
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,10 +61,6 @@ bees = list(data_dir.glob('bees/*'))
|
|
| 61 |
print(bees[0])
|
| 62 |
PIL.Image.open(str(bees[0]))
|
| 63 |
|
| 64 |
-
bees = list(data_dir.glob('bees/*'))
|
| 65 |
-
print(bees[0])
|
| 66 |
-
PIL.Image.open(str(bees[0]))
|
| 67 |
-
|
| 68 |
batch_size = 32
|
| 69 |
img_height = 180
|
| 70 |
img_width = 180
|
|
@@ -90,21 +86,6 @@ val_ds = tf.keras.utils.image_dataset_from_directory(
|
|
| 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 |
-
for image_batch, labels_batch in train_ds:
|
| 104 |
-
print(image_batch.shape)
|
| 105 |
-
print(labels_batch.shape)
|
| 106 |
-
break
|
| 107 |
-
|
| 108 |
AUTOTUNE = tf.data.AUTOTUNE
|
| 109 |
|
| 110 |
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
|
|
@@ -112,12 +93,6 @@ val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
|
|
| 112 |
|
| 113 |
normalization_layer = layers.Rescaling(1./255)
|
| 114 |
|
| 115 |
-
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
|
| 116 |
-
image_batch, labels_batch = next(iter(normalized_ds))
|
| 117 |
-
first_image = image_batch[0]
|
| 118 |
-
# Notice the pixel values are now in `[0,1]`.
|
| 119 |
-
print(np.min(first_image), np.max(first_image))
|
| 120 |
-
|
| 121 |
num_classes = len(class_names)
|
| 122 |
|
| 123 |
data_augmentation = keras.Sequential(
|
|
@@ -128,17 +103,10 @@ data_augmentation = keras.Sequential(
|
|
| 128 |
]
|
| 129 |
)
|
| 130 |
|
| 131 |
-
|
| 132 |
-
for images, _ in train_ds.take(1):
|
| 133 |
-
for i in range(9):
|
| 134 |
-
augmented_images = data_augmentation(images)
|
| 135 |
-
ax = plt.subplot(3, 3, i + 1)
|
| 136 |
-
plt.imshow(augmented_images[0].numpy().astype("uint8"))
|
| 137 |
-
plt.axis("off")
|
| 138 |
-
|
| 139 |
model = Sequential([
|
| 140 |
data_augmentation,
|
| 141 |
-
|
| 142 |
layers.Conv2D(32, 3, padding='same', activation='relu'),
|
| 143 |
layers.MaxPooling2D(),
|
| 144 |
layers.Conv2D(64, 3, padding='same', activation='relu'),
|
|
@@ -147,9 +115,14 @@ model = Sequential([
|
|
| 147 |
layers.MaxPooling2D(),
|
| 148 |
layers.Conv2D(256, 3, padding='same', activation='relu'),
|
| 149 |
layers.MaxPooling2D(),
|
| 150 |
-
layers.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
layers.Flatten(),
|
| 152 |
-
layers.Dense(
|
|
|
|
| 153 |
layers.Dense(num_classes, activation='softmax')
|
| 154 |
])
|
| 155 |
|
|
@@ -168,7 +141,7 @@ history = model.fit(
|
|
| 168 |
|
| 169 |
def predict_image(img):
|
| 170 |
img = np.array(img)
|
| 171 |
-
img_resized = tf.image.resize(img, (
|
| 172 |
img_4d = tf.expand_dims(img_resized, axis=0)
|
| 173 |
prediction = model.predict(img_4d)[0]
|
| 174 |
return {class_names[i]: float(prediction[i]) for i in range(len(class_names))}
|
|
@@ -176,7 +149,6 @@ def predict_image(img):
|
|
| 176 |
image = gr.Image()
|
| 177 |
label = gr.Label(num_top_classes=1)
|
| 178 |
|
| 179 |
-
# Define custom CSS for background image
|
| 180 |
custom_css = """
|
| 181 |
body {
|
| 182 |
background-image: url('extracted_files/Pest_Dataset/bees/bees (444).jpg');
|
|
|
|
| 61 |
print(bees[0])
|
| 62 |
PIL.Image.open(str(bees[0]))
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
batch_size = 32
|
| 65 |
img_height = 180
|
| 66 |
img_width = 180
|
|
|
|
| 86 |
class_names = train_ds.class_names
|
| 87 |
print(class_names)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
AUTOTUNE = tf.data.AUTOTUNE
|
| 90 |
|
| 91 |
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
|
|
|
|
| 93 |
|
| 94 |
normalization_layer = layers.Rescaling(1./255)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
num_classes = len(class_names)
|
| 97 |
|
| 98 |
data_augmentation = keras.Sequential(
|
|
|
|
| 103 |
]
|
| 104 |
)
|
| 105 |
|
| 106 |
+
# Define a deeper convolutional neural network
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
model = Sequential([
|
| 108 |
data_augmentation,
|
| 109 |
+
normalization_layer,
|
| 110 |
layers.Conv2D(32, 3, padding='same', activation='relu'),
|
| 111 |
layers.MaxPooling2D(),
|
| 112 |
layers.Conv2D(64, 3, padding='same', activation='relu'),
|
|
|
|
| 115 |
layers.MaxPooling2D(),
|
| 116 |
layers.Conv2D(256, 3, padding='same', activation='relu'),
|
| 117 |
layers.MaxPooling2D(),
|
| 118 |
+
layers.Conv2D(512, 3, padding='same', activation='relu'),
|
| 119 |
+
layers.MaxPooling2D(),
|
| 120 |
+
layers.Conv2D(512, 3, padding='same', activation='relu'),
|
| 121 |
+
layers.MaxPooling2D(),
|
| 122 |
+
layers.Dropout(0.5),
|
| 123 |
layers.Flatten(),
|
| 124 |
+
layers.Dense(1024, activation='relu'),
|
| 125 |
+
layers.Dropout(0.5),
|
| 126 |
layers.Dense(num_classes, activation='softmax')
|
| 127 |
])
|
| 128 |
|
|
|
|
| 141 |
|
| 142 |
def predict_image(img):
|
| 143 |
img = np.array(img)
|
| 144 |
+
img_resized = tf.image.resize(img, (img_height, img_width))
|
| 145 |
img_4d = tf.expand_dims(img_resized, axis=0)
|
| 146 |
prediction = model.predict(img_4d)[0]
|
| 147 |
return {class_names[i]: float(prediction[i]) for i in range(len(class_names))}
|
|
|
|
| 149 |
image = gr.Image()
|
| 150 |
label = gr.Label(num_top_classes=1)
|
| 151 |
|
|
|
|
| 152 |
custom_css = """
|
| 153 |
body {
|
| 154 |
background-image: url('extracted_files/Pest_Dataset/bees/bees (444).jpg');
|