Skip to content

revanapriyandi/tomato-leaf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

65 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Tomato Disease Classifier - README

🌱 Overview

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.

✨ Features

βœ… 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

🎯 Disease Classes (10)

  1. Bacterial Spot - Bacterial infection causing dark spots
  2. Early Blight - Fungal disease with target-like spots
  3. Healthy - No disease present
  4. Late Blight - Aggressive fungal disease (Phytophthora infestans)
  5. Leaf Mold - Fungal disease on leaf undersides
  6. Septoria Leaf Spot - Fungal disease with circular spots
  7. Spider Mites (Two-spotted) - Pest damage causing yellowing
  8. Target Spot - Fungal disease with concentric rings
  9. Tomato Mosaic Virus - Viral disease causing mottling
  10. Tomato Yellow Leaf Curl Virus - Viral disease causing leaf curl

πŸ”§ Technical Stack

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+

πŸ“Š Model Architecture

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

πŸš€ Quick Start

1. Installation

pip install -r requirements.txt

2. Prepare Dataset (Local)

dataset/
β”œβ”€β”€ 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)

3. Run Training (Kaggle)

# In Kaggle notebook
%run tomato_classifier.py /kaggle/input/tomato-dataset

4. Local Training

python tomato_classifier.py /path/to/dataset

5. Dry Run (Test - 1 epoch each)

python tomato_classifier.py /path/to/dataset --dry-run

πŸ“ˆ Training Pipeline

Phase 1: Feature Extraction (20 epochs)

  • Objective: Train custom head with frozen base model
  • Learning Rate: 0.001
  • Optimizer: Adam
  • Loss: Categorical Crossentropy
  • Checkpoint: best_model_feature_extraction.keras

Phase 2: Fine-Tuning (50 epochs)

  • 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

Callbacks

  • 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)

πŸ“ Project Structure

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

βš™οΈ Configuration Parameters

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 detection

Or in config.yaml for centralized management.

πŸ“Š Expected Performance

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

πŸ“€ Model Export

TFLite Conversion

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

πŸ” Evaluation Metrics

The training script generates:

  1. Overall Metrics

    • Accuracy
    • Precision, Recall, F1-score
  2. Per-Class Metrics

    • Per-disease accuracy
    • Per-disease precision/recall/F1
  3. Visualizations

    • Training curves (loss & accuracy)
    • Confusion matrix (normalized)
    • Per-class performance bar chart
    • Confidence distribution histogram
    • Model architecture diagram
  4. OOD Detection Analysis

    • Confidence statistics
    • Below-threshold predictions

πŸ’» Usage Examples

Training from Kaggle

# Kaggle notebook cell
!pip install -q tensorflow keras scikit-learn
%run tomato_classifier.py /kaggle/input/tomato-dataset

Single Image Prediction

from 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)

Batch Prediction

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%})")

Using Command Line

# 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.json

πŸ› Troubleshooting

GPU Not Detected

import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
print(f"GPUs available: {len(gpus)}")

Out of Memory

Reduce batch size in tomato_classifier.py:

BATCH_SIZE = 16  # or 8

Slow Training

  • Enable GPU acceleration in Kaggle notebook settings
  • Reduce number of epochs for testing
  • Use --dry-run flag for quick test

Dataset Not Found

Verify dataset structure:

python -c "from tomato_classifier import check_dataset_path; check_dataset_path('path/to/dataset')"

πŸ“š Documentation

🀝 Contributing

See CONTRIBUTING.md for guidelines on:

  • Code style and conventions
  • Development setup
  • Making changes and testing
  • Pull request process

πŸ—ΊοΈ Roadmap

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

πŸ“ž Support

For issues or questions:

  1. Check QUICKSTART.md
  2. Review troubleshooting section above
  3. Open an issue on GitHub

πŸ™ Acknowledgments

Last Updated: December 2025
Status: Production Ready βœ…
Framework: Keras 3 + TensorFlow 2.13+

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages