{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Overview:\n\nCassava anthracnose disease (CAD) is widespread in most of the cassava growing regions of Africa. The disease is caused by a fungus (Collectothricum gloeosporioides) that is also capable of causing diseases on other food crops. It is estimated that CAD causes yield losses in the neighbourhood of 30% or more in susceptible cultivars. The disease affects both leaf and stem production. Severe anthracnose attacks can cause death of stems which can affect the availability of planting materials especially in large scale production systems.\n\n**There are Four kind of disease introduced here**\n\n**1. Cassava Bacterial Blight (CBB):** At first, angular, water-soaked spots occur on the leaves which are restricted by the veins; the spots are more clearly seen on the lower leaf surface. The spots expand rapidly, join together, especially along the margins of the leaves, and turn brown with yellow borders (in the picture, on the left). Droplets of a creamy-white ooze occur at the center of the spots; later, they turn yellow. Stem infections block the flow of water and food and the leaves above wilt, die, and fall, and branches die back (in the picture, on the right). \n    \n    Main characteristics to leverage: **angular spots, brown spots with yellow borders, yellow leaves, leaves wilting**\n    \n   <img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1865449%2Fbe9cdd94efb9b1660066ad10b55c8626%2Fbact_bright.jpeg?generation=1605827469211692&alt=media\" style=\"width:500px;height:300px\">\n\n**2. Cassava Brown Streak Disease (CBSD):** Symptoms of cassava brown streak disease appear as patches of yellow areas mixed with normal green color. The characteristic yellow or necrotic vein banding may enlarge and coalesce to form large yellow patches. \n\n    The infected leaves **do not become distorted in shape as occurs with leaves infected by Cassava mosaic disease.** \n    \n    Main characteristics to leverage: **yellow spots**\n    \n   <img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1865449%2Ffeba3dafc914d04517659650d137b77a%2Fbrown_st.jpeg?generation=1605830407530983&alt=media\" style=\"width:500px;height:300px\">\n\n**3. Cassava Green Mottle (CGM):** Young leaves are puckered with faint to distinct yellow spots (in the picture, on the left), green patterns (mosaics), and twisted margins (in the picture, on the right). Occasionally, plants become severely stunted.\n\n    Main characteristics to leverage: **yellow patterns, irregular patches of yellow and green, leaf margins distortion, stunted**\n    \n   <img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1865449%2F4f2975866feb2a1d4ef4111c2d57db29%2Fgreen_mottle.jpeg?generation=1605829101431013&alt=media\" style=\"width:500px;height:300px\">\n\n**4. Cassava Mosaic Disease (CMD):** Cassava Mosaic Disease is characterized by severe mosaic symptoms on leaves, with affected leaves showing mottling and light-green, yellow or white spots. Discoloration, malformation, and puckering of the leaf blade occur.\n    \n    Main characteristics to leverage: **severe shape distortion, mosaic patterns**\n    \n   <img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1865449%2F36990f77ded6667e5c30d19b5405d4d3%2Fmosaic_disease.jpeg?generation=1605829705010773&alt=media\" style=\"width:500px;height:300px\">\n   \nSource: Discussion from **Jacopo Repossi**"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#  importing libraries\nimport os\nimport json\nimport numpy as np\nimport pandas as pd\nimport missingno as msno\nfrom glob import glob\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\n#bokeh\nfrom bokeh.models import ColumnDataSource, HoverTool, Panel, FactorRange\nfrom bokeh.plotting import figure\nfrom bokeh.io import output_notebook, show, output_file\nfrom bokeh.palettes import Spectral6\n\nimport warnings\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Setup directory structure"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Setup Directory Structure and Environment variables\nBASE_DIR = \"../input/cassava-leaf-disease-classification/\"\nTRAIN_IMAGES = os.path.join(BASE_DIR, \"train_images/\")\nTEST_IMAGES = os.path.join(BASE_DIR, \"test_images/\")\nTRAIN_DF = os.path.join(BASE_DIR, \"train.csv\")\nLABELS = os.path.join(BASE_DIR, \"label_num_to_disease_map.json\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# The mapping between each disease code and the real disease name.\nwith open(LABELS) as label:\n    classes = json.loads(label.read())\n    \nprint(json.dumps(classes, indent = 3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Total number of Training and Testing Images\nprint(\"Number of Training Images are {}\".format(len(os.listdir(TRAIN_IMAGES))))\nprint(\"Number of Testing Images are {}\".format(len(os.listdir(TEST_IMAGES))))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Data Exploration"},{"metadata":{"trusted":true},"cell_type":"code","source":"# reading dataframe\ndf_train = pd.read_csv(TRAIN_DF)\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# shape of data\ndf_train.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We have 21397 Training images, So it should also be having 21397 rows in dataframe"},{"metadata":{"trusted":true},"cell_type":"code","source":"# checking for null values\nprint(df_train.isnull().sum())\n\nmsno.matrix(df_train, color=(207/255, 196/255, 171/255), fontsize = 10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We have labels for each image present and no missing data found."},{"metadata":{},"cell_type":"markdown","source":"We have label code in dataframe and mapping is present in json. So, lets add class_name and image_path in dataframe."},{"metadata":{"trusted":true},"cell_type":"code","source":"# mapping class_name to train.csv\ndf_train[\"class_name\"] = df_train[\"label\"].astype(str).map(classes)\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# mapping image path to train.csv\ndf_train[\"path\"] = df_train[\"image_id\"].map(lambda x:TRAIN_IMAGES + \"/\" + x)\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We have images present for 5 categories. Let's count and visualize number of images per each class."},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train[\"class_name\"].value_counts(sort=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# number of images in each category\n\nCategories = [\"Cassava Mosaic Disease (CMD)\", \"Healthy\", \"Cassava Green Mottle (CGM)\", \"Cassava Brown Streak Disease (CBSD)\", \"Cassava Bacterial Blight (CBB)\"]\ncounts = list(df_train[\"class_name\"].value_counts(sort=True))\n\nsource = ColumnDataSource(data = dict(Categories = Categories, counts = counts, color = Spectral6))\n\np = figure(x_range = Categories, y_range = (0,15000),plot_width = 1000,plot_height = 500, title = \"Distribution of the number of images in the training set\",\n           tools = \"hover, pan, box_zoom, wheel_zoom, reset, save\", tooltips = (\"@Categories: @counts\"))\n\np.vbar(x = 'Categories', top = 'counts', width = 0.9, color = 'color', legend_field = \"Categories\", source = source)\n\np.xgrid.grid_line_color = None\np.legend.orientation = \"horizontal\"\np.legend.location = \"top_center\"\n\noutput_notebook()\nshow(p)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Images Exploration"},{"metadata":{"trusted":true},"cell_type":"code","source":"CMD = df_train[df_train['class_name'] == 'Cassava Mosaic Disease (CMD)']\nHealthy = df_train[df_train['class_name'] == 'Healthy']\nCGM = df_train[df_train['class_name'] == 'Cassava Green Mottle (CGM)']\nCBSD = df_train[df_train['class_name'] == 'Cassava Brown Streak Disease (CBSD)']\nCBB = df_train[df_train['class_name'] == 'Cassava Bacterial Blight (CBB)']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Extract 9 random images from CMD\nrandom_images = [np.random.choice((CMD['image_id'].values)) for i in range(9)]\n\nprint('Display CMD Images')\n\n# Adjust the size of your images\nplt.figure(figsize=(10,8))\n\n# Iterate and plot random images\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    img = plt.imread(os.path.join(TRAIN_IMAGES, random_images[i]))\n    plt.imshow(img, cmap = 'gray')\n    plt.axis('off')\n    \n# Adjust subplot parameters to give specified padding\nplt.tight_layout() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Extract 9 random images from Healthy\nrandom_images = [np.random.choice((Healthy['image_id'].values)) for i in range(9)]\n\nprint('Display Healthy Images')\n\n# Adjust the size of your images\nplt.figure(figsize=(10,8))\n\n# Iterate and plot random images\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    img = plt.imread(os.path.join(TRAIN_IMAGES, random_images[i]))\n    plt.imshow(img, cmap = 'gray')\n    plt.axis('off')\n    \n# Adjust subplot parameters to give specified padding\nplt.tight_layout() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Extract 9 random images from CGM\nrandom_images = [np.random.choice((CGM['image_id'].values)) for i in range(9)]\n\nprint('Display CGM Images')\n\n# Adjust the size of your images\nplt.figure(figsize=(10,8))\n\n# Iterate and plot random images\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    img = plt.imread(os.path.join(TRAIN_IMAGES, random_images[i]))\n    plt.imshow(img, cmap = 'gray')\n    plt.axis('off')\n    \n# Adjust subplot parameters to give specified padding\nplt.tight_layout() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Extract 9 random images from CBSD\nrandom_images = [np.random.choice((CBSD['image_id'].values)) for i in range(9)]\n\nprint('Display CBSD Images')\n\n# Adjust the size of your images\nplt.figure(figsize=(10,8))\n\n# Iterate and plot random images\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    img = plt.imread(os.path.join(TRAIN_IMAGES, random_images[i]))\n    plt.imshow(img, cmap = 'gray')\n    plt.axis('off')\n    \n# Adjust subplot parameters to give specified padding\nplt.tight_layout() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Extract 9 random images from CBB\nrandom_images = [np.random.choice((CBB['image_id'].values)) for i in range(9)]\n\nprint('Display CBB Images')\n\n# Adjust the size of your images\nplt.figure(figsize=(10,8))\n\n# Iterate and plot random images\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    img = plt.imread(os.path.join(TRAIN_IMAGES, random_images[i]))\n    plt.imshow(img, cmap = 'gray')\n    plt.axis('off')\n    \n# Adjust subplot parameters to give specified padding\nplt.tight_layout() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# images shape distribution\nimages_shape = []\n\nfor k, image_name in enumerate(df_train['image_id']):\n    image = Image.open(TRAIN_IMAGES + \"/\" + image_name)\n    images_shape.append(image.size)\n\nimages_shape_df = pd.DataFrame(data = images_shape, columns = ['H', 'W'], dtype='object')\nimages_shape_df['Size'] = '[' + images_shape_df['H'].astype(str) + ',' + images_shape_df['W'].astype(str) + ']'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_shape_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"We have {} types of different shapes in training images\".format(len(list(images_shape_df['Size'].unique()))))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We have all Images with same shape of 800x600. "}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}