Local-first crop disease diagnosis system with a FastAPI inference API, PyTorch EfficientNet-B0 training pipeline, Grad-CAM visualization, and a React agricultural dashboard.
The project is designed for PlantVillage training and PlantDoc field-image generalization testing. Dataset and model weights are not committed to GitHub.
- Plant disease classification API with JSON responses.
- EfficientNet-B0 transfer learning pipeline for PlantVillage.
- Grad-CAM heatmap generation for visual diagnosis.
- Batch prediction endpoint for multiple leaf images.
- React + Vite + Tailwind dashboard with upload, result cards, Top-5 probabilities, heatmap, and local diagnosis history.
- CPU by default, CUDA automatically used when available.
Primary training dataset:
- PlantVillage GitHub: https://github.com/spMohanty/PlantVillage-Dataset
- PlantVillage Hugging Face: https://huggingface.co/datasets/mohanty/PlantVillage
Generalization dataset:
- PlantDoc GitHub: https://github.com/pratikkayal/PlantDoc-Dataset
Recommended layout after preparation:
data/processed/plantvillage/
├─ train/
├─ val/
└─ test/
Windows PowerShell:
cd D:\archive\crop-disease-ai-api
powershell -ExecutionPolicy Bypass -File .\scripts\setup_backend_windows.ps1
.\.venv\Scripts\Activate.ps1Manual install:
python -m venv .venv
.venv\Scripts\python -m pip install --upgrade pip
.venv\Scripts\python -m pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126
.venv\Scripts\python -m pip install -r backend/requirements.txtSmoke-test download with a small subset:
python backend/scripts/download_data.py --plantvillage --limit 600
python backend/scripts/prepare_data.py --input data/raw/plantvillage --output data/processed/plantvillageFull dataset:
python backend/scripts/download_data.py --plantvillage --plantdoc
python backend/scripts/prepare_data.py --input data/raw/plantvillage --output data/processed/plantvillagepython backend/scripts/train.py --data-dir data/processed/plantvillage --epochs 10 --batch-size 32Output:
backend/models/plant_disease_effnet_b0.pt
backend/models/class_names.json
python backend/scripts/evaluate.py --data-dir data/processed/plantvillage/testOutputs:
outputs/evaluation/evaluation_report.json
outputs/evaluation/confusion_matrix.png
uvicorn backend.app.main:app --reload --host 127.0.0.1 --port 8000Health endpoint:
curl http://127.0.0.1:8000/api/healthPrediction endpoint:
curl -X POST http://127.0.0.1:8000/api/predict ^
-F "file=@path\to\leaf.jpg"If no model has been trained yet, the API still starts and returns a clear model-missing message from /api/health.
cd frontend
pnpm install
pnpm devOpen:
http://127.0.0.1:5173
Returns service status, model loading state, class count, and device.
Returns all known disease classes after class_names.json is available.
Input: one uploaded image file.
Output:
{
"prediction_id": "uuid",
"crop": "Tomato",
"disease": "Late Blight",
"class_name": "Tomato___Late_blight",
"confidence": 0.94,
"risk_level": "high",
"top_k": [
{"class_name": "Tomato___Late_blight", "confidence": 0.94}
],
"heatmap_url": "/api/predictions/{id}/heatmap",
"advice": {
"en": "Increase field inspection and consider targeted fungicide control.",
"zh": "建议加强田间巡查,并考虑针对性药剂防治。"
}
}Input: multiple uploaded image files. Each file returns either a prediction or an error.
Returns the Grad-CAM heatmap image for a prediction.
crop-disease-ai-api/
├─ backend/
│ ├─ app/
│ ├─ scripts/
│ └─ models/
├─ frontend/
│ └─ src/
├─ data/
├─ outputs/
└─ scripts/
- This tool supports agronomic decision-making but should not replace expert field diagnosis.
- Real field images can be harder than PlantVillage lab-background images. Use PlantDoc to evaluate practical generalization.
- Model checkpoints, datasets, and generated heatmaps are ignored by git.