Professional Documents
Culture Documents
ipynb - Colaboratory
Objective
- To classify the Collected Wheat_leaf image as healthy, Strip_rust,Leaf_rust,Powdery,Septoria,or TanSpot
- Multiclass classification problem
-bla bla....
Data Sources
-The dataset is originally collected from Research_Field @ Kulumsa_Agricultural_Research_Institute.
- A few dataset also collected from Previously collected by Researchers/Experts @KARC
DataSet_Folder structure
DataSet
Leaf_rust
LR1.jpg
LR2.jpg
...
Strip_rust
SR1.jpg
SR2.jpg
Automatic saving failed. This file was updated remotely or in another tab. Show diff
...
Powdery
Powd1.jpg
Powd2.jpg
...
Septoria
Sept1.jpg
Sept2.jpg
...
TanSpot
Tan1.jpg
Tan2.jpg
...
Healthy
Hlthy1.jpg
Hlthy2.jpg
...
Dependencies
import os
import numpy as np
import seaborn as sns
import pathlib
import PIL
from glob import glob
import matplotlib.pyplot as plt
from collections import Counter
from sklearn.metrics import confusion_matrix, classification_report
from tensorflow.keras.models import Sequential,Model
from tensorflow.keras.layers import Dense,Flatten,Dropout,Conv2D, MaxPooling2D, GlobalAveragePooling2D,Input
from tensorflow.keras.applications import ResNet50, resnet50, Xception, xception
from tensorflow.keras.applications.xception import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 1/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
from tensorflow.keras.losses import CategoricalCrossentropy
import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img, img_to_array
print(tf.__version__)
############ settings ############
2.9.2
Mounted at /content/drive
data_dir = "/content/drive/MyDrive/My_Recent_Data/WheatLeaf_Label_Resiz_Augm"
batch_size = 32
img_height = 200
img_width = 200
train_path = pathlib.Path(data_dir)
image_count = len(list(train_path.glob('*/*')))
print(image_count)
4554
Data preprocessing
Automatic saving failed. This file was updated remotely or in another tab. Show diff
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
EDA
# Target distribution
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 2/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
## Sample images
class_names = train_ds.class_names
import matplotlib.pyplot as plt
plt.figure(figsize=(17, 17))
for images, labels in train_ds.take(1):
for i in range(6):
ax = plt.subplot(4, 6, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
Model Training
Automatic saving failed. This file was updated remotely or in another tab. Show diff
# Configure the dataset for performance
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
# Model architecture
num_classes = 6
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(img_height, img_width, 3)),
tf.keras.layers.experimental.preprocessing.Rescaling(1./255),
tf.keras.layers.Conv2D(16, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(64, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(num_classes, activation='softmax')
])
model.summary()
base_model.trainable=False
input_img=Input(shape=(200,200,3),name='input_image')
resnet=base_model(input_img,training=False)
resnet=GlobalAveragePooling2D()(resnet)
resnet=Dense(6,activation='softmax',name='classif_layer')(resnet)
resnet=Dropout(rate=0.2,name='drop_out')(resnet)
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 3/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
resnet_model=Model(inputs=input_img,outputs=resnet,name='WLD_Prediction_Model')
resnet_model.summary()
Model: "WLD_Prediction_Model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_image (InputLayer) [(None, 200, 200, 3)] 0
=================================================================
Total params: 23,600,006
Trainable params: 12,294
Non-trainable params: 23,587,712
_________________________________________________________________
model.summary()
input_img=Input(shape=(200,200,3),name='input_image')
xception=base_model(input_img,training=False)
xception=GlobalAveragePooling2D()(xception)
#xception=Dense(512,activation='relu')(xception)
xception=Dropout(rate=0.2,name='drop_out')(xception)
xception=Dense(6,activation='softmax',name='classif_layer')(xception)
xception_model=Model(inputs=input_img,outputs=xception,name='WLD_Prediction_Model')
xception_model.summary()
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 4/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
=================================================================
Total params: 20,873,774
Trainable params: 12,294
Non-trainable params: 20,861,480
_________________________________________________________________
base_learning_rate = 0.001
resnet_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
earlystop_callback = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy',
min_delta=0.0001,
# patience=5
)
history = resnet_model.fit(train_ds,
validation_data=val_ds,
epochs=20,
# callbacks=[earlystop_callback]
)
Epoch 1/20
114/114 [==============================] - 1917s 16s/step - loss: 1.0149 - accuracy: 0.6358 - val_loss: 0.6820 - val_accuracy: 0.7758
Automatic
Epochsaving
2/20 failed. This file was updated remotely or in another tab. Show diff
114/114 [==============================] - 13s 116ms/step - loss: 0.5432 - accuracy: 0.8304 - val_loss: 0.5335 - val_accuracy: 0.8374
Epoch 3/20
114/114 [==============================] - 13s 114ms/step - loss: 0.4157 - accuracy: 0.8729 - val_loss: 0.4643 - val_accuracy: 0.8549
Epoch 4/20
114/114 [==============================] - 13s 113ms/step - loss: 0.3393 - accuracy: 0.9018 - val_loss: 0.4235 - val_accuracy: 0.8670
Epoch 5/20
114/114 [==============================] - 13s 112ms/step - loss: 0.2863 - accuracy: 0.9229 - val_loss: 0.3959 - val_accuracy: 0.8758
Epoch 6/20
114/114 [==============================] - 13s 113ms/step - loss: 0.2470 - accuracy: 0.9341 - val_loss: 0.3758 - val_accuracy: 0.8802
Epoch 7/20
114/114 [==============================] - 13s 113ms/step - loss: 0.2163 - accuracy: 0.9457 - val_loss: 0.3604 - val_accuracy: 0.8879
Epoch 8/20
114/114 [==============================] - 13s 113ms/step - loss: 0.1918 - accuracy: 0.9517 - val_loss: 0.3484 - val_accuracy: 0.8934
Epoch 9/20
114/114 [==============================] - 13s 113ms/step - loss: 0.1716 - accuracy: 0.9621 - val_loss: 0.3389 - val_accuracy: 0.8989
Epoch 10/20
114/114 [==============================] - 13s 112ms/step - loss: 0.1547 - accuracy: 0.9682 - val_loss: 0.3314 - val_accuracy: 0.9022
Epoch 11/20
114/114 [==============================] - 13s 112ms/step - loss: 0.1404 - accuracy: 0.9731 - val_loss: 0.3252 - val_accuracy: 0.9022
Epoch 12/20
114/114 [==============================] - 13s 112ms/step - loss: 0.1281 - accuracy: 0.9789 - val_loss: 0.3201 - val_accuracy: 0.9033
Epoch 13/20
114/114 [==============================] - 13s 112ms/step - loss: 0.1174 - accuracy: 0.9808 - val_loss: 0.3159 - val_accuracy: 0.9033
Epoch 14/20
114/114 [==============================] - 13s 113ms/step - loss: 0.1081 - accuracy: 0.9827 - val_loss: 0.3123 - val_accuracy: 0.9055
Epoch 15/20
114/114 [==============================] - 13s 116ms/step - loss: 0.0999 - accuracy: 0.9849 - val_loss: 0.3093 - val_accuracy: 0.9077
Epoch 16/20
114/114 [==============================] - 13s 114ms/step - loss: 0.0926 - accuracy: 0.9863 - val_loss: 0.3068 - val_accuracy: 0.9055
Epoch 17/20
114/114 [==============================] - 13s 112ms/step - loss: 0.0861 - accuracy: 0.9877 - val_loss: 0.3048 - val_accuracy: 0.9055
Epoch 18/20
114/114 [==============================] - 13s 114ms/step - loss: 0.0803 - accuracy: 0.9879 - val_loss: 0.3033 - val_accuracy: 0.9077
Epoch 19/20
114/114 [==============================] - 13s 116ms/step - loss: 0.0751 - accuracy: 0.9896 - val_loss: 0.3022 - val_accuracy: 0.9088
Epoch 20/20
114/114 [==============================] - 13s 115ms/step - loss: 0.0705 - accuracy: 0.9904 - val_loss: 0.3015 - val_accuracy: 0.9099
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 5/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
loss, acc = resnet_model.evaluate(train_ds, batch_size=batch_size)
print("Train accuracy :", round(acc, 2))
print("Train loss :", round(loss,2))
Model Evaluation
Feature Extraction
train_loss = history.history['loss']
train_acc = history.history['accuracy']
valid_loss = history.history['val_loss']
valid_acc = history.history['val_accuracy']
# Accuracy plots
plt.figure(figsize=(8, 4))
plt.plot(train_acc, color='green', linestyle='-', label='train accuracy')
plt.plot(valid_acc, color='blue', linestyle='-', label='val accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
#Automatic
loss plots
saving failed. This file was updated remotely or in another tab. Show diff
plt.figure(figsize=(8, 4))
plt.plot(train_loss, color='orange', linestyle='-', label='train loss')
plt.plot(valid_loss, color='red', linestyle='-', label='val loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 6/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
y_pred.append(np.argmax(preds, axis = - 1))
print(classification_report(correct_labels, predicted_labels,target_names=class_names))
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 7/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
Tanspot 0.96 0.91 0.94 139
Fine Tuning
base_model.trainable=False
input_img=Input(shape=(200,200,3),name='input_image')
resnet=base_model(input_img,training=False)
resnet=GlobalAveragePooling2D()(resnet)
resnet=Dropout(rate=0.2,name='drop_out')(resnet)
resnet=Dense(6,activation='softmax',name='classif_layer')(resnet)
resnet_model=Model(inputs=input_img,outputs=resnet,name='WLD_Prediction_Model')
resnet_model.summary()
Model: "WLD_Prediction_Model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_image (InputLayer) [(None, 200, 200, 3)] 0
Automatic saving failed. This file was updated remotely or in another tab. Show diff
resnet50 (Functional) (None, 7, 7, 2048) 23587712
=================================================================
Total params: 23,600,006
Trainable params: 12,294
Non-trainable params: 23,587,712
_________________________________________________________________
base_model.trainable = True
# Let's take a look to see how many layers are in the base model
print("Number of layers in the base model: ", len(base_model.layers))
resnet_model.summary()
Model: "WLD_Prediction_Model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_image (InputLayer) [(None, 200, 200, 3)] 0
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 8/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
=================================================================
Total params: 23,600,006
Trainable params: 10,002,438
Non-trainable params: 13,597,568
_________________________________________________________________
base_learning_rate = 0.0001
resnet_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
earlystop_callback = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy',
min_delta=0.0001,
#patience=5
)
history = resnet_model.fit(train_ds,
validation_data=val_ds,
epochs=20,
#callbacks=[earlystop_callback]
)
Epoch 1/20
114/114 [==============================] - 22s 156ms/step - loss: 0.7808 - accuracy: 0.7234 - val_loss: 0.3717 - val_accuracy: 0.8692
Epoch 2/20
114/114 [==============================] - 17s 150ms/step - loss: 0.2477 - accuracy: 0.9160 - val_loss: 0.2919 - val_accuracy: 0.8934
Epoch 3/20
114/114 [==============================] - 18s 152ms/step - loss: 0.1239 - accuracy: 0.9569 - val_loss: 0.3970 - val_accuracy: 0.8780
Epoch 4/20
114/114 [==============================] - 18s 151ms/step - loss: 0.0723 - accuracy: 0.9767 - val_loss: 0.2983 - val_accuracy: 0.9154
Automatic
Epochsaving
5/20 failed. This file was updated remotely or in another tab. Show diff
114/114 [==============================] - 18s 151ms/step - loss: 0.0630 - accuracy: 0.9800 - val_loss: 0.2993 - val_accuracy: 0.9077
Epoch 6/20
114/114 [==============================] - 17s 149ms/step - loss: 0.0519 - accuracy: 0.9816 - val_loss: 0.3018 - val_accuracy: 0.9330
Epoch 7/20
114/114 [==============================] - 17s 150ms/step - loss: 0.0271 - accuracy: 0.9901 - val_loss: 0.3588 - val_accuracy: 0.9077
Epoch 8/20
114/114 [==============================] - 19s 167ms/step - loss: 0.0189 - accuracy: 0.9909 - val_loss: 0.3371 - val_accuracy: 0.9341
Epoch 9/20
114/114 [==============================] - 17s 147ms/step - loss: 0.0166 - accuracy: 0.9912 - val_loss: 0.3597 - val_accuracy: 0.9286
Epoch 10/20
114/114 [==============================] - 17s 149ms/step - loss: 0.0270 - accuracy: 0.9904 - val_loss: 0.4720 - val_accuracy: 0.9110
Epoch 11/20
114/114 [==============================] - 18s 151ms/step - loss: 0.0690 - accuracy: 0.9737 - val_loss: 0.3185 - val_accuracy: 0.9264
Epoch 12/20
114/114 [==============================] - 17s 148ms/step - loss: 0.0787 - accuracy: 0.9731 - val_loss: 0.4264 - val_accuracy: 0.8989
Epoch 13/20
114/114 [==============================] - 17s 148ms/step - loss: 0.0306 - accuracy: 0.9871 - val_loss: 0.3037 - val_accuracy: 0.9253
Epoch 14/20
114/114 [==============================] - 17s 150ms/step - loss: 0.0340 - accuracy: 0.9868 - val_loss: 0.3596 - val_accuracy: 0.9286
Epoch 15/20
114/114 [==============================] - 18s 151ms/step - loss: 0.0389 - accuracy: 0.9874 - val_loss: 0.3287 - val_accuracy: 0.9209
Epoch 16/20
114/114 [==============================] - 18s 151ms/step - loss: 0.0359 - accuracy: 0.9855 - val_loss: 0.3251 - val_accuracy: 0.9330
Epoch 17/20
114/114 [==============================] - 17s 149ms/step - loss: 0.0451 - accuracy: 0.9868 - val_loss: 0.4635 - val_accuracy: 0.9209
Epoch 18/20
114/114 [==============================] - 17s 150ms/step - loss: 0.0329 - accuracy: 0.9871 - val_loss: 0.3353 - val_accuracy: 0.9253
Epoch 19/20
114/114 [==============================] - 17s 149ms/step - loss: 0.0168 - accuracy: 0.9912 - val_loss: 0.3786 - val_accuracy: 0.9308
Epoch 20/20
114/114 [==============================] - 18s 152ms/step - loss: 0.0117 - accuracy: 0.9931 - val_loss: 0.3196 - val_accuracy: 0.9407
resnet_model.save("ResNet_Model_9953_9407.h5")
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 9/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
train_loss = history.history['loss']
train_acc = history.history['accuracy']
valid_loss = history.history['val_loss']
valid_acc = history.history['val_accuracy']
# Accuracy plots
plt.figure(figsize=(8, 4))
plt.plot(train_acc, color='green', linestyle='-', label='train accuracy')
plt.plot(valid_acc, color='blue', linestyle='-', label='val accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
# loss plots
plt.figure(figsize=(8, 4))
plt.plot(train_loss, color='orange', linestyle='-', label='train loss')
plt.plot(valid_loss, color='red', linestyle='-', label='val loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
Automatic saving failed. This file was updated remotely or in another tab.
plt.show() Show diff
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 10/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
print(classification_report(correct_labels, predicted_labels,target_names=class_names))
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 11/12
2/8/23, 2:13 AM WLeaf Disease Classification_ResNet50.ipynb - Colaboratory
Automatic saving failed. This file was updated remotely or in another tab. Show diff
https://colab.research.google.com/drive/1zGDYxQMqj0aijE6TtdVQHbRVSvfn0AdS#scrollTo=NmQLaeywDyD-&printMode=true 12/12