Skip to content

zarakel/Leaffliction

Repository files navigation

Leaffliction: Plant Disease Classification using Computer Vision and Deep Learning

The Leaffliction project focuses on analyzing leaf image datasets, as well as designing, training, and evaluating a Deep Learning model capable of classifying plant diseases. This project implements a complete pipeline ranging from exploratory class distribution analysis to final predictions, including image preprocessing, data augmentation, and the optimization of a custom ResNet-type convolutional neural network (CNN) adapted for efficient training on CPU.


Project Structure

The repository consists of several independent and complementary modules:

  • Distribution.py: Exploratory data analysis to count and visualize image distribution across classes.
  • Augmentation.py: Script for geometric and colorimetric image augmentation using the Pillow library.
  • Transformation.py: Analysis and visual feature extraction from leaves using PlantCV and OpenCV.
  • train.py: Classification model training pipeline with data optimization (tf.data) and a custom Mini-ResNet architecture.
  • predict.py: Prediction tool for a given image, including preprocessing through background removal.

Computer Vision and Preprocessing Techniques

1. Distribution Analysis and Visualization

  • Data extraction: Using pathlib.Path to securely and robustly traverse directory structures and filter common image formats.
  • Statistical visualization: Automatic generation of plots (bar charts for raw counts and pie charts for relative proportions) using matplotlib to detect potential class imbalances.

2. Data Augmentation (Pillow / PIL)

Augmentation artificially increases the diversity of training data to prevent overfitting:

  • Geometric transformations: Rotations with bi-cubic interpolation and dimension adjustment (expand=True), horizontal flips (mirroring), and random cropping (scaling) followed by resizing.
  • Colorimetric transformations: Random alteration of contrast and brightness (illumination) using ImageEnhance modules.
  • Projective transformations: Application of geometric shears via affine homography matrices using Image.Transform.AFFINE.

3. Digital Image Processing and Segmentation (PlantCV & OpenCV)

  • Color space conversions: Converting from RGB to HSV and LAB color spaces. The "a" channel of the LAB color space (representing the green-red axis) is heavily utilized to isolate the green component of the leaf from the background.
  • Filtering and denoising: Applying Gaussian Blur to smooth the image and remove high-frequency noise.
  • Otsu's Thresholding: Automatic calculation of an optimal binarization threshold to segment the object of interest (the leaf) from the background.
  • Morphological operations: Using morphological opening and closing (cv2.morphologyEx) to clean small mask imperfections and fill holes.
  • Shape and distribution analysis: Defining a Region of Interest (ROI), analyzing object size, and plotting empirical cumulative distribution functions (ECDF).
  • Spectral analysis: Visualizing pixel intensity distributions using multi-channel histograms comparing RGB, HSV, and LAB channels.

Deep Learning and Network Architecture Techniques

1. Architecture Design (Custom Mini-ResNet)

To balance network depth with hardware constraints (training on standard CPUs), the model implements an optimized, lightweight version of ResNet:

  • The Stem (Input Layer): Using a convolution with a large 7x7 kernel and a stride of 2, followed by 3x3 Max Pooling with a stride of 2. This configuration reduces the spatial dimension of the images from 128x128 to 32x32 right at the input, reducing the computational load of subsequent layers by a factor of 16.
  • Residual Blocks: Implementing skip connections (shortcuts) that add the block's input directly to its output (layers.Add). This method facilitates gradient propagation (backpropagation) and solves the vanishing gradient problem encountered in deep networks.
  • Dimension Matching: Using 1x1 convolutions in the shortcut connection when the number of filters changes or when downsampling (stride greater than 1) is applied, ensuring mathematical compatibility during addition.
  • Complexity Reduction (Width Scaling): Limiting the number of filters to [32, 64, 128] (unlike classic ResNet architectures which scale up to 512 or more) to keep the total number of model parameters low.
  • Classification Head: Replacing the parameter-heavy Flatten layer with a Global Average Pooling layer (GlobalAveragePooling2D), followed by a 50% Dropout layer to prevent overfitting, and a final Dense layer with Softmax activation.

2. Data Pipeline Optimization (tf.data)

  • Caching (.cache()): Storing decoded images in RAM after the first load to avoid repetitive disk access.
  • Asynchronous Loading and Parallelism (prefetch & num_parallel_calls): Using tf.data.AUTOTUNE to prepare the next batches of data in the background while the CPU processes the current batch.
  • TensorFlow-to-Python Bridge: Integrating custom PIL augmentation functions directly into the TensorFlow pipeline using tf.numpy_function.
  • Training Regulation: Employing strategic callbacks:
    • EarlyStopping: Automatic termination if the validation loss does not decrease for 5 consecutive epochs.
    • ReduceLROnPlateau: Reducing the learning rate by a factor of 2 in case of stagnation to refine convergence.
    • ModelCheckpoint: Exclusively saving the best weights observed on the validation set.

3. Metrics and Evaluation

  • Classification Report: Computing precision, recall, and F1-score per class using scikit-learn.
  • Confusion Matrix: Matrix visualization to analyze classification errors and confusion between pathological classes in detail.
  • Export and Distribution: Automatic compression into a zip archive (learnings.zip) containing the trained model, class mapping in JSON format, and augmented image samples.

Usage Instructions

Prerequisites

Install the required dependencies listed in the configuration file:

pip install -r requirements.txt

1. Dataset Distribution Analysis

To analyze the number of images per class and generate distribution plots:

python Distribution.py <dataset_path> --output_dir distribution_reports

2. Image Augmentation

To generate 6 augmented variations of a specific image:

python Augmentation.py <image_path> --output-dir <output_directory>

3. Image Transformations

To apply PlantCV filters and transformations to an image or directory:

python Transformation.py <image_or_directory_path> --output_dir transformed_output

4. Model Training

To start training the convolutional neural network on the image folder:

python train.py <dataset_path> --model_out leaf_model.h5 --epochs 8 --batch_size 32

5. Prediction on a New Image

To perform classification on an image with background removal and result visualization:

python predict.py leaf_model.h5 leaf_model.classes.json <image_to_predict_path>

About

A computer vision and deep learning pipeline for plant disease classification. Features custom image augmentation, leaf segmentation (PlantCV/OpenCV), and an optimized Mini-ResNet architecture designed for efficient CPU training.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors