Created
July 14, 2022 14:34
-
-
Save AhAzizPy/0a0966ebaacf0d0999c9573d2e141d2e to your computer and use it in GitHub Desktop.
Plant Seedlings Image Classification using CNNs in Keras.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "name": "Plant Seedlings Image Classification using CNNs in Keras.ipynb", | |
| "provenance": [], | |
| "collapsed_sections": [], | |
| "authorship_tag": "ABX9TyPZYy/oQHKUJ7fw+z46NK42", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| }, | |
| "gpuClass": "standard", | |
| "accelerator": "GPU" | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/AhAzizPy/0a0966ebaacf0d0999c9573d2e141d2e/plant-seedlings-image-classification-using-cnns-in-keras.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Plant Seedlings Image Classification using CNNs in Keras\n", | |
| "*by Ahmad Abdelaziz*\n", | |
| "\n", | |
| "Background and Context\n", | |
| "\n", | |
| "You are provided with a dataset of images of plant seedlings at various stages of grown. Each image has a filename that is its unique id. The dataset comprises 12 plant species. The goal of the project is to create a classifier capable of determining a plant's species from a photo\n", | |
| "\n", | |
| "Objective\n", | |
| "\n", | |
| "Can you differentiate a weed from a crop seedling? The ability to do so effectively can mean better crop yields and better stewardship of the environment.The Aarhus University Signal Processing group, in collaboration with University of Southern Denmark, hasrecently released a dataset containing images of unique plants belonging to 12 species at several growth stages\n", | |
| "\n", | |
| "### Contents:\n", | |
| "\n", | |
| "### A. Data Overview\n", | |
| "* Dataset features identification\n", | |
| "* Displaying an image from each class\n", | |
| "\n", | |
| "### B. EDA\n", | |
| "* Data imbalance check\n", | |
| "* Plotting mean image from each class\n", | |
| "\n", | |
| "### C. Data Pre-processing\n", | |
| "* Resizing the images\n", | |
| "* Denoising the images\n", | |
| "* Data Splitting\n", | |
| " * 4 Different split sets are prepared (Reduced size, Reduced size & Blurred, Original size, original size balanced)\n", | |
| "* One hot encoding\n", | |
| "* Data Normalization\n", | |
| "\n", | |
| "### D. Model building\n", | |
| "* Model 1 (Standard CNN model)\n", | |
| "* Model 2 (data Augmentation)\n", | |
| "* Model 3 (Transfer learning)\n", | |
| "\n", | |
| "### E. Conclusion and key takeaways" | |
| ], | |
| "metadata": { | |
| "id": "h76z7OQFWf81" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "## Data and libraries importation" | |
| ], | |
| "metadata": { | |
| "id": "xSA-fGGzYFLG" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Library for creating data paths\n", | |
| "import os\n", | |
| "\n", | |
| "# Library for randomly selecting data points\n", | |
| "import random\n", | |
| "\n", | |
| "# Library for performing numerical computations\n", | |
| "import numpy as np\n", | |
| "\n", | |
| "# importing pandas\n", | |
| "import pandas as pd\n", | |
| "\n", | |
| "# Library for creating and showing plots\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "\n", | |
| "# Library for reading and showing images\n", | |
| "import matplotlib.image as mpimg\n", | |
| "import seaborn as sns \n", | |
| "import cv2\n", | |
| "\n", | |
| "# Importing all the required sub-modules from Keras\n", | |
| "from keras.models import Sequential, Model\n", | |
| "from keras.applications.vgg16 import VGG16\n", | |
| "from keras.preprocessing.image import ImageDataGenerator\n", | |
| "from keras.preprocessing.image import img_to_array, load_img\n", | |
| "from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, BatchNormalization, Dropout\n", | |
| "\n", | |
| "# Tensorflow modules\n", | |
| "import tensorflow as tf\n", | |
| "from tensorflow.keras import backend\n", | |
| "from tensorflow.keras.preprocessing.image import ImageDataGenerator # Importing the ImageDataGenerator for data augmentation\n", | |
| "from tensorflow.keras.models import Sequential # Importing the sequential module to define a sequential model\n", | |
| "from tensorflow.keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPooling2D,BatchNormalization # Defining all the layers to build our CNN Model\n", | |
| "from tensorflow.keras.optimizers import Adam,SGD \n", | |
| "\n", | |
| "#Scikit modules\n", | |
| "from sklearn import preprocessing # Importing the preprocessing module to preprocess the data\n", | |
| "from sklearn.model_selection import train_test_split # Importing train_test_split function to split the data into train and test\n", | |
| "from sklearn.metrics import confusion_matrix # Importing confusion_matrix to plot the confusion matrix\n", | |
| "from sklearn.preprocessing import LabelBinarizer\n", | |
| "\n", | |
| "# Display images using OpenCV\n", | |
| "from google.colab.patches import cv2_imshow # Importing cv2_imshow from google.patches to display images\n", | |
| "\n", | |
| "# Ignore warnings\n", | |
| "import warnings\n", | |
| "warnings.filterwarnings('ignore')" | |
| ], | |
| "metadata": { | |
| "id": "KZdtdUF2X-e9" | |
| }, | |
| "execution_count": 1, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "from google.colab import drive\n", | |
| "drive.mount('/content/drive')" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "NjhEXZ_HYMYz", | |
| "outputId": "a7ea83fc-0a93-44c9-b30f-de672cbaab3c" | |
| }, | |
| "execution_count": 2, | |
| "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": [ | |
| "#loading the plant array pictures\n", | |
| "plant = np.load('/content/drive/My Drive/AIML/CV_Project/images.npy')\n", | |
| "\n", | |
| "#Reading the species of plant dataset\n", | |
| "spec = pd.read_csv('/content/drive/My Drive/AIML/CV_Project/Labels.csv')" | |
| ], | |
| "metadata": { | |
| "id": "szR0FNR7YdVF" | |
| }, | |
| "execution_count": 3, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "## A. Data Overview" | |
| ], | |
| "metadata": { | |
| "id": "iGqc_s0zK-DH" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "#Display the dataset structure\n", | |
| "plant.shape\n", | |
| "spec.shape" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "NnbtEYZSd8Nj", | |
| "outputId": "981fc49b-fd29-49f6-f23c-b2da12750948" | |
| }, | |
| "execution_count": 4, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "(4750, 1)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 4 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Here the data is stored in a **4-dimensional NumPy array**. The first dimension **4750** is denoting **the number of images in the dataset**, and each image is stacked on top of the other as a 3-dimensional NumPy array. The second dimension **128** is denoting **the number of pixels along the x-axis**, **the third dimension 128 is denoting the number of pixels along the y-axis** and **the fourth dimension 3 is the total number of channels in those images** i.e. these are colored images consisting of RGB (Red, Green, and Blue) channels." | |
| ], | |
| "metadata": { | |
| "id": "6QzWelXkly2E" | |
| } | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "Below is a 3-dimensional NumPy representation of the first image in the training data. **Each pixel in an image has 3 values - the intensity of R, G, and B channels**, and the size of each image is 128x128. So, each image is represented by 128 arrays of shape 128x3." | |
| ], | |
| "metadata": { | |
| "id": "rlvK7QEqmIeI" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "#Showing the array structure for first image in the numpy array\n", | |
| "plant[0]" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "xb_2rOpHl-R-", | |
| "outputId": "ffc6e248-0dd3-4987-aa8e-a02c7e28259f" | |
| }, | |
| "execution_count": 5, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "array([[[ 35, 52, 78],\n", | |
| " [ 36, 49, 76],\n", | |
| " [ 31, 45, 69],\n", | |
| " ...,\n", | |
| " [ 78, 95, 114],\n", | |
| " [ 76, 93, 110],\n", | |
| " [ 80, 95, 109]],\n", | |
| "\n", | |
| " [[ 33, 46, 68],\n", | |
| " [ 37, 50, 73],\n", | |
| " [ 48, 65, 83],\n", | |
| " ...,\n", | |
| " [ 81, 96, 113],\n", | |
| " [ 74, 89, 105],\n", | |
| " [ 83, 95, 109]],\n", | |
| "\n", | |
| " [[ 34, 50, 68],\n", | |
| " [ 35, 52, 72],\n", | |
| " [ 70, 85, 101],\n", | |
| " ...,\n", | |
| " [ 83, 97, 112],\n", | |
| " [ 79, 94, 108],\n", | |
| " [ 79, 94, 107]],\n", | |
| "\n", | |
| " ...,\n", | |
| "\n", | |
| " [[ 35, 50, 69],\n", | |
| " [ 42, 57, 73],\n", | |
| " [ 42, 57, 72],\n", | |
| " ...,\n", | |
| " [ 60, 76, 92],\n", | |
| " [ 67, 81, 97],\n", | |
| " [ 64, 77, 95]],\n", | |
| "\n", | |
| " [[ 36, 52, 67],\n", | |
| " [ 48, 63, 78],\n", | |
| " [ 41, 57, 73],\n", | |
| " ...,\n", | |
| " [ 44, 66, 83],\n", | |
| " [ 58, 76, 91],\n", | |
| " [ 57, 74, 90]],\n", | |
| "\n", | |
| " [[ 44, 58, 70],\n", | |
| " [ 43, 57, 73],\n", | |
| " [ 40, 55, 72],\n", | |
| " ...,\n", | |
| " [ 41, 70, 92],\n", | |
| " [ 55, 78, 97],\n", | |
| " [ 61, 79, 96]]], dtype=uint8)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 5 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "#Adding label clases into a list and displaying it\n", | |
| "cat = list(spec['Label'].unique())\n", | |
| "cat" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "HFkpCem3mmVl", | |
| "outputId": "246bae40-7bef-45db-9492-202b60fbb858" | |
| }, | |
| "execution_count": 6, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "['Small-flowered Cranesbill',\n", | |
| " 'Fat Hen',\n", | |
| " 'Shepherds Purse',\n", | |
| " 'Common wheat',\n", | |
| " 'Common Chickweed',\n", | |
| " 'Charlock',\n", | |
| " 'Cleavers',\n", | |
| " 'Scentless Mayweed',\n", | |
| " 'Sugar beet',\n", | |
| " 'Maize',\n", | |
| " 'Black-grass',\n", | |
| " 'Loose Silky-bent']" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 6 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "#Plotting random images from each plant class\n", | |
| "\n", | |
| "fig, axes = plt.subplots(3, 4, figsize = (16, 8))\n", | |
| "fig.set_size_inches(16, 16)\n", | |
| "\n", | |
| "for (label, pic) in zip(cat, axes.flatten()):\n", | |
| " index = spec.loc[spec['Label']==label].index[0]\n", | |
| " pic.imshow(plant[index])\n", | |
| " pic.set_title(label)\n", | |
| " pic.axis('off')\n", | |
| " \n", | |
| "plt.show()" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 855 | |
| }, | |
| "id": "xspvlm4Bn6RW", | |
| "outputId": "e943b41c-8f5f-4a99-bff4-1fea8bcc4866" | |
| }, | |
| "execution_count": 7, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "text/plain": [ | |
| "<Figure size 1152x1152 with 12 Axes>" | |
| ], |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment