Mango Leaf Disease Dataset
I have built 2 model to predict the diseases found on Mango leaves using the given Mango Leaf Disease Dataset on Kaggle, mango-leaf-disease dataset. Mango Leaf Disease Dataset can be a very helpful area to be explored for the summer season when the mangoes are in market the most. The ability to predict and classify the diseases on the mango leafs which can have a wide range diseases, such as "Anthracnose", "Bacterial Canker", "Cutting Weevil", "Die Back", "Gall Midge", "Healthy", "Powdery Mildew", "Sooty Mould". In this post, we built a Classification model using VGG16 Neural Network Model and Sequential Model from keras library.
First Step:
We generate an api to access the kaggle Dataset in google colab, it will give us a kaggle.json file which we have to upload in our notebook files menu. We download the required dataset from kaggle to train our model. Our Dataset has images of mango leaves with different diseases along with the healthy ones with multiple labels. We just unzip the downloaded Dataset in a dataset folder in google colab, then access it from files, which you can see it here and there is one another way in which we can upload the file to our google drive and then access it from there by giving the notebook file access to your google dataset.
!kaggle datasets download -d aryashah2k/mango-leaf-disease-dataset
!mkdir -p /content/Datacard
!unzip mango-leaf-disease-dataset.zip -d /content/Datacard
Second Step:
Now, we use the Python libraries to load and preprocess the data. We will use Keras Library, a library to build a model for classifying images of mango leaves. We will also split the dataset in training validation and test sets. We used a function to make a directory of the training testing and validation datasets.
import shutil, os
import numpy as np
import pandas as pd
from sklearn.utils import shuffle
import matplotlib.pyplot as plt
import random
import glob
import tensorflow as tf
import keras
from keras.applications.vgg16 import VGG16
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data import Dataset, DataLoader
import PIL
import PIL.Image
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
from keras.callbacks import Callback
from keras.callbacks import ModelCheckpoint
We used a pretrained VGG16 model given in the keras library. Also we made a data spliter library, which helps divide the image dataset.into training, testing and validation set.
def data_spliter(all_data_dir=data_dir,val_data_dir=val_dir,train_data_dir=train_dir,test_data_dir=test_dir,ttr=train_test_ratio):
for subdir,dirs,files in os.walk(all_data_dir):
#split all the data code
We build a model with the initial layers having VGG16 maodel which has 16 layers and add some more layers in between and then stack the output layer at the end
base_model = VGG16(input_shape = (224, 224, 3), include_top = False, weights = 'imagenet')
for layer in base_model.layers:
layer.trainable = False
base_model_ouput = base_model.output
x = tf.keras.layers.GlobalAveragePooling2D()(base_model_ouput)
x = tf.keras.layers.Dense(128, activation='relu')(x)
x = tf.keras.layers.Dense(num_classes, activation='softmax', name='fcnew')(x)
model = tf.keras.Model(inputs=base_model.input, outputs=x)
Now we compile the whole model.
model.compile(optimizer = opt , loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) , metrics = ['accuracy'])
Third Step:
We fit our data in the model using model.fit() module in keras Library. We can gather accuracy, validation_accuracy, loss and validation_loss.
model.fit("training_dataset_variable">, epochs = 10, validation_data = val_it, callbacks = callbacks)
We can store this fiting operation in a variable and then we can call the predefined accuracy and loss functions to find the values. Also, we can see where our model is overfitting by comparing training loss and validation loss. So we can make a different model variable where we give less epochs according to the overfitting we saw earlier.
I also Implemented a Callback module from Keras Library to prevent our model from overfitting.
model.fit(train_it, epochs = 10, validation_data = val_it, callbacks = callbacks)
Next Step:
We also built another Sequential model for image classification using keras layers and then we added these layers in the model as given in the code below.
model = Sequential()
model.add(Conv2D(32,3,padding="same", activation="relu", input_shape=(224,224,3)))
model.add(MaxPool2D())
model.add(Conv2D(32, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Conv2D(64, 3, padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128,activation="relu"))
model.add(Dense(2, activation="softmax"))
model.summary()
Motivation:
This project assignment was to understand and implement the image classification model using Keras Library. The best part of building this model was to distribute the data in training, testing and validation sets. Also making the model using different keras layers was a fun objective to work on.
Conclusion and Future Scope:
I experienced how to preprocess the image data and build a classification model based on our data. I have made a Google Colab notebook of it, which you can check out it is pretty easy to understand. Also, the future work on this project can be to build the disease classification model for all plants.
Try competitions on Kaggle it is fun and good for practicing your skills and keeping uptodate with current technology.
Sumedh Wairagade