PLANT DISEASE DETECTION
PROBLEM:
In Agriculture field all farmers facing the problem of plant disease.in olden days their are various way to destroy these disease but in technological time through detection we can easily detect which type of disease are available in particular plant.
Basically we will first train our CNN models with a lot of images of potato,pepper and tomato.
Why CNN: As we have seen in CNN Tutorial,CNN reads a very large image in a simple manner. CNN most commonly used to analyze visual imagery and are frequently working behind the scenes in image classification.
DATASET:
Various datasets are available on internet to detect your plant disease and train your model with these datasets. We can also create our own data set and train our model. we can download data set from kaggle.To download data set GET HERE
IMPLEMENTATION:
IMPORTING LIBRARIES
# Intialization of Program. by Importing various LIbraries
import numpy as np
import matplotlib.pyplot as plt
# here we are working on Tensorflow version 2.1.0 so we need to write tensorflow.keras.
#keras is in built function in Tensorflow .
import os
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Input, Dropout,Flatten, Conv2D
from tensorflow.keras.layers import BatchNormalization, Activation, MaxPooling2D
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras.utils import plot_model
from IPython.display import SVG, Image |
NOTE:
Here we import various libraries like numpy, matplotlib, os, tensorflow etc. we use here tensorflow version 2.1.0 and keras is also available inside this tensorflow version. we use various layers and model,optimizer for train our model.
SPECIAL POINT
# For checking out that how many images are available in the train set we can use import OS
for types in os.listdir("D:\pant\drive\My Drive/train_set/"):
print(str(len(os.listdir("D:\pant\drive\My Drive/train_set/"+ types)))+" "+ types+' images') |
we use os.listdir for fatching list of all the images in the folder.inside this we give the path ("highlight")where images are store.here we take only train dataset images from our plant disease dataset. |
OUTPUT:
IMAGE LOADING :
# Complete Dataset images can be loaded using ImageDataGenerator function
img_size=48
batch_size=64
datagen_train=ImageDataGenerator(horizontal_flip=True)
train_generator=datagen_train.flow_from_directory("D:\pant\drive\My Drive/train_set",
target_size=(img_size,img_size),
batch_size=batch_size,
class_mode='categorical',
shuffle=True)
datagen_test=ImageDataGenerator(horizontal_flip=True)
validation_generator=datagen_test.flow_from_directory("D:\pant\drive\My Drive/test_data",
target_size=(img_size,img_size),
batch_size=batch_size,
class_mode='categorical',
shuffle=True) |
OUTPUT:
DESCRIPTION:
This part of code is used to define train and test data into the model. The train dataset has 16222 images belonging to 15 classes and the test dataset has 1254 images belonging to 15 classes. here we use class mode 'categorical' because more then 2 classes are available here. |
MODEL LOADING:
DESCRIPTION:
Here we use sequential model.The Sequential model API is a way of creating deep learning models where a sequential class is created and model layers are created and added to it. |
layers addition
#convolutional layer-1
detection.add(Conv2D(64,(3,3),padding='same',input_shape=(48,48,3)))
detection.add(BatchNormalization())
detection.add(Activation('relu'))
detection.add(MaxPooling2D(pool_size=(2,2)))
detection.add(Dropout(0.25))
#2 -convolutional layer-2
detection.add(Conv2D(128,(5,5),padding='same'))
detection.add(BatchNormalization())
detection.add(Activation('relu'))
detection.add(MaxPooling2D(pool_size=(2,2)))
detection.add(Dropout(0.25))
#3 -convolutional layer-3
detection.add(Conv2D(256,(3,3),padding='same'))
detection.add(BatchNormalization())
detection.add(Activation('relu'))
detection.add(MaxPooling2D(pool_size=(2,2)))
detection.add(Dropout(0.25))
#4 -convolutional layer-4
detection.add(Conv2D(512,(3,3),padding='same'))
detection.add(BatchNormalization())
detection.add(Activation('relu'))
detection.add(MaxPooling2D(pool_size=(2,2)))
detection.add(Dropout(0.25))
#5 -convolutional layer-5
detection.add(Conv2D(512,(3,3),padding='same'))
detection.add(BatchNormalization())
detection.add(Activation('relu'))
detection.add(MaxPooling2D(pool_size=(2,2)))
detection.add(Dropout(0.25))
detection.add(Flatten())
detection.add(Dense(256))
detection.add(BatchNormalization())
detection.add(Activation('relu'))
detection.add(Dropout(0.25))
detection.add(Dense(512))
detection.add(BatchNormalization())
detection.add(Activation('relu'))
detection.add(Dropout(0.25)
detection.add(Dense(15,activation='softmax'))
optimum=Adam(lr=0.005)
#lr-learning rate
detection.compile(optimizer=optimum,loss='categorical_crossentropy',metrics=['accuracy']) |
DESCRIPTION:
Here we add various convolutional layers with the Relu activation function (Rectified Linear Unit).We pass the images through the layers more than once for better feature extraction.
Inside the conv2D first perameter show the number of neurons,second show the size of filter/kernal,Input_shape represent here the RGB format of images.The convolutional layer is then pass to MaxPooling layer,pooliing size is the window size.
Flatten is used for convert data into 1D form. Dense layer is used for all neurons to the previous layer neurons so it is fully connected layer. Droupout function is use for not taking all the units in the neural network.optimize Adam is used for optimize our data with learning rate.
A metric is a function that is used to judge the performance of your model. The optimizers are used for improving speed and performance for training a specific model.
SUMMARY OF ADDED LAYERS:
detection.summary()
# This is used to get all the summary related to model , added layers descriptions. |
|
OUTPUT:
TRAINING OF MODEL (MODEL CREATION):
MODEL SAVING
ephocs=10
steps_per_epoch=train_generator.n//train_generator.batch_size
steps_per_epoch
validation_steps=validation_generator.n//validation_generator.batch_size
validation_steps
detection.fit(x=train_generator,
steps_per_epoch=steps_per_epoch,
epochs=ephocs,
validation_data=validation_generator,
validation_steps=validation_steps)
detection.save('Plant_Disease_Detection.h5') |
|
NOTE:
This is most important part of our model.here we take 10 epochs for train our model. More epochs increase the accuracy and decrease the loss.
The most important thing about this,it run only one time not run this step again and again because epochs takes time once you run this step do not repeat it again.
Here we save this model and if we want to use it in future then load this model.
|
OUTPUT:
PREDICTION :
PREDICTION #1
from tensorflow.keras.models import load_model
Detection=load_model('Plant_Disease_Detection.h5')
from tensorflow.keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
import cv2
test_img=image.load_img("D:\pant\drive\My Drive/test_data/Tomato_healthy/0d515778-61ef-4f0b-ab54-75607c80220f___RS_HL 9745.jpg",target_size=(48,48))
plt.imshow(test_img)
test_img=image.img_to_array(test_img)
test_img=np.expand_dims(test_img,axis=0)
result=Detection.predict(test_img)
a=result.argmax()
# print('a:',a)
classes=train_generator.class_indices
# print(classes)
# print(len(classes))
category=[]
for i in classes:
category.append(i)
for i in range(len(classes)):
if(i==a):
output=category[i]
output |
OUTPUT:
Tomato_healthy
PREDICTION #2
from tensorflow.keras.models import load_model
Detection=load_model('Plant_Disease_Detection.h5')
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
import cv2
test_img=image.load_img("D:\pant\drive\MyDrive/test_data/Pepper__bell___healthy/1dd1b153-8ded-439f-8c9e-c9970c67e642___JR_HL 8163.jpg",target_size=(48,48))
plt.imshow(test_img)
test_img=image.img_to_array(test_img)
test_img=np.expand_dims(test_img,axis=0)
result=Detection.predict(test_img)
a=result.argmax()
# print('a',a)
classes=train_generator.class_indices
category=[]
for i in classes:
category.append(i)
for i in range(len(classes)):
if(i==a):
output=category[i]
output
|
OUTPUT:
Pepper__bell___healthy
THIS PROJECT IS DEVLOPED BY :
NAME: BHOOPENDRA JOSHI ; ECE ; 6th SEM.
COLLEGE : GLOBAL INSTITUTE OF TECHNOLOGY , JAIPUR
THIS PROJECT IS DEVLOPED DURING "GOEDUHUB ONLINE SUMMER TRAINING IN MACHINE LEARNING , PYTHON AND DEEP LEARNING".