β»οΈ Smart Waste Classifier (waste_classifier.tflite)
A lightweight MobileNetV2-based image classification model converted to TensorFlow Lite (8.92 MB) for identifying common waste materials from single-item photos and powering real-time recycling guidance.
- Live Web App: http://15.134.218.61/
- Source Code: GitHub β mdAsadAlamgithub/smart-waste-classifier
- Hugging Face Repo: asadalam/waste_classifier
π Model Details
| Property | Value |
|---|---|
| Base Architecture | MobileNetV2 (weights="imagenet", include_top=False) |
| Framework | TensorFlow / Keras β TensorFlow Lite (TFLiteConverter) |
| Model File | waste_classifier.tflite (8,916,644 bytes / 8.92 MB) |
| Input Shape | (1, 224, 224, 3) (float32, RGB, pixel range [0, 255]) |
| Output Shape | (1, 6) (float32 Softmax probabilities) |
| Classes (6) | cardboard, glass, metal, organic, paper, plastic |
Important Preprocessing Note: A
Rescaling(1.0 / 127.5, offset=-1)layer is built directly into the model graph. Pass raw RGB pixel values in the[0, 255]range asfloat32βdo not divide by255.0or apply external[-1, 1]normalization.
π·οΈ Supported Classes
The output vector indices match class_names.json in alphabetical order:
| Index | Label | Examples |
|---|---|---|
0 |
cardboard |
Boxes, cartons, packaging |
1 |
glass |
Bottles, jars |
2 |
metal |
Aluminium cans, tins, scrap |
3 |
organic |
Food scraps, fruit and vegetable waste |
4 |
paper |
Newspapers, office paper, magazines |
5 |
plastic |
Bottles, containers, packaging |
π How to Use
1. Installation
Run inference using any lightweight TFLite runtime (ai-edge-litert, tflite-runtime, or full tensorflow):
pip install ai-edge-litert pillow numpy
2. Inference Example (Python)
import json
import numpy as np
from PIL import Image, ImageOps
from ai_edge_litert.interpreter import Interpreter
# Load model and class names
interpreter = Interpreter(model_path="waste_classifier.tflite")
interpreter.allocate_tensors()
with open("class_names.json", "r") as f:
class_names = json.load(f)
def preprocess(image_path: str) -> np.ndarray:
img = Image.open(image_path)
img = ImageOps.exif_transpose(img)
img = img.convert("RGB").resize((224, 224))
# Do not normalize: the model includes its own Rescaling(1/127.5, -1) layer
return np.expand_dims(np.array(img, dtype="float32"), axis=0)
def predict(image_path: str, top_k: int = 3, confidence_threshold: float = 0.60):
x = preprocess(image_path)
inp = interpreter.get_input_details()[0]
out = interpreter.get_output_details()[0]
interpreter.set_tensor(inp["index"], x)
interpreter.invoke()
probs = interpreter.get_tensor(out["index"])[0]
order = np.argsort(probs)[::-1][:top_k]
results = [
{"label": class_names[i], "confidence": float(probs[i])}
for i in order
]
status = "ok" if results[0]["confidence"] >= confidence_threshold else "uncertain"
return status, results
status, top_predictions = predict("sample.jpg")
print("Status:", status)
for pred in top_predictions:
print(f" {pred['label']:10s}: {pred['confidence'] * 100:.1f}%")
(If deploying with full tensorflow instead of ai-edge-litert, replace the import with import tensorflow as tf and set Interpreter = tf.lite.Interpreter.)
π§ Training Details
Dataset Split (2,990 Images Total)
Images were balanced (capping organic at 600 samples) and split 70% / 15% / 15% into train, validation, and test sets:
| Class | Train | Validation | Test | Total |
|---|---|---|---|---|
cardboard |
282 | 60 | 61 | 403 |
glass |
350 | 75 | 76 | 501 |
metal |
287 | 61 | 62 | 410 |
organic |
420 | 90 | 90 | 600 |
paper |
415 | 89 | 90 | 594 |
plastic |
337 | 72 | 73 | 482 |
| Total | 2,091 | 447 | 452 | 2,990 |
Architecture & Hyperparameters
- Data Augmentation (in-graph):
RandomFlip("horizontal"),RandomRotation(0.1),RandomZoom(0.15),RandomBrightness(0.2),RandomContrast(0.2) - Classification Head:
GlobalAveragePooling2DβDropout(0.3)βDense(6, activation="softmax") - Class Imbalance Handling: Balanced class weights via
sklearn.utils.class_weight.compute_class_weight - Phase A (Head Training): Base frozen,
Adam(1e-3),categorical_crossentropy, up to15epochs withEarlyStopping(monitor="val_accuracy", patience=4, restore_best_weights=True)andReduceLROnPlateau(factor=0.3, patience=2, min_lr=1e-7) - Phase B (Fine-Tuning): Top
30layers ofMobileNetV2unfrozen,Adam(1e-5), up to10epochs (best validation weights retained between Phase A and Phase B)
π Evaluation Results
Evaluated on the held-out test set (452 images):
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
cardboard |
0.898 | 0.869 | 0.883 | 61 |
glass |
0.797 | 0.724 | 0.759 | 76 |
metal |
0.757 | 0.903 | 0.824 | 62 |
organic |
0.968 | 1.000 | 0.984 | 90 |
paper |
0.925 | 0.822 | 0.871 | 90 |
plastic |
0.818 | 0.863 | 0.840 | 73 |
| Accuracy | 0.865 | 452 | ||
| Macro Avg | 0.861 | 0.863 | 0.860 | 452 |
| Weighted Avg | 0.868 | 0.865 | 0.865 | 452 |
β οΈ Limitations & Intended Use
- Intended Use: Educational, prototype, and consumer waste-sorting assistance (such as the Go green web app).
- Single-Item Assumption: Designed to classify one dominant item per photo; cluttered scenes or multiple overlapping items reduce accuracy.
- Visual Ambiguity: Clear glass, reflective metal, and transparent plastic can look similar depending on lighting and background.
- Out-of-Distribution Items: The model only predicts the 6 trained classes. Hazardous/special items (batteries, electronics, medicines, chemicals) are not supportedβin production, predictions below a confidence threshold (e.g.,
0.60) should be flagged as uncertain rather than forced into a category. - Regional Rules: Disposal recommendations are general guidance; users should follow local municipal recycling regulations.
- Downloads last month
- 9
Evaluation results
- Test Accuracyself-reported0.865
- Macro F1self-reported0.860
- Weighted F1self-reported0.865