Anomaly Detection on Grape Images Using CNNs

Sapienza University of Rome
Bachelor's Thesis Project - European Canopies Initiative
European Canopies Initiative Overview

The European Canopies initiative: advancing human-robot collaboration in precision agriculture

๐Ÿ‡ 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.

The system focuses on automatically distinguishing between healthy and damaged grapes to improve crop quality monitoring and plant health assessment, contributing to more efficient and sustainable agricultural practices.

๐Ÿ“˜ Problem & Solution

  • 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.

Organization:

  • 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, effectively balancing the dataset and improving model generalization.

๐Ÿง  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 benchmark paper:

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

๐Ÿ› ๏ธ Implementation & Optimization

Technical Configuration:

  • 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 Google Colab time constraints and ensure reproducibility.

๐Ÿ” Results

Performance Metrics:

  • 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
  • Good generalization with proper data augmentation

โš ๏ธ 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 in single images)
  • Lack of precise centering, unlike the benchmark dataset
  • Inconsistent lighting and background conditions

๐Ÿ“Œ Main Insight:

Better image acquisition and labeling are crucial for achieving state-of-the-art results in agricultural computer vision applications. The quality of training data often matters more than model complexity.

๐Ÿ”ฎ Future Improvements:

  • Improved data collection with consistent lighting and positioning
  • More sophisticated data augmentation techniques
  • Integration with real-time robotic systems for field deployment

BibTeX

@misc{decarlo2023grapes,
  title={Anomaly Detection on Grape Images Using CNNs},
  author={De Carlo, Andrea},
  year={2023},
  institution={Sapienza University of Rome},
  type={Bachelor's Thesis},
  note={European Canopies Initiative}
}