This project implements a Convolutional Neural Network (CNN) to predict leaf diseases from images. The CNN is trained on a dataset of leaf images, with the goal of classifying the diseases accurately. The project includes image preprocessing, data augmentation, and visualization tools to enhance the training process and results.
The dataset consists of images of leaves, categorized into different disease classes. Each image is labeled with the corresponding disease, allowing the model to learn the features associated with each class.
The CNN architecture is defined in the LeafCNN
class, which includes:
- Convolutional layers for feature extraction.
- MaxPooling layers for down-sampling.
- Fully connected layers for classification.
- Dropout layers for regularization.
The training process involves the following steps:
- Data Preprocessing: Images are resized, normalized, and split into training, validation, and test sets.
- Data Augmentation: Augmentations such as rotation, cropping, and color jittering are applied to increase dataset diversity.
- Model Training: The model is trained using the training set, with validation performed on the validation set.
- Early Stopping: Training is stopped early if the validation loss does not improve for a specified number of epochs.
- Model Evaluation: The model's performance is evaluated using the test set.
The model's performance is evaluated using the following metrics:
- Cross Entropy Loss: Measures the difference between the predicted and true distributions.
- Accuracy: The proportion of correct predictions.
- Precision: The proportion of true positive predictions among all positive predictions.
- Recall: The proportion of true positive predictions among all actual positives.
- F1 Score: The harmonic mean of precision and recall.
- Confusion Matrix: A summary of prediction results for classification problems.
Image preprocessing is performed using the Transformation.py
script, which includes:
- Conversion to grayscale.
- Gaussian blur application.
- Mask creation and application.
- Landmark analysis and drawing.
- Color histogram extraction and plotting.
Data augmentation is performed using the Augmentation.py
script, which applies random augmentations to the images to increase dataset diversity. The augmentations include rotation, cropping, brightness and contrast adjustment, perspective transformation, and blurring.
To run the project, ensure you have the following dependencies installed:
pip install -r requirements.txt
To augment one image, or balance the dataset:
python Augmentation.py <src>
To train the model, use the following command:
python train.py <data_dir> --epochs 100 --patience 10
To make predictions using the trained model, use the following command:
python predict.py <image_path> <weights_path>
To visualize image transformations and color histograms, use the following command:
python Transformation.py <file_path> <dst>
To visualize the distribution of images in a directory, use the following command:
python Distribution.py <directory_path>
The Transformation.py
script generates visualizations of image transformations, including grayscale conversion, mask application, and landmark analysis.
The Transformation.py
script also generates color histograms for the RGB, LAB, and HSV color spaces.
The Distribution.py
script generates distribution charts to visualize the number of images in each class.