Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cassava Leaf Disease Classification #547

Merged
merged 11 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cassava Leaf Disease Classification/Dataset/LInk.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://drive.google.com/drive/folders/1KLfnDtRlBaVWG_5xMfst-d9M9GhYTLno

Uploaded due to GITHUB limit.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
294 changes: 294 additions & 0 deletions Cassava Leaf Disease Classification/Model/CNN.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "TPU"
},
"cells": [
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 0
},
"id": "ZgKNAgwylDkM",
"outputId": "a33dd5ae-ad9a-4939-e18a-506f5215b336"
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import tensorflow as tf\n",
"import re\n",
"from tensorflow.keras.preprocessing.text import one_hot\n",
"import matplotlib.pyplot as py\n",
"from tensorflow.keras.models import Sequential,load_model\n",
"from sklearn.model_selection import train_test_split\n"
],
"metadata": {
"id": "zq1juT-2nfgA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"\n",
"main_directory = '/content/drive/MyDrive/data'\n",
"# Define image dimensions and batch size\n",
"img_height, img_width = 224, 224\n",
"batch_size = 32\n",
"\n",
"# Use ImageDataGenerator for data augmentation and normalization\n",
"datagen = ImageDataGenerator(\n",
" rescale=1./255,\n",
" shear_range=0.2,\n",
" zoom_range=0.2,\n",
" horizontal_flip=True,\n",
" validation_split=0.2 # Set the validation split\n",
")\n",
"\n",
"# Create training data generator\n",
"train_generator = datagen.flow_from_directory(\n",
" main_directory,\n",
" target_size=(img_height, img_width),\n",
" batch_size=batch_size,\n",
" class_mode='categorical',\n",
" subset='training' # Specify 'training' for training data\n",
")\n",
"\n",
"# Create validation data generator\n",
"validation_generator = datagen.flow_from_directory(\n",
" main_directory,\n",
" target_size=(img_height, img_width),\n",
" batch_size=batch_size,\n",
" class_mode='categorical',\n",
" subset='validation' # Specify 'validation' for validation data\n",
")\n",
"\n",
"# Define your CNN model using TensorFlow's Keras API\n",
"model = tf.keras.models.Sequential([\n",
" tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)),\n",
" tf.keras.layers.MaxPooling2D(2, 2),\n",
" tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n",
" tf.keras.layers.MaxPooling2D(2, 2),\n",
" tf.keras.layers.Flatten(),\n",
" tf.keras.layers.Dense(128, activation='relu'),\n",
" tf.keras.layers.Dense(5, activation='softmax') # 4 classes for diseases\n",
"])\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n",
"\n",
"# Train the model\n",
"model.fit(\n",
" train_generator,\n",
" steps_per_epoch=train_generator.samples // batch_size,\n",
" validation_data=validation_generator,\n",
" validation_steps=validation_generator.samples // batch_size,\n",
" epochs=10 # Set the number of epochs\n",
")\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ohjGWycknjcT",
"outputId": "26181de4-4136-45e4-b697-0a5b423cf7d2"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Found 17120 images belonging to 5 classes.\n",
"Found 4277 images belonging to 5 classes.\n",
"Epoch 1/10\n",
"535/535 [==============================] - 445s 826ms/step - loss: 1.1809 - accuracy: 0.6193 - val_loss: 1.0219 - val_accuracy: 0.6262\n",
"Epoch 2/10\n",
"535/535 [==============================] - 481s 899ms/step - loss: 0.9930 - accuracy: 0.6361 - val_loss: 0.9264 - val_accuracy: 0.6527\n",
"Epoch 3/10\n",
"535/535 [==============================] - 425s 795ms/step - loss: 0.9094 - accuracy: 0.6527 - val_loss: 0.8733 - val_accuracy: 0.6624\n",
"Epoch 4/10\n",
"535/535 [==============================] - 423s 791ms/step - loss: 0.8665 - accuracy: 0.6751 - val_loss: 0.8820 - val_accuracy: 0.6703\n",
"Epoch 5/10\n",
"535/535 [==============================] - 419s 784ms/step - loss: 0.8286 - accuracy: 0.6883 - val_loss: 0.8381 - val_accuracy: 0.6805\n",
"Epoch 6/10\n",
"535/535 [==============================] - 477s 892ms/step - loss: 0.8065 - accuracy: 0.6976 - val_loss: 0.8142 - val_accuracy: 0.6823\n",
"Epoch 7/10\n",
"535/535 [==============================] - 408s 763ms/step - loss: 0.7731 - accuracy: 0.7116 - val_loss: 0.8000 - val_accuracy: 0.6969\n",
"Epoch 8/10\n",
"535/535 [==============================] - 411s 769ms/step - loss: 0.7556 - accuracy: 0.7154 - val_loss: 0.8063 - val_accuracy: 0.6969\n",
"Epoch 9/10\n",
"535/535 [==============================] - 403s 753ms/step - loss: 0.7326 - accuracy: 0.7245 - val_loss: 0.8010 - val_accuracy: 0.6924\n",
"Epoch 10/10\n",
"535/535 [==============================] - 406s 759ms/step - loss: 0.7127 - accuracy: 0.7324 - val_loss: 0.7961 - val_accuracy: 0.7023\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<keras.src.callbacks.History at 0x7e6f006ac1f0>"
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"source": [
"model.save(\"/content/drive/MyDrive\")\n"
],
"metadata": {
"id": "Tv3WJTGC9Dt1"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"evaluation_result = model.evaluate(validation_generator, steps=validation_generator.samples // batch_size)\n",
"print(\"Validation Loss:\", evaluation_result[0])\n",
"print(\"Validation Accuracy:\", evaluation_result[1])"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tGE3OAL4_YWp",
"outputId": "66ee96e8-5664-49bf-b9b8-71ec6b9f62b9"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"133/133 [==============================] - 84s 630ms/step - loss: 0.8085 - accuracy: 0.7002\n",
"Validation Loss: 0.8085159063339233\n",
"Validation Accuracy: 0.7001879811286926\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import tensorflow as tf\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"from tensorflow.keras.applications import VGG16\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense, Flatten, Dropout\n",
"\n",
"\n",
"# Load pre-trained VGG16 model\n",
"base_model = VGG16(weights='imagenet', include_top=False, input_shape=(img_height, img_width, 3))\n",
"\n",
"# Freeze the layers of the pre-trained model\n",
"for layer in base_model.layers:\n",
" layer.trainable = False\n",
"\n",
"# Create a new model on top of the pre-trained model\n",
"model = Sequential()\n",
"model.add(base_model)\n",
"model.add(Flatten())\n",
"model.add(Dense(128, activation='relu'))\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(5, activation='softmax')) # 5 classes for your task\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n",
"\n",
"# Train the model\n",
"model.fit(\n",
" train_generator,\n",
" steps_per_epoch=train_generator.samples // batch_size,\n",
" validation_data=validation_generator,\n",
" validation_steps=validation_generator.samples // batch_size,\n",
" epochs=10 # Set the number of epochs\n",
")\n",
"\n",
"# Evaluate the model on the validation set\n",
"evaluation_result = model.evaluate(validation_generator, steps=validation_generator.samples // batch_size)\n",
"print(\"Validation Loss:\", evaluation_result[0])\n",
"print(\"Validation Accuracy:\", evaluation_result[1])\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LUKlFJfZ-66C",
"outputId": "74a256ac-9a0a-411e-f58c-8158927c5d72"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5\n",
"58889256/58889256 [==============================] - 2s 0us/step\n",
"Epoch 1/10\n",
"535/535 [==============================] - 442s 809ms/step - loss: 1.1297 - accuracy: 0.6065 - val_loss: 0.9465 - val_accuracy: 0.6147\n",
"Epoch 2/10\n",
"535/535 [==============================] - 488s 913ms/step - loss: 1.0326 - accuracy: 0.6147 - val_loss: 0.9351 - val_accuracy: 0.6163\n",
"Epoch 3/10\n",
"535/535 [==============================] - 434s 812ms/step - loss: 1.0177 - accuracy: 0.6149 - val_loss: 0.9210 - val_accuracy: 0.6161\n",
"Epoch 4/10\n",
"535/535 [==============================] - 432s 807ms/step - loss: 1.0118 - accuracy: 0.6149 - val_loss: 0.9234 - val_accuracy: 0.6147\n",
"Epoch 5/10\n",
"535/535 [==============================] - 480s 898ms/step - loss: 1.0028 - accuracy: 0.6149 - val_loss: 0.9272 - val_accuracy: 0.6151\n",
"Epoch 6/10\n",
"535/535 [==============================] - 415s 776ms/step - loss: 0.9988 - accuracy: 0.6149 - val_loss: 0.9163 - val_accuracy: 0.6158\n",
"Epoch 7/10\n",
"535/535 [==============================] - 479s 896ms/step - loss: 0.9945 - accuracy: 0.6149 - val_loss: 0.9155 - val_accuracy: 0.6147\n",
"Epoch 8/10\n",
"535/535 [==============================] - 420s 784ms/step - loss: 0.9990 - accuracy: 0.6149 - val_loss: 0.9119 - val_accuracy: 0.6163\n",
"Epoch 9/10\n",
"535/535 [==============================] - 472s 883ms/step - loss: 0.9939 - accuracy: 0.6149 - val_loss: 0.9141 - val_accuracy: 0.6156\n",
"Epoch 10/10\n",
"535/535 [==============================] - 415s 776ms/step - loss: 0.9950 - accuracy: 0.6147 - val_loss: 0.9141 - val_accuracy: 0.6140\n",
"133/133 [==============================] - 84s 629ms/step - loss: 0.9111 - accuracy: 0.6151\n",
"Validation Loss: 0.9110695123672485\n",
"Validation Accuracy: 0.6151315569877625\n"
]
}
]
}
]
}
Loading
Loading