CropIntel ML Pipeline
Machine learning pipeline for crop disease classification using TensorFlow Lite.
Working without Kaggle (collaborators)
Training datasets are on Kaggle, but you do not need Kaggle to run the web app if someone shares trained weights.
- Install inference deps from repo root:
pip install -r ml/requirements-inference.txt - Download a zip of
ml/models/(same layout as after training:corn/<version>/β¦, etc.):
export CROPINTEL_MODELS_URL='https://β¦/cropintel-models.zip'
python3 -m ml.scripts.fetch_models
- Run Next.js;
/api/predictwill callscripts/predict.py.
Docker: see repo root docker-compose.yml and set CROPINTEL_MODELS_URL before docker compose up.
Maintainers: package your local ml/models/ for release:
python3 -m ml.scripts.package_models -o cropintel-models.zip
Overview
This ML pipeline trains deep learning models to classify crop diseases from leaf images. Models are trained using transfer learning with EfficientNetB0 and exported to TensorFlow Lite format for efficient production inference.
Structure
ml/
βββ config.py # Configuration and hyperparameters
βββ training/ # Training scripts
β βββ train_crop.py # Train model for single crop
β βββ train_all_crops.py # Train models for all crops
βββ inference/ # Inference modules
β βββ tflite_predictor.py # TFLite predictor for production
βββ utils/ # Utilities
β βββ data_loader.py # Dataset loading and preprocessing
β βββ model_builder.py # Model architecture
β βββ evaluation.py # Model evaluation metrics
β βββ tflite_converter.py # TFLite conversion
βββ scripts/ # Utility scripts
β βββ download_datasets.py # Download datasets from Kaggle
β βββ create_synthetic_dataset.py # Random images for pipeline smoke tests (no Kaggle)
βββ data/ # Dataset storage (gitignored)
βββ models/ # Trained models (gitignored)
βββ {crop}/
βββ {version}/
βββ model.tflite
βββ metadata.json
βββ label_map.json
βββ metrics.json
βββ training_info.json
Setup
Why a fresh git clone does not show βpaperβ accuracy: ml/data/ and ml/models/ are not in git. You need either Kaggle downloads + training, or the synthetic smoke-test path below.
Option A β Real data (Kaggle)
- Install dependencies (from repository root):
pip install -r ml/requirements.txt
Set up Kaggle API credentials:
- Install Kaggle CLI:
pip install kaggle - Download
kaggle.jsonfrom your Kaggle account settings - Place it at
~/.kaggle/kaggle.json - Accept dataset terms on Kaggle website
- Install Kaggle CLI:
Download datasets:
python -m ml.scripts.download_datasets
Option B β No Kaggle (pipeline smoke test only)
Random noise images are not diagnostically meaningful; they only prove training, evaluation, and TFLite export run on your machine.
pip install -r ml/requirements.txt
python -m ml.scripts.create_synthetic_dataset --crop corn --force
python -m ml.training.train_crop --crop corn --epochs 2 --no-fine-tune
Use --crop all on the synthetic script to populate every crop before train_all_crops.
Option C β Docker (same commands inside a container)
From the repository root:
docker compose -f docker-compose.ml.yml build
docker compose -f docker-compose.ml.yml run --rm ml \
python -m ml.scripts.create_synthetic_dataset --crop corn --force
docker compose -f docker-compose.ml.yml run --rm ml \
python -m ml.training.train_crop --crop corn --epochs 2 --no-fine-tune
Download with Kaggle inside Docker (host must have ~/.kaggle/kaggle.json):
docker compose -f docker-compose.ml.yml run --rm -v "$HOME/.kaggle:/root/.kaggle:ro" ml \
python -m ml.scripts.download_datasets
Optional: improve soybean healthy-class coverage
If soybean healthy predictions are weak, add extra healthy soybean images from:
Place extracted data in one of:
ml/data/soybean_mendeley/Healthy/ml/data/soybean_healthy/Healthy/ml/data/soybean_extra/Healthy/
The loader auto-includes these healthy images during soybean training.
By default it also uses ~/Soybean Healthy and Diseased Images Dataset/Soybean Healthy when that folder exists (same class label: Healthy). To point elsewhere:
export CROPINTEL_SOYBEAN_HEALTHY_DIRS="/path/to/Soybean Healthy:/path/to/more/healthy"
python -m ml.training.train_crop --crop soybean
(On macOS/Linux, separate multiple folders with : in that variable.)
Training
Train a single crop model:
python -m ml.training.train_crop --crop corn --epochs 50
Train all crops:
python -m ml.training.train_all_crops --epochs 50
Options:
--crop: Crop name (corn, soybean, wheat, rice)--epochs: Number of training epochs--no-fine-tune: Skip fine-tuning phase
Model Architecture
- Base Model: EfficientNetB0 (pre-trained on ImageNet)
- Transfer Learning: Two-phase training
- Train classifier head with frozen base model
- Fine-tune top layers of base model
- Output: Multi-class classification (diseases + healthy)
- Export Format: TensorFlow Lite (optimized for mobile/edge)
Inference
from ml.inference.tflite_predictor import TFLitePredictor
from PIL import Image
# Initialize predictor
predictor = TFLitePredictor(crop="corn")
# Predict from image
image = Image.open("path/to/image.jpg")
result = predictor.predict(image)
print(f"Disease: {result['disease']}")
print(f"Confidence: {result['confidence']:.2%}")
print(f"Is Healthy: {result['is_healthy']}")
Model Versioning
Models are versioned by timestamp: v1_YYYYMMDD_HHMMSS
Each version includes:
model.tflite: TensorFlow Lite modelmetadata.json: Model metadata and class nameslabel_map.json: Label to class name mappingmetrics.json: Evaluation metricstraining_info.json: Training configuration and resultsconfusion_matrix.png: Confusion matrix visualization
Evaluation Metrics
Models are evaluated on held-out test sets with:
- Accuracy
- Precision, Recall, F1-score (weighted and per-class)
- Confusion matrix
- Classification report
Production Considerations
- Confidence Threshold: 0.7 (configurable in
config.py) - Input Size: 224x224x3 RGB images
- Preprocessing: Normalize to [0, 1] range
- Quantization: Float16 by default (can use int8 for smaller models)
- Model Size: ~5-15 MB per crop (depending on quantization)
Supported Crops
- Corn: Common Rust, Gray Leaf Spot, Blight, Healthy
- Soybean: Multiple diseases including mosaic virus, blight, rust, etc.
- Wheat: Rusts, smut, blight, powdery mildew, pests, Healthy
- Rice: Blast, bacterial blight, brown spot, Healthy