Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -6,6 +6,7 @@ from tensorflow.keras import layers, models
6
  import numpy as np
7
  from PIL import Image, UnidentifiedImageError
8
  import os
 
9
 
10
  # -----------------------------
11
  # CONFIGURATION
@@ -14,7 +15,7 @@ MODEL_PATH = "waste_classifier.h5"
14
  DATASET_DIR = "dataset-resized/dataset-resized"
15
  IMG_SIZE = (128, 128)
16
  BATCH_SIZE = 32
17
- EPOCHS = 10
18
 
19
  # Fixed class labels
20
  CLASSES = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
@@ -87,7 +88,10 @@ def train_and_save_model():
87
 
88
  datagen = ImageDataGenerator(
89
  rescale=1./255,
90
- validation_split=0.2
 
 
 
91
  )
92
 
93
  train_data = datagen.flow_from_directory(
@@ -110,7 +114,20 @@ def train_and_save_model():
110
  shuffle=True
111
  )
112
 
113
- # CNN Model
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  model = models.Sequential([
115
  layers.Input(shape=(128, 128, 3)),
116
 
@@ -125,9 +142,12 @@ def train_and_save_model():
125
 
126
  layers.Flatten(),
127
 
128
- layers.Dense(128, activation='relu'),
129
  layers.Dropout(0.5),
130
 
 
 
 
131
  layers.Dense(len(CLASSES), activation='softmax')
132
  ])
133
 
@@ -144,7 +164,8 @@ def train_and_save_model():
144
  train_data,
145
  validation_data=val_data,
146
  epochs=1,
147
- verbose=1
 
148
  )
149
 
150
  progress_bar.progress((epoch + 1) / EPOCHS)
@@ -208,7 +229,14 @@ def predict_waste(image):
208
 
209
  probabilities = prediction[0]
210
 
211
- predicted_index = np.argmax(probabilities)
 
 
 
 
 
 
 
212
  predicted_class = CLASSES[predicted_index]
213
  confidence = probabilities[predicted_index] * 100
214
 
@@ -285,7 +313,7 @@ if uploaded_file is not None:
285
  st.subheader("🌱 Sustainability Suggestion")
286
  st.write(TIPS.get(predicted_class, "Dispose responsibly."))
287
 
288
- # AI Environmental Analysis
289
  st.subheader("🤖 AI Environmental Analysis")
290
  st.success(
291
  AI_MESSAGES.get(
 
6
  import numpy as np
7
  from PIL import Image, UnidentifiedImageError
8
  import os
9
+ from sklearn.utils.class_weight import compute_class_weight
10
 
11
  # -----------------------------
12
  # CONFIGURATION
 
15
  DATASET_DIR = "dataset-resized/dataset-resized"
16
  IMG_SIZE = (128, 128)
17
  BATCH_SIZE = 32
18
+ EPOCHS = 20 # Increased for better accuracy
19
 
20
  # Fixed class labels
21
  CLASSES = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
 
88
 
89
  datagen = ImageDataGenerator(
90
  rescale=1./255,
91
+ validation_split=0.2,
92
+ rotation_range=15,
93
+ zoom_range=0.1,
94
+ horizontal_flip=True
95
  )
96
 
97
  train_data = datagen.flow_from_directory(
 
114
  shuffle=True
115
  )
116
 
117
+ # -----------------------------
118
+ # CLASS WEIGHTS FOR BALANCED TRAINING
119
+ # -----------------------------
120
+ class_weights = compute_class_weight(
121
+ class_weight='balanced',
122
+ classes=np.unique(train_data.classes),
123
+ y=train_data.classes
124
+ )
125
+
126
+ class_weights = dict(enumerate(class_weights))
127
+
128
+ # -----------------------------
129
+ # CNN MODEL
130
+ # -----------------------------
131
  model = models.Sequential([
132
  layers.Input(shape=(128, 128, 3)),
133
 
 
142
 
143
  layers.Flatten(),
144
 
145
+ layers.Dense(256, activation='relu'),
146
  layers.Dropout(0.5),
147
 
148
+ layers.Dense(128, activation='relu'),
149
+ layers.Dropout(0.3),
150
+
151
  layers.Dense(len(CLASSES), activation='softmax')
152
  ])
153
 
 
164
  train_data,
165
  validation_data=val_data,
166
  epochs=1,
167
+ verbose=1,
168
+ class_weight=class_weights
169
  )
170
 
171
  progress_bar.progress((epoch + 1) / EPOCHS)
 
229
 
230
  probabilities = prediction[0]
231
 
232
+ trash_index = CLASSES.index("trash")
233
+
234
+ # Trash threshold boost
235
+ if probabilities[trash_index] > 0.40:
236
+ predicted_index = trash_index
237
+ else:
238
+ predicted_index = np.argmax(probabilities)
239
+
240
  predicted_class = CLASSES[predicted_index]
241
  confidence = probabilities[predicted_index] * 100
242
 
 
313
  st.subheader("🌱 Sustainability Suggestion")
314
  st.write(TIPS.get(predicted_class, "Dispose responsibly."))
315
 
316
+ # AI Analysis
317
  st.subheader("🤖 AI Environmental Analysis")
318
  st.success(
319
  AI_MESSAGES.get(