Skip to content

Repository files navigation

Plant Disease Detection — Computer Vision

A practical implementation of computer vision techniques for plant disease detection. The project covers the full ML pipeline: dataset preparation, model training, evaluation, and inference — across two tasks (image classification and object detection) using three model architectures.

Author: Phan Quoc Huy · April–June 2025


Overview

Task Model Dataset Best mAP@50
Image Classification EfficientNet B7 (fine-tuned) PlantVillage (38 classes)
Object Detection YOLOv8 / YOLO11 PlantDoc (27 classes) ~0.75+
Object Detection Faster R-CNN (ResNet-50 FPN) PlantDoc (27 classes) 0.90

Project Structure

.
├── Plant_Disease_Detection_CV.ipynb   # Main notebook (full pipeline)
├── datasets/                          # Datasets (downloaded separately)
│   ├── PlantDoc-Dataset/              # Object detection dataset with bounding boxes
│   └── plantvillage dataset/          # Classification dataset (38 disease classes)
├── export/
│   ├── metric/                        # Training metrics (JSON) and plots
│   └── weight/                        # Trained model weights (.pth)
├── runs/detect/                       # YOLO training run outputs
├── test_images/                       # Sample images and videos for inference
├── requirements.txt
└── .env.example                       # Environment variable template

Datasets

PlantVillage

PlantDoc


Setup

# 1. Clone the repo
git clone git@github.com:Aer-3888/Plant_detection.git
cd Plant_detection

# 2. Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate        # Linux/macOS
.venv\Scripts\activate           # Windows

# 3. Install dependencies
pip install -r requirements.txt

# 4. Set up environment variables
cp .env.example .env
# Edit .env and fill in your KAGGLE_KEY and KAGGLE_USERNAME

# 5. Open the notebook
jupyter notebook Plant_Disease_Detection_CV.ipynb

GPU recommended. Training was done on a local CUDA-capable GPU. CPU fallback is supported but will be very slow for training.


Models

1. EfficientNet B7 — Image Classification

Fine-tunes the EfficientNet B7 backbone on PlantVillage for single-label leaf disease classification. The trained backbone's feature extraction layers are also reused as a backbone for Faster R-CNN.

  • Input size: 224×224 (hardware constraint; B7 is optimized for 600×600)
  • Loss: CrossEntropyLoss
  • Optimizer: AdamW (lr=1e-4, weight_decay=1e-2)

2. YOLO (v8 / v11) — Object Detection

Trains YOLO on the PlantDoc dataset with proper bounding box annotations. Includes data preparation scripts to convert CSV annotations into YOLO label format.

  • Pretrained weights: yolov8m.pt / yolo11m.pt
  • Training: 50 epochs on PlantDoc
  • Includes image and video inference

3. Faster R-CNN (ResNet-50 FPN) — Object Detection

Fine-tunes the torchvision fasterrcnn_resnet50_fpn model on PlantDoc. Includes a full custom evaluation pipeline with per-class precision, recall, F1, mAP@50, and mAP@50:95.

Best results (15 epochs):

Metric Value
mAP@50 0.90
mAP@50:95 0.69
Precision 74.9%
Recall 85.8%
F1 Score 79.9%

Results

Faster R-CNN — Training Curves

Precision/recall and mAP curves are saved to export/metric/ as JSON and can be visualized using the valuation_graph() function in the notebook.

YOLO — Inference Examples

The notebook includes multi-image grid visualization and video inference for both YOLO and Faster R-CNN.


Key Learnings

  • Dataset choice is critical: PlantVillage works for classification only. PlantDoc with bounding box annotations is necessary for object detection.
  • Model-hardware alignment: EfficientNet B7 is optimized for 600×600 input; 224×224 constrained by local GPU led to suboptimal feature extraction.
  • Robust evaluation matters: Custom metric code must handle zero-division edge cases. Using torchmetrics and scikit-learn with zero_division=0 is more reliable.
  • Background class indexing in Faster R-CNN: Class 0 is reserved for background — misalignment here causes systematic misclassification.

Environment Variables

Create a .env file (see .env.example) with:

KAGGLE_KEY=your_kaggle_api_key
KAGGLE_USERNAME=your_kaggle_username

License

This project was built for educational purposes. Datasets are subject to their original licenses (PlantVillage: MIT, PlantDoc: CC BY 4.0).

About

Computer-vision pipeline for plant disease detection: image classification (EfficientNet-B7) and object detection (YOLOv8, Faster R-CNN) on PlantDoc/PlantVillage.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages