Skip to content

Latest commit

Β 

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 

Repository files navigation

Hierarchical Plant Disease Detection System

A production-ready deep learning system for hierarchical plant disease detection using MobileNetV3 with transfer learning.

🌟 System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    INPUT IMAGE                               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚   STAGE 1: Plant Classifier β”‚
        β”‚   (Pre-trained Model)       β”‚
        β”‚   - Apple                   β”‚
        β”‚   - Tomato                  β”‚
        β”‚   - Potato                  β”‚
        β”‚   - Corn                    β”‚
        β”‚   - Pepper                  β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚  Route to Specific Disease  β”‚
        β”‚      Classifier             β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β–Ό                       β–Ό           β–Ό           β–Ό           β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ Apple   β”‚            β”‚ Tomato  β”‚  β”‚ Potato  β”‚  β”‚  Corn   β”‚  β”‚ Pepper  β”‚
    β”‚ Disease β”‚            β”‚ Disease β”‚  β”‚ Disease β”‚  β”‚ Disease β”‚  β”‚ Disease β”‚
    β”‚Classifierβ”‚           β”‚Classifierβ”‚ β”‚Classifierβ”‚ β”‚Classifierβ”‚ β”‚Classifierβ”‚
    β”‚(4 class)β”‚            β”‚(10 class)β”‚ β”‚(3 class)β”‚ β”‚(4 class)β”‚ β”‚(2 class)β”‚
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜            β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
         β”‚                      β”‚            β”‚            β”‚            β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚
                                            β–Ό
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚   FINAL PREDICTION      β”‚
                              β”‚   Plant + Disease       β”‚
                              β”‚   + Confidence Scores   β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‹ Disease Classes by Plant

Apple (4 classes)

  • Black_rot
  • Cedar_apple_rust
  • Apple_scab
  • healthy

Tomato (10 classes)

  • Bacterial_spot
  • Early_blight
  • Late_blight
  • Leaf_Mold
  • Septoria_leaf_spot
  • Spider_mites Two-spotted_spider_mite
  • Target_Spot
  • Tomato_Yellow_Leaf_Curl_Virus
  • Tomato_mosaic_virus
  • healthy

Potato (3 classes)

  • Early_blight
  • Late_blight
  • healthy

Corn (4 classes)

  • Cercospora_leaf_spot Gray_leaf_spot
  • Common_rust
  • Northern_Leaf_Blight
  • healthy

Pepper (2 classes)

  • Bacterial_spot
  • healthy

πŸ—οΈ Model Architecture

Base Architecture: MobileNetV3Large

  • Pretrained on: ImageNet
  • Input size: 224x224x3
  • Parameters: ~5.4M (per model)
  • Optimization: Mixed precision training (FP16)

Classifier Head

Input (224x224x3)
    ↓
MobileNetV3Large (frozen initially)
    ↓
GlobalAveragePooling2D
    ↓
Dropout(0.3)
    ↓
Dense(256, relu)
    ↓
Dropout(0.3)
    ↓
Dense(num_classes, softmax)

Training Strategy

  1. Phase 1: Train classifier head (base frozen)

    • Epochs: 15
    • Learning rate: 0.001.
  2. Phase 2: Fine-tune top 30 layers

    • Epochs: 15
    • Learning rate: 0.0001

πŸ“Š Performance Metrics

Plant Classes Val Accuracy Parameters
Apple 4 ~98% 5.4M
Tomato 10 ~95% 5.4M
Potato 3 ~99% 5.4M
Corn 4 ~97% 5.4M
Pepper 2 ~99% 5.4M

πŸš€ Quick Start

Prerequisites

  • Python: Recommended version 3.11 or 3.12. (Note: Python 3.14+ may have compatibility issues with current deep learning libraries).
  • Virtual Environment: It is highly recommended to use a virtual environment for dependency isolation.

Installation & Setup

  1. Clone the repository:

    git clone <repository_url>
    cd zali-backend
  2. Create and Activate Virtual Environment:

    Windows (PowerShell):

    python -m venv venv
    .\venv\Scripts\Activate.ps1

    Windows (Git Bash / MINGW64):

    python -m venv venv
    source venv/Scripts/activate

    macOS / Linux:

    python -m venv venv
    source venv/bin/activate

    Note for Python 3.14 users on Windows: There is a known bug where venv creation fails with "Unable to copy venvlauncher.exe". If you encounter this, use the venv Python binary directly without activation (see step 2 in Running the Application below).

  3. Install Dependencies:

    pip install -r app/requirements.txt

Running the Application

  1. Navigate to the app directory:

    cd app
  2. Start the FastAPI server (with venv activated):

    python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload

    Alternatively, if venv activation fails (Python 3.14 bug), run directly with the venv Python binary:

    PowerShell:

    ..\venv\Scripts\python.exe -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload

    Git Bash:

    ../venv/Scripts/python.exe -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload

The API will now be available at http://localhost:8000. You can access the automatic documentation at http://localhost:8000/docs.

πŸ—οΈ Technical Stack

  • Framework: FastAPI
  • Deep Learning Library: PyTorch (using .pth models)
  • Computer Vision: PIL (Pillow), Torchvision
  • Server: Uvicorn

Inference

Single Image

from inference import HierarchicalPlantDiseaseDetector

detector = HierarchicalPlantDiseaseDetector(
    plant_classifier_path='plant_classifier.h5',
    disease_models_path='disease_models/'
)

result = detector.predict('path/to/image.jpg')
print(f"Plant: {result['plant']}")
print(f"Disease: {result['disease']}")
print(f"Confidence: {result['disease_confidence']:.2%}")

Batch Processing

results = detector.predict_batch(['img1.jpg', 'img2.jpg', 'img3.jpg'])
for result in results:
    print(f"{result['image_path']}: {result['full_diagnosis']}")

Command Line

# Single image
python inference.py --image test.jpg

# Batch processing
python inference.py --batch ./test_images/ --output results.csv

πŸ“ File Structure

.
β”œβ”€β”€ hierarchical_plant_disease_detection.ipynb  # Main training notebook
β”œβ”€β”€ inference.py                                 # Standalone inference script
β”œβ”€β”€ README.md                                    # This file
β”œβ”€β”€ disease_models/                              # Trained models directory
β”‚   β”œβ”€β”€ Apple_disease_classifier.h5
β”‚   β”œβ”€β”€ Apple_class_indices.json
β”‚   β”œβ”€β”€ Tomato_disease_classifier.h5
β”‚   β”œβ”€β”€ Tomato_class_indices.json
β”‚   β”œβ”€β”€ Potato_disease_classifier.h5
β”‚   β”œβ”€β”€ Potato_class_indices.json
β”‚   β”œβ”€β”€ Corn_disease_classifier.h5
β”‚   β”œβ”€β”€ Corn_class_indices.json
β”‚   β”œβ”€β”€ Pepper_disease_classifier.h5
β”‚   β”œβ”€β”€ Pepper_class_indices.json
β”‚   β”œβ”€β”€ system_metadata.json
β”‚   └── model_performance_summary.csv
└── reorganized_dataset/                         # Training data
    β”œβ”€β”€ Apple/
    β”‚   β”œβ”€β”€ train/
    β”‚   └── val/
    β”œβ”€β”€ Tomato/
    β”œβ”€β”€ Potato/
    β”œβ”€β”€ Corn/
    └── Pepper/

πŸ”§ Configuration

Training Configuration

class Config:
    IMG_SIZE = (224, 224)
    BATCH_SIZE = 32
    EPOCHS = 30
    LEARNING_RATE = 0.001
    VALIDATION_SPLIT = 0.2

Data Augmentation

  • Random rotation: Β±15Β°
  • Width/height shift: Β±10%
  • Zoom: Β±20%
  • Horizontal flip: True
  • Shear: 0.1
  • Brightness: [0.8, 1.2]

🎯 Key Features

1. Hierarchical Architecture

  • Memory Efficient: Only loads relevant disease model
  • Scalable: Easy to add new plants or diseases
  • Fast Inference: Lazy loading of models

2. Transfer Learning

  • Leverages ImageNet pretrained weights
  • Fast convergence
  • High accuracy with limited data

3. Mixed Precision Training

  • 2x faster training on modern GPUs
  • Reduced memory footprint
  • Maintained accuracy

4. Production Ready

  • Comprehensive error handling
  • Batch processing support
  • CSV export functionality
  • Command-line interface

πŸ“ˆ Training Best Practices

Data Organization

PlantVillage/
β”œβ”€β”€ Apple___Black_rot/
β”œβ”€β”€ Apple___Cedar_apple_rust/
β”œβ”€β”€ Tomato___Bacterial_spot/
└── ...

Callbacks Used

  1. EarlyStopping: Prevents overfitting

    • Monitor: val_accuracy
    • Patience: 5 epochs
  2. ReduceLROnPlateau: Adaptive learning rate

    • Monitor: val_loss
    • Factor: 0.5
    • Patience: 3 epochs
  3. ModelCheckpoint: Saves best model

    • Monitor: val_accuracy
    • Save best only

πŸ”„ Deployment Options

Option 1: Python API

# Flask example
from flask import Flask, request, jsonify
from inference import HierarchicalPlantDiseaseDetector

app = Flask(__name__)
detector = HierarchicalPlantDiseaseDetector(
    plant_classifier_path='models/plant_classifier.h5',
    disease_models_path='models/disease_models/'
)

@app.route('/predict', methods=['POST'])
def predict():
    file = request.files['image']
    file.save('temp.jpg')
    result = detector.predict('temp.jpg')
    return jsonify(result)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Option 2: TensorFlow Lite (Mobile)

import tensorflow as tf

# Convert to TFLite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()

with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

Option 3: ONNX (Cross-platform)

import tf2onnx

# Convert to ONNX
spec = (tf.TensorSpec((None, 224, 224, 3), tf.float32, name="input"),)
model_proto, _ = tf2onnx.convert.from_keras(model, input_signature=spec)

with open("model.onnx", "wb") as f:
    f.write(model_proto.SerializeToString())

Option 4: Docker Container

FROM tensorflow/tensorflow:latest-gpu

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY inference.py .
COPY disease_models/ disease_models/
COPY plant_classifier.h5 .

EXPOSE 5000
CMD ["python", "api.py"]

πŸ§ͺ Testing

Unit Tests

import unittest
from inference import HierarchicalPlantDiseaseDetector

class TestDetector(unittest.TestCase):
    def setUp(self):
        self.detector = HierarchicalPlantDiseaseDetector(
            plant_classifier_path='plant_classifier.h5',
            disease_models_path='disease_models/'
        )
    
    def test_single_prediction(self):
        result = self.detector.predict('test_image.jpg')
        self.assertIn('plant', result)
        self.assertIn('disease', result)
        self.assertIsInstance(result['disease_confidence'], float)

Performance Benchmarks

import time

# Measure inference time
images = ['img1.jpg', 'img2.jpg', 'img3.jpg']
start = time.time()
results = detector.predict_batch(images)
elapsed = time.time() - start

print(f"Average inference time: {elapsed/len(images):.3f}s per image")

πŸ“ Output Format

Single Prediction

{
  "plant": "Tomato",
  "plant_confidence": 0.9876,
  "disease": "Early_blight",
  "disease_confidence": 0.9543,
  "full_diagnosis": "Tomato___Early_blight",
  "top_predictions": [
    {"disease": "Early_blight", "confidence": 0.9543},
    {"disease": "Late_blight", "confidence": 0.0321},
    {"disease": "healthy", "confidence": 0.0087}
  ],
  "image_path": "path/to/image.jpg"
}

Batch Results (CSV)

Image,Plant,Plant_Confidence,Disease,Disease_Confidence,Full_Diagnosis
img1.jpg,Apple,0.99,Black_rot,0.96,Apple___Black_rot
img2.jpg,Tomato,0.98,Early_blight,0.94,Tomato___Early_blight
img3.jpg,Potato,0.99,healthy,0.97,Potato___healthy

πŸŽ“ Educational Resources

Understanding the Architecture

  1. Why Hierarchical?

    • Reduces complexity (5 binary classifiers vs 1 multi-class)
    • Better accuracy per plant
    • More interpretable predictions
  2. Transfer Learning Benefits

    • Faster training
    • Better generalization
    • Requires less data
  3. MobileNetV3 Advantages

    • Lightweight (5.4M params)
    • Fast inference
    • Mobile-friendly

πŸ› Troubleshooting

Common Issues

Issue: Out of memory during training

# Solution: Reduce batch size
config.BATCH_SIZE = 16  # Instead of 32

Issue: Model not loading

# Solution: Use absolute paths
import os
model_path = os.path.abspath('disease_models/Apple_disease_classifier.h5')

Issue: Low accuracy on new images

# Solution: Check image preprocessing
# Ensure images are RGB and normalized
img = img.convert('RGB')
img_array = img_array / 255.0

πŸ“Š Monitoring & Logging

Add TensorBoard

from tensorflow.keras.callbacks import TensorBoard

tensorboard = TensorBoard(
    log_dir='./logs',
    histogram_freq=1,
    write_graph=True
)

model.fit(train_gen, callbacks=[tensorboard, ...])

View Logs

tensorboard --logdir=./logs

🀝 Contributing

We welcome contributions! Please follow these guidelines:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new features
  4. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see LICENSE file for details.

πŸ™ Acknowledgments

  • PlantVillage dataset creators
  • TensorFlow/Keras team
  • MobileNetV3 authors

πŸ“§ Contact

For questions or issues, please open a GitHub issue or contact:

πŸ”— Related Resources


Built with ❀️ for sustainable agriculture

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages