{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import json\nimport datetime\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport keras \n\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow.keras import models, layers, optimizers, initializers\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.losses import CategoricalCrossentropy, SparseCategoricalCrossentropy\n\nimport tensorflow_addons as tfa\n\nfrom keras import activations\nfrom keras.models import load_model\nfrom keras.applications import InceptionV3, Xception\n\nimport pandas as pd\nimport numpy as np\nimport os\nimport shutil\nimport json\nimport pickle\nimport cv2\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# EDA"},{"metadata":{"trusted":true},"cell_type":"code","source":"input_dir = \"../input/cassava-leaf-disease-classification\"\n\ntrain_images_path = os.path.join(input_dir,\"train_images\")\ntest_images_path = os.path.join(input_dir,'test_images')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mapping = pd.read_json(\n    '../input/cassava-leaf-disease-classification/label_num_to_disease_map.json', \n    lines=True\n).transpose()[0].to_dict()\nmapping","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\nmapping = {0:'CBB', 1:'CBSD', 2:'CGM', 3:'CMD',\n           4:'Healthy'}\ndata['disease'] = data.apply(lambda x: mapping[x['label']], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(1,2, figsize=(15,6))\nsns.countplot(data.disease, ax=ax[0])\nsns.countplot(data.label, ax=ax[1]);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_samples(class_):\n    \n    print(f'Some Sample Images belonging to Class {mapping[class_]}')\n    \n    sample_images = data[data.label == class_].sample(8)\n    \n    plt.rcParams[\"axes.grid\"] = False\n\n    fig,ax = plt.subplots(nrows=2,ncols=4,figsize=(20,7))\n\n    for e,img in enumerate(sample_images.image_id):\n        image_path = os.path.join(input_dir,f'train_images/{img}')\n        image = cv2.imread(image_path)\n        ax[e//4][e%4].imshow(image)\n    \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_samples(0)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Symptoms**\n\nSmall, angular, brown, water-soaked lesions between leaf veins on lower surfaces of leaves; leaf blades turning brown as lesion expands; lesions may have a yello halo; lesions coalesce to form large necrotic patches; defoliation occurs with leaf petioles remaining in horizontal position as leaves drop; dieback of shoots; brown gum may be present on stems, leaves and petioles\n\n**Cause**\n\nBacterium\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_samples(1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Symptoms**\nLeaves: \n- chlorotic or necrotic vein banding in mature leaves which may merge later to form large yellow patches\n\nStems:\n\n- Brown elongated necrotic lesions on young stems\n\n**Cause**\n\nVirus"},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_samples(2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Symptoms**\n\nYoung leaves are puckered with faint to distinct yellow spots (Photo 1), green patterns (mosaics), and twisted margins (Photo 2). Usually, the shoots recover from symptoms and appear healthy. Occasionally, plants become severely stunted, edible roots are absent or, if present, they are small and woody when cooked.\n\n**Cause**\nVirus"},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_samples(3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Symptoms**\nDiscolored pale green, yellow or white mottled leaves which may be distorted with a reduced size; in highly susceptible cassava cultivars plant growth may be stunted, resulting in poor root yield and low quality stem cuttings. Note that infected plants can express a range of symptoms and the exact symptoms depend on the species of virus and the strain as well as the environmental conditions and and the sensitivity of the cassava host.\n\n1. Patches of discolouration (chlorosis) in the leaves that vary from yellow to green.\n2. The leaves display size variation and are often severely distorted.\n3. Leaf blades sometimes fold depending on severity shrivel.\n\n**Cause**\nVirus"},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_samples(4)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":" The leaves are palmate (hand-shaped) and dark green in color. The cone-shaped roots are starch storage organs covered with a papery bark and a pink to white cortex. The flesh ranges from bright white to soft yellow. Over five thousand varieties of cassava are known, each of which has its own distinctive qualities and is adapted to different environmental conditions."},{"metadata":{},"cell_type":"markdown","source":"# Data Augmentation"},{"metadata":{"trusted":true},"cell_type":"code","source":"mkdir augmentation","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BATCH_SIZE = 8  #Mini-Batch Gradient Descent\nSTEPS_PER_EPOCH = len(data)*0.8 / BATCH_SIZE\nVALIDATION_STEPS = len(data)*0.2 / BATCH_SIZE\nEPOCHS = 20\nTARGET_SIZE = 512","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1./255,\n                             rotation_range = 23,\n                             zoom_range = [0.64, 0.47],\n                             horizontal_flip = True,\n                             vertical_flip = True,\n                             fill_mode = 'constant',\n                             cval= 0.01,\n                             channel_shift_range=-27.4,\n                             shear_range = 0.3,\n                             brightness_range = [0.05, 0.2],\n                             height_shift_range = [0.05, 0.2],\n                             width_shift_range = [0.05, 0.2])\n\n#datagen = ImageDataGenerator(rescale=127)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#rm augmentation/CBB -r","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nfor idx in range(5):\n    directory = data[data.label==idx].disease.iloc[0]\n    print(directory)\n    os.makedirs(f'augmentation/{data[data.label==idx].disease.iloc[0]}',\n                exist_ok=True)\n\n    i = 0\n    for batch in datagen.flow_from_dataframe(\n                             data[data['label']==idx],\n                             #data[data.image_id.isin(['1000015157.jpg','1001320321.jpg', '1003888281.jpg'])],\n                             directory = '../input/cassava-leaf-disease-classification/train_images',\n                             x_col = \"image_id\",\n                             y_col = \"disease\",\n                             target_size = (TARGET_SIZE, TARGET_SIZE),\n                             batch_size = BATCH_SIZE,\n                             class_mode = \"categorical\",\n                             seed=278941,\n                             interpolation='hamming',      \n                             save_to_dir=f'augmentation/{directory}/',\n                             save_prefix='gen',\n                             save_format=\"jpg\"):\n\n        if i > 13_000:\n            break\n        i += BATCH_SIZE","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}