<jupyter_start><jupyter_text>Dataset Downloading<jupyter_code>!pip install -U -q kaggle
!mkdir -p ~/.kaggle
#Download your api kaggle.jdon from kaggle and upload it here
from google.colab import files
files.upload()
! mkdir ~/.kaggle
! cp kaggle.json ~/.kaggle/
! chmod 600 ~/.kaggle/kaggle.json
! kaggle datasets download -d nurnob101/rice-disease
#unziping the file
from zipfile import ZipFile
file_name = '/content/rice-disease.zip'

with ZipFile(file_name, 'r') as zip:
  zip.extractall()
  print('Done')<jupyter_output>Done<jupyter_text>Libraries<jupyter_code>import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, BatchNormalization, MaxPool2D, Dropout
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import matplotlib.pyplot as plt
from glob import glob<jupyter_output><empty_output><jupyter_text>Data Visualization<jupyter_code>gudi_rotten = glob('/content/Data-image/Gudi_rotten/*.jpg')
apex_blast = glob('/content/Data-image/apex_blast/*.jpg')
leaf_blast = glob('/content/Data-image/leaf_blast/*.jpg')
leaf_burn = glob('/content/Data-image/leaf_burn/*.jpg')
neck_blast_paddy = glob('/content/Data-image/neck_blast_paddy/*.jpg')
plt.title('Gudi Rotten')
plt.imshow(image.load_img(np.random.choice(gudi_rotten)))
plt.axis('off')
plt.show()

plt.title('Apex Blast')
plt.imshow(image.load_img(np.random.choice(apex_blast)))
plt.axis('off')
plt.show()

plt.title('Leaf Blast')
plt.imshow(image.load_img(np.random.choice(leaf_blast)))
plt.axis('off')
plt.show()
plt.title('Leaf Burn')
plt.imshow(image.load_img(np.random.choice(leaf_burn)))
plt.axis('off')
plt.show()

plt.title('Neck Blast Paddy')
plt.imshow(image.load_img(np.random.choice(neck_blast_paddy)))
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>pip install split-folders tqdm
import splitfolders  
splitfolders.ratio("/content/Data-image/", output="/content/Dataset/", seed=1337, ratio=(.8, .1, .1), group_prefix=None)<jupyter_output>Copying files: 2500 files [01:31, 27.24 files/s]<jupyter_text>Image Preprocessing<jupyter_code>img_width = 150
img_height = 150

datagen = ImageDataGenerator(rescale=1/255.0, horizontal_flip=True, validation_split = 0.2)
train_data_gen = datagen.flow_from_directory(directory='/content/Dataset/train', target_size = (img_width, img_height), subset='training')
vali_data_gen = datagen.flow_from_directory(directory='/content/Dataset/test',target_size = (img_width, img_height), subset='validation')<jupyter_output>Found 50 images belonging to 5 classes.<jupyter_text>Model Creation<jupyter_code>model = Sequential()

model.add(Conv2D(32, (3,3), input_shape = (img_width, img_height, 3), activation='relu', padding='same'))
model.add(MaxPool2D(2,2))

model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
model.add(MaxPool2D(2,2))

model.add(Conv2D(128, (3,3), activation='relu', padding='same'))
model.add(MaxPool2D(2,2))

model.add(Conv2D(192, (3,3), activation='relu', padding='same'))
model.add(MaxPool2D(2,2))

model.add(Flatten())

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


model.add(Dense(228, activation='relu'))
model.add(Dropout(0.3))



model.add(Dense(5, activation='softmax'))
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
callback = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5)<jupyter_output><empty_output><jupyter_text>Model Training<jupyter_code>r = model.fit_generator(generator=train_data_gen, steps_per_epoch=len(train_data_gen), epochs=10, validation_data= vali_data_gen, validation_steps = len(vali_data_gen), callbacks=[callback])<jupyter_output>/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1844: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  warnings.warn('`Model.fit_generator` is deprecated and '<jupyter_text>Accuracy<jupyter_code>import math   
print(f'The traning accuracy of model is {math.floor(r.history["accuracy"][-1] * 100)} %')
print(f'The validation accuracy of model is {math.floor(r.history["val_accuracy"][-1] * 100)} %')<jupyter_output>The traning accuracy of model is 93 %
The validation accuracy of model is 92 %<jupyter_text>Loss<jupyter_code>print(f'The traning loss of model is {r.history["loss"][-1]}')
print(f'The validation loss of model is {r.history["val_loss"][-1]}')
# this clearly shows that model has trained perfectly, no overfitting or underfitting<jupyter_output><empty_output><jupyter_text>Loss and Accuracy Visualization<jupyter_code>plt.title('Loss')
plt.plot(r.history['loss'], label='loss')
plt.plot(r.history['val_loss'], label='val_loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend()
plt.title('Accuracy')
plt.plot(r.history['accuracy'], label='acc')
plt.plot(r.history['val_accuracy'], label='val_acc')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend()<jupyter_output><empty_output><jupyter_text>Model Saving<jupyter_code>model.save('Rice_Diseases.h5')<jupyter_output><empty_output><jupyter_text>Model Prediction<jupyter_code>from PIL import Image
from tensorflow.keras.preprocessing import image
x = [2,0,1,3,4]
y = []
txt = ['Gudi_Rotten', 'Apex_Blast', 'Leaf_Blast', 'Leaf_Burn', 'Neck_Blast_Paddy']
test_image= '/content/Dataset/val/leaf_blast/IMG20201109190117_00.jpg'
Orginal = "Leaf Blast"
open_image=Image.open(test_image)
test_image=image.load_img(test_image,target_size=(150,150))
test_image=image.img_to_array(test_image)
test_image=test_image/255.0
test_image=np.expand_dims(test_image,axis=0)
result=model.predict(test_image)
result=np.argmax(result)
y.append(result)
Name=txt[result]
plt.imshow(open_image)
plt.title(f'Real: {Orginal} \n Predicted: {Name}')
plt.axis('off')
plt.show()
test_image= '/content/Dataset/val/apex_blast/IMG20201109203053_00.jpg'
Orginal = "Apex Blast"
open_image=Image.open(test_image)
test_image=image.load_img(test_image,target_size=(150,150))
test_image=image.img_to_array(test_image)
test_image=test_image/255.0
test_image=np.expand_dims(test_image,axis=0)
result=model.predict(test_image)
result=np.argmax(result)
y.append(result)
Name=txt[result]
plt.imshow(open_image)
plt.title(f'Real: {Orginal} \n Predicted: {Name}')
plt.axis('off')
plt.show()
test_image= '/content/Dataset/val/Gudi_rotten/IMG20201109095426_00.jpg'
Orginal = "Gudi_Rotten"
open_image=Image.open(test_image)
test_image=image.load_img(test_image,target_size=(150,150))
test_image=image.img_to_array(test_image)
test_image=test_image/255.0
test_image=np.expand_dims(test_image,axis=0)
result=model.predict(test_image)
result=np.argmax(result)
y.append(result)
Name=txt[result]
plt.imshow(open_image)
plt.title(f'Real: {Orginal} \n Predicted: {Name}')
plt.axis('off')
plt.show()
test_image= '/content/Dataset/val/leaf_burn/IMG20201109120340_00.jpg'
Orginal = "Leaf Burn"
open_image=Image.open(test_image)
test_image=image.load_img(test_image,target_size=(150,150))
test_image=image.img_to_array(test_image)
test_image=test_image/255.0
test_image=np.expand_dims(test_image,axis=0)
result=model.predict(test_image)
result=np.argmax(result)
y.append(result)
Name=txt[result]
plt.imshow(open_image)
plt.title(f'Real: {Orginal} \n Predicted: {Name}')
plt.axis('off')
plt.show()
test_image= '/content/Dataset/val/neck_blast_paddy/IMG20201108163604_00.jpg'
Orginal = "<NAME>"
open_image=Image.open(test_image)
test_image=image.load_img(test_image,target_size=(150,150))
test_image=image.img_to_array(test_image)
test_image=test_image/255.0
test_image=np.expand_dims(test_image,axis=0)
result=model.predict(test_image)
result=np.argmax(result)
y.append(result)
Name=txt[result]
plt.imshow(open_image)
plt.title(f'Real: {Orginal} \n Predicted: {Name}')
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>The model has predicts all the five classes successfully !!!<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1519018708957998, 'fraction_numerical': 0.031346797175071245, 'mean_word_length': 8.892156862745098, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '85638', 'license': 'Apache-2.0', 'n_tokens_mistral': 3282, 'n_tokens_neox': 3061, 'n_words': 455, 'path': 'Rice Diseases Classification and Prediction/Notebook/Rice_Leaf_Diseases_Classification_and_Prediction_92_accuracy.ipynb', 'repo_name': 'NavinBondade/Rice-Leaf-Diseases-Detection-And-Classification', 'size': '8081'}