An end-to-end deep learning system for tomato leaf disease classification and severity estimation, deployed as a Hugging Face Space using a hybrid FastAPI + Gradio architecture.
This project goes beyond basic classification by estimating disease severity using Grad-CAM–based spatial analysis, making predictions more interpretable and practically useful.
Hugging Face Space
👉 https://huggingface.co/spaces/utkarshsingh0013/crop-detection-detection
Upload a tomato leaf image to get:
- Disease name
- Prediction confidence
- Estimated severity (%)
Most crop disease models only answer:
“What disease is this?”
In real agricultural settings, farmers also need to know:
“How severe is it?”
Severity determines:
- urgency of treatment
- choice of intervention
- potential yield impact
This project addresses both disease identification and severity estimation in a single pipeline.
The system performs:
- Disease Classification using a CNN
- Model Explainability using Grad-CAM
- Severity Estimation using spatial activation analysis
- Deployment via API and interactive UI
User (Browser)
├── Gradio UI (Hugging Face Space)
│ └── Image Upload
│
└── FastAPI Backend (/api/predict)
├── ResNet18 classifier
├── Grad-CAM localization
├── Leaf extraction (GrabCut)
└── Top-K severity computation
- Gradio → interactive user interface
- FastAPI → reusable backend API
- Docker → reproducible deployment environment
- Architecture: ResNet18
- Pretraining: ImageNet weights
- Dataset: PlantVillage (Tomato subset)
- Number of classes: 10
- Input size: 224 × 224
- Training strategy:
- Frozen backbone
- Trainable classification head
- A model can be highly confident about a disease
- The actual infected area may still be small
Hence, confidence ≠ severity.
- Grad-CAM highlights regions responsible for the prediction
- Leaf-only masking removes background influence (GrabCut)
- Top-K CAM strategy:
- Only the strongest activation regions are considered
- Prevents severity inflation due to diffuse attention
Severity = percentage of leaf area belonging to the top-K disease-relevant regions
This produces visually consistent and interpretable estimates.
{
"disease": "Tomato_Early_blight",
"confidence": 0.93,
"severity_percent": 20.0
}The Gradio UI provides:
- Image upload
- One-click prediction
- Clear numerical outputs
Designed for:
- Demonstrations
- Academic evaluation
- Non-technical users
Input
- Image file (
.jpg,.png)
Response
{
"disease": "Tomato_Late_blight",
"confidence": 0.91,
"severity_percent": 27.5
}This API can be consumed by:
- Web applications
- Mobile apps
- External services
crop-disease-detection/
│
├── app/
│ ├── main.py # FastAPI + Gradio entrypoint
│ ├── api/ # API routes
│ ├── core/ # ML logic (model, Grad-CAM, severity)
│ ├── ui/ # Gradio UI
│ └── config/ # Configuration
│
├── models/
│ └── best_resnet.pth # Trained model weights
│
├── Dockerfile
├── requirements.txt
└── README.md
Docker is used to ensure:
- consistent runtime environment
- OpenCV system dependency support
- reproducibility across machines
- production-like deployment
This is especially important for FastAPI + ML systems.
-
Severity is attention-based, not pixel-level ground truth
-
Grad-CAM highlights model evidence, not medical diagnosis
-
Best suited for:
- academic projects
- demonstrations
- early-stage decision support
- True lesion segmentation
- Multi-crop support
- Severity calibration per disease
- Yield-loss estimation
- Classification alone is insufficient for real-world ML systems
- Explainability improves trust and debugging
- Severity is a designed metric, not a free by-product
- Backend architecture is as important as model accuracy
MIT License
- PlantVillage Dataset
- PyTorch & TorchVision
- Hugging Face Spaces
- Gradio & FastAPI