NORLIE JHON MALAGDAO commited on
Commit
69f3420
·
verified ·
1 Parent(s): dd45bd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -100
app.py CHANGED
@@ -1,24 +1,15 @@
1
- import gradio as gr
2
- import matplotlib.pyplot as plt
3
- import numpy as np
4
  import os
5
- import PIL
 
 
6
  import tensorflow as tf
7
-
8
  from tensorflow import keras
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
-
22
 
23
  # Define the Google Drive shareable link
24
  gdrive_url = 'https://drive.google.com/file/d/1HjHYlQyRz5oWt8kehkt1TiOGRRlKFsv8/view?usp=drive_link'
@@ -48,9 +39,9 @@ except zipfile.BadZipFile:
48
  os.remove(local_zip_file)
49
 
50
  # Convert the extracted directory path to a pathlib.Path object
51
- data_dir = pathlib.Path(extracted_path)
52
 
53
- # Print the directory structure to debug
54
  for root, dirs, files in os.walk(extracted_path):
55
  level = root.replace(extracted_path, '').count(os.sep)
56
  indent = ' ' * 4 * (level)
@@ -59,128 +50,151 @@ for root, dirs, files in os.walk(extracted_path):
59
  for f in files:
60
  print(f"{subindent}{f}")
61
 
62
- import pathlib
63
- # Path to the dataset directory
64
- data_dir = pathlib.Path('extracted_files/Pest_Dataset')
65
- data_dir = pathlib.Path(data_dir)
66
-
67
-
68
- bees = list(data_dir.glob('bees/*'))
69
- print(bees[0])
70
- PIL.Image.open(str(bees[0]))
71
-
72
 
73
- bees = list(data_dir.glob('bees/*'))
74
- print(bees[0])
75
- PIL.Image.open(str(bees[0]))
76
-
77
-
78
- img_height,img_width=180,180
79
- batch_size=32
80
  train_ds = tf.keras.preprocessing.image_dataset_from_directory(
81
- data_dir,
82
- validation_split=0.2,
83
- subset="training",
84
- seed=123,
85
- image_size=(img_height, img_width),
86
- batch_size=batch_size)
87
-
88
 
89
  val_ds = tf.keras.preprocessing.image_dataset_from_directory(
90
- data_dir,
91
- validation_split=0.2,
92
- subset="validation",
93
- seed=123,
94
- image_size=(img_height, img_width),
95
- batch_size=batch_size)
96
-
97
 
98
  class_names = train_ds.class_names
99
  print(class_names)
100
 
101
-
102
- import matplotlib.pyplot as plt
103
-
104
  plt.figure(figsize=(10, 10))
105
  for images, labels in train_ds.take(1):
106
- for i in range(9):
107
- ax = plt.subplot(3, 3, i + 1)
108
- plt.imshow(images[i].numpy().astype("uint8"))
109
- plt.title(class_names[labels[i]])
110
- plt.axis("off")
111
-
112
 
 
113
  data_augmentation = keras.Sequential(
114
- [
115
- layers.RandomFlip("horizontal",
116
- input_shape=(img_height,
117
- img_width,
118
- 3)),
119
- layers.RandomRotation(0.1),
120
- layers.RandomZoom(0.1),
121
- ]
122
  )
123
 
124
-
125
  plt.figure(figsize=(10, 10))
126
  for images, _ in train_ds.take(1):
127
- for i in range(9):
128
- augmented_images = data_augmentation(images)
129
- ax = plt.subplot(3, 3, i + 1)
130
- plt.imshow(augmented_images[0].numpy().astype("uint8"))
131
- plt.axis("off")
132
-
133
 
 
134
  num_classes = len(class_names)
135
  model = Sequential([
136
- data_augmentation,
137
- layers.Rescaling(1./255),
138
- layers.Conv2D(16, 3, padding='same', activation='relu'),
139
- layers.MaxPooling2D(),
140
- layers.Conv2D(32, 3, padding='same', activation='relu'),
141
- layers.MaxPooling2D(),
142
- layers.Conv2D(64, 3, padding='same', activation='relu'),
143
- layers.MaxPooling2D(),
144
- layers.Dropout(0.2),
145
- layers.Flatten(),
146
- layers.Dense(128, activation='relu'),
147
- layers.Dense(num_classes, activation='softmax', name="outputs") # Use softmax here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  ])
149
 
150
- model.compile(optimizer='adam',
151
- loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), # Change from_logits to False
152
  metrics=['accuracy'])
153
 
154
  model.summary()
155
 
 
 
156
 
157
- epochs = 15
 
 
 
158
  history = model.fit(
159
- train_ds,
160
- validation_data=val_ds,
161
- epochs=epochs
 
162
  )
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
-
166
- import gradio as gr
167
- import numpy as np
168
- import tensorflow as tf
169
-
170
  def predict_image(img):
171
  img = np.array(img)
172
  img_resized = tf.image.resize(img, (180, 180))
173
  img_4d = tf.expand_dims(img_resized, axis=0)
174
  prediction = model.predict(img_4d)[0]
175
- return {class_names[i]: float(prediction[i]) for i in range(len(class_names))}
 
 
 
176
 
 
177
  image = gr.Image()
178
- label = gr.Label(num_top_classes=12)
179
 
180
  # Define custom CSS for background image
181
  custom_css = """
182
  body {
183
- background-image: url('\extracted_files\Pest_Dataset\bees\bees (444).jpg');
184
  background-size: cover;
185
  background-repeat: no-repeat;
186
  background-attachment: fixed;
@@ -193,7 +207,6 @@ gr.Interface(
193
  inputs=image,
194
  outputs=label,
195
  title="Welcome to Agricultural Pest Image Classification",
196
- description="The image data set used was obtaied from Kaggle and has a collection of 12 different types of agricultral pests: Ants, Bees, Beetles, Caterpillars, Earthworms, Earwigs, Grasshoppers, Moths, Slugs, Snails, Wasps, and Weevils",
197
  css=custom_css
198
  ).launch(debug=True)
199
-
 
 
 
 
1
  import os
2
+ import zipfile
3
+ import gdown
4
+ import pathlib
5
  import tensorflow as tf
 
6
  from tensorflow import keras
7
  from tensorflow.keras import layers
8
  from tensorflow.keras.models import Sequential
 
 
9
  from PIL import Image
10
+ import matplotlib.pyplot as plt
11
+ import gradio as gr
12
+ import numpy as np
 
 
 
 
 
13
 
14
  # Define the Google Drive shareable link
15
  gdrive_url = 'https://drive.google.com/file/d/1HjHYlQyRz5oWt8kehkt1TiOGRRlKFsv8/view?usp=drive_link'
 
39
  os.remove(local_zip_file)
40
 
41
  # Convert the extracted directory path to a pathlib.Path object
42
+ data_dir = pathlib.Path('extracted_files/Pest_Dataset')
43
 
44
+ # Verify the directory structure
45
  for root, dirs, files in os.walk(extracted_path):
46
  level = root.replace(extracted_path, '').count(os.sep)
47
  indent = ' ' * 4 * (level)
 
50
  for f in files:
51
  print(f"{subindent}{f}")
52
 
53
+ # Set image dimensions and batch size
54
+ img_height, img_width = 180, 180
55
+ batch_size = 32
 
 
 
 
 
 
 
56
 
57
+ # Create training and validation datasets
 
 
 
 
 
 
58
  train_ds = tf.keras.preprocessing.image_dataset_from_directory(
59
+ data_dir,
60
+ validation_split=0.2,
61
+ subset="training",
62
+ seed=123,
63
+ image_size=(img_height, img_width),
64
+ batch_size=batch_size
65
+ )
66
 
67
  val_ds = tf.keras.preprocessing.image_dataset_from_directory(
68
+ data_dir,
69
+ validation_split=0.2,
70
+ subset="validation",
71
+ seed=123,
72
+ image_size=(img_height, img_width),
73
+ batch_size=batch_size
74
+ )
75
 
76
  class_names = train_ds.class_names
77
  print(class_names)
78
 
79
+ # Display some sample images
 
 
80
  plt.figure(figsize=(10, 10))
81
  for images, labels in train_ds.take(1):
82
+ for i in range(9):
83
+ ax = plt.subplot(3, 3, i + 1)
84
+ plt.imshow(images[i].numpy().astype("uint8"))
85
+ plt.title(class_names[labels[i]])
86
+ plt.axis("off")
 
87
 
88
+ # Enhanced data augmentation
89
  data_augmentation = keras.Sequential(
90
+ [
91
+ layers.RandomFlip("horizontal", input_shape=(img_height, img_width, 3)),
92
+ layers.RandomRotation(0.2),
93
+ layers.RandomZoom(0.2),
94
+ layers.RandomContrast(0.2),
95
+ layers.RandomBrightness(0.2),
96
+ ]
 
97
  )
98
 
99
+ # Display augmented images
100
  plt.figure(figsize=(10, 10))
101
  for images, _ in train_ds.take(1):
102
+ for i in range(9):
103
+ augmented_images = data_augmentation(images)
104
+ ax = plt.subplot(3, 3, i + 1)
105
+ plt.imshow(augmented_images[0].numpy().astype("uint8"))
106
+ plt.axis("off")
 
107
 
108
+ # Define a deeper CNN model with more regularization techniques
109
  num_classes = len(class_names)
110
  model = Sequential([
111
+ data_augmentation,
112
+ layers.Rescaling(1./255),
113
+
114
+ layers.Conv2D(32, 3, padding='same', activation='relu', kernel_regularizer=keras.regularizers.l2(0.001)),
115
+ layers.BatchNormalization(),
116
+ layers.MaxPooling2D(),
117
+
118
+ layers.Conv2D(64, 3, padding='same', activation='relu', kernel_regularizer=keras.regularizers.l2(0.001)),
119
+ layers.BatchNormalization(),
120
+ layers.MaxPooling2D(),
121
+
122
+ layers.Conv2D(128, 3, padding='same', activation='relu', kernel_regularizer=keras.regularizers.l2(0.001)),
123
+ layers.BatchNormalization(),
124
+ layers.MaxPooling2D(),
125
+
126
+ layers.Conv2D(256, 3, padding='same', activation='relu', kernel_regularizer=keras.regularizers.l2(0.001)),
127
+ layers.BatchNormalization(),
128
+ layers.MaxPooling2D(),
129
+
130
+ layers.Conv2D(512, 3, padding='same', activation='relu', kernel_regularizer=keras.regularizers.l2(0.001)),
131
+ layers.BatchNormalization(),
132
+ layers.MaxPooling2D(),
133
+
134
+ layers.Dropout(0.5),
135
+ layers.Flatten(),
136
+
137
+ layers.Dense(256, activation='relu', kernel_regularizer=keras.regularizers.l2(0.001)),
138
+ layers.Dropout(0.5),
139
+
140
+ layers.Dense(num_classes, activation='softmax', name="outputs")
141
  ])
142
 
143
+ model.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-4),
144
+ loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
145
  metrics=['accuracy'])
146
 
147
  model.summary()
148
 
149
+ # Implement early stopping
150
+ from tensorflow.keras.callbacks import EarlyStopping
151
 
152
+ early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
153
+
154
+ # Train the model
155
+ epochs = 30
156
  history = model.fit(
157
+ train_ds,
158
+ validation_data=val_ds,
159
+ epochs=epochs,
160
+ callbacks=[early_stopping]
161
  )
162
 
163
+ # Define category descriptions
164
+ category_descriptions = {
165
+ "Ants": "Ants are small insects known for their complex social structures and teamwork.",
166
+ "Bees": "Bees are flying insects known for their role in pollination and producing honey.",
167
+ "Beetles": "Beetles are a group of insects with hard exoskeletons and wings. They are the largest order of insects.",
168
+ "Caterpillars": "Caterpillars are the larval stage of butterflies and moths, known for their voracious appetite.",
169
+ "Earthworms": "Earthworms are segmented worms that are crucial for soil health and nutrient cycling.",
170
+ "Earwigs": "Earwigs are insects with pincers on their abdomen and are known for their nocturnal activity.",
171
+ "Grasshoppers": "Grasshoppers are insects known for their powerful hind legs, which they use for jumping.",
172
+ "Moths": "Moths are nocturnal insects related to butterflies, known for their attraction to light.",
173
+ "Slugs": "Slugs are soft-bodied mollusks that are similar to snails but lack a shell.",
174
+ "Snails": "Snails are mollusks with a coiled shell, known for their slow movement and slimy trail.",
175
+ "Wasps": "Wasps are stinging insects that can be solitary or social, and some species are important pollinators.",
176
+ "Weevils": "Weevils are a type of beetle with a long snout, known for being pests to crops and stored grains."
177
+ }
178
 
179
+ # Define the prediction function
 
 
 
 
180
  def predict_image(img):
181
  img = np.array(img)
182
  img_resized = tf.image.resize(img, (180, 180))
183
  img_4d = tf.expand_dims(img_resized, axis=0)
184
  prediction = model.predict(img_4d)[0]
185
+ predicted_class = np.argmax(prediction)
186
+ predicted_label = class_names[predicted_class]
187
+ predicted_description = category_descriptions[predicted_label]
188
+ return {predicted_label: f"{float(prediction[predicted_class]):.2f} - {predicted_description}"}
189
 
190
+ # Set up Gradio interface
191
  image = gr.Image()
192
+ label = gr.Label(num_top_classes=1)
193
 
194
  # Define custom CSS for background image
195
  custom_css = """
196
  body {
197
+ background-image: url('extracted_files/Pest_Dataset/bees/bees (444).jpg');
198
  background-size: cover;
199
  background-repeat: no-repeat;
200
  background-attachment: fixed;
 
207
  inputs=image,
208
  outputs=label,
209
  title="Welcome to Agricultural Pest Image Classification",
210
+ description="The image data set used was obtained from Kaggle and has a collection of 12 different types of agricultural pests: Ants, Bees, Beetles, Caterpillars, Earthworms, Earwigs, Grasshoppers, Moths, Slugs, Snails, Wasps, and Weevils",
211
  css=custom_css
212
  ).launch(debug=True)