A deep learning-based waste classification system using ResNet18 and PyTorch Lightning
Features β’ Installation β’ Usage β’ Documentation β’ Results
This project implements an image classification system to automatically categorize waste materials into six classes: cardboard, glass, metal, paper, plastic, and trash. Built with PyTorch Lightning and ResNet18, it provides a modular, scalable solution for waste management automation.
- β¨ ResNet18 backbone fine-tuned for garbage classification
- β‘ PyTorch Lightning framework for clean, scalable training
- π Comprehensive EDA with visualization notebooks
- π Batch and single-image prediction support
- π Automatic metrics tracking with loss curves and performance reports
- π Auto-generated documentation using pdoc3
- π LaTeX report generation for academic documentation
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# Activate uv
source $HOME/.local/bin/env# Create Virtual Environment
uv venv garbage-env --python 3.12
# Install PyPI package
uv pip install -U -i https://pypi.org/simple/ garbage-classifier
# Launch demo
garbage-app# Clone the repository
git clone https://github.com/NeoLafuente/garbage_classifier.git
cd garbage_classifier
# Sync dependencies
uv sync
# Train the model
uv run source/train.py
# Make a prediction
uv run source/predict.py path/to/image.jpgThe project uses the Garbage Classification Dataset from Kaggle.
The notebook notebooks/create_sample_dataset.ipynb automatically prepares the dataset:
- Downloads the Garbage Classification Dataset
- Creates the
sample_datasetfolder insidedata/raw - Reduces dataset size for lightweight experimentation
Dataset Structure:
- 6 classes: cardboard, glass, metal, paper, plastic, trash
- Location:
data/raw/Garbage_Dataset_Classification/ - Metadata: Class distributions and image statistics in
metadata.csv
Train the GarbageClassifier model (ResNet18 with PyTorch Lightning):
uv run source/train.pyOutput:
- Model checkpoint:
models/weights/model_resnet18_garbage.ckpt - Loss curves:
models/performance/loss_curves/ - Training logs with metrics (accuracy, precision, recall, F1-score)
Configuration:
Edit source/utils/config.py to customize:
- Batch size
- Learning rate
- Number of epochs
- Train/validation split ratio
Load the trained model and classify new images.
# Predict a specific image
uv run source/predict.py img.jpg
# Use default image from config
uv run source/predict.pyProcess all images in a folder:
uv run source/predict.py data/test_images/
uv run source/predict.py ../new_samples/Features:
- Auto-detects valid image files (
.jpg,.jpeg,.png,.bmp,.gif,.tiff,.tif) - Progress indicators for batch processing
- Summary table with all predictions
Example Output:
Predicting: cardboard_sample.jpg
Class: cardboard | Confidence: 98.5%
- Backbone: ResNet18 (pretrained on ImageNet)
- Framework: PyTorch Lightning
- Input: 224x224 RGB images
- Output: 6-class probability distribution
- GarbageDataModule: PyTorch Lightning DataModule for efficient data loading
- LossCurveCallback: Custom callback for tracking and saving loss curves
- GarbageClassifier: Main model class with training/validation logic
HTML documentation is auto-generated from source code docstrings using pdoc3.
View Documentation:
- Open
docs/index.htmlin your browser
Regenerate Documentation:
uv run scripts/generate_docs.pyInteractive Jupyter notebooks for exploration and analysis:
| Notebook | Description |
|---|---|
create_sample_dataset.ipynb |
Dataset preparation and sampling |
dataset_exploration.ipynb |
EDA with class distribution and visualizations |
performance_analysis.ipynb |
Model evaluation, confusion matrices, error analysis |
Academic-style report with methodology and results:
- Source:
reports/main.tex - Compiled PDF:
reports/compiled/ - Figures:
reports/figures/EDA/- Exploratory data analysisreports/figures/performance/- Model metrics and evaluation
Central configuration in source/utils/config.py:
# Dataset configuration
CLASSES = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
DATA_PATH = 'data/raw/Garbage_Dataset_Classification'
# Model hyperparameters
BATCH_SIZE = 32
LEARNING_RATE = 0.001
NUM_EPOCHS = 50
# Data split
TRAIN_RATIO = 0.8
VAL_RATIO = 0.2.
βββ data
β βββ interim # Intermediate data transformations
β βββ processed # Final preprocessed data
β βββ raw # Original unprocessed datasets
β βββ Garbage_Dataset_Classification
β βββ images # Image files by class
β βββ metadata.csv # Dataset statistics
βββ docs # Auto-generated HTML documentation
βββ models
β βββ performance # Loss curves and metrics
β β βββ loss_curves
β βββ weights # Trained model checkpoints (.ckpt)
βββ notebooks # Jupyter notebooks for EDA and analysis
β βββ create_sample_dataset.ipynb
β βββ dataset_exploration.ipynb
β βββ performance_analysis.ipynb
βββ reports # LaTeX reports and figures
β βββ compiled # PDF reports
β βββ figures
β β βββ EDA # Exploratory analysis plots
β β βββ performance # Model evaluation plots
β βββ main.tex # Main report source
βββ scripts # Utility scripts
β βββ generate_docs.py # Documentation generator
βββ source # Main source code
β βββ predict.py # Prediction script
β βββ train.py # Training script
β βββ utils
β βββ config.py # Configuration file
β βββ custom_classes # Model implementations
β βββ GarbageClassifier.py
β βββ GarbageDataModule.py
β βββ LossCurveCallback.py
βββ pyproject.toml # Project dependencies (uv)
βββ README.md
Note: dummy.txt files are placeholders to preserve empty folder structure in Git.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Dataset: Garbage Classification Dataset by zlatan599 on Kaggle
- Framework: PyTorch Lightning for the excellent deep learning framework
- Model: ResNet18 architecture from torchvision
Neo Lafuente - @NeoLafuente
Project Link: https://github.com/NeoLafuente/garbage_classifier