{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Import all required libraries. Few I have added down wherever it was required.\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt \nimport plotly.express as px\nimport os\nimport cv2\nfrom PIL import Image\nimport keras\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nimport tensorflow as tf\nfrom tensorflow.keras import models, layers\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n#from tensorflow.keras.applications import EfficientNetB0, Xception\nfrom tensorflow.keras.applications import Xception\nfrom tensorflow.keras.optimizers import Adam","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# First fetch train and test dataset in your notebook\n\ninput_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":"train_images_path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ntrain.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape # Number of images in folder.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[train['image_id'].str.contains('1001742395')]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_list = train['image_id'].to_list()\nlabel_list = train['label'].to_list()\nlen(image_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(5):\n    print(image_list[i],\"   \",label_list[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import json\n\nwith open(\"../input/cassava-leaf-disease-classification/label_num_to_disease_map.json\") as f:\n    class_mapping = json.load(f)\n\nclass_mapping2 ={int(k):v for k,v in class_mapping.items()}\n\nclass_mapping2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Distribution of Diseases"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8,5))\n\nsns.set_style('whitegrid')\n\nax=sns.countplot(data=train, x='label', palette=\"Pastel1\")\n\n\n#  '0': 'Cassava Bacterial Blight (CBB)\n#  '1': 'Cassava Brown Streak Disease (CBSD)\n#  '2': 'Cassava Green Mottle (CGM)\n#  '3': 'Cassava Mosaic Disease (CMD)\n#  '4': 'Healthy","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train2 = train.copy()\ntrain2.replace({\"label\": class_mapping2}, inplace=True)\n\npie_df = train2['label'].value_counts().reset_index()\npie_df.columns = ['label', 'count']\nfig = px.pie(pie_df, values = 'count', names = 'label', hole=.3, color_discrete_sequence = px.colors.qualitative.Pastel1)\nfig.show()","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 {class_mapping[f\"{class_}\"]}')\n    \n    sample_images = train[train.label == class_].sample(8)\n    \n    plt.rcParams[\"axes.grid\"] = False\n\n    fig,ax = plt.subplots(nrows=2,ncols=4,figsize=(20,12))\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) #Cassava Bacterial Blight","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_samples(1) #Cassava Brown Streak Disease","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_samples(2) #Cassava Green Mottle","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_samples(3) #Cassava Mosaic Disease","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Converting from BGR to RGB"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Cassava Bacterial Blight (CBB) Samples¶\nsample_images = train[train.label == 0].sample(5)\nplt.figure(figsize=(35, 20))\nfor e,img in enumerate(sample_images.image_id):\n    plt.subplot(1, 5, e + 1)\n    img = cv2.imread(os.path.join(input_dir,f'train_images/{img}'))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.imshow(img)\n    \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Cassava Brown Streak Disease (CBSD) Samples\nsample_images = train[train.label == 1].sample(5)\nplt.figure(figsize=(35, 20))\nfor e,img in enumerate(sample_images.image_id):\n    plt.subplot(1, 5, e + 1)\n    img = cv2.imread(os.path.join(input_dir,f'train_images/{img}'))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.imshow(img)\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Cassava Green Mottle (CGM) Samples\nsample_images = train[train.label == 2].sample(5)\nplt.figure(figsize=(35, 20))\nfor e,img in enumerate(sample_images.image_id):\n    plt.subplot(1, 5, e + 1)\n    img = cv2.imread(os.path.join(input_dir,f'train_images/{img}'))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.imshow(img)\n    \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Cassava Mosaic Disease (CMD) Samples\nsample_images = train[train.label == 3].sample(5)\nplt.figure(figsize=(35, 20))\nfor e,img in enumerate(sample_images.image_id):\n    plt.subplot(1, 5, e + 1)\n    img = cv2.imread(os.path.join(input_dir,f'train_images/{img}'))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.imshow(img)\n    \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Healty Samples\nsample_images = train[train.label == 4].sample(5)\nplt.figure(figsize=(35, 20))\nfor e,img in enumerate(sample_images.image_id):\n    plt.subplot(1, 5, e + 1)\n    img = cv2.imread(os.path.join(input_dir,f'train_images/{img}'))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    plt.imshow(img)\n    \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model Implemetation and Augmentation\nBATCH_SIZE =8 #Mini-Batch Gradient Descent\nSTEPS_PER_EPOCH = len(train)*0.8 / BATCH_SIZE\nVALIDATION_STEPS = len(train)*0.2 / BATCH_SIZE\nEPOCHS = 20\nTARGET_SIZE = 350","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.label = train.label.astype('str')\nfrom keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(\n                    rotation_range = 40,\n                    width_shift_range = 0.2,\n                    height_shift_range = 0.2,\n                    shear_range = 0.2,\n                    zoom_range = 0.2,\n                    horizontal_flip = True,\n                    vertical_flip = True,\n                    fill_mode = 'nearest')\n\ntrain_generator = train_datagen.flow_from_dataframe(train,\n                         directory = os.path.join('../input/cassava-leaf-disease-classification/train_images'),\n                         subset = \"training\",\n                         x_col = \"image_id\",\n                         y_col = \"label\",\n                         target_size = (TARGET_SIZE, TARGET_SIZE),\n                         batch_size = BATCH_SIZE,\n                         class_mode = \"sparse\",\n                         shuffle= True)\n\n\nvalidation_datagen = ImageDataGenerator(validation_split = 0.2)\n\nvalidation_generator = validation_datagen.flow_from_dataframe(train,\n                         directory = os.path.join('../input/cassava-leaf-disease-classification/train_images'),\n                         subset = \"validation\",\n                         x_col = \"image_id\",\n                         y_col = \"label\",\n                         target_size = (TARGET_SIZE, TARGET_SIZE),\n                         batch_size = BATCH_SIZE,\n                         class_mode = \"sparse\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_path = os.path.join('../input/cassava-leaf-disease-classification/train_images/1003442061.jpg')\nimg = image.load_img(img_path, target_size = (TARGET_SIZE, TARGET_SIZE))\nimg_tensor = image.img_to_array(img)\nimg_tensor = np.expand_dims(img_tensor, axis = 0)\nimg_tensor /= 255.\n\nplt.imshow(img_tensor[0])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"generator = train_datagen.flow_from_dataframe(train.iloc[17:18],\n                         directory = os.path.join('../input/cassava-leaf-disease-classification/train_images'),\n                         x_col = \"image_id\",\n                         y_col = \"label\",\n                         target_size = (TARGET_SIZE, TARGET_SIZE),\n                         batch_size = BATCH_SIZE,\n                         class_mode = \"sparse\")\n\naug_images = [generator[0][0][0]/255 for i in range(10)]\nfig, axes = plt.subplots(2, 5, figsize = (20, 10))\naxes = axes.flatten()\nfor img, ax in zip(aug_images, axes):\n    ax.imshow(img)\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_model_old():\n    conv_base = Xception(include_top=False, input_tensor=None,\n    pooling=None, input_shape=(TARGET_SIZE, TARGET_SIZE, 3), classifier_activation='softmax')\n                               \n    model = conv_base.output\n    model = layers.GlobalAveragePooling2D()(model)\n    model = layers.Dense(5, activation = \"softmax\")(model)\n    model = models.Model(conv_base.input, model)\n\n    model.compile(optimizer = Adam(lr = 0.001),\n                  loss = \"sparse_categorical_crossentropy\",\n                  metrics = [\"acc\"])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check for TensorFlow or Thieno\nfrom keras import backend as K\nimg_width = 150\nimg_height = 150\nif K.image_data_format() == 'channels_first':\n    input_shape = (3, img_width, img_height)\nelse:\n    input_shape = (img_width, img_height, 3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.layers import Activation, Dropout, Flatten, Dense\nfrom keras import backend as K\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_model_1():\n    model = Sequential()\n    # Conv2D : Two dimenstional convulational model.\n    # 32 : Input for next layer\n    # (3,3) convulonational windows size\n    model.add(Conv2D(512, (3, 3), input_shape=(TARGET_SIZE, TARGET_SIZE, 3)))\n    model.add(Activation('relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n\n    model.add(Conv2D(256, (3, 3)))\n    model.add(Activation('relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n\n    model.add(Conv2D(128, (3, 3)))\n    model.add(Activation('relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n\n    model.add(Flatten()) # Output convert into one dimension layer and will go to Dense layer\n    #model.add(Dense(512))\n    #model.add(Activation('relu'))\n    #model.add(bias_regularizer('tf.keras.regularizers.L1L2(l1=0.01, l2=0.001)'))\n    model.add(Dense(512, activation = 'relu', bias_regularizer=tf.keras.regularizers.L1L2(l1=0.01, l2=0.001)))\n    model.add(Dropout(0.7))\n    model.add(Dense(5))\n    model.add(Activation('softmax'))\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = create_model_1()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#model = create_model()\n#model = create_model_1()\n#import keras\n#from keras import optimizers\n#model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(lr=.0001), metrics=['accuracy'])\n#model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model = keras.models.load_model('../input/xception-best-weights/Xception_best_weights.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Submission\nsubmission_file = pd.read_csv(os.path.join('../input/cassava-leaf-disease-classification/sample_submission.csv'))\nsubmission_file","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = []\n\nfor image_id in submission_file.image_id:\n    print(image_id)\n    image = Image.open(os.path.join(f'../input/cassava-leaf-disease-classification/test_images/{image_id}'))\n    image\n    image = image.resize((TARGET_SIZE, TARGET_SIZE))\n    image = np.expand_dims(image, axis = 0)\n    model.predict(image)\n    preds.append(np.argmax(model.predict(image)))\n\nsubmission_file['label'] = preds\nsubmission_file","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_file.to_csv('submission.csv', index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nTEST_DIR = '../input/cassava-leaf-disease-classification/test_images/'\ntest_images = os.listdir(TEST_DIR)\npredictions = []\n\nfor image in test_images:\n    img = Image.open(TEST_DIR + image)\n    img = img.resize((TARGET_SIZE, TARGET_SIZE))\n    img = np.expand_dims(img, axis=0)\n    predictions.extend(model.predict(img).argmax(axis = 1))\npredictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.DataFrame({'image_id': test_images, 'label': predictions})\ndisplay(sub)","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}