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
| 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 |
.
├── 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
- Task: Image Classification
- Classes: 38 (plant species × disease combinations)
- Source: Kaggle – abdallahalidev/plantvillage-dataset
- Download: Set Kaggle credentials in
.envand run the dataset download cell
- Task: Object Detection (bounding boxes)
- Classes: 27 diseased leaf types
- Source: GitHub – pratikkayal/PlantDoc-Object-Detection-Dataset
- Download: Cloned automatically via
git clonein the notebook
# 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.ipynbGPU recommended. Training was done on a local CUDA-capable GPU. CPU fallback is supported but will be very slow for training.
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)
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
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% |
Precision/recall and mAP curves are saved to export/metric/ as JSON and can be visualized using the valuation_graph() function in the notebook.
The notebook includes multi-image grid visualization and video inference for both YOLO and Faster R-CNN.
- 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
torchmetricsandscikit-learnwithzero_division=0is more reliable. - Background class indexing in Faster R-CNN: Class 0 is reserved for background — misalignment here causes systematic misclassification.
Create a .env file (see .env.example) with:
KAGGLE_KEY=your_kaggle_api_key
KAGGLE_USERNAME=your_kaggle_username
This project was built for educational purposes. Datasets are subject to their original licenses (PlantVillage: MIT, PlantDoc: CC BY 4.0).