Anomaly Detection on Grape Images Using CNNs

Bachelor's thesis project @ UniRoma1

๐Ÿ‡ Project Overview

This project is part of the European Canopies initiative, aimed at enhancing human-robot collaboration in precision agriculture.
The goal is to automatically detect anomalies in grape berries using Convolutional Neural Networks (CNNs), particularly a LeNet-based architecture, and compare its performance with a benchmark study.


๐Ÿ“˜ Summary

  • Problem: Automatically distinguish between healthy and damaged grapes to improve crop quality and plant health monitoring.
  • Solution: A CNN trained on a custom dataset of grape images, optimized via hyperparameter tuning and evaluated against a reference paper.
  • Tools: Python, PyTorch, Google Colab, Matplotlib, Seaborn.

๐Ÿ—‚๏ธ Dataset

The dataset is composed of RGB patches at multiple zoom levels (focus on 1.5x), each containing a single grape.
Organized into:

  • good/ (healthy grapes)
  • bad/ (damaged grapes)

To handle dataset imbalance (1:4 bad to good), image augmentation was applied via 90ยฐ, 180ยฐ, and 270ยฐ rotations.

Left: structure of the dataset. Middle: example of a good grape. Right: example of a damaged grape.

๐Ÿง  State of the Art

The benchmark paper used a LeNet CNN and compared it with a deeper ResNet50 (pretrained and from scratch).
Despite the complexity of ResNet, LeNet showed greater stability and accuracy in the specific binary classification task.

Performance from the paper:

  • Training accuracy: 96.4%
  • Validation accuracy: 97%
  • Precision: 96.26%
  • Recall: 93.34%

๐Ÿ› ๏ธ Implementation & Optimization

  • Weight Initialization: Xavier (Glorot)
  • Loss Function: BCEWithLogitsLoss
  • Optimizers Compared: SGD vs Adam โ†’ Chosen: SGD
  • Hyperparameter tuning: Grid Search
    • Learning rate: 0.001
    • Momentum: 0
    • Weight decay: 0.001

Model saving via torch.save() was used to handle Colab time constraints.


๐Ÿ” Results

  • Validation Accuracy: ~80%
  • Precision: 75%
  • Recall: 85%
  • Training stopped at epoch 70 to prevent overfitting.

Strengths:

  • Effective in detecting damaged berries with darker hues and rough textures.

Weaknesses:

  • False positives: Blurry or shiny healthy grapes misclassified as damaged.
  • False negatives: Subtly damaged grapes within healthy clusters.

๐Ÿงพ Conclusions

While the model performed reasonably well, it did not match the reference paper due to dataset quality issues:

  • Mixed-class patches (both healthy and damaged berries)
  • Lack of precise centering, unlike the benchmark dataset

๐Ÿ“Œ Main insight: Better image acquisition and labeling are crucial for achieving state-of-the-art results.


๐Ÿ“„ Download Report

You can download the full project report here.