Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crop Disease AI API

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.

Features

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

Datasets

Primary training dataset:

Generalization dataset:

Recommended layout after preparation:

data/processed/plantvillage/
├─ train/
├─ val/
└─ test/

Quick Start

1. Backend environment

Windows PowerShell:

cd D:\archive\crop-disease-ai-api
powershell -ExecutionPolicy Bypass -File .\scripts\setup_backend_windows.ps1
.\.venv\Scripts\Activate.ps1

Manual 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.txt

2. Download and prepare data

Smoke-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/plantvillage

Full dataset:

python backend/scripts/download_data.py --plantvillage --plantdoc
python backend/scripts/prepare_data.py --input data/raw/plantvillage --output data/processed/plantvillage

3. Train model

python backend/scripts/train.py --data-dir data/processed/plantvillage --epochs 10 --batch-size 32

Output:

backend/models/plant_disease_effnet_b0.pt
backend/models/class_names.json

4. Evaluate

python backend/scripts/evaluate.py --data-dir data/processed/plantvillage/test

Outputs:

outputs/evaluation/evaluation_report.json
outputs/evaluation/confusion_matrix.png

5. Run API

uvicorn backend.app.main:app --reload --host 127.0.0.1 --port 8000

Health endpoint:

curl http://127.0.0.1:8000/api/health

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

6. Run UI

cd frontend
pnpm install
pnpm dev

Open:

http://127.0.0.1:5173

API

GET /api/health

Returns service status, model loading state, class count, and device.

GET /api/classes

Returns all known disease classes after class_names.json is available.

POST /api/predict

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": "建议加强田间巡查,并考虑针对性药剂防治。"
  }
}

POST /api/predict/batch

Input: multiple uploaded image files. Each file returns either a prediction or an error.

GET /api/predictions/{id}/heatmap

Returns the Grad-CAM heatmap image for a prediction.

Project Structure

crop-disease-ai-api/
├─ backend/
│  ├─ app/
│  ├─ scripts/
│  └─ models/
├─ frontend/
│  └─ src/
├─ data/
├─ outputs/
└─ scripts/

Notes

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages