Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Garbage Classification API

A CNN-powered REST API that identifies recyclable waste categories from photos in real time — enabling automated sorting decisions at recycling facilities and smart bin deployments.

Python PyTorch Streamlit Accuracy License: MIT


Business Problem

Manual waste sorting is one of the largest operational costs in recycling infrastructure — misclassification rates above 20% cause contamination of recyclable streams, leading to material rejection and landfill overflow. An automated vision system that correctly identifies cardboard, glass, metal, paper, plastic, and general trash from a single photo enables real-time conveyor belt sorting, reduces human labor costs, and directly improves recycling yield rates for facilities processing thousands of items per hour.


Demo

Upload an image through the Streamlit interface and get an instant classification result.

Supported waste categories: cardboard · glass · metal · paper · plastic · trash


Results

Metric Score
Train Accuracy 77.44%
Test Accuracy 75.3%

Model: Custom 3-block CNN (Conv2d → BatchNorm2d → ReLU → MaxPool2d), trained from scratch, no transfer learning.

Baseline (random classifier, 6 classes): Accuracy = 16.7% ↑ +58.6 pp improvement vs baseline


Dataset

  • Source: Garbage Classification (Kaggle) — asdasdasasdas/garbage-classification
  • Size: ~2,527 real-world waste images across 6 categories
  • Split: 80% train / 20% test via random_split (seed=42)
  • Preprocessing: resized to 128×128 for both training and inference
  • Class balance: near-balanced, no resampling required

Approach

  1. Data Loading — downloaded via kagglehub, loaded with torchvision.datasets.ImageFolder; 80/20 train/test split with fixed seed
  2. Augmentation (train only)RandomHorizontalFlip, RandomRotation(15), ColorJitter(brightness=0.3, contrast=0.3) to improve robustness to real-world photo variability (lighting, angle)
  3. NormalizationNormalize([0.5]*3, [0.5]*3), identical for train/test
  4. Model Architecture — 3-block CNN: Conv2d(3→32→64→128) + BatchNorm2d
    • ReLU + MaxPool2d(2) per block; classifier head with Dropout(0.4) and Linear(128*16*16 → 256 → 6)
  5. Training — 30 epochs, Adam (lr=0.0003), CrossEntropyLoss, loss reduced from ~123 to ~54 over training
  6. Inference App — Streamlit interface with image upload, cached model loading (@st.cache_resource), CUDA/CPU auto-detection

Key Challenges & Solutions

Small real-world dataset (~2,500 images) prone to overfitting With ~400 images per class and significant real-world photo variability (lighting, background, angle), a plain CNN overfits quickly → added RandomHorizontalFlip, RandomRotation(15), ColorJitter on the training split and Dropout(0.4) in the classifier head → train/test gap stayed under 3 percentage points (77.44% vs 75.3%), indicating the model generalizes rather than memorizes.

Consistent train/inference preprocessing Mismatched resize/normalization between the training notebook and the production app is a common source of silent accuracy drops → both GarbageClassification.ipynb and main.py use identical Resize((128,128))

  • Normalize([0.5]*3, [0.5]*3), and the same GarbageClassifier class definition is duplicated exactly in both files → guarantees the served model's feature map dimensions match the trained checkpoint.

Uploaded images in arbitrary formats (PNG/RGBA, JPEG) Raw uploads can arrive as RGBA, grayscale, or other formats incompatible with the 3-channel input the model expects → Image.open(...).convert('RGB') applied unconditionally before the transform pipeline → all supported formats are normalized to a valid model input, with errors caught and surfaced via st.error() instead of crashing the app.


Tech Stack

Category Tools
Language Python 3.11
ML PyTorch, torchvision
App Streamlit
Data KaggleHub, Pillow, Matplotlib
Regularization BatchNorm2d, Dropout

Project Structure

GarbageClassification/
├── .gitignore
├── readme.md
├── requirements.txt
└── GarbageClassification/
    ├── GarbageClassification.ipynb
    ├── datasets/
    │   └── trash_dataset.zip
    ├── labels_GarbageClassification.pth
    ├── main.py
    ├── model_GarbageClassification.pth
    ├── sample_images.png
    └── tests/
        ├── cardboard117.jpg
        ├── glass11.jpg
        ├── metal103.jpg
        ├── paper104.jpg
        ├── plastic103.jpg
        └── trash113.jpg

How to Run

# 1. Clone and install
git clone https://github.com/your-username/garbage-classification
cd garbage-classification/GarbageClassification
pip install torch torchvision streamlit pillow
# 2. Train the model (optional — pretrained weights included)
# open and run GarbageClassification.ipynb (Colab-ready, GPU recommended)
# 3. Launch the app
streamlit run main.py
# Opens at http://localhost:8501

Business Impact

  • ↓ reduction in manual sorting labor costs at recycling facilities vs fully manual classification pipelines (estimated)
  • ↑ 75.3% automated classification accuracy across 6 material types, trained from scratch on a compact real-world dataset with no external pretrained weights
  • ↑ Streamlit interface enables non-technical staff to validate sorting decisions without API integration overhead
  • ↑ Retrainable on proprietary facility-specific waste categories with minimal code changes — no vendor lock-in

About

CNN-based waste sorting classifier (6 categories, 75% test accuracy) trained from scratch on PyTorch, served via Streamlit

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages