Crop: Plant Disease Identification Using Mobile App.
By: Manikanta Munnangi
Tags: Deep Learning, Machine Learning, Plants, Convolutional Network, Keras

In this article, I’m going to explain how we can use the Deep Learning Models to detect and classify the diseases of plants and guide the farmers through videos and give instant remedies to overcome the loss of plants and fields. At first, we have to understand

What is the cause and how to overcome the cause?

2. What are the benefits of doing this?

3. Can we solve this problem with “Deep Learning technology”?

4. In Deep Learning “which Algorithm” is used to address this problem ? and how to do that ?.

NOTE: I did it only for Tomato and Potato plants. you can do it for other plants by collecting data of that plant.

It also deployed to Web App. Check out below Link.

1. The Cause And Introduction.

Since the past days and in the present too, farmers usually detect the crop diseases with their naked eye which makes them take tough decisions on which fertilisers to use. It requires detailed knowledge the types of diseases and lot of experience needed to make sure the actual disease detection. Some of the diseases look almost similar to farmers often leaves them confused. Look at the below image for more understanding.

Similar Symptoms but different diseases.

They look the same and almost similar. In case the farmer makes wrong predictions and uses the wrong fertilizers or more than the normal dose (or) threshold or Limit (every plant has some threshold fertilizers spraying to be followed), it will mess up the whole plant (or) soil and cause enough damage to plant and fields.

Cause of overdose of fertilizers. (Source: jainsusa.com)

So, How to prevent this from happening?

To prevent this situation we need better and perfect guidance on which fertilizers to use, to make the correct identification of diseases, and the ability to distinguish between two or more similar types of diseases in visuals.

This is where Artificial Neural Networks comes handy. In short ANN

2. What is ANN?

Artificial Neural Nets are computational model based on structure of biological neural nets designed to mimic the actual behaviour of a biological neural network which are present in our brain.

Combining the Multiple ANN’s.

So we can assign some stuff to it, it gets our job done. ANN helps us to make the correct identification of disease and also guiding the right quantity of fertilizers.

Only a single ANN can’t get our job done. So, we make a bunch of them stacking one on other forming a layer which we can make multiple layers in between input layer(where weights and data are given) and output layer(result) those multiple layers called Hidden layers and it then makes a Deep Neural Network and the study of it called Deep Learning.

2.1 How does it looks like The Deep Neural Network.

Simple vs Deep Neural Nets.

Simple Neural Nets are good at learning the weights with one hidden layer which is in between the input and output layer. But, it’s not good at complex feature learning.

On another hand Deep Learning Neural Nets, the series of layers between input and output layer are called hidden layers that can perform identification of features and creating new series of features from data, just as our brain. The more layers we push into the more features it will learn and perform complex operations. The output layer combines all features and makes predictions.

Therefore, Simple Neural Nets are used for simple tasks and bulk data isn’t required to train itself. whereas in Deep learning Neural Network can be expensive and require massive data sets to train itself on. I’m not gonna discuss this topic cause it goes beyond this article.

IF you are absolute beginner to Deep Learning concept below link would help to get all basics.

2.2 What type of Deep Learning Model is best for this scenario??

There you go Convolutional Neural Network(CNN or Conv Nets). It is well known for its widely used in applications of image and video recognition and also in recommender systems and Natural Language Processing(NLP). However, convolutional is more efficient because it reduces the number of parameters which makes different from other deep learning models.

CNN Architecture.

To keep it simple I will explain only the brief understanding of this model and the steps that are used in building Convolutional Neural Network.

Main Steps to build a CNN (or) Conv net:

Convolution Operation ReLU Layer (Rectified Linear Unit) Pooling Layer (Max Pooling) Flattening Fully Connected Layer

Start Writing Code.

1. Convolution is the first layer to extract features from the input image and it learns the relationship between features using kernel or filters with input images.

2. ReLU Layer: ReLU stands for the Rectified Linear Unit for a non-linear operation. The output is ƒ(x) = max(0,x). we use this because to introduce the non-linearity to CNN.

3. Pooling Layer: it is used to reduce the number of parameters by downsampling and retain only the valuable information to process further. There are types of Pooling:

Max Pooling (Choose this).

Average and Sum pooling.

4. Flattening: we flatten our entire matrix into a vector like a vertical one. so, that it will be passed to the input layer.

5. Fully Connected Layer: we pass our flatten vector into input Layer .we combined these features to create a model. Finally, we have an activation function such as softmax or sigmoid to classify the outputs.

3. Done understanding CNN Operations. What next??

Gathering Data (Images)

Gather the data sets as many as you can with Images affected by diseases and also which are healthy. you should require bulk data.

2. Building CNN.

Build CNN using some popularly used open-source Libraries for the development of AI, Machine Learning and also Deep Learning.

Open Source Libraries.

3. Choose Any Cloud-Based Data Science IDE’s.

It is good to train models in the cloud as it requires massive computation power our normal machines laptops and the computer won’t sustain. If you have a good GPU config laptop you can train in your local machine. I choose Google colab you can choose whatever cloud you like.

Google Colab:

Google colab is a free cloud service which offers free GPU(12Gb ram). It’s a hassle free and fastest way to train our model no need to installation of any libraries on our machine. it works completely on cloud. It comes pre-installed with all dependencies.

Google Colab offers free GPU.

Sign-in to colab and create a new python notebook(ipynb) switch to GPU mode and start writing code.

4. Start Writing Code.

Data should be stored in Google drive before writing code.

Source Code can be found here My GitHub link .

Step 1: Mount data from google drive.

from google.colab import drive

drive.mount(‘/content/your path’)

Step 2: Import Libraries.

# Import Libraries

import os

import glob

import matplotlib.pyplot as plt

import numpy as np # Keras API

import keras

from keras.models import Sequential

from keras.layers import Dense,Dropout,Flatten

from keras.layers import Conv2D,MaxPooling2D,Activation,AveragePooling2D,BatchNormalization

from keras.preprocessing.image import ImageDataGenerator

Step 3: Load train and test data into separate variables.

# My data is in google drive.

train_dir ="drive/My Drive/train_set/"

test_dir="drive/My Drive/test_data/"

Step 4: Function to Get count of images in train and test data.

# function to get count of images

def get_files(directory):

if not os.path.exists(directory):

return 0

count=0

for current_path,dirs,files in os.walk(directory):

for dr in dirs:

count+= len(glob.glob(os.path.join(current_path,dr+"/*")))

return count

Step 5: View number of images in each.

train_samples =get_files(train_dir)

num_classes=len(glob.glob(train_dir+"/*"))

test_samples=get_files(test_dir)

print(num_classes,"Classes")

print(train_samples,"Train images")

print(test_samples,"Test images")

5.Output.

12 Classes == 12 types of diseases images are collected.

14955 Train Images

432 Test images (i took only a few images to test mistakenly).

For prediction, I took only a few samples from unseen data. we can evaluate using validation data which is part of train data.

Step 6: Pre-processing our raw data into usable format.

# Pre-processing data with parameters.

train_datagen=ImageDataGenerator(rescale=1./255,

shear_range=0.2,

zoom_range=0.2,

horizontal_flip=True)

test_datagen=ImageDataGenerator(rescale=1./255)

Rescaling image values between (0 –1) called normalization.

whatever pre-processing you do with the train it should be done to test parallelly.

All these parameters are stored in the variable “train_datagen and test_datagen”.

Step 7: Generating augmented data from train and test directories.

# set height and width and color of input image.

img_width,img_height =256,256

input_shape=(img_width,img_height,3)

batch_size =32 train_generator =train_datagen.flow_from_directory(train_dir,

target_size=(img_width,img_height), batch_size=batch_size)

test_generator=test_datagen.flow_from_directory(test_dir,shuffle=True,target_size=(img_width,img_height), batch_size=batch_size)

Takes the path to a directory, and generates batches of augmented data. Yields batches indefinitely, in an infinite loop.

Batch Size refers to the number of training examples utilized in one iteration.

7.Output.

Step 8: Get 12 Diseases Names/classes.

# The name of the 12 diseases.

train_generator.class_indices

8.Disease Names.

Step 9: Building CNN model

# CNN building.

model = Sequential()

model.add(Conv2D(32, (5, 5),input_shape=input_shape,activation='relu'))

model.add(MaxPooling2D(pool_size=(3, 3)))

model.add(Conv2D(32, (3, 3),activation='relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3),activation='relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())

model.add(Dense(512,activation='relu'))

model.add(Dropout(0.25))

model.add(Dense(128,activation='relu'))

model.add(Dense(num_classes,activation='softmax'))

model.summary()

9.Summary of layers.

9. Output.

CNN shrinks the parameters and learns features and stores valuable information output shape is decreasing after every layer. Can we see the output of every layer? Yes!! We can.

Step 10: Visualisation of images after every layer.

from keras.preprocessing import image

import numpy as np

img1 = image.load_img('/content/drive/My Drive/Train_d/Tomato___Early_blight/Tomato___Early_blight/

(100).JPG')

plt.imshow(img1);

#preprocess image

img1 = image.load_img('/content/drive/MyDrive/Train_d/

Tomato___Early_blight/Tomato___Early_blight(100).JPG', target_size=(256, 256))

img = image.img_to_array(img1)

img = img/255

img = np.expand_dims(img, axis=0)

Take a sample image from the train data set and visualize the output after every layer.

NOTE: Pre-processing is needed for the new sample image.