Upload a photo of waste → the CNN model classifies it and tells you how to dispose of it.
This is the most common error when loading Keras .h5 models.
Three lines of code fix it permanently:
import os
# Build the path relative to THIS file, not the working directory
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(BASE_DIR, "waste_classifier_model.h5")
# Then load:
import tensorflow as tf
model = tf.keras.models.load_model(MODEL_PATH)| Cause | Broken code | Fixed code |
|---|---|---|
Relative path – Python looks in CWD, not the script's folder |
load_model("model.h5") |
load_model(os.path.join(BASE_DIR,"model.h5")) |
| Wrong Keras API (TF 2.x vs standalone Keras 3) | import keras; keras.models.load_model(...) |
Try tf.keras first, then keras as fallback |
| Model not copied to deployment folder | File missing in production | Copy .h5 next to app.py and use BASE_DIR path |
| TF version mismatch | Model saved with TF 2.9, loaded with TF 2.3 | pip install --upgrade tensorflow |
garbage_classification_project/
│
├── app.py # Flask web app (main entry point)
├── requirements.txt
├── waste_classifier_model.h5 # ← copy YOUR model here
│
├── utils/
│ └── model_loader.py # Robust loader utility (import anywhere)
│
├── templates/
│ └── index.html # Web UI
│
├── static/
│ ├── css/style.css
│ └── js/app.js
│
└── uploads/ # Temp upload directory (auto-created)
pip install -r requirements.txt# Copy your model into the project root (same folder as app.py)
cp /path/to/waste_classifier_model.h5 garbage_classification_project/cd garbage_classification_project
python app.py
# → Open http://localhost:5000curl http://localhost:5000/health
# → {"status":"ok","model_path":"...","model_exists":true}Edit the top of app.py to match your model:
# Input size used during training
IMG_SIZE = (224, 224) # change to (128,128) or (150,150) if needed
# Class labels – must match your training order
CLASS_LABELS = {
0: {"name": "Cardboard", "emoji": "📦", "type": "Recyclable", "color": "#8B4513"},
1: {"name": "Glass", "emoji": "🍾", "type": "Recyclable", "color": "#00CED1"},
# … add or remove to match your dataset classes
}The project expects a standard CNN .h5 saved with model.save("waste_classifier_model.h5").
Typical training pattern:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(224,224,3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128,(3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(NUM_CLASSES, activation='softmax'),
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# After training:
model.save("waste_classifier_model.h5") # saves to CWD| Endpoint | Method | Body | Response |
|---|---|---|---|
/ |
GET | — | Web UI |
/predict |
POST | multipart/form-data with file field |
JSON result |
/predict |
POST | application/json with {"image":"data:image/...;base64,..."} |
JSON result |
/health |
GET | — | {"status":"ok","model_exists":true} |
Example response:
{
"class_index": 4,
"class_name": "Plastic",
"emoji": "🧴",
"waste_type": "Recyclable",
"color": "#FF6347",
"confidence": 91.3,
"disposal_tip":"♻️ Place in the blue recycling bin. Rinse containers before recycling.",
"top3": [
{"name":"Plastic", "confidence":91.3},
{"name":"Metal", "confidence":5.1},
{"name":"Cardboard", "confidence":2.4}
]
}| Error | Cause | Fix |
|---|---|---|
OSError: No such file or directory |
Relative path | Use BASE_DIR pattern (see top of README) |
ModuleNotFoundError: No module named 'tensorflow' |
Not installed | pip install tensorflow |
ValueError: Unknown layer |
Custom layers in model | Pass custom_objects={"MyLayer": MyLayer} to load_model |
| Low confidence on all classes | Wrong IMG_SIZE |
Set IMG_SIZE to match training input shape |
| All predictions same class | Wrong class order | Reorder CLASS_LABELS to match model.fit generator |
MIT – free for personal and commercial use.