{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Loading the libraries\nimport numpy as np\nfrom numpy.random import seed\nseed(1)\nimport tensorflow as tf\ntf.random.set_seed(1)\nimport pandas as pd \nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\n%matplotlib inline\nimport seaborn as sns\nimport os\nimport json\nimport shutil\nimport random","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#Loading the dataset\ndf_train = pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Loading the sample submission file\ndf_submit = pd.read_csv(\"../input/cassava-leaf-disease-classification/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_submit.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Defining utlity function for submission\ndef submit(df_submit, disease_label):\n    df_submit['label'] = disease_label\n    df_submit.to_csv('submission.csv', index = False)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Loading the numeric label to disease name mapping JSON file\nwith open('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json') as file:\n  label_num_to_disease = json.load(file)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# The disease name corresponding to the numeric labels\nlabel_num_to_disease","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Making a copy of the train DataFrame for further investigation\ndf_train_1 = df_train.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Adding the disease names to the DataFrame\ndf_train_1['disease'] = df_train_1['label'].astype(str).replace(label_num_to_disease)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train_1.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Looking at unique disease names corresponding to the dataset\ndf_train_1['disease'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualising the Label distribution\nplt.figure(figsize=(18,10))\nsns.countplot(data=df_train_1, x='disease')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Number of images of each disease\ncount = df_train_1[['disease']].value_counts().reset_index()\ncount.columns = ['disease', 'count of images']\ncount","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Percentage of each disease\ncount['Percent presence'] = count['count of images'] / len(df_train_1['disease']) * 100\ncount","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"_kg_hide-input":false,"trusted":true,"collapsed":true},"cell_type":"code","source":"# Creating seperate directories for each class\nlabels = df_train_1['disease'].unique()\nfor i in labels:\n    disease = str(i)\n    os.makedirs(\"/kaggle/working/train_img/\" + disease)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Copying images to their respective folders\nfor image_row in range(len(df_train_1['image_id'])):\n    image = df_train_1['image_id'].iloc[image_row]\n    if df_train_1['disease'].iloc[image_row] == 'Cassava Bacterial Blight (CBB)':\n        shutil.copy(\"/kaggle/input/cassava-leaf-disease-classification/train_images/\"+image,\"/kaggle/working/train_img/Cassava Bacterial Blight (CBB)/\")\n    elif df_train_1['disease'].iloc[image_row] == 'Cassava Mosaic Disease (CMD)':\n        shutil.copy(\"/kaggle/input/cassava-leaf-disease-classification/train_images/\"+image,\"/kaggle/working/train_img/Cassava Mosaic Disease (CMD)/\")\n    elif df_train_1['disease'].iloc[image_row] == 'Cassava Brown Streak Disease (CBSD)':\n        shutil.copy(\"/kaggle/input/cassava-leaf-disease-classification/train_images/\"+image,\"/kaggle/working/train_img/Cassava Brown Streak Disease (CBSD)/\")\n    elif df_train_1['disease'].iloc[image_row] == 'Cassava Green Mottle (CGM)':\n        shutil.copy(\"/kaggle/input/cassava-leaf-disease-classification/train_images/\"+image,\"/kaggle/working/train_img/Cassava Green Mottle (CGM)/\")\n    elif df_train_1['disease'].iloc[image_row] == 'Healthy':\n        shutil.copy(\"/kaggle/input/cassava-leaf-disease-classification/train_images/\"+image,\"/kaggle/working/train_img/Healthy/\")\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Utility function to print images of each disease\ndef show_disease_images(label_name):\n    plt.figure(figsize = ((10,10)))\n    for i in range(0,9):\n        plt.subplot(3,3,i+1)\n        image_path = \"/kaggle/working/train_img/\" + label_name + \"/\"\n        image = imread(image_path + random.choice(os.listdir(image_path)))\n        plt.imshow(image)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_disease_images('Healthy')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_disease_images('Cassava Green Mottle (CGM)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_disease_images('Cassava Brown Streak Disease (CBSD)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_disease_images('Cassava Mosaic Disease (CMD)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_disease_images('Cassava Bacterial Blight (CBB)')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}