Leaf Disease Detection Using Deep Learning
Our "Leaf Disease Detection Using Deep Learning" project uses advanced CNN models to spot plant diseases directly from leaf images. This tool helps farmers catch issues early, reducing crop losses and boosting yields. With image enhancements and data augmentation, we’ve achieved high accuracy, making it a reliable solution for healthier crops and a great example of deep learning’s potential in agriculture.
Project Overview
Welcome to the "Leaf Disease Detection Using Deep Learning" project. This impressive project addresses the problem of diagnosing and categorizing leaf disease using deep learning methods.
From the perspective of recognition simplicity, this project really enhances the process of early detection for farmers who want to protect their crops or garden lovers who want to know how healthy their plants are. Thus, based on convolutional neural network (CNN) models such as VGG16, VGG19, and EfficientNet-B4, we simultaneously achieved high diagnostic accuracy of diseases from different plant species, ensuring correct identification of diseases and minimizing crop losses to improve yield.
Prerequisites
Before trying your hand at this particular project, you must have the following:
-
Python programming proficiency (from basic to intermediate level).
-
Understanding of the core principles of Machine Learning and Deep Learning.
-
Experience with TensorFlow and Keras for building and developing models.
-
Competence in Google Colab to execute the code and retrieve the datasets.
-
Basic knowledge of image processing and convolutional neural networks (CNNs).
Approach
This project approach is to use transfer learning techniques to detect leaf disease from images. We used three pre-trained models. Which are Vgg-16, Vgg-19 and Efficient-B4. Our models are trained with thousands of images of diseased leaves and healthy leaves. Which ultimately learns to distinguish between different categories with impressive accuracy.
The ultimate objective is to make disease diagnosis easy and alleviate the burden of plant diseases through the use of artificial intelligence.
Workflow and Methodology details
The broad overview of this project is as follows:
-
Data Collection: First, we gathered the datasets made of the images of healthy and infected leaf samples.
-
Data preprocessing: Once we gathered samples, the images were normalized and then divided into data for training and others for validation.
-
Model Building: Implementing transfer learning using the pre-trained models of VGG16, VGG19 and EfficientNet-b4
-
Training the Model: subsequently, models are then developed on training databases in a manner to efficiently diagnose leaf disease.
-
Evaluation: Carrying out model testing and its performance assessment.
The methodology includes:
-
CNNs for Feature Extraction: Implementing Convolution Layers for the Important Features on the Images.
-
Transfer Learning: Implementation of pre-trained models (VGG16, VGG19, Efficient-b4) that reduce the time for learning the target model.
-
Data Augmentation: Enhancement of the model performance via augmentation methods over the dataset.
Data Collection
For this research, the dataset was collected from the Kaggle platform, which included pictures of healthy as well as infected leaves. During the analysis of images, it was clear that there were classes with a lot fewer images than the rest, resulting in an unequal dataset.
We have a dataset that comprises 8320 training pictures and 2080 validation images. They are divided into 26 classes of plant leaves disease and healthy leaves. These pictures were divided according to the different crops, such as apples, corn, grapes, etc.
Data preparation
In our case, after labeling the dataset, images are resized to 128x128 pixels for easy fitting. OpenCV and TensorFlow allow images to be processed by:
-
Resizing and normalizing pixel values.
-
Label the images according to the respective class (disease or healthy).
-
Augmentation techniques like flipping, rotating, and zooming to balance the dataset.
-
Normalize the pixel values to speed up model training.
Data Preparation Workflow
-
Load images and corresponding labels.
-
Resize images to 128x128.
-
Normalize the pixels.
-
Divide the available data into training and validation data.
Code Explanation
Here's what is happening under the hood. Let's go through it step by step:
STEP 1:
Mounting Google Drive
Mount your Google Drive to access and save datasets, models, and other resources.
from google.colab import drive
drive.mount('/content/drive')
Package installation
Installs the TensorFlow packages for building and training machine learning and deep learning models.
!pip install tensorflow
Importing Libraries
Libraries like os, PIL, OpenCV, Matplotlib, and NumPy are imported for interacting with the file system, managing image input/output, processing, and visualization. Keras and TensorFlow are well-known libraries for constructing neural networks. Various layers, models, and utilities are employed to define, compile, and train deep learning models.
import os
import keras
import numpy as np
from tqdm import tqdm
from keras.models import Sequential
from keras.callbacks import ModelCheckpoint
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras import optimizers
from keras.preprocessing import image
from PIL import Image,ImageOps
import cv2
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, classification_report
import matplotlib.pyplot as plt
import tensorflow
from tensorflow.keras import Model
from tensorflow.keras.layers import Input, BatchNormalization, ReLU, ELU, Dropout, Conv2D, Dense, MaxPool2D, AvgPool2D, GlobalAvgPool2D, Concatenate
import tensorflow as tf
import tensorflow.keras
from tensorflow.keras import models, layers
from tensorflow.keras.models import Model, model_from_json, Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, SeparableConv2D, UpSampling2D, BatchNormalization, Input, GlobalAveragePooling2D
from tensorflow.keras.regularizers import l2
from tensorflow.keras.optimizers import SGD, RMSprop
from tensorflow.keras.utils import to_categorical
Data Loading and Preprocessing
This section specifies the location of the dataset folders. The dataset is divided into two parts: training and validation. All images are resized to a standard dimension of 128x128 pixels to ensure uniformity throughout the dataset.
dataset='/content/drive/MyDrive/Leaf_Disease_Datasets'
train_folder = os.path.join(dataset,"training")
test_folder = os.path.join(dataset,"validation")
This code section is to collect all the subdirectories present in the train_folder into the categories list and then print the category names.
img_size = 128
categories = []
for i in os.listdir(train_folder):
categories.append(i)
print(categories)
STEP 2:
Data Processing
Here, the function process_data() iterates through every class (plant disease or healthy in this case). Each image file is read and resized to the size already defined, and the data list is appended with the image and its label.
A progress monitor is integrated in the form of a bar chart (using tqdm), which shows the status of image processing for both categories. Finally, the function returns the prepared dataset and the number of images per class for later visualization.
# Function to process data
def process_data(folder, categories, img_size):
data = []
class_counts = {category: 0 for category in categories}
for c in categories:
path = os.path.join(folder, c)
class_num = categories.index(c)
for img in tqdm(os.listdir(path), desc=f"Processing {c}"):
try:
img_array = cv2.imread(os.path.join(path, img))
img_resized = cv2.resize(img_array, (img_size, img_size))
data.append([img_resized, class_num])
class_counts[c] += 1
except Exception as e:
pass
print(f"Class '{c}' has {class_counts[c]} images")
return data, class_counts
Processing Training Data
This processes the training images and prints the total number of images in each class.
training_data, train_class_counts = process_data(train_folder, categories, img_size)
print(f"Total training data: {len(training_data)}")
Plotting Class Distributions
This code creates a bar plot to visualize the class distribution of the training data. It is useful to assess whether the dataset is balanced or imbalanced among various categories. Because an imbalanced dataset can influence the performance of the model.
plt.figure(figsize=(10, 8))
plt.bar(train_class_counts.keys(), train_class_counts.values())
plt.xlabel('Categories')
plt.ylabel('Number of Images')
plt.title('Class Distribution (Training Data)')
plt.xticks(rotation=90, ha='right')
# Add labels to the bars
colors = plt.cm.get_cmap('viridis', len(train_class_counts))
for i, bar in enumerate(plt.gca().patches):
bar.set_color(colors(i))
plt.tight_layout()
plt.show()
Processing Validation Data
This processes the validation images and prints the total number of images in each class.
validation_data, val_class_counts = process_data(test_folder, categories, img_size)
print(f"Total validation data: {len(validation_data)}")
Plotting Validation Data Distribution
This code creates a bar plot to visualize the class distribution of the validation data.
plt.figure(figsize=(10, 8))
plt.bar(val_class_counts.keys(), val_class_counts.values())
plt.xlabel('Categories')
plt.ylabel('Number of Images')
plt.title('Class Distribution (Validation Data)')
plt.xticks(rotation=90, ha='right')
# Add labels to the bars
colors = plt.cm.get_cmap('viridis', len(val_class_counts))
for i, bar in enumerate(plt.gca().patches):
bar.set_color(colors(i))
plt.tight_layout()
plt.show()
STEP 3:
Preparing Data for Model
This block of code converts the images and labels into numpy arrays for efficient computation. Then reshape the images into 4D arrays (number of images, height, width, color channels). Normalize the pixel values by dividing by 255 to bring them into the range [0, 1]. This is important for improving model performance and stability during training.
X_train = []
Y_train = []
for img, label in training_data:
X_train.append(img)
Y_train.append(label)
X_train = np.array(X_train).astype('float32').reshape(-1, img_size, img_size, 3)
Y_train = np.array(Y_train)
print(f"X_train= {X_train.shape} Y_train= {Y_train.shape}")
This code sets up the validation data for testing by arranging images and labels into arrays, reshaping them, normalizing the pixel values, and displaying their shapes.
X_test = []
Y_test = []
for features,label in validation_data:
X_test.append(features)
Y_test.append(label)
X_test = np.array(X_test).astype('float32').reshape(-1, img_size, img_size, 3)
Y_test = np.array(Y_test)
print(f"X_test= {X_test.shape} Y_test= {Y_test.shape}")
X_train, X_test = X_train / 255.0, X_test / 255.0
Visualizing Random Training Images
This function randomly selects and displays one image from each category using Matplotlib. It provides a visual overview of the dataset.
import matplotlib.pyplot as plt
import random
# Function to show one image from each class
def show_sample_images(train_folder, categories):
plt.figure(figsize=(20, 20))
for i, category in enumerate(categories):
category_path = os.path.join(train_folder, category)
images = os.listdir(category_path)
random_image = random.choice(images)
img_path = os.path.join(category_path, random_image)
img = plt.imread(img_path)
ax = plt.subplot(6, 5, i + 1)
plt.imshow(img)
plt.title(category)
plt.axis("off")
plt.tight_layout()
plt.show()
# Call the function to show sample images
show_sample_images(train_folder, categories)
STEP 4:
Leaf Disease Detection Using vgg16 custom model, modified vgg19 model and efficientnet_b4 Model.
- VGG16 Custom Model: This model is based on the original VGG16 but customized to better suit the specific requirements of leaf disease detection. Its 16-layer architecture helps in accurately identifying disease patterns in leaf images.
- Modified VGG19 Model: This model is an enhanced version of the VGG19, modified to improve its performance in detecting leaf diseases. The additional layers provide more depth and precision in image analysis.
- EfficientNet_B4 Model: Known for its balance of efficiency and accuracy, this state-of-the-art model is designed to perform well with fewer resources while maintaining high accuracy. It is particularly effective for complex image classification tasks like identifying leaf diseases.
By using these models, my project aims to accurately detect diseases in leaves from images, leveraging the strengths of each model to improve overall performance.
Model Building
VGG16
In this block, we load a pre-trained VGG16 model using weights that have been trained on the ImageNet dataset. By setting include_top=False, we exclude the fully connected (dense) layers.
To preserve the pre-trained layers, set trainable=False. This approach ensures that these layers remain unchanged during training. Which conserves computational resources and time, allowing us to concentrate on fine-tuning only the newly added layers.
The custom model is based on the VGG16 architecture. It incorporates global average pooling, followed by two dense layers that include batch normalization and dropout for regularization. The Dropout layer prevents overfitting by randomly dropping neurons during training. Finally, it features an output layer using softmax activation for multi-class classification (with 26 classes in this case) designed to predict class probabilities.
The model is compiled with the Adam optimizer and sparse categorical cross-entropy loss function. The accuracy metric is also tracked during training. After that, the model is trained for 100 epochs with a batch size of 64.
input_shape = (img_size, img_size, 3)
num_classes = 26
from keras.applications import VGG16
vgg16_model = VGG16(weights='imagenet', include_top=False, input_shape=input_shape)
for layer in vgg16_model.layers:
layer.trainable = False
vgg16_custom_model = Sequential()
vgg16_custom_model.add(vgg16_model)
vgg16_custom_model.add(GlobalAveragePooling2D())
vgg16_custom_model.add(Dense(512, activation='relu'))
vgg16_custom_model.add(BatchNormalization())
vgg16_custom_model.add(Dropout(0.5))
vgg16_custom_model.add(Dense(512, activation='relu'))
vgg16_custom_model.add(BatchNormalization())
vgg16_custom_model.add(Dropout(0.5))
vgg16_custom_model.add(Dense(num_classes, activation='softmax'))
# Compile the model
vgg16_custom_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Print model summary
vgg16_custom_model.summary()
Training the VGG16 Custom Model
vgg16_pretrained = vgg16_custom_model.fit(
x=X_train,
y=Y_train,
epochs=100,
validation_data=(X_test, Y_test),
batch_size=64
)
Plotting Training History
This function creates plots for the accuracy and loss curves of both training and validation data, offering a clear visual representation of how the model learns over time.
def plot_history(history, title):
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.title(f'{title} Accuracy Curves')
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train_loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.title(f'{title} Loss Curves')
plt.show()
plot_history(vgg16_pretrained, 'VGG16')
Evaluating Model Performance
The purpose of this code is to evaluate the custom VGG16 model on both the validation and the training datasets. It visualizes the accuracy and loss of each dataset. This serves a very important purpose. It assesses how well the model performs and whether it can be generalized.
valid_loss, valid_acc = vgg16_custom_model.evaluate(X_test, Y_test)
train_loss, train_acc = vgg16_custom_model.evaluate(X_train, Y_train)
print('\nValidation Accuracy:', valid_acc)
print('\nValidation Loss:', valid_loss)
print('\nTrain Accuracy:', train_acc)
print('\nTrain Loss:', train_loss)
Evaluating the Model
loss, accuracy = vgg16_custom_model.evaluate(X_test, Y_test)
print(f"Accuracy: {accuracy * 100:.2f}%")
Plotting Confusion Matrix
The plot_confusion_matrix function is used to visualize a confusion matrix. It requires four parameters and illustrates how well the model performs by comparing actual categories to predicted ones. This visualization is useful for pinpointing which categories the model tends to confuse.
def plot_confusion_matrix(model, X_test, Y_test, categories, title):
Y_pred = model.predict(X_test)
Y_pred_classes = np.argmax(Y_pred, axis=1)
cm = confusion_matrix(Y_test, Y_pred_classes)
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=categories, yticklabels=categories)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title(title)
plt.show()
plot_confusion_matrix(vgg16_custom_model, X_test, Y_test, categories, 'VGG16 Confusion Matrix')
Classification Report
Here, we present a classification report that details precision, recall, and F1-score for each class in the dataset. This offers a clearer insight into the model's performance across various leaf disease categories.
Y_pred = vgg16_custom_model.predict(X_test)
Y_pred_classes = np.argmax(Y_pred, axis=1)
print(classification_report(Y_test, Y_pred_classes, target_names=categories))
STEP 5:
Modified VGG19
This block of code illustrates how to create a modified VGG19 model using TensorFlow and Keras. The model utilizes transfer learning by freezing the base VGG19 layers and adding custom layers on top to tackle a specific classification task.
It features a GlobalAveragePooling layer followed by two fully connected (Dense) layers with 1024 and 512 neurons, respectively, both employing ReLU activation for non-linearity. A dropout layer is included after each Dense layer with a dropout rate of 0.5 to help reduce overfitting. Finally, the last Dense layer uses softmax activation to produce probabilities for each category in our dataset, where len(categories) indicates the number of target classes.
The model is compiled with the Adam optimizer and sparse categorical cross-entropy loss function. The accuracy metric is also tracked during training. After that, the model is trained for 100 epochs with a batch size of 32.
from tensorflow.keras.applications.vgg19 import VGG19
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
from tensorflow.keras.models import Model
import tensorflow as tf
# Now you can use VGG19 in your code
base_model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape)
# Freeze the layers in the base model
for layer in base_model.layers:
layer.trainable = False
# Add custom layers on top of the base model
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(len(categories), activation='softmax')(x) # Number of categories in your dataset
# Create the modified VGG19 model
modified_vgg19_model = Model(inputs=base_model.input, outputs=x)
# Compile the model
modified_vgg19_model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# Display the model summary
modified_vgg19_model.summary()
Training the modified vgg19 model
history = modified_vgg19_model.fit(
X_train, Y_train,
validation_data=(X_test, Y_test),
epochs=100,
batch_size=32,
callbacks=[tf.keras.callbacks.EarlyStopping(patience=5, restore_best_weights=True)]
)
Save the model
modified_vgg19_model.save('/content/drive/MyDrive/Leaf_Disease_Detection/modified_vgg19_model.h5')
Plotting Training History
This function creates plots for the accuracy and loss curves of both training and validation data, offering a clear visual representation of how the model learns over time.
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train accuracy')
plt.plot(history.history['val_accuracy'], label='validation accuracy')
plt.title('Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train loss')
plt.plot(history.history['val_loss'], label='validation loss')
plt.title('Loss')
plt.legend()
plt.show()
Evaluating Model Performance
The purpose of this code is to evaluate the custom VGG19 model on both the validation and the training datasets. It visualizes the accuracy and loss of each dataset. This serves a very important purpose. It assesses how well the model performs and whether it can be generalized.
valid_loss, valid_acc = modified_vgg19_model.evaluate(X_test, Y_test)
train_loss, train_acc = modified_vgg19_model.evaluate(X_train, Y_train)
print('\nValidation Accuracy:', valid_acc)
print('\nValidation Loss:', valid_loss)
print('\nTrain Accuracy:', train_acc)
print('\nTrain Loss:', train_loss)
Evaluating the Model
We load the best model and evaluate it on the test data. There is the accuracy of 92.31%.
test_loss, test_acc = modified_vgg19_model.evaluate(X_test, Y_test)
print(f"Test Accuracy: {test_acc * 100:.2f}%")
Plotting Confusion Matrix
The plot_confusion_matrix function is used to visualize a confusion matrix. It requires four parameters and illustrates how well the model performs by comparing actual categories to predicted ones. This visualization is useful for pinpointing which categories the model tends to confuse.
def plot_confusion_matrix(model, X_test, Y_test, categories, title):
Y_pred = model.predict(X_test)
Y_pred_classes = np.argmax(Y_pred, axis=1)
cm = confusion_matrix(Y_test, Y_pred_classes)
plt.figure(figsize=(12, 10))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=categories, yticklabels=categories)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title(title)
plt.show()
plot_confusion_matrix(modified_vgg19_model, X_test, Y_test, categories, 'Modified VGG19 Confusion Matrix')
Classification Report
Here, we present a classification report that details precision, recall, and F1-score for each class in the dataset. This offers a clearer insight into the model's performance across various leaf disease categories.
Y_pred = modified_vgg19_model.predict(X_test)
Y_pred_classes = np.argmax(Y_pred, axis=1)
# Print classification report
print(classification_report(Y_test, Y_pred_classes, target_names=categories))
STEP 6:
Efficient-B4 Model
This code installs the EfficientNet library, which offers pre-trained EfficientNet models. It also imports the necessary EfficientNet modules for use in defining the model later on.
!pip install -q efficientnet
import efficientnet.tfkeras as efn
The code creates a custom EfficientB4 model using global average pooling, dense layers, batch normalization, dropout, and softmax activation for multi-class classification.
The model is compiled using the Adam optimizer and sparse categorical cross-entropy loss function. The accuracy metric is also tracked during training.After that, the model is trained for 100 epochs with a batch size of 64.
enet = efn.EfficientNetB4(
input_shape=input_shape,
weights='imagenet',
include_top=False
)
x = enet.output
x = tf.keras.layers.GlobalMaxPooling2D()(x)
x = tf.keras.layers.Dense(256, activation='relu')(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Dropout(0.5)(x)
y = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
e_model_b4 = tf.keras.Model(inputs=enet.input, outputs=y)
e_model_b4.compile(
optimizer = tf.keras.optimizers.Adam(learning_rate=5e-4),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
Training the efficientnet_b4 model
efficientnet_b4 = e_model_b4.fit(x=X_train, y=Y_train, epochs=100, validation_data=(X_test, Y_test), batch_size=64)
Plotting Training History
This function creates plots for the accuracy and loss curves of both training and validation data, offering a clear visual representation of how the model learns over time.
def plot_history(history, title):
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='train_accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.title(f'{title} Accuracy Curves')
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='train_loss')
plt.plot(history.history['val_loss'], label='val_loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.title(f'{title} Loss Curves')
plt.show()
plot_history(efficientnet_b4, 'EfficientNetB4')
Evaluating Model Performance
valid_loss, valid_acc = e_model_b4.evaluate(X_test, Y_test)
train_loss, train_acc = e_model_b4.evaluate(X_train, Y_train)
print('\nValidation Accuracy:', valid_acc)
print('\nValidation Loss:', valid_loss)
print('\nTrain Accuracy:', train_acc)
print('\nTrain Loss:', train_loss)
Evaluating the Model
We load the best model and evaluate it on the test data. There is the accuracy of 99.42%.
loss, accuracy = e_model_b4.evaluate(X_test, Y_test)
print(f"Accuracy: {accuracy * 100:.2f}%")
Save the model
e_model_b4.save('/content/drive/MyDrive/Leaf_Disease_Detection/efficientnet_b4_model.h5')
Plotting Confusion Matrix
def plot_confusion_matrix(model, X_test, Y_test, categories, title):
Y_pred = model.predict(X_test)
Y_pred_classes = np.argmax(Y_pred, axis=1)
cm = confusion_matrix(Y_test, Y_pred_classes)
plt.figure(figsize=(12, 10))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=categories, yticklabels=categories)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title(title)
plt.show()
plot_confusion_matrix(e_model_b4, X_test, Y_test, categories, 'EfficientNetB4 Confusion Matrix')
Classification Report
We plot the confusion matrix and print the classification report.
Y_pred_enetb4 = e_model_b4.predict(X_test)
Y_pred_classes_enetb4 = np.argmax(Y_pred_enetb4, axis=1)
print("\nEfficientNetB4 Classification Report:")
print(classification_report(Y_test, Y_pred_classes_enetb4, target_names=categories))
STEP 7:
Prediction
Load The Training Model
!pip install keras-applications
import tensorflow as tf
from keras.layers import Dropout
class FixedDropout(Dropout):
def _get_noise_shape(self, inputs):
if self.noise_shape is None:
return self.noise_shape
symbolic_shape = tf.shape(inputs)
noise_shape = [symbolic_shape[axis] if shape is None else shape
for axis, shape in enumerate(self.noise_shape)]
return tuple(noise_shape)
with tf.keras.utils.custom_object_scope({'FixedDropout': FixedDropout}):
loaded_model = tf.keras.models.load_model('/content/drive/MyDrive/new_projects/p5/modified_vgg19_model.h5')
img_array = cv2.imread('/content/drive/MyDrive//Leaf_Disease_Datasets/04125537-801d-4e15-b66c-224b09b4e1a7___RS_HL 7457.JPG') # Replace with your image path
img_resized = cv2.resize(img_array, (img_size, img_size))
img_array = img_resized / 255.0
img_array = np.expand_dims(img_array, axis=0)
prediction = loaded_model.predict(img_array)
predicted_class_index = np.argmax(prediction)
predicted_class = categories[predicted_class_index]
print("Predicted Class:", predicted_class)
# Display the image
plt.imshow(cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB))
plt.title(f"Predicted: {predicted_class}")
plt.axis('off')
plt.show()
img_array = cv2.imread('/content/drive/MyDrive/Leaf_Disease_Datasets/0169b9ac-07b9-4be1-8b85-da94481f05a4___NREC_B.Spot 9169.JPG') # Replace with your image path
img_resized = cv2.resize(img_array, (img_size, img_size))
img_array = img_resized / 255.0
img_array = np.expand_dims(img_array, axis=0)
prediction = loaded_model.predict(img_array)
predicted_class_index = np.argmax(prediction)
predicted_class = categories[predicted_class_index]
print("Predicted Class:", predicted_class)
# Display the image
plt.imshow(cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB))
plt.title(f"Predicted: {predicted_class}")
plt.axis('off')
plt.show()
Project Conclusion
The Leaf Disease Detection Using Deep Learning project uses powerful deep learning methods like VGG16, VGG19, and EfficientB4 to differentiate between healthy and diseased plants.
The model achieves a high prediction accuracy of 99.4, it demonstrates the potential of leaf disease classification using CNNs for early disease detection. Thus make it suitable for early disease detection. This AI farming solution is ideal for farmers and agronomists as the global demand for smart farming and AI agricultural solutions increases. In transfer learning, our model fared well and was able to develop fast and thus can be adjusted to perform for several crop diseases.
If you want to find more information about Computer Vision, feel free to visit our website.
Challenges and Troubleshooting
Here are some common challenges that the users can encounter during the execution of the project, including their solutions:
-
Large Dataset Size: Operating with a large dataset might bring about some memory issues.
- Solution: Make use of batch processing and take advantage of the free GPU in Google Colab to manage memory efficiently.
-
Model Overfitting: The model tends to do very well on the training data but poorly on the validation data.
- Solution: Use dropout and batch normalization layers to curb overfitting.
-
Slow Training Time: Training deep networks such as VGG16 and VGG19 tends to eat up time.
- Solution: Decrease the number of epochs or take on a more optimized architecture like EfficientNetB4, which is faster to train.
-
Data Imbalance: Some classes can have fewer images than other classes. This makes an imbalanced dataset
- Solution: The augmentation technique can solve this issue.
FAQ
-
What is transfer learning in leaf disease detection?
-
Answer: Transfer learning uses pre-trained deep learning models like VGG16, VGG19, and EfficientNet. These models help in identifying and detecting leaf diseases. These models are already pre-trained on a huge dataset like Imagenet.
-
-
What are the main benefits of transfer learning for image classification in agriculture?
-
Answer: Transfer learning lets us fine-tune pre-trained models for leaf disease identification. Even with insufficient data, this speeds up training and improves plant disease classification.
-
-
Where can I find leaf disease detection datasets?
-
Answer: We can find the dataset from Kaggle.
-
-
How does EfficientNetB4 detect leaf diseases compared to VGG16 and VGG19?
-
Answer: Compound scaling makes EfficientNetB4 more accurate and efficient because it optimizes accuracy and computational efficiency, making it better than VGG16 and VGG19. However in this project on validation data, all models in the research exceeded 92% accuracy.
-
-
How can I use transfer learning to detect leaf disease?
-
Answer: Google Colab supports transfer learning leaf disease detection. At first Load a dataset of leaf images. If the dataset is small then use augmentation techniques to create a diver dataset. Then select a pretrained model and fine-tune it for classification tasks using TensorFlow or PyTorch.
-