Utilizing Deep Learning for Waste Detection and Management

Association:

iSchool, University of Toronto

Duration:

1 week

Machine Learning

CNN

PyTorch

For our group project in INF2179H Machine Learning with Applications in Python, we wanted to leverage AI to optimize waste detection and management in Toronto. I'm responsible for building a Convolutional Neural Network (CNN) using a dataset of labeled waste images.

Problem Statement

Litter pollution in Toronto is a significant environmental issue, with over 50,000 large plastic trash items removed from the Toronto Harbour alone in 2025. The city spends approximately 27 million dollars annually on City Litter Operations, including cleanup and collection. The vast operations run for twenty hours per day, seven days per week with ample staff labouring to empty bins and sweep streets. Despite these efforts, effective waste collection and management is still a challenge. Waste misclassification leads to inefficiencies in recycling and waste management. Many items that could be recycled end up in landfills simply because they are sorted incorrectly. Thus, we envision a trash-collecting robot with advanced computer vision to automatically, efficiently, and correctly detect waste items.

Solution

Our comprehensive proposed solution addresses two aspects

  • Where to deploy the robots: By using Random Forest, we can uncover patterns that lead to higher amounts of littering in specific areas. Identifying these key features will allow us to anticipate when and where littering is likely to increase, enabling proactive measures such as deploying temporary bins or reallocating waste collection resources. Our objective is to identify the features most strongly correlated with litter accumulation. These insights will empower the city to optimize cleanup operations, ensuring that resources are directed efficiently to high-litter zones at the right times.

  • How the robots operate: We aim to develop a neural network model that can accurately classify images of household garbage into 12 distinct categories (e.g., paper, cardboard, metal, plastic, glass, clothes, shoes, batteries, etc.). This system would be able to identify litter in real time from images captured by cameras mounted along Toronto’s streets or other waste-prone areas. This would enable more targeted cleaning efforts, reducing time spent on unnecessary collection and allowing for quicker response times to areas that require attention.

CNN

I trained a Convolutional Neural Network (CNN) using a dataset of labeled waste images. The model learns to recognize patterns and features unique to each trash type. The dataset consists of 12 garbage classes, including batteries, cardboard, plastic, and more.

A CNN is well-suited for this task because it excels at image recognition by automatically detecting relevant visual patterns, such as textures, shapes, and colors, which are crucial for distinguishing different types of waste. Due to resource constraints and quick turnaround, I relied on the pre-trained model ResNet-50 for this task.

# Training data generator with augmentation
train_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.3,              # Increased from 0.2 to 0.3
    horizontal_flip=True,
    vertical_flip=True,
    brightness_range=[0.8, 1.2], # Random brightness adjustment
    fill_mode='nearest',
    validation_split=0.2
)

# Validation data generator (no augmentation, only rescaling)
val_datagen = ImageDataGenerator(
    rescale=1./255,
    validation_split=0.2
)

# Training data flow
train_gen = train_datagen.flow_from_directory(
    data_dir,
    target_size=img_size,
    batch_size=batch_size,
    class_mode='categorical',
    subset='training',
    shuffle=True,                # Ensure shuffling for better training
    seed=42                      # For reproducibility
)

# Validation data flow
val_gen = val_datagen.flow_from_directory(
    data_dir,
    target_size=img_size,
    batch_size=batch_size,
    class_mode='categorical',
    subset='validation',
    shuffle=False,              # No need to shuffle validation data
    seed=42
)

The baseline model uses GlobalAveragePooling2D() to reduce spatial dimensions before dense layers:

  • Fine tuning: Last 10 layers

  • Dense layer: 1

  • Dropout: 0.3 (single)

  • Evaluation: Basic accuracy plot

  • Epochs: 10

After that, I fine-tuned the model with better model architecture and advanced training strategies to improve performance and optimize efficiency:

  • Fine-tuning: Last 15 layers

  • Dense layers: 2 (512 → 256)

  • Dropout: 0.4 + 0.2 (two layers)

  • Callbacks: LR reduction, early stopping

  • Evaluation: Confusion matrix + full metrics

  • Epochs: 30 (with early stopping)

def build_model(num_classes):
    # Load ResNet50 (pre-trained)
    base_model = ResNet50(
        weights='imagenet',
        include_top=False,
        input_shape=(224, 224, 3)
    )

    # Unfreeze the last 15 layers for fine-tuning
    for layer in base_model.layers[:-15]:
        layer.trainable = False

    # Add custom layers
    x = GlobalAveragePooling2D()(base_model.output)
    x = Dense(512, activation='relu')(x)
    x = Dropout(0.3)(x)  # Moderate dropout
    x = Dense(256, activation='relu')(x)  # Additional layer
    x = Dropout(0.2)(x)  # Second dropout layer
    output = Dense(num_classes, activation='softmax')(x)

    # Create model
    model = Model(inputs=base_model.input, outputs=output)

    return model

def create_callbacks(model_path='best_garbage_classifier.keras'):
    # Reduce learning rate when plateau occurs
    reduce_lr = ReduceLROnPlateau(
        monitor='val_loss',
        factor=0.5,
        patience=3,
        min_lr=1e-5,
        verbose=1
    )

    # Early stopping to prevent overfitting
    early_stopping = EarlyStopping(
        monitor='val_accuracy',
        patience=5,
        restore_best_weights=True
    )

    # Save best model
    model_checkpoint = ModelCheckpoint(
        model_path,
        monitor='val_accuracy',
        save_best_only=True,
        mode='max'
    )

    return [reduce_lr, early_stopping, model_checkpoint]
def plot_training_history(history):
    plt.figure(figsize=(12,4))

    # Accuracy subplot
    plt.subplot(1,2,1)
    plt.plot(history.history['accuracy'], label='Training Accuracy')
    plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
    plt.title('Model Accuracy')
    plt.xlabel('Epoch')
    plt.ylabel('Accuracy')
    plt.legend()

    # Loss subplot
    plt.subplot(1,2,2)
    plt.plot(history.history['loss'], label='Training Loss')
    plt.plot(history.history['val_loss'], label='Validation Loss')
    plt.title('Model Loss')
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.legend()

    plt.tight_layout()
    plt.savefig('training_history.png')
    plt.close()

def plot_confusion_matrix(model, validation_generator):
    # Predict classes
    predictions = model.predict(validation_generator)
    y_pred = np.argmax(predictions, axis=1)
    y_true = validation_generator.classes

    # Get class labels
    class_labels = list(validation_generator.class_indices.keys())

    # Compute confusion matrix
    cm = confusion_matrix(y_true, y_pred)

    # Plot
    plt.figure(figsize=(10,8))
    sns.heatmap(cm, annot=True, fmt='d',
                xticklabels=class_labels,
                yticklabels=class_labels)
    plt.title('Confusion Matrix')
    plt.xlabel('Predicted Label')
    plt.ylabel('True Label')
    plt.tight_layout()
    plt.savefig('confusion_matrix.png')
    plt.close()

Results

Final Test Loss: 1.2593

Final Test Accuracy: 59.81%

While the model is performing well, there is still room for improvement due to quality and imbalances in the data it was trained on. Addressing these gaps will further improve its accuracy and dependability.

Demo

This project demonstrates the potential of AI in improving waste classification and sustainability efforts. With further refinements, this technology could significantly enhance recycling efficiency.

Let's Talk

Let's Talk

Let's Talk

© 2025. All rights Reserved.

© 2025. All rights Reserved.

© 2025. All rights Reserved.