Automating Trash Sorting with AI: Building a CNN Model to Classify Waste
In today’s fast-paced world, waste management has become one of the most pressing environmental challenges. With urbanization and population growth, the amount of waste we generate continues to skyrocket. The traditional way of handling waste involves manual sorting — a process that is time-consuming, labor-intensive, and prone to human error.
But what if we could automate this process using artificial intelligence (AI)? Imagine a system that could identify and classify waste — distinguishing recyclable materials like glass, plastic, or paper from non-recyclable trash — all in a matter of seconds.
This is exactly what we set out to achieve in our project: Automated Image Recognition for Trash Sorting.
In this blog, we’ll walk you through how we built a Convolutional Neural Network (CNN) model to classify trash images using Python and TensorFlow. This is purely a software solution — a trained AI model ready to take in images and predict their category.
Why Automate Trash Sorting?
Before diving into the technical details, let’s understand why this project matters.
-
Environmental ImpactImproper waste segregation leads to recyclable materials ending up in landfills. Automating this process increases recycling efficiency and reduces environmental pollution.
-
Reducing Human EffortManual sorting requires people to handle dirty or hazardous waste. Automating the classification helps improve safety for waste management workers.
-
ScalabilityOnce trained, the model can classify thousands of images quickly, making it suitable for large-scale waste management systems.
-
Real-World Applications
-
Smart bins that sort trash automatically
-
AI-powered recycling plants
-
Municipal waste monitoring systems
-
Awareness apps to teach proper waste segregation
-
This project is a step towards sustainable, smart waste management solutions using AI.
Project Overview
Here’s what we set out to do:
-
Goal:Build a CNN model that can classify trash into categories such as:
-
Cardboard
-
Glass
-
Metal
-
Paper
-
Plastic
-
General Trash (non-recyclable)
-
-
Dataset:We used the TrashNet dataset, which is widely used for waste classification research. It contains labeled images of waste materials across these six categories.
-
Automation Workflow:
-
Train a CNN model using Python and TensorFlow.
-
Create a script that automatically classifies images placed in a folder.
-
Save the results for reporting or visualization (e.g., database or dashboard).
-
Tools and Technologies Used
-
Programming Language: Python
-
Libraries:
-
TensorFlow – for building and training the CNN
-
NumPy – for numerical operations
-
Pandas – for handling datasets
-
OpenCV – for image handling
-
Matplotlib – for visualizing data
-
Scikit-learn – for splitting data into train and validation sets
-
-
Development Environment: Google Colab (free GPU support)
Step-by-Step Project Implementation
Step 1: Understanding the Dataset
The TrashNet dataset contains images of six waste categories:
-
Cardboard
-
Glass
-
Metal
-
Paper
-
Plastic
-
Trash (non-recyclable)
The images vary in lighting, angle, and background, making the dataset realistic for training a robust model.
The folder structure looks like this:
dataset-resized/│├── cardboard/├── glass/├── metal/├── paper/├── plastic/└── trash/
Step 2: Setting Up the Environment
We began by installing the necessary Python libraries:
!pip install tensorflow opencv-python pillow numpy pandas matplotlib scikit-learn --quiet
This step ensures that we have all the tools required to preprocess images, build the CNN, and train it effectively.
Step 3: Preparing the Dataset
After uploading the dataset ZIP file to Colab, we extracted it and verified that all six class folders were present.
Then, we split the dataset into training (80%) and validation (20%) sets using train_test_split from Scikit-learn. This is crucial for evaluating the model’s performance on unseen data.
The final folder structure became:
trashnet_ready/│├── train/│ ├── cardboard/│ ├── glass/│ ├── metal/│ ├── paper/│ ├── plastic/│ └── trash/│└── val/├── cardboard/├── glass/├── metal/├── paper/├── plastic/└── trash/
Result:
2019 training images and 508 validation images were prepared.
Step 4: Image Preprocessing
For image classification tasks, preprocessing is key.
We applied the following techniques:
-
Rescaling:Normalizing pixel values to the range
[0, 1]. -
Data Augmentation:To make our model robust, we introduced variations like:
-
Random rotations
-
Width and height shifts
-
Zooming
-
Horizontal flips
-
Here’s the code:
IMG_SIZE = (128, 128)BATCH_SIZE = 32train_datagen = ImageDataGenerator(rescale=1./255,rotation_range=30,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)val_datagen = ImageDataGenerator(rescale=1./255)
Step 5: Building the CNN Model
We designed a Convolutional Neural Network with three convolutional layers, each followed by a max-pooling layer. This allows the model to capture complex patterns in the images.
Model Architecture:
-
Conv2D + MaxPooling2D
-
Conv2D + MaxPooling2D
-
Conv2D + MaxPooling2D
-
Flatten Layer
-
Dense Layer (256 neurons with ReLU activation)
-
Dropout Layer (50% to prevent overfitting)
-
Output Layer (6 neurons with Softmax activation)
Here’s the code:
model = Sequential([Conv2D(32, (3,3), activation='relu', input_shape=(128, 128, 3)),MaxPooling2D(2,2),Conv2D(64, (3,3), activation='relu'),MaxPooling2D(2,2),Conv2D(128, (3,3), activation='relu'),MaxPooling2D(2,2),Flatten(),Dense(256, activation='relu'),Dropout(0.5),Dense(6, activation='softmax')])model.compile(optimizer=Adam(learning_rate=0.0001),loss='categorical_crossentropy',metrics=['accuracy'])
Step 6: Training the Model
We trained the model for 10 epochs.
Sample output during training:
Epoch 1/10accuracy: 0.27 - val_accuracy: 0.38Epoch 5/10accuracy: 0.46 - val_accuracy: 0.48Epoch 10/10accuracy: 0.55 - val_accuracy: 0.47
Observations
The accuracy started at 27% and improved to ~55% by the final epoch.
With hyperparameter tuning and more epochs, this can be further improved.
Step 7: Saving the Model
Once trained, we saved the model for future use:
model.save("trash_sorter_model.h5")
This allows us to load it later and classify new images without retraining.
Step 8: Automating Image Classification
The final part of our project was automation.
We wrote a Python script that:
-
Monitors a folder for new images.
-
Automatically classifies any image dropped into that folder.
-
Outputs the predicted category.
This script can later be connected to a web dashboard or database to visualize results in real time.
Challenges Faced
No project is without its hurdles. Here are some challenges we encountered:
-
Imbalanced DatasetSome classes had more images than others, which affected model performance.
-
Low Validation AccuracyWhile the model learned from the training data, generalizing to unseen data was harder.Solution: Added dropout layers and used data augmentation.
-
Hardware LimitationsSince we didn’t use GPUs locally, we relied on Google Colab’s free GPU service.
Real-World Impact
While this project is currently a proof of concept, its potential applications are huge:
-
Smart Cities: Automated trash segregation in public bins.
-
Recycling Plants: Faster sorting without relying heavily on manual labor.
-
Educational Tools: Teach people about recycling through AI-powered apps.
Future Enhancements
There’s plenty of room to grow:
-
Increase Accuracy:Use transfer learning with pre-trained models like MobileNet or ResNet.
-
Add More Categories:Classify hazardous waste or e-waste.
-
Build a Web App:Deploy the model online for easy access.
-
Edge Device Integration:Connect the model to IoT hardware for real-time classification in smart bins.
Conclusion
This project demonstrates how AI can be applied to environmental sustainability. By building a CNN model to classify waste, we’ve taken a small but meaningful step toward solving one of the world’s most persistent problems.
Key takeaways:
-
Machine learning is a powerful tool for automating repetitive, error-prone tasks.
-
Even a simple model can significantly improve efficiency in waste management.
-
With more data and refinements, this solution could be scaled for real-world use.
By combining technology and environmental awareness, we can create a cleaner, greener future — one image at a time.
This blog presents key insights from our project report for the ‘Machine Learning’ course (MBA 2024–26, 4th trimester) at Amrita School of Business, Coimbatore, under the guidance of Dr. Prashobhan Palakkel.
Comments
Post a Comment