import tensorflow as tf
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.applications import VGG19
from tensorflow.keras.preprocessing.image import ImageDataGenerator
vgg= VGG19(include_top= False, weights= "imagenet", input_shape= (224, 224, 3))
for layer in vgg.layers:
layer.trainable = False
x= Flatten()(vgg.output)
pred= Dense(4, activation= "softmax")(x)
model= tf.keras.Model(vgg.inputs, pred)
model.compile(loss= "categorical_crossentropy", optimizer= "adam", metrics= ["accuracy"])
By using imagedatagenerator let us create a variable named train_generator
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)
lets now assign the training images into train_data and testing images into test_data
train_data = train_generator.flow_from_directory( 'E:\\CottonDisease\\train', target_size=(224, 224),
batch_size=32,class_mode = 'categorical')
test_data = test_generator.flow_from_directory( 'E:\\CottonDisease\\test', target_size=(224, 224),
batch_size=32,class_mode = 'categorical')
model.summary()
h= model.fit_generator(train_data, steps_per_epoch= len(train_data), epochs=10, validation_data= test_data, validation_steps= len(test_data))
import matplotlib.pyplot as plt
plt.plot(h.history['accuracy'])
plt.plot(h.history['val_accuracy'])
plt.plot(h.history['loss'])
plt.plot(h.history['val_loss'])
plt.title("Model Accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(["Accuracy","Validation Accuracy","loss","Validation Loss"])
plt.show()
import numpy as np
from tensorflow.keras.preprocessing import image
test_image = image.load_img('E:\\CottonDisease\\train\\fresh cotton leaf\\d.jpg', target_size = (224, 224))
imgplot = plt.imshow(test_image)
test_image = image.img_to_array(test_image)
test_image=test_image/255
test_image = np.expand_dims(test_image, axis = 0)
preds = model.predict(test_image)
preds
preds = np.argmax(preds, axis=1)
if preds==0:
print("The leaf is diseased cotton leaf")
elif preds==1:
print("The leaf is diseased cotton plant")
elif preds==2:
print("The leaf is fresh cotton leaf")
else:
print("The leaf is fresh cotton plant")
test_image = image.load_img('E:\\CottonDisease\\train\\diseased cotton leaf\\dis_leaf (3)_iaip.jpg', target_size = (224, 224))
imgplot = plt.imshow(test_image)
test_image = image.img_to_array(test_image)
test_image=test_image/255
test_image = np.expand_dims(test_image, axis = 0)
preds = model.predict(test_image)
preds = np.argmax(preds, axis=1)
if preds==0:
print("The leaf is diseased cotton leaf")
elif preds==1:
print("The leaf is diseased cotton plant")
elif preds==2:
print("The leaf is fresh cotton leaf")
else:
print("The leaf is fresh cotton plant")
test_image = image.load_img('E:\\CottonDisease\\train\\diseased cotton plant\\dd (1)_iaip.jpg', target_size = (224, 224))
imgplot = plt.imshow(test_image)
test_image = image.img_to_array(test_image)
test_image=test_image/255
test_image = np.expand_dims(test_image, axis = 0)
preds = model.predict(test_image)
preds = np.argmax(preds, axis=1)
if preds==0:
print("The leaf is diseased cotton leaf")
elif preds==1:
print("The leaf is diseased cotton plant")
elif preds==2:
print("The leaf is fresh cotton leaf")
else:
print("The leaf is fresh cotton plant")
#Save the model as h5 file
from tensorflow.keras.models import load_model
model.save('model_inception.h5')