{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":1097.067837,"end_time":"2024-03-13T21:54:43.137269","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-03-13T21:36:26.069432","version":"2.5.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"\n# <p style=\"text-align:center;font-family:Arial;font-weight: bold;font-size:120%\">Cassava Leaf Disease Classification </p>","metadata":{"papermill":{"duration":0.018648,"end_time":"2024-03-13T21:36:28.836468","exception":false,"start_time":"2024-03-13T21:36:28.81782","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"\n## Team Members \n- Samir Mohamed Samir \n- Seif Eldein Abdelfatah Mahmoud \n- Ahmed Hussien Ibrahim Yousse \n\n\n","metadata":{"papermill":{"duration":0.018351,"end_time":"2024-03-13T21:36:28.907889","exception":false,"start_time":"2024-03-13T21:36:28.889538","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Problem Statement\n\n- Teams of 2 students(minimum) to 3 students(maximum)\n- You can choose to work locally (Download the data), use Colab (use Kaggle API), or inside Kaggle Notebooks (No Downloads needed).\n- You need to submit a Notebook to this assignment (1 per team) with your experiments.\n- Two mertics:\n    Inside the notebook, you need to split the dataset into 70% train, 20% validation and 10% test\n    and report the resulted accuracy on each subset.\n    (Optional) Report Accuracy on Kaggle testset (Submit your model to Kaggle).\n\n- Datset Link: https://www.kaggle.com/c/cassava-leaf-disease-classification/overview","metadata":{"papermill":{"duration":0.017555,"end_time":"2024-03-13T21:36:29.154213","exception":false,"start_time":"2024-03-13T21:36:29.136658","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"\n\n\n<body>\n\n  <h1>Main Information:</h1>\n\n  <p>\n    Cassava diseases refer to various plant illnesses that impact cassava (<em>Manihot esculenta</em>), a staple\n    food crop for millions of people in tropical and subtropical regions. Cassava, a starchy root crop, serves as a\n    vital source of carbohydrates in the diet of many people, particularly in Africa, Asia, and South America.\n  </p>\n\n</body>\n\n\n\n\n_________\n\n<table>\n  <tr>\n    <th>Disease</th>\n    <th>Cause</th>\n    <th>Symptoms</th>\n  </tr>\n  <tr>\n    <td>Cassava Mosaic Disease (CMD)</td>\n    <td>Caused by various species of begomoviruses</td>\n    <td>🔍 Mosaic patterns, yellowing, stunting, distorted growth. Severe infections can lead to complete crop loss.</td>\n  </tr>\n  <tr>\n    <td>Cassava Brown Streak Disease (CBSD)</td>\n    <td>Caused by two distinct species of viruses (Potyviridae family)</td>\n    <td>🍠 Necrotic lesions on storage roots, yellowing, leaf symptoms.</td>\n  </tr>\n  <tr>\n    <td>Cassava Bacterial Blight (CBB)</td>\n    <td>Caused by Xanthomonas axonopodis pv. manihotis</td>\n    <td>💧 Water-soaked lesions on leaves, stems, and petioles. Can lead to wilting, defoliation, and plant death.</td>\n  </tr>\n  <tr>\n    <td>Cassava Anthracnose Disease (CAD)</td>\n    <td>Caused by Colletotrichum gloeosporioides</td>\n    <td>⚫ Dark, sunken lesions on leaves, stems, and roots. Advanced stages can lead to reduced yields.</td>\n  </tr>\n  <tr>\n    <td>Cassava Bacterial Wilt (CBW)</td>\n    <td>Caused by Clavibacter michiganensis subsp. michiganensis</td>\n    <td>🌱 Wilting, yellowing, necrosis. Vascular tissues may become discolored, leading to plant death.</td>\n  </tr>\n</table>\n","metadata":{"papermill":{"duration":0.018148,"end_time":"2024-03-13T21:36:29.049094","exception":false,"start_time":"2024-03-13T21:36:29.030946","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"\n\n<head>\n  <style>\n    body {\n      font-family: 'Arial', sans-serif;\n    }\n\n    h1 {\n      color: #2ecc71; /* Green color */\n    }\n\n    p {\n      margin-bottom: 20px;\n    }\n  </style>\n</head>\n\n<body>\n\n  <h1>CropNet: Cassava Guardian</h1>\n\n  <p>\n    Cassava consists of leaf images for the cassava plant, illustrating healthy conditions, as well as four disease\n    conditions: Cassava Mosaic Disease (CMD), Cassava Bacterial Blight (CBB), Cassava Green Mite (CGM), and Cassava Brown\n    Streak Disease (CBSD). The dataset comprises a total of 9,430 labeled images.\n  </p>\n\n  <p>\n    <strong>Split:</strong>\n    <br>\n    - Training Set: 5,656 images\n    <br>\n    - Test Set: 1,885 images\n    <br>\n    - Validation Set: 1,889 images\n  </p>\n\n  <p>\n    <strong>Class Distribution:</strong>\n    <br>\n    - CMD and CBSD classes account for 72% of the images, but the dataset has unbalanced class distribution.\n  </p>\n\n  <p>\n    <strong>Dataset Information:</strong>\n    <br>\n    - <strong>Homepage:</strong> <a href=\"https://www.kaggle.com/c/cassava-disease/overview\" target=\"_blank\">Cassava Disease Detection Kaggle Competition</a>\n    <br>\n    - <strong>Download Size:</strong> 1.26 GiB\n    <br>\n    - <strong>File Format:</strong> TFRecord\n    <br>\n    - <strong>Features:</strong>\n    <ul>\n      <li>'image': Image data with shape (None, None, 3) and dtype uint8.</li>\n      <li>'image/filename': Text data with the filename of the image.</li>\n      <li>'label': ClassLabel data with shape () and dtype int64, covering 5 classes.</li>\n    </ul>\n  </p>\n\n</body>","metadata":{"papermill":{"duration":0.017111,"end_time":"2024-03-13T21:36:29.084438","exception":false,"start_time":"2024-03-13T21:36:29.067327","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"---","metadata":{"papermill":{"duration":0.017904,"end_time":"2024-03-13T21:36:29.18976","exception":false,"start_time":"2024-03-13T21:36:29.171856","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"\n## Importing The Main Libraries","metadata":{"papermill":{"duration":0.017525,"end_time":"2024-03-13T21:36:29.224996","exception":false,"start_time":"2024-03-13T21:36:29.207471","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras import layers,models\nfrom sklearn.metrics import accuracy_score\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.efficientnet import EfficientNetB3\nfrom tensorflow.keras import models, layers, optimizers\nimport warnings\nwarnings.simplefilter(\"ignore\")\nfrom PIL import Image\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:45:24.581348Z","iopub.execute_input":"2025-12-15T12:45:24.581663Z","iopub.status.idle":"2025-12-15T12:45:24.588401Z","shell.execute_reply.started":"2025-12-15T12:45:24.581641Z","shell.execute_reply":"2025-12-15T12:45:24.586992Z"},"papermill":{"duration":13.484245,"end_time":"2024-03-13T21:36:42.727363","exception":false,"start_time":"2024-03-13T21:36:29.243118","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n \n## Loading The Dataset\n\n","metadata":{"papermill":{"duration":0.019401,"end_time":"2024-03-13T21:36:42.767928","exception":false,"start_time":"2024-03-13T21:36:42.748527","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import os\nDir = '../input/cassava-leaf-disease-classification'\nos.listdir(Dir)","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:45:29.014498Z","iopub.execute_input":"2025-12-15T12:45:29.014838Z","iopub.status.idle":"2025-12-15T12:45:29.023735Z","shell.execute_reply.started":"2025-12-15T12:45:29.014817Z","shell.execute_reply":"2025-12-15T12:45:29.022661Z"},"papermill":{"duration":0.036923,"end_time":"2024-03-13T21:36:42.824471","exception":false,"start_time":"2024-03-13T21:36:42.787548","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## EDA\n","metadata":{"papermill":{"duration":0.019108,"end_time":"2024-03-13T21:36:42.863221","exception":false,"start_time":"2024-03-13T21:36:42.844113","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(len(os.listdir('../input/cassava-leaf-disease-classification/train_images')))","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:45:41.621992Z","iopub.execute_input":"2025-12-15T12:45:41.622511Z","iopub.status.idle":"2025-12-15T12:45:41.937996Z","shell.execute_reply.started":"2025-12-15T12:45:41.622489Z","shell.execute_reply":"2025-12-15T12:45:41.936846Z"},"papermill":{"duration":0.324393,"end_time":"2024-03-13T21:36:43.206586","exception":false,"start_time":"2024-03-13T21:36:42.882193","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(os.listdir('../input/cassava-leaf-disease-classification/test_images')))","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:45:44.895088Z","iopub.execute_input":"2025-12-15T12:45:44.895414Z","iopub.status.idle":"2025-12-15T12:45:44.905756Z","shell.execute_reply.started":"2025-12-15T12:45:44.895394Z","shell.execute_reply":"2025-12-15T12:45:44.904715Z"},"papermill":{"duration":0.030888,"end_time":"2024-03-13T21:36:43.256933","exception":false,"start_time":"2024-03-13T21:36:43.226045","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:45:49.015132Z","iopub.execute_input":"2025-12-15T12:45:49.01609Z","iopub.status.idle":"2025-12-15T12:45:49.109232Z","shell.execute_reply.started":"2025-12-15T12:45:49.016057Z","shell.execute_reply":"2025-12-15T12:45:49.10786Z"},"papermill":{"duration":0.058183,"end_time":"2024-03-13T21:36:43.335263","exception":false,"start_time":"2024-03-13T21:36:43.27708","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nsns.countplot(train_df , x = 'label')","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:45:53.115108Z","iopub.execute_input":"2025-12-15T12:45:53.115812Z","iopub.status.idle":"2025-12-15T12:45:53.541574Z","shell.execute_reply.started":"2025-12-15T12:45:53.115786Z","shell.execute_reply":"2025-12-15T12:45:53.539787Z"},"papermill":{"duration":0.301889,"end_time":"2024-03-13T21:36:43.656845","exception":false,"start_time":"2024-03-13T21:36:43.354956","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"There is an imbalance in the data.","metadata":{"papermill":{"duration":0.018498,"end_time":"2024-03-13T21:36:43.698685","exception":false,"start_time":"2024-03-13T21:36:43.680187","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:46:27.099681Z","iopub.execute_input":"2025-12-15T12:46:27.100005Z","iopub.status.idle":"2025-12-15T12:46:27.112723Z","shell.execute_reply.started":"2025-12-15T12:46:27.099982Z","shell.execute_reply":"2025-12-15T12:46:27.11178Z"},"papermill":{"duration":0.035456,"end_time":"2024-03-13T21:36:43.819726","exception":false,"start_time":"2024-03-13T21:36:43.78427","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.round((train_df['label'].value_counts()/len(train_df['label']))*100, 2)","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:46:30.336097Z","iopub.execute_input":"2025-12-15T12:46:30.337533Z","iopub.status.idle":"2025-12-15T12:46:30.347925Z","shell.execute_reply.started":"2025-12-15T12:46:30.337484Z","shell.execute_reply":"2025-12-15T12:46:30.346883Z"},"papermill":{"duration":0.031676,"end_time":"2024-03-13T21:36:43.870385","exception":false,"start_time":"2024-03-13T21:36:43.838709","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_df)","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:46:33.659037Z","iopub.execute_input":"2025-12-15T12:46:33.659425Z","iopub.status.idle":"2025-12-15T12:46:33.666522Z","shell.execute_reply.started":"2025-12-15T12:46:33.659403Z","shell.execute_reply":"2025-12-15T12:46:33.665249Z"},"papermill":{"duration":0.028076,"end_time":"2024-03-13T21:36:43.91785","exception":false,"start_time":"2024-03-13T21:36:43.889774","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n## Mapping The Cassava Diseases\n","metadata":{"papermill":{"duration":0.019138,"end_time":"2024-03-13T21:36:43.956639","exception":false,"start_time":"2024-03-13T21:36:43.937501","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import json\nwith open('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json') as file:\n    print(json.dumps(json.loads(file.read()), indent=4))","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:46:49.562135Z","iopub.execute_input":"2025-12-15T12:46:49.562492Z","iopub.status.idle":"2025-12-15T12:46:49.574167Z","shell.execute_reply.started":"2025-12-15T12:46:49.562471Z","shell.execute_reply":"2025-12-15T12:46:49.573252Z"},"papermill":{"duration":0.030301,"end_time":"2024-03-13T21:36:44.006446","exception":false,"start_time":"2024-03-13T21:36:43.976145","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n<head>\n  <title>Cassava Disease Classes</title>\n  <style>\n    table {\n      width: 80%;\n      border-collapse: collapse;\n      margin: 20px 0;\n    }\n\n    th, td {\n      border: 1px solid #dddddd;\n      text-align: left;\n      padding: 8px;\n    }\n\n    th {\n      background-color: #2ecc71; /* Green color */\n      color: white;\n    }\n  </style>\n</head>\n\n<body>\n\n  <h2>Cassava Disease Classes</h2>\n\n  <table>\n    <tr>\n      <th>Class Code</th>\n      <th>Class Name</th>\n      <th>Disease Name</th>\n    </tr>\n    <tr>\n      <td>cbb</td>\n      <td>Bacterial Blight</td>\n      <td>Bacterial Blight Disease</td>\n    </tr>\n    <tr>\n      <td>cbsd</td>\n      <td>Brown Streak Disease</td>\n      <td>Brown Streak Disease</td>\n    </tr>\n    <tr>\n      <td>cgm</td>\n      <td>Green Mite</td>\n      <td>Green Mite Disease</td>\n    </tr>\n    <tr>\n      <td>cmd</td>\n      <td>Mosaic Disease</td>\n      <td>Cassava Mosaic Disease</td>\n    </tr>\n    <tr>\n      <td>healthy</td>\n      <td>Healthy</td>\n      <td>Healthy Cassava</td>\n    </tr>\n    <tr>\n      <td>unknown</td>\n      <td>Unknown</td>\n      <td>Unknown Condition</td>\n    </tr>\n  </table>\n\n</body>\n\n\n","metadata":{"papermill":{"duration":0.021572,"end_time":"2024-03-13T21:36:44.047587","exception":false,"start_time":"2024-03-13T21:36:44.026015","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import cv2\nsample = train_df[train_df.label == 0].sample(9)\nplt.figure(figsize=(12,12))\nfor ind, (image_id, label) in enumerate(zip(sample.image_id, sample.label)):\n    plt.subplot(3, 3, ind + 1)\n    image = cv2.imread(os.path.join(\"../input/cassava-leaf-disease-classification/train_images\", image_id))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:47:23.970302Z","iopub.execute_input":"2025-12-15T12:47:23.970637Z","iopub.status.idle":"2025-12-15T12:47:25.453572Z","shell.execute_reply.started":"2025-12-15T12:47:23.970609Z","shell.execute_reply":"2025-12-15T12:47:25.452098Z"},"papermill":{"duration":1.542051,"end_time":"2024-03-13T21:36:45.649195","exception":false,"start_time":"2024-03-13T21:36:44.107144","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## __Identification of Cassava Disease Symptoms__:","metadata":{"papermill":{"duration":0.045286,"end_time":"2024-03-13T21:36:45.740866","exception":false,"start_time":"2024-03-13T21:36:45.69558","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"\n## Technical Instructions:\n- Look for leaves drying and dying early.\n- Check for angular spots on the leaves.\n- Cut out small pieces of the leaf from the edge of the spots and place them in a drop of water.\n- Observe for bacterial streaming - appears as white streaks in the water.\n- Inspect for dark brown to black streaks on the green part of the stem.\n- Check for the presence of sticky liquid.\n- Look for browning in the vascular tissues after peeling the bark and splitting the stem.","metadata":{"papermill":{"duration":0.044975,"end_time":"2024-03-13T21:36:45.830634","exception":false,"start_time":"2024-03-13T21:36:45.785659","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sample = train_df[train_df.label == 1].sample(9)\nplt.figure(figsize=(12,12))\nfor ind, (image_id, label) in enumerate(zip(sample.image_id, sample.label)):\n    plt.subplot(4, 3, ind + 1)\n    image = cv2.imread(os.path.join(\"../input/cassava-leaf-disease-classification/train_images\", image_id))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:48:21.218087Z","iopub.execute_input":"2025-12-15T12:48:21.218424Z","iopub.status.idle":"2025-12-15T12:48:22.545598Z","shell.execute_reply.started":"2025-12-15T12:48:21.218402Z","shell.execute_reply":"2025-12-15T12:48:22.544327Z"},"papermill":{"duration":1.377092,"end_time":"2024-03-13T21:36:47.253039","exception":false,"start_time":"2024-03-13T21:36:45.875947","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n\n## Identification\n\n### Technical Instructions:\n- Look for yellow blotches along the veins from the midrib; these become patches as they join together.\n- Look for occasional streaks on the stems, and dry brown root rots.\n","metadata":{"papermill":{"duration":0.068124,"end_time":"2024-03-13T21:36:47.390167","exception":false,"start_time":"2024-03-13T21:36:47.322043","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sample = train_df[train_df.label == 2].sample(9)\nplt.figure(figsize=(12,12))\nfor ind, (image_id, label) in enumerate(zip(sample.image_id, sample.label)):\n    plt.subplot(3, 3, ind + 1)\n    image = cv2.imread(os.path.join(\"../input/cassava-leaf-disease-classification/train_images\", image_id))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:49:16.399968Z","iopub.execute_input":"2025-12-15T12:49:16.401047Z","iopub.status.idle":"2025-12-15T12:49:17.977854Z","shell.execute_reply.started":"2025-12-15T12:49:16.401009Z","shell.execute_reply":"2025-12-15T12:49:17.976558Z"},"papermill":{"duration":1.342194,"end_time":"2024-03-13T21:36:48.799407","exception":false,"start_time":"2024-03-13T21:36:47.457213","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Identification**:\n\nLook for yellow patterns on the leaves, from small dots to irregular patches of yellow and green. Look for leaf margins that are distorted. The plants may be stunted.","metadata":{"papermill":{"duration":0.091338,"end_time":"2024-03-13T21:36:48.989581","exception":false,"start_time":"2024-03-13T21:36:48.898243","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sample = train_df[train_df.label == 3].sample(9)\nplt.figure(figsize=(12,12))\nfor ind, (img_id, lab) in enumerate(zip(sample.image_id, sample.label)):\n    plt.subplot(3,3,ind+1)\n    image = cv2.imread(os.path.join(\"../input/cassava-leaf-disease-classification/train_images\", img_id))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:49:27.105766Z","iopub.execute_input":"2025-12-15T12:49:27.106358Z","iopub.status.idle":"2025-12-15T12:49:28.388123Z","shell.execute_reply.started":"2025-12-15T12:49:27.106334Z","shell.execute_reply":"2025-12-15T12:49:28.387054Z"},"papermill":{"duration":1.335272,"end_time":"2024-03-13T21:36:50.416551","exception":false,"start_time":"2024-03-13T21:36:49.081279","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Identification**:\n\nInfected leaves are white or pale yellow with pale green patches and will often be twisted, an unusual shape, and stunted. Cassava mosaic disease causes low yields.","metadata":{"papermill":{"duration":0.12084,"end_time":"2024-03-13T21:36:50.656687","exception":false,"start_time":"2024-03-13T21:36:50.535847","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### Let's see the healthy leaves.","metadata":{"papermill":{"duration":0.122503,"end_time":"2024-03-13T21:36:50.90851","exception":false,"start_time":"2024-03-13T21:36:50.786007","status":"completed"},"tags":[]}},{"cell_type":"code","source":"sample = train_df[train_df.label == 4].sample(9)\nplt.figure(figsize=(12,12))\nfor ind, (img_id, lab) in enumerate(zip(sample.image_id, sample.label)):\n    plt.subplot(3,3,ind+1)\n    image = cv2.imread(os.path.join(\"../input/cassava-leaf-disease-classification/train_images\", img_id))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:49:37.940308Z","iopub.execute_input":"2025-12-15T12:49:37.940618Z","iopub.status.idle":"2025-12-15T12:49:39.274565Z","shell.execute_reply.started":"2025-12-15T12:49:37.940596Z","shell.execute_reply":"2025-12-15T12:49:39.273013Z"},"papermill":{"duration":1.619706,"end_time":"2024-03-13T21:36:52.651402","exception":false,"start_time":"2024-03-13T21:36:51.031696","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ny_pred = [3] * len(train_df.label)\nprint(\"The baseline accuracy is {}\".format(accuracy_score(y_pred, train_df.label)))","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:50:05.620445Z","iopub.execute_input":"2025-12-15T12:50:05.621289Z","iopub.status.idle":"2025-12-15T12:50:05.633316Z","shell.execute_reply.started":"2025-12-15T12:50:05.62124Z","shell.execute_reply":"2025-12-15T12:50:05.632304Z"},"papermill":{"duration":0.164197,"end_time":"2024-03-13T21:36:52.962178","exception":false,"start_time":"2024-03-13T21:36:52.797981","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"If around 61% of the leaves belong to only 3 categories out of all possible categories, it means that a large majority of the leaves fall into those three categories. This suggests that these three categories might be the most common or prevalent among the leaves being examined.","metadata":{"papermill":{"duration":0.14627,"end_time":"2024-03-13T21:36:53.249839","exception":false,"start_time":"2024-03-13T21:36:53.103569","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"\n## Model Evaluating\n","metadata":{"papermill":{"duration":0.158722,"end_time":"2024-03-13T21:36:53.552967","exception":false,"start_time":"2024-03-13T21:36:53.394245","status":"completed"},"tags":[]}},{"cell_type":"code","source":"Batch_size = 16\nimg_height, img_width = 300, 300","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:50:59.269985Z","iopub.execute_input":"2025-12-15T12:50:59.270344Z","iopub.status.idle":"2025-12-15T12:50:59.275417Z","shell.execute_reply.started":"2025-12-15T12:50:59.270319Z","shell.execute_reply":"2025-12-15T12:50:59.274199Z"},"papermill":{"duration":0.159539,"end_time":"2024-03-13T21:36:53.869293","exception":false,"start_time":"2024-03-13T21:36:53.709754","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['label'].dtype","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:51:01.542191Z","iopub.execute_input":"2025-12-15T12:51:01.542617Z","iopub.status.idle":"2025-12-15T12:51:01.549466Z","shell.execute_reply.started":"2025-12-15T12:51:01.542593Z","shell.execute_reply":"2025-12-15T12:51:01.548217Z"},"papermill":{"duration":0.192009,"end_time":"2024-03-13T21:36:54.21841","exception":false,"start_time":"2024-03-13T21:36:54.026401","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n## Data Augmentation\n\n","metadata":{"papermill":{"duration":0.153237,"end_time":"2024-03-13T21:36:54.521652","exception":false,"start_time":"2024-03-13T21:36:54.368415","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_df['label'] = train_df['label'].astype('str')\ngen = ImageDataGenerator(\n    horizontal_flip = True,\n    vertical_flip = True,\n    validation_split = 0.2,\n)\n\ntrain_datagen = gen.flow_from_dataframe(\n    train_df,\n    directory = os.path.join(Dir, \"train_images\"),\n    batch_size = Batch_size,\n    target_size = (img_height, img_width),\n    subset = \"training\",\n    seed = 42,\n    x_col = \"image_id\",\n    y_col = \"label\",\n    class_mode = \"categorical\"\n)","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:51:14.578172Z","iopub.execute_input":"2025-12-15T12:51:14.579183Z","iopub.status.idle":"2025-12-15T12:52:12.004101Z","shell.execute_reply.started":"2025-12-15T12:51:14.579152Z","shell.execute_reply":"2025-12-15T12:52:12.003064Z"},"papermill":{"duration":23.413932,"end_time":"2024-03-13T21:37:18.085169","exception":false,"start_time":"2024-03-13T21:36:54.671237","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_gen = ImageDataGenerator(\n    validation_split = 0.2\n)\n\nval_datagen = val_gen.flow_from_dataframe(\n    train_df,\n    directory = os.path.join(Dir, \"train_images\"),\n    batch_size = Batch_size,\n    target_size = (img_height, img_width),\n    subset = \"validation\",\n    seed = 42,\n    x_col = \"image_id\",\n    y_col = \"label\",\n    class_mode = \"categorical\"\n)","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:52:49.996987Z","iopub.execute_input":"2025-12-15T12:52:49.998044Z","iopub.status.idle":"2025-12-15T12:53:16.891677Z","shell.execute_reply.started":"2025-12-15T12:52:49.998014Z","shell.execute_reply":"2025-12-15T12:53:16.890495Z"},"papermill":{"duration":9.35092,"end_time":"2024-03-13T21:37:27.87758","exception":false,"start_time":"2024-03-13T21:37:18.52666","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"There are 4279 validation images.","metadata":{"papermill":{"duration":0.168989,"end_time":"2024-03-13T21:37:28.198586","exception":false,"start_time":"2024-03-13T21:37:28.029597","status":"completed"},"tags":[]}},{"cell_type":"code","source":"len(train_datagen), len(val_datagen)","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:53:56.073234Z","iopub.execute_input":"2025-12-15T12:53:56.07365Z","iopub.status.idle":"2025-12-15T12:53:56.081593Z","shell.execute_reply.started":"2025-12-15T12:53:56.073623Z","shell.execute_reply":"2025-12-15T12:53:56.080369Z"},"papermill":{"duration":0.1539,"end_time":"2024-03-13T21:37:28.501233","exception":false,"start_time":"2024-03-13T21:37:28.347333","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n\nWhen training a model with 21,397 images, we divide them into training and validation sets.\n\n## For the training set:\n- 80% of the images (21,397 * 0.8)\n- Divide this by the batch size (16)\n- Result: Training batch length is 1070, meaning the model processes 1070 batches of 16 images each.\n\n## For the validation set:\n- 20% of the images (21,397 * 0.2)\n- Divide this by the batch size (16)\n- Result: Validation batch length is 268, meaning the model processes 268 batches of 16 images each.\n\n--- \n","metadata":{"papermill":{"duration":0.145715,"end_time":"2024-03-13T21:37:28.790196","exception":false,"start_time":"2024-03-13T21:37:28.644481","status":"completed"},"tags":[]}},{"cell_type":"code","source":"img, label = next(train_datagen)","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:54:05.326135Z","iopub.execute_input":"2025-12-15T12:54:05.326506Z","iopub.status.idle":"2025-12-15T12:54:05.589625Z","shell.execute_reply.started":"2025-12-15T12:54:05.326483Z","shell.execute_reply":"2025-12-15T12:54:05.588692Z"},"papermill":{"duration":0.288615,"end_time":"2024-03-13T21:37:29.220992","exception":false,"start_time":"2024-03-13T21:37:28.932377","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"next() is used to get the next batch of images and labels.","metadata":{"papermill":{"duration":0.144271,"end_time":"2024-03-13T21:37:29.512265","exception":false,"start_time":"2024-03-13T21:37:29.367994","status":"completed"},"tags":[]}},{"cell_type":"code","source":"label","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:54:09.609407Z","iopub.execute_input":"2025-12-15T12:54:09.609735Z","iopub.status.idle":"2025-12-15T12:54:09.616832Z","shell.execute_reply.started":"2025-12-15T12:54:09.609707Z","shell.execute_reply":"2025-12-15T12:54:09.615811Z"},"papermill":{"duration":0.160086,"end_time":"2024-03-13T21:37:29.819251","exception":false,"start_time":"2024-03-13T21:37:29.659165","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Steps_per_train = float(train_datagen.n) / train_datagen.batch_size\nSteps_per_val = float(val_datagen.n) / val_datagen.batch_size","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:54:27.727619Z","iopub.execute_input":"2025-12-15T12:54:27.727924Z","iopub.status.idle":"2025-12-15T12:54:27.732407Z","shell.execute_reply.started":"2025-12-15T12:54:27.727902Z","shell.execute_reply":"2025-12-15T12:54:27.731326Z"},"papermill":{"duration":0.150155,"end_time":"2024-03-13T21:37:30.111847","exception":false,"start_time":"2024-03-13T21:37:29.961692","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Steps_per_train, Steps_per_val","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:54:19.947923Z","iopub.execute_input":"2025-12-15T12:54:19.948777Z","iopub.status.idle":"2025-12-15T12:54:19.954386Z","shell.execute_reply.started":"2025-12-15T12:54:19.948745Z","shell.execute_reply":"2025-12-15T12:54:19.953427Z"},"papermill":{"duration":0.153009,"end_time":"2024-03-13T21:37:30.406477","exception":false,"start_time":"2024-03-13T21:37:30.253468","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"I have tried out various models like:\n\n1. A CNN model with 3 convolution layers and the accuracy came out to be 61.65 which is near to the base accuracy only.\n2. A CNN model with 4 convolution layers and the accuracy came out to be 67.35 which is better than previous accuracy.\n3. A CNN model with 4 convolution layers with Dropout, Batch Normalization and the accuracy is 63.59.\n4. Tranfer Learning: I tried out various models like ResNet, DenseNet, VGG16, EfficientNet. Finally the maximum accuracy I got is through Efficient Net.","metadata":{"papermill":{"duration":0.140828,"end_time":"2024-03-13T21:37:30.689071","exception":false,"start_time":"2024-03-13T21:37:30.548243","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"\n\n## Model Building\n\n\n\n","metadata":{"papermill":{"duration":0.144294,"end_time":"2024-03-13T21:37:30.979382","exception":false,"start_time":"2024-03-13T21:37:30.835088","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\ndef create_model():\n    model = models.Sequential()\n    model.add(EfficientNetB3(include_top=False, weights='imagenet',\n                             input_shape=(img_height, img_width, 3)))\n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Flatten())\n    model.add(layers.Dense(256, activation=\"relu\"))\n    model.add(layers.Dropout(0.3))\n    model.add(layers.Dense(5, activation='softmax'))\n    \n    loss = tf.keras.losses.CategoricalCrossentropy(\n        label_smoothing=0.0001,\n        name='categorical_crossentropy'\n    )\n    optimizer = optimizers.Adam(learning_rate=1e-4)\n    \n    model.compile(optimizer=optimizer,\n                  loss=loss,\n                  metrics=[\"categorical_accuracy\"])\n    return model\n\nmodel = create_model()\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:55:07.216869Z","iopub.execute_input":"2025-12-15T12:55:07.217197Z","iopub.status.idle":"2025-12-15T12:55:12.872242Z","shell.execute_reply.started":"2025-12-15T12:55:07.217174Z","shell.execute_reply":"2025-12-15T12:55:12.870636Z"},"papermill":{"duration":4.962043,"end_time":"2024-03-13T21:37:36.083052","exception":false,"start_time":"2024-03-13T21:37:31.121009","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Build the model\nmodel.build((None))\n\n# Plot the model\ntf.keras.utils.plot_model(model, show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2025-12-15T12:56:06.59993Z","iopub.execute_input":"2025-12-15T12:56:06.600286Z","iopub.status.idle":"2025-12-15T12:56:07.03024Z","shell.execute_reply.started":"2025-12-15T12:56:06.600245Z","shell.execute_reply":"2025-12-15T12:56:07.029323Z"},"papermill":{"duration":0.535832,"end_time":"2024-03-13T21:37:36.764578","exception":false,"start_time":"2024-03-13T21:37:36.228746","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"rlronp=tf.keras.callbacks.ReduceLROnPlateau(monitor=\"val_loss\",\n                                            factor=0.2,\n                                            mode = \"min\",\n                                            min_lr=1e-6,\n                                            patience=2, \n                                            verbose=1)\n\nestop=tf.keras.callbacks.EarlyStopping(monitor=\"val_loss\", \n                                       mode= \"min\",\n                                       patience=3, \n                                       verbose=1,\n                                       restore_best_weights=True)\n\nhistory = model.fit(\n    train_datagen,\n    steps_per_epoch=int(Steps_per_train),\n    epochs=5,\n    verbose =1,\n    validation_data=val_datagen,\n    validation_steps=int(Steps_per_val),\n    callbacks=[rlronp, estop]\n)\n\n","metadata":{"papermill":{"duration":1014.720543,"end_time":"2024-03-13T21:54:31.634831","exception":false,"start_time":"2024-03-13T21:37:36.914288","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Saving the model\nmodel.save(\"Casava_Model.h5\")","metadata":{"papermill":{"duration":0.420745,"end_time":"2024-03-13T21:54:32.468669","exception":false,"start_time":"2024-03-13T21:54:32.047924","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history.history.keys()","metadata":{"papermill":{"duration":0.491214,"end_time":"2024-03-13T21:54:34.208099","exception":false,"start_time":"2024-03-13T21:54:33.716885","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ntrain_acc = history.history[\"categorical_accuracy\"]\nval_acc = history.history[\"val_categorical_accuracy\"]\nepochs = range(1, len(train_acc)+1)\nplt.plot(epochs, train_acc, \"bo\", label = \"Training Accuracy\")\nplt.plot(epochs, val_acc, \"b\", label = \"Validation Accuracy\")\nplt.title(\"Training and Validation Accuracy\")\nplt.legend()","metadata":{"papermill":{"duration":0.686107,"end_time":"2024-03-13T21:54:35.306994","exception":false,"start_time":"2024-03-13T21:54:34.620887","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nplt.figure(figsize=(8,6))\ntrain_loss = history.history[\"loss\"]\nval_loss = history.history[\"val_loss\"]\nepochs = range(1, len(train_loss)+1)\nplt.plot(epochs, train_loss, \"bo\", label = \"Training Loss\")\nplt.plot(epochs, val_loss, \"b\", label = \"Validation Loss\")\nplt.title(\"Training and Validation Loss\")\nplt.legend()","metadata":{"execution":{"iopub.execute_input":"2024-03-13T21:54:36.142696Z","iopub.status.busy":"2024-03-13T21:54:36.142288Z","iopub.status.idle":"2024-03-13T21:54:36.480472Z","shell.execute_reply":"2024-03-13T21:54:36.479532Z"},"papermill":{"duration":0.756216,"end_time":"2024-03-13T21:54:36.482559","exception":false,"start_time":"2024-03-13T21:54:35.726343","status":"completed"},"tags":[]},"outputs":[],"execution_count":null}]}