Deployment-ready pathology detection system featuring edge-optimized inference, an AWS-hosted FastAPI/Streamlit architecture, and Explainable AI.
TeaLeaf Lens automates tea-leaf quality control by detecting 8 disease classes (e.g., Anthracnose, Red Leaf Spot, Algal Leaf). The same INT8 TensorFlow Lite model powers:
- Edge inference on low-power microcontrollers and mobile hardware.
- Cloud inference on an AWS EC2 stack that exposes a REST API and Streamlit dashboard.
The system prioritizes compact models, transparent decisions via Grad-CAM, and full experiment traceability through MLflow/DagsHub.
- Sub-megabyte inference graph optimized for MobileNetV3-Small and PTQ.
- Grad-CAM driven diagnostics that informed both software tuning and optics recommendations.
- Remote serving pipeline hardened for t2.micro instances (swap provisioning, FastAPI concurrency, Streamlit UI).
Data β Training (Keras + MLflow) β Quantization (TFLite) β
ββββββββββββββββββββββββ βββββββββββββββββββββββββββββ
β Edge Device β β AWS EC2 Cloud Stack β
β β’ Microcontroller β β β’ FastAPI backend (REST) β
β β’ Mobile deployment β β β’ Streamlit UI β
ββββββββββββββββββββββββ βββββββββββββββββββββββββββββ
- Modeling: TensorFlow/Keras, MobileNetV3-Small backbone, OpenCV, NumPy.
- Optimization: Post-Training Quantization β INT8
.tflite. - Explainability: Grad-CAM for saliency-backed validation.
- MLOps: MLflow tracking hosted on DagsHub.
- Cloud: AWS EC2 (Ubuntu 24.04), FastAPI, Streamlit, Uvicorn, swap-tuned t2.micro setup.
| Metric | Original (MobileNetV3) | TeaLeaf Lens (Quantized) | Improvement |
|---|---|---|---|
| Model Size | ~9.2 MB | 1.06 MB | 9x Compression |
| Format | FP32 Keras (.h5) | INT8 TFLite | Edge Compatible |
| Accuracy | 78.4% (Fine-Tuned) | ~77.9% | <1% Drop |
The model uses the Tea Sickness Dataset (Kaggle, natural field imagery).
| Metric | Value | Notes |
|---|---|---|
| Total Images | 885 | High scarcity challenge |
| Classes | 8 | 7 Pathologies + 1 Healthy |
| Split Strategy | 80% Train / 20% Val | Stratified split |
| Preprocessing | 224Γ224 px | MobileNetV3 Input Standard |
- Fungal/Bacterial: Anthracnose, Algal Leaf, Bird Eye Spot, Brown Blight, Gray Blight, Red Leaf Spot, White Spot
- Control: Healthy
Aggressive augmentation offsets the small dataset (~110 images/class):
- Geometric: Random rotation (Β±30Β°), horizontal flip, 20% zoom.
- Positional: 20% width/height shifts to mimic imprecise framing.
Artifacts live under results/ for audit trails and reporting.
- Training Dynamics β accuracy/loss curves for overfitting diagnosis.

- Confusion Matrix β highlights confusing pathology pairs.

- Inference Grid β batch predictions with confidences generated via
inference.ipynb.

- Grad-CAM Visual Reasoning β success/failure attention heatmaps.
Grad-CAM exposed that "Healthy" leaves shot with flash were flagged as diseased because the network associated specular highlights with white lesion spots.
Mitigation: Beyond collecting more data, the deployment guide now recommends polarizing filters (TeaRetina camera) to suppress glare, proving how XAI can influence optical designβnot just model tuning.
- MobileNetV3-Small Backbone β optimal accuracy/latency compromise for ARM-class CPUs.
- Transfer Learning β ImageNet initialization prevents overfitting on 885 samples.
- Aggressive Augmentation β simulates user handling variance.
- Post-Training Quantization β compresses to 1.06β―MB with negligible accuracy loss.
- Explainability Feedback Loop β Grad-CAM drives both model debugging and hardware guidance.
- MLOps-First Mindset β every experiment logged to MLflow/DagsHub for reproducibility.
- Experiment Tracking: Hyperparameters, metrics, and artifacts versioned via MLflow (remote backend on DagsHub).
- Metric Logging: Correlates validation accuracy with
.tflitefootprint to enforce device budgets. - Artifacts: Best INT8 model per run kept for auditing.
To reproduce:
- Fork/create a DagsHub repo.
- Set
REPO_OWNERandREPO_NAMEinsideTeaLeaf_Lens_v2.ipynb. - Execute the notebook; MLflow automatically syncs runs, metrics, and TFLite artifacts.
TEA-LEAF-LENS/
βββ mlruns/ # MLflow artifacts
βββ results/ # Plots, Grad-CAM visuals, dashboards
βββ tea-sickness-dataset/ # Kaggle dataset (ignored in git)
βββ test images/ # Held-out inference samples
βββ app.py # FastAPI backend
βββ ui.py # Streamlit frontend
βββ inference.ipynb
βββ TeaLeaf_Lens_v1.ipynb
βββ TeaLeaf_Lens_v2.ipynb # Main training + MLflow notebook
βββ tealeaf.tflite # Quantized model
βββ requirements.txt
βββ README.md
Dataset directories stay out of version control via
.gitignoreto keep the repo portable.
The AWS deployment mirrors the edge graph for centralized inference and provides both human and machine interfaces.
- Provider: AWS EC2 (Ubuntu 24.04 LTS,
t2.micro). - Backend: FastAPI + Uvicorn exposing REST + auto Swagger docs.
- Frontend: Streamlit dashboard for analysts and agronomists.
- Model Runtime: TensorFlow Lite interpreter with server-side preprocessing.
- Ops Hardening: 2β―GB swap file ensures TensorFlow installs cleanly on 1β―GB RAM nodes.
- REST API: Documented via Swagger, portable for mobile/IoT integrations.
- Real-time Inference: Sub-second latency thanks to INT8 execution.
- Interactive UI: Upload, preview, and inspect per-class confidences.
- Robust Preprocessing: Validates tensor shapes to match MobileNetV3 expectations.
| Interactive Web UI (Streamlit) | API Documentation (Swagger UI) |
|---|---|
![]() |
![]() |
| Real-time classification dashboard | Auto-generated API contract |
Here is the end-to-end process used to stand up TeaLeaf Lens on AWS.
- Log into the AWS Console and open EC2.
- Click Launch Instance and set Name =
Tea-Leaf-Inference-Server. - Choose Ubuntu Server 24.04 LTS (Free Tier eligible) as the AMI.
- Select instance type
t2.micro(1 vCPU, 1β―GB RAM). - Create/download an SSH key pair (e.g.,
my-aws-key.pem).
Allow traffic needed for remote management, API inference, and the UI.
| Type | Port | Source | Purpose |
|---|---|---|---|
| SSH | 22 | 0.0.0.0/0 |
Remote terminal access |
| Custom TCP | 8000 | 0.0.0.0/0 |
FastAPI backend |
| Custom TCP | 8501 | 0.0.0.0/0 |
Streamlit UI |
Restrict sources to your corporate CIDR if required by policy.
# Set key permissions (Linux/macOS)
chmod 400 my-aws-key.pem
# Replace with the EC2 public IP or DNS
ssh -i "my-aws-key.pem" ubuntu@<YOUR-EC2-PUBLIC-IP>Once on the server:
- Update the OS
sudo apt update && sudo apt upgrade -y - Provision Swap (critical for t2.micro + TensorFlow)
sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile
- Install Python Tooling
sudo apt install python3-pip python3-venv -y
- Clone the Repository
git clone https://github.com/LasithaAmarasinghe/Tea-Leaf-Lens.git cd Tea-Leaf-Lens - Create Virtual Environment + Install Dependencies
python3 -m venv venv source venv/bin/activate pip install tensorflow-cpu fastapi uvicorn streamlit requests pillow python-multipart - Start Backend and Frontend (persist via nohup)
nohup uvicorn app:app --host 0.0.0.0 --port 8000 > backend.log 2>&1 & nohup streamlit run ui.py --server.port 8501 > frontend.log 2>&1 &
- Streamlit UI:
http://<YOUR-EC2-PUBLIC-IP>:8501 - FastAPI Swagger:
http://<YOUR-EC2-PUBLIC-IP>:8000/docs
Both services can be monitored through backend.log and frontend.log (tail the files to verify uptime).
- Clone the Repo
git clone https://github.com/LasithaAmarasinghe/Tea-Leaf-Lens.git cd Tea-Leaf-Lens - Create Environment (Conda recommended)
conda create -n tea python=3.9 -y conda activate tea pip install -r requirements.txt
- Run Training Notebook
Execute all cells to connect to DagsHub, fine-tune MobileNetV3-Small, quantize to TFLite, and log MLflow artifacts.
jupyter notebook TeaLeaf_Lens_v2.ipynb
- Optional Local MLflow UI
Inspect runs locally or on DagsHub depending on your backend URL.
mlflow ui
- Explore quantization-aware training (QAT) to squeeze additional accuracy from INT8 deployment.
- Grow the dataset with multi-season, multi-region samples for better climate/cultivar robustness.





