NOTE: This Use Case is not purposed for resource constrained devices.
Cotton Plant Disease Prediction¶
Credit: AITS Cainvas Community
Photo by Pest Control Room
Aggriculture is an important backbone of any country and it boosts various industries such as food industry and textile industry.And when it comes to textile industry cotton producing crops hold a special position amongst all.Though there are some challenges faced by the cotton farmers and it is hard to cultivate them.After so many hardships if their crops get exposed to diseases and are not treated timely then it affects the life of farmers greatly.So in this notebook we have tried to implement a deep learning model to detect diseased cotton plants.
Importing the Dataset¶
In [1]:
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/Cotton_Disease.zip"
!unzip -qo Cotton_Disease.zip
!rm Cotton_Disease.zip
Importing Libraries¶
In [2]:
# import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
from keras.layers import Dense, Flatten, AveragePooling2D, Dropout
from keras.models import Model
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
Data Visualization¶
In [3]:
# Check images
img = cv2.imread("Cotton Disease/test/diseased cotton leaf/dis_leaf (124).jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.title("Diseased Cotton Leaf")
Out[3]:
In [4]:
# Check images
img = cv2.imread("Cotton Disease/test/fresh cotton leaf/d (122)_iaip.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.title("Fresh Cotton Leaf")
Out[4]:
In [5]:
# Check images
img = cv2.imread("Cotton Disease/test/diseased cotton plant/dd (16)_iaip.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.title("Diseased Cotton Plant")
Out[5]:
In [6]:
# Check images
img = cv2.imread("Cotton Disease/test/fresh cotton plant/dsd (140)_iaip.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.title("Fresh Cotton Plant")
Out[6]:
Loading the Data¶
In [7]:
train_generator= ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
test_generator= ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
In [8]:
train_data = train_generator.flow_from_directory( 'Cotton Disease/train', target_size=(224, 224),
batch_size=64)
test_data = test_generator.flow_from_directory( 'Cotton Disease/test', target_size=(224, 224),
batch_size=64)
In [9]:
train_data.class_indices
Out[9]:
Using VGG-16 for Detecting plant disease¶
In [10]:
# Model creation with changes
model = VGG16(input_shape=(224,224,3),include_top=False)
for layer in model.layers:
layer.trainable = False
newModel = model.output
newModel = AveragePooling2D()(newModel)
newModel = Flatten()(newModel)
newModel = Dense(128, activation="relu")(newModel)
newModel = Dropout(0.5)(newModel)
newModel = Dense(4, activation='softmax')(newModel)
model = Model(inputs=model.input, outputs=newModel)
In [11]:
model.summary()
Model Training¶
In [12]:
opt=Adam(learning_rate=0.001)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
In [13]:
history = model.fit(train_data,
validation_data=test_data,
epochs=10)
Training Plots¶
In [14]:
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs=range(len(acc))
In [15]:
plt.plot(epochs,acc,label='Trainin_acc',color='blue')
plt.plot(epochs,val_acc,label='Validation_acc',color='red')
plt.legend()
plt.title("Training and Validation Accuracy")
Out[15]:
In [16]:
plt.plot(epochs,loss,label='Training_loss',color='blue')
plt.plot(epochs,val_loss,label='Validation_loss',color='red')
plt.legend()
plt.title("Training and Validation loss")
Out[16]:
Accessing the Performance of Model¶
In [17]:
class_dict = {0:'diseased cotton leaf',
1:'diseased cotton plant',
2:'fresh cotton leaf',
3:'fresh cotton plant' }
In [18]:
# New Data for testing
file_path = 'Cotton Disease/test/diseased cotton leaf/dis_leaf (153)_iaip.jpg'
test_image = cv2.imread(file_path)
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)
test_image = cv2.resize(test_image, (224,224),interpolation=cv2.INTER_CUBIC)
plt.imshow(test_image)
test_image = np.expand_dims(test_image,axis=0)
probs = model.predict(test_image)
pred_class = np.argmax(probs)
pred_class = class_dict[pred_class]
print(pred_class)
In [19]:
file_path = 'Cotton Disease/test/diseased cotton plant/dd (885)_iaip.jpg'
test_image = cv2.imread(file_path)
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)
test_image = cv2.resize(test_image, (224,224),interpolation=cv2.INTER_CUBIC)
plt.imshow(test_image)
test_image = np.expand_dims(test_image,axis=0)
probs = model.predict(test_image)
pred_class = np.argmax(probs)
pred_class = class_dict[pred_class]
print(pred_class)
In [20]:
file_path = 'Cotton Disease/test/fresh cotton leaf/d (133)_iaip.jpg'
test_image = cv2.imread(file_path)
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)
test_image = cv2.resize(test_image, (224,224),interpolation=cv2.INTER_CUBIC)
plt.imshow(test_image)
test_image = np.expand_dims(test_image,axis=0)
probs = model.predict(test_image)
pred_class = np.argmax(probs)
pred_class = class_dict[pred_class]
print(pred_class)
In [21]:
file_path = 'Cotton Disease/test/fresh cotton plant/dsd (223).jpg'
test_image = cv2.imread(file_path)
test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)
test_image = cv2.resize(test_image, (224,224),interpolation=cv2.INTER_CUBIC)
plt.imshow(test_image)
test_image = np.expand_dims(test_image,axis=0)
probs = model.predict(test_image)
pred_class = np.argmax(probs)
pred_class = class_dict[pred_class]
print(pred_class)