Skip to content

SNMiguel/PlantGuard-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

26 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌿 PlantGuard-AI

A reasoning agent that turns a single crop-leaf photo into a grounded, cited action plan β€” diagnosis, differential reasoning, treatment, and economic impact.

Python PyTorch Accuracy Foundry IQ License

Microsoft Agents League β€” Reasoning Agents track. Integrates Microsoft Foundry IQ (the required Microsoft IQ intelligence layer) for agentic, cited knowledge retrieval.

Watch the PlantGuard-AI demo
β–Ά Watch the 3-minute demo


🎯 What it is

A diagnosis alone doesn't tell a farmer what to do. PlantGuard-AI pairs a 95%-accurate vision model with a multi-step reasoning agent that:

  1. Detects the disease from a leaf photo (ResNet18, 38 classes / 14 crops).
  2. Reasons through a differential diagnosis when the call is ambiguous β€” running separate Foundry IQ retrievals per candidate, weighing distinguishing symptoms, and telling the farmer what to physically inspect to confirm.
  3. Grounds every recommendation in cited agricultural knowledge via Microsoft Foundry IQ (Azure AI Search agentic retrieval) β€” reducing hallucination.
  4. Quantifies the stakes with a deterministic economic-exposure estimate ($ at risk for a given field size).

It degrades gracefully: with no cloud credentials it falls back to a curated, citation-backed knowledge base and offline reasoning, so the demo always runs.

🧠 How it works

 leaf image
     β”‚
     β–Ό
 PlantDoctor (predict.py)          ResNet18 + checkpoint β†’ top-k disease classes
     β”‚
     β–Ό
 ReasoningAgent (agent.py) ──────────────────────────────────────────────┐
     β”‚  Step 1  Differential diagnosis  (parallel, multi-retrieval)        β”‚
     β”‚          β€’ separate Foundry IQ retrieval per candidate              β”‚
     β”‚          β€’ weigh distinguishing symptoms β†’ verdict + what to check  β”‚
     β”‚  Step 2  Action plan: diagnosis Β· severity Β· treatment Β· economics  β”‚
     β”‚          β€’ grounded + cited                                         β”‚
     β–Ό                                                                     β”‚
 FoundryIQ (foundry.py)  ◄───── live: Azure AI Search agentic retrieval β”€β”€β”˜
     β”‚                          fallback: curated cited agronomy KB
     β–Ό
 economics.py β†’ "$X–$Y at risk if untreated"  (deterministic estimate)
     β”‚
     β–Ό
 Web app (api.py + website/)  /  Gradio app (app.py)

✨ Key features

  • Vision model β€” ResNet18 transfer learning, 95.02% test accuracy over 38 PlantVillage classes.
  • Differential-diagnosis reasoning loop β€” genuine multi-step, tool-using reasoning that adjudicates competing diagnoses with grounded evidence (runs the retrievals in parallel for ~6s latency).
  • Microsoft Foundry IQ grounding β€” answers are retrieved and cited from an Azure AI Search knowledge base built from the project's agronomy docs.
  • Economic exposure calculator β€” a transparent, code-computed $ at risk estimate (not LLM-guessed), labeled as an estimate.
  • Confidence-aware & safe β€” low-confidence calls are de-escalated and flagged; healthy leaves produce no false alarms; every claim carries a citation and an "consult your extension service" disclaimer.
  • Always demoable β€” automatic offline fallback for both reasoning and grounding.

πŸš€ Quick start

pip install -r requirements.txt

Model checkpoint β€” the trained ResNet18 weights (resnet18_best.pth, ~129 MB) are too large for a normal Git repo, so they're distributed separately. Get them one of two ways:

  • Download from the repo's Releases and place at models/saved/resnet18_best.pth, or
  • Retrain: python main.py --data_dir data/raw --epochs 10 (needs the PlantVillage dataset).
# Single-image diagnosis (CLI)
python predict.py demo_images/1_tomato_late_blight.jpg

# Launch the full web app β€” marketing site + in-page leaf scanning (one server)
python api.py            # β†’ http://127.0.0.1:8000

# Or the standalone Gradio demo (alternative UI)
python app.py            # β†’ http://127.0.0.1:7860

Optional: enable live LLM reasoning + Foundry IQ

Copy .env.example to .env and fill in credentials (see the file for options β€” GitHub Models / Azure OpenAI for reasoning, Azure AI Search for Foundry IQ). Everything works without these via offline fallbacks.

python foundry.py --selftest-live      # validate the live Foundry IQ wiring

To provision your own Foundry IQ knowledge base from the bundled agronomy docs, see tools/export_kb.py and tools/setup_foundry_iq.py.

πŸ“ Project structure

PlantGuard-AI/
β”œβ”€β”€ predict.py            # Vision inference (ResNet18 checkpoint) β†’ top-k classes
β”œβ”€β”€ agent.py              # Multi-step reasoning agent (differential + action plan)
β”œβ”€β”€ foundry.py            # Microsoft Foundry IQ grounding (live + offline fallback)
β”œβ”€β”€ economics.py          # Deterministic economic-exposure estimator
β”œβ”€β”€ config.py             # Env/.env configuration for LLM + Foundry IQ
β”œβ”€β”€ app.py                # Gradio demo UI (standalone/fallback)
β”œβ”€β”€ api.py                # FastAPI backend: /api/diagnose + serves website/
β”œβ”€β”€ main.py / train.py    # Training pipeline (transfer learning)
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ resnet_model.py   # ResNetClassifier
β”‚   └── saved/            # Trained checkpoint (resnet18_best.pth)
β”œβ”€β”€ utils/                # data_loader, transforms, metrics, visualizer
β”œβ”€β”€ knowledge_base/       # 27 cited agronomy docs (indexed by Foundry IQ)
β”œβ”€β”€ tools/
β”‚   β”œβ”€β”€ export_kb.py          # Export the agronomy KB to upload-ready files
β”‚   └── setup_foundry_iq.py   # Provision the Azure AI Search knowledge base
β”œβ”€β”€ website/              # Single-page web app + marketing site (served by api.py)
β”œβ”€β”€ demo_images/          # Curated sample leaves for the demo
β”œβ”€β”€ DEMO_SCRIPT.md        # 60-second demo video script
└── SUBMISSION.md         # Hackathon submission notes

πŸ“Š Model performance

Metric Value
Test accuracy 95.02%
Precision 95.75%
Recall 95.02%
F1-score 95.01%
Classes / crops 38 / 14

🌱 Supported crops & diseases

Crops: Apple, Blueberry, Cherry, Corn, Grape, Orange, Peach, Pepper, Potato, Raspberry, Soybean, Squash, Strawberry, Tomato β€” 38 disease + healthy classes total.

πŸ› οΈ Technical notes

  • Vision: ResNet18 (ImageNet pre-trained), 224Γ—224 RGB, FC head replaced for 38 classes, 70/15/15 split, Adam + ReduceLROnPlateau.
  • Reasoning engine: Azure AI Foundry / any OpenAI-compatible endpoint, with an offline knowledge-base composer as fallback.
  • Grounding: Foundry IQ knowledge base backed by Azure AI Search agentic retrieval (minimal reasoning effort = no extra LLM cost), with a curated cited KB as fallback.
  • Platform: num_workers=0 throughout for Windows compatibility.

πŸ“„ License

MIT License.

πŸ‘€ Author

Miguel Shema Ngabonziza

πŸ™ Acknowledgments

  • PlantVillage dataset (Penn State University).
  • Agronomy knowledge grounded in public extension sources (UC IPM, Cornell, PennState, CABI, FAO, APS).
  • Built for the Microsoft Agents League @ AISF 2026 β€” Reasoning Agents track.

About

Deep learning plant disease detection system using PyTorch and transfer learning. Classifies 38 crop diseases across 14 plant species with ResNet and EfficientNet models. Built for agricultural AI applications.

Topics

Resources

License

Stars

2 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors