A deep learning system for classifying tomato leaf diseases using DenseNet121 with transfer learning. This project uses Keras 3 and TensorFlow 2.13+ to detect 10 different disease classes from tomato leaf images.
β
Transfer Learning - DenseNet121 pretrained on ImageNet
β
Two-Phase Training - Feature extraction + Fine-tuning
β
Data Augmentation - RandomFlip, RandomRotation, RandomZoom, RandomBrightness
β
Out-of-Distribution Detection - Confidence-based filtering
β
Mobile Deployment - TFLite quantization (float16)
β
Comprehensive Evaluation - Confusion matrix, per-class metrics, confidence analysis
β
Reproducible - Fixed random seeds for consistent results
- Bacterial Spot - Bacterial infection causing dark spots
- Early Blight - Fungal disease with target-like spots
- Healthy - No disease present
- Late Blight - Aggressive fungal disease (Phytophthora infestans)
- Leaf Mold - Fungal disease on leaf undersides
- Septoria Leaf Spot - Fungal disease with circular spots
- Spider Mites (Two-spotted) - Pest damage causing yellowing
- Target Spot - Fungal disease with concentric rings
- Tomato Mosaic Virus - Viral disease causing mottling
- Tomato Yellow Leaf Curl Virus - Viral disease causing leaf curl
| Component | Version |
|---|---|
| Python | 3.8+ |
| TensorFlow | 2.13.0+ |
| Keras | 3.0.0+ |
| NumPy | 1.24.0+ |
| Scikit-learn | 1.3.0+ |
| Matplotlib | 3.7.0+ |
| Seaborn | 0.12.0+ |
Input (224x224x3)
β
Data Augmentation (RandomFlip, Rotation, Zoom, Brightness)
β
Preprocessing (DenseNet preprocess_input)
β
DenseNet121 (ImageNet weights, frozen initially)
β
GlobalAveragePooling2D
β
Dense(256, ReLU)
β
Dropout(0.3)
β
Dense(10, Softmax) β Output
pip install -r requirements.txtdataset/
βββ train/ (70% of data)
β βββ Bacterial_spot/
β βββ Early_blight/
β βββ ... (10 disease folders)
βββ val/ (10% of data)
β βββ ... (10 disease folders)
βββ test/ (20% of data)
βββ ... (10 disease folders)
# In Kaggle notebook
%run tomato_classifier.py /kaggle/input/tomato-datasetpython tomato_classifier.py /path/to/datasetpython tomato_classifier.py /path/to/dataset --dry-run- Objective: Train custom head with frozen base model
- Learning Rate: 0.001
- Optimizer: Adam
- Loss: Categorical Crossentropy
- Checkpoint:
best_model_feature_extraction.keras
- Objective: Fine-tune last 100 DenseNet layers with low learning rate
- Learning Rate: 0.00001
- Unfrozen Layers: Last 100 layers of DenseNet121
- Checkpoint:
best_model_fine_tuning.keras
- EarlyStopping: Patience=10 on validation accuracy
- ModelCheckpoint: Save best model based on val_accuracy
- ReduceLROnPlateau: Reduce LR if val_loss plateaus (factor=0.5, patience=5)
tomato-classifier/
βββ tomato_classifier.py # Main training script
βββ inference.py # Inference/prediction script
βββ requirements.txt # Dependencies
βββ config.yaml # Configuration file
βββ README.md # This file
βββ QUICKSTART.md # Quick start guide
βββ CONTRIBUTING.md # Contribution guidelines
βββ ROADMAP.md # Future improvements
βββ .copilot-instructions.md # GitHub Copilot instructions
β
βββ train/ # Training dataset (70%)
β βββ [10 disease folders]
βββ val/ # Validation dataset (10%)
β βββ [10 disease folders]
βββ test/ # Test dataset (20%)
β βββ [10 disease folders]
β
βββ outputs/
βββ best_model_feature_extraction.keras
βββ best_model_fine_tuning.keras
βββ final_model.keras
βββ tomato_disease_model.tflite
βββ training_history_*.png
βββ confusion_matrix.png
βββ per_class_metrics.png
βββ confidence_distribution.png
βββ model_architecture.png
Edit in tomato_classifier.py:
IMG_SIZE = 224 # Input image size
BATCH_SIZE = 32 # Batch size for training
INITIAL_LR = 0.001 # Feature extraction learning rate
FINE_TUNE_LR = 0.00001 # Fine-tuning learning rate
OOD_THRESHOLD = 0.7 # Confidence threshold for OOD detectionOr in config.yaml for centralized management.
Based on typical DenseNet121 + fine-tuning:
| Metric | Typical Value |
|---|---|
| Test Accuracy | ~95%+ |
| Per-class F1-score | 0.92-0.98 |
| Average Precision | ~0.96 |
| Average Recall | ~0.96 |
Note: Actual values depend on dataset quality and augmentation parameters
Automatically performed during training:
tomato_disease_model.tflite # ~45-50 MB (float16 quantized)For Mobile Deployment:
- Android: Use TFLite Support Library
- iOS: Use TensorFlow Lite for iOS
- File size: ~50% of original after quantization
The training script generates:
-
Overall Metrics
- Accuracy
- Precision, Recall, F1-score
-
Per-Class Metrics
- Per-disease accuracy
- Per-disease precision/recall/F1
-
Visualizations
- Training curves (loss & accuracy)
- Confusion matrix (normalized)
- Per-class performance bar chart
- Confidence distribution histogram
- Model architecture diagram
-
OOD Detection Analysis
- Confidence statistics
- Below-threshold predictions
# Kaggle notebook cell
!pip install -q tensorflow keras scikit-learn
%run tomato_classifier.py /kaggle/input/tomato-datasetfrom inference import TomatoDiseasePredictor
import keras
# Load model
predictor = TomatoDiseasePredictor('final_model.keras')
# Predict
result = predictor.predict_single('path/to/image.jpg')
predictor.print_result(result)from inference import TomatoDiseasePredictor
predictor = TomatoDiseasePredictor('final_model.keras')
results = predictor.predict_batch('path/to/image/directory')
# Access results
for result in results:
print(f"{result['image']}: {result['predicted_class']} ({result['confidence']:.2%})")# Single image
python inference.py --model final_model.keras --image path/to/image.jpg
# Directory of images
python inference.py --model final_model.keras --directory path/to/images --output results.jsonimport tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
print(f"GPUs available: {len(gpus)}")Reduce batch size in tomato_classifier.py:
BATCH_SIZE = 16 # or 8- Enable GPU acceleration in Kaggle notebook settings
- Reduce number of epochs for testing
- Use
--dry-runflag for quick test
Verify dataset structure:
python -c "from tomato_classifier import check_dataset_path; check_dataset_path('path/to/dataset')"- QUICKSTART.md - Quick setup and first run
- CONTRIBUTING.md - How to contribute
- ROADMAP.md - Future improvements
- .copilot-instructions.md - GitHub Copilot guidelines
See CONTRIBUTING.md for guidelines on:
- Code style and conventions
- Development setup
- Making changes and testing
- Pull request process
See ROADMAP.md for planned features:
- v1.1: Model ensemble, TFLite optimization
- v1.2: Web/mobile deployment
- v1.3: Explainability, active learning
- v2.0: Vision Transformers, advanced architectures
For issues or questions:
- Check QUICKSTART.md
- Review troubleshooting section above
- Open an issue on GitHub
- DenseNet121: Huang et al., 2016
- Tomato Dataset: Kaggle Tomato Disease Dataset
- TensorFlow & Keras: Google AI
Last Updated: December 2025
Status: Production Ready β
Framework: Keras 3 + TensorFlow 2.13+