{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Cassava Leaf Disease Classification"},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"papermill":{"duration":0.075396,"end_time":"2020-12-09T10:15:58.059012","exception":false,"start_time":"2020-12-09T10:15:57.983616","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Import Necessary Libraries"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:15:58.218864Z","iopub.status.busy":"2020-12-09T10:15:58.218101Z","iopub.status.idle":"2020-12-09T10:16:07.028276Z","shell.execute_reply":"2020-12-09T10:16:07.027115Z"},"papermill":{"duration":8.894256,"end_time":"2020-12-09T10:16:07.028402","exception":false,"start_time":"2020-12-09T10:15:58.134146","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\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\nfrom tensorflow.keras.applications import EfficientNetB0, Xception\nfrom tensorflow.keras.optimizers import Adam","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.execute_input":"2020-12-09T10:16:07.185665Z","iopub.status.busy":"2020-12-09T10:16:07.185015Z","iopub.status.idle":"2020-12-09T10:16:07.188907Z","shell.execute_reply":"2020-12-09T10:16:07.189363Z"},"papermill":{"duration":0.088453,"end_time":"2020-12-09T10:16:07.189511","exception":false,"start_time":"2020-12-09T10:16:07.101058","status":"completed"},"tags":[],"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":{"execution":{"iopub.execute_input":"2020-12-09T10:16:07.362772Z","iopub.status.busy":"2020-12-09T10:16:07.362108Z","iopub.status.idle":"2020-12-09T10:16:07.406401Z","shell.execute_reply":"2020-12-09T10:16:07.405497Z"},"papermill":{"duration":0.141597,"end_time":"2020-12-09T10:16:07.406528","exception":false,"start_time":"2020-12-09T10:16:07.264931","status":"completed"},"tags":[],"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":{"execution":{"iopub.execute_input":"2020-12-09T10:16:07.614978Z","iopub.status.busy":"2020-12-09T10:16:07.613894Z","iopub.status.idle":"2020-12-09T10:16:07.62128Z","shell.execute_reply":"2020-12-09T10:16:07.622005Z"},"papermill":{"duration":0.142913,"end_time":"2020-12-09T10:16:07.62218","exception":false,"start_time":"2020-12-09T10:16:07.479267","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"print(\"Total Number of Images in Training Data : \",train.shape[0])","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:08.040501Z","iopub.status.busy":"2020-12-09T10:16:08.039356Z","iopub.status.idle":"2020-12-09T10:16:08.046378Z","shell.execute_reply":"2020-12-09T10:16:08.047072Z"},"papermill":{"duration":0.201691,"end_time":"2020-12-09T10:16:08.04725","exception":false,"start_time":"2020-12-09T10:16:07.845559","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"image_list = train['image_id'].to_list()\nlabel_list = train['label'].to_list()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:08.464332Z","iopub.status.busy":"2020-12-09T10:16:08.46343Z","iopub.status.idle":"2020-12-09T10:16:08.478885Z","shell.execute_reply":"2020-12-09T10:16:08.479542Z"},"papermill":{"duration":0.212568,"end_time":"2020-12-09T10:16:08.479724","exception":false,"start_time":"2020-12-09T10:16:08.267156","status":"completed"},"tags":[],"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":{"papermill":{"duration":0.132817,"end_time":"2020-12-09T10:16:08.739116","exception":false,"start_time":"2020-12-09T10:16:08.606299","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Distribution of Diseases:"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:08.974908Z","iopub.status.busy":"2020-12-09T10:16:08.974095Z","iopub.status.idle":"2020-12-09T10:16:09.52832Z","shell.execute_reply":"2020-12-09T10:16:09.527835Z"},"papermill":{"duration":0.681992,"end_time":"2020-12-09T10:16:09.528426","exception":false,"start_time":"2020-12-09T10:16:08.846434","status":"completed"},"tags":[],"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":{"execution":{"iopub.execute_input":"2020-12-09T10:16:09.697545Z","iopub.status.busy":"2020-12-09T10:16:09.696914Z","iopub.status.idle":"2020-12-09T10:16:10.930867Z","shell.execute_reply":"2020-12-09T10:16:10.930032Z"},"papermill":{"duration":1.323846,"end_time":"2020-12-09T10:16:10.930978","exception":false,"start_time":"2020-12-09T10:16:09.607132","status":"completed"},"tags":[],"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":{"papermill":{"duration":0.076556,"end_time":"2020-12-09T10:16:11.084578","exception":false,"start_time":"2020-12-09T10:16:11.008022","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Visualization"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:11.248346Z","iopub.status.busy":"2020-12-09T10:16:11.24756Z","iopub.status.idle":"2020-12-09T10:16:11.251295Z","shell.execute_reply":"2020-12-09T10:16:11.250818Z"},"papermill":{"duration":0.089444,"end_time":"2020-12-09T10:16:11.251406","exception":false,"start_time":"2020-12-09T10:16:11.161962","status":"completed"},"tags":[],"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":{"execution":{"iopub.execute_input":"2020-12-09T10:16:11.408317Z","iopub.status.busy":"2020-12-09T10:16:11.407697Z","iopub.status.idle":"2020-12-09T10:16:14.182932Z","shell.execute_reply":"2020-12-09T10:16:14.183433Z"},"papermill":{"duration":2.856769,"end_time":"2020-12-09T10:16:14.183582","exception":false,"start_time":"2020-12-09T10:16:11.326813","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"plot_samples(0) #Cassava Bacterial Blight","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:14.389922Z","iopub.status.busy":"2020-12-09T10:16:14.389258Z","iopub.status.idle":"2020-12-09T10:16:17.012235Z","shell.execute_reply":"2020-12-09T10:16:17.01274Z"},"papermill":{"duration":2.727084,"end_time":"2020-12-09T10:16:17.01287","exception":false,"start_time":"2020-12-09T10:16:14.285786","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"plot_samples(1) #Cassava Brown Streak Disease","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:17.259643Z","iopub.status.busy":"2020-12-09T10:16:17.258933Z","iopub.status.idle":"2020-12-09T10:16:20.290208Z","shell.execute_reply":"2020-12-09T10:16:20.29076Z"},"papermill":{"duration":3.156494,"end_time":"2020-12-09T10:16:20.290905","exception":false,"start_time":"2020-12-09T10:16:17.134411","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"plot_samples(2) #Cassava Green Mottle","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:20.577825Z","iopub.status.busy":"2020-12-09T10:16:20.577088Z","iopub.status.idle":"2020-12-09T10:16:23.13443Z","shell.execute_reply":"2020-12-09T10:16:23.134937Z"},"papermill":{"duration":2.704933,"end_time":"2020-12-09T10:16:23.135066","exception":false,"start_time":"2020-12-09T10:16:20.430133","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"plot_samples(3) #Cassava Mosaic Disease","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.163933,"end_time":"2020-12-09T10:16:23.462946","exception":false,"start_time":"2020-12-09T10:16:23.299013","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Converting from BGR to RGB"},{"metadata":{"papermill":{"duration":0.162984,"end_time":"2020-12-09T10:16:23.79931","exception":false,"start_time":"2020-12-09T10:16:23.636326","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Cassava Bacterial Blight (CBB) Samples"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:24.145238Z","iopub.status.busy":"2020-12-09T10:16:24.137948Z","iopub.status.idle":"2020-12-09T10:16:25.911685Z","shell.execute_reply":"2020-12-09T10:16:25.912152Z"},"papermill":{"duration":1.949342,"end_time":"2020-12-09T10:16:25.912356","exception":false,"start_time":"2020-12-09T10:16:23.963014","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"sample_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":{"papermill":{"duration":0.200443,"end_time":"2020-12-09T10:16:26.302856","exception":false,"start_time":"2020-12-09T10:16:26.102413","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Cassava Brown Streak Disease (CBSD) Samples"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:26.691051Z","iopub.status.busy":"2020-12-09T10:16:26.690236Z","iopub.status.idle":"2020-12-09T10:16:28.536779Z","shell.execute_reply":"2020-12-09T10:16:28.53726Z"},"papermill":{"duration":2.047833,"end_time":"2020-12-09T10:16:28.537385","exception":false,"start_time":"2020-12-09T10:16:26.489552","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"sample_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":{"papermill":{"duration":0.210214,"end_time":"2020-12-09T10:16:28.961172","exception":false,"start_time":"2020-12-09T10:16:28.750958","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Cassava Green Mottle (CGM) Samples"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:29.406886Z","iopub.status.busy":"2020-12-09T10:16:29.405317Z","iopub.status.idle":"2020-12-09T10:16:31.96621Z","shell.execute_reply":"2020-12-09T10:16:31.966814Z"},"papermill":{"duration":2.78825,"end_time":"2020-12-09T10:16:31.966997","exception":false,"start_time":"2020-12-09T10:16:29.178747","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"sample_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":{"papermill":{"duration":0.23993,"end_time":"2020-12-09T10:16:32.452488","exception":false,"start_time":"2020-12-09T10:16:32.212558","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Cassava Mosaic Disease (CMD) Samples"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:32.942324Z","iopub.status.busy":"2020-12-09T10:16:32.941374Z","iopub.status.idle":"2020-12-09T10:16:34.721976Z","shell.execute_reply":"2020-12-09T10:16:34.722466Z"},"papermill":{"duration":2.028432,"end_time":"2020-12-09T10:16:34.722615","exception":false,"start_time":"2020-12-09T10:16:32.694183","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"sample_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":{"papermill":{"duration":0.270031,"end_time":"2020-12-09T10:16:35.25854","exception":false,"start_time":"2020-12-09T10:16:34.988509","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Healty Samples "},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:35.808705Z","iopub.status.busy":"2020-12-09T10:16:35.807276Z","iopub.status.idle":"2020-12-09T10:16:37.630451Z","shell.execute_reply":"2020-12-09T10:16:37.63105Z"},"papermill":{"duration":2.104659,"end_time":"2020-12-09T10:16:37.631207","exception":false,"start_time":"2020-12-09T10:16:35.526548","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"sample_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":{"papermill":{"duration":0.309514,"end_time":"2020-12-09T10:16:38.30692","exception":false,"start_time":"2020-12-09T10:16:37.997406","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Model Implemetation and Augmentation"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:16:38.896821Z","iopub.status.busy":"2020-12-09T10:16:38.896038Z","iopub.status.idle":"2020-12-09T10:16:38.900048Z","shell.execute_reply":"2020-12-09T10:16:38.899186Z"},"papermill":{"duration":0.300529,"end_time":"2020-12-09T10:16:38.900153","exception":false,"start_time":"2020-12-09T10:16:38.599624","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"BATCH_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":{"execution":{"iopub.execute_input":"2020-12-09T10:16:39.560359Z","iopub.status.busy":"2020-12-09T10:16:39.5499Z","iopub.status.idle":"2020-12-09T10:16:55.747728Z","shell.execute_reply":"2020-12-09T10:16:55.746883Z"},"papermill":{"duration":16.560326,"end_time":"2020-12-09T10:16:55.747838","exception":false,"start_time":"2020-12-09T10:16:39.187512","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"train.label = train.label.astype('str')\n\ntrain_datagen = ImageDataGenerator(validation_split = 0.2,\n                                     rotation_range = 45,\n                                     zoom_range = 0.3,\n                                     horizontal_flip = True,\n                                     vertical_flip = True,\n                                     fill_mode = 'nearest',\n                                     shear_range = 0.1,\n                                     height_shift_range = 0.1,\n                                     width_shift_range = 0.1,\n                                     featurewise_center = True,\n                                     featurewise_std_normalization = True)\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":{"execution":{"iopub.execute_input":"2020-12-09T10:16:56.332418Z","iopub.status.busy":"2020-12-09T10:16:56.331776Z","iopub.status.idle":"2020-12-09T10:16:56.658165Z","shell.execute_reply":"2020-12-09T10:16:56.658678Z"},"papermill":{"duration":0.620251,"end_time":"2020-12-09T10:16:56.658849","exception":false,"start_time":"2020-12-09T10:16:56.038598","status":"completed"},"tags":[],"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":{"execution":{"iopub.execute_input":"2020-12-09T10:16:57.256268Z","iopub.status.busy":"2020-12-09T10:16:57.244717Z","iopub.status.idle":"2020-12-09T10:17:00.940261Z","shell.execute_reply":"2020-12-09T10:17:00.940787Z"},"papermill":{"duration":3.987724,"end_time":"2020-12-09T10:17:00.940924","exception":false,"start_time":"2020-12-09T10:16:56.9532","status":"completed"},"tags":[],"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":{"execution":{"iopub.execute_input":"2020-12-09T10:17:01.699092Z","iopub.status.busy":"2020-12-09T10:17:01.698099Z","iopub.status.idle":"2020-12-09T10:17:01.701588Z","shell.execute_reply":"2020-12-09T10:17:01.700707Z"},"papermill":{"duration":0.351813,"end_time":"2020-12-09T10:17:01.701691","exception":false,"start_time":"2020-12-09T10:17:01.349878","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"def create_model():\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":{"execution":{"iopub.execute_input":"2020-12-09T10:17:02.366839Z","iopub.status.busy":"2020-12-09T10:17:02.366123Z","iopub.status.idle":"2020-12-09T10:17:08.896652Z","shell.execute_reply":"2020-12-09T10:17:08.895739Z"},"papermill":{"duration":6.869841,"end_time":"2020-12-09T10:17:08.896799","exception":false,"start_time":"2020-12-09T10:17:02.026958","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"model = create_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:17:09.662584Z","iopub.status.busy":"2020-12-09T10:17:09.660704Z","iopub.status.idle":"2020-12-09T10:17:09.66323Z","shell.execute_reply":"2020-12-09T10:17:09.663719Z"},"papermill":{"duration":0.344835,"end_time":"2020-12-09T10:17:09.663845","exception":false,"start_time":"2020-12-09T10:17:09.31901","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"model_save = ModelCheckpoint('./Xception_best_weights2.h5', \n                              save_best_only = True, \n                              save_weights_only = True,\n                              monitor = 'val_loss', \n                              mode = 'min', verbose = 1)\nearly_stop = EarlyStopping(monitor = 'val_loss', min_delta = 0.001, \n                            patience = 5, mode = 'min', verbose = 1,\n                            restore_best_weights = True)\nreduce_lr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.3, \n                               patience = 2, min_delta = 0.001, \n                               mode = 'min', verbose = 1) #reduced learning rate\n\n\nhistory = model.fit(\n     train_generator,\n     steps_per_epoch = STEPS_PER_EPOCH,\n     epochs = EPOCHS,\n     validation_data = validation_generator,\n     validation_steps = VALIDATION_STEPS,\n     callbacks = [model_save, early_stop, reduce_lr])","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:17:10.343677Z","iopub.status.busy":"2020-12-09T10:17:10.342047Z","iopub.status.idle":"2020-12-09T10:17:10.348279Z","shell.execute_reply":"2020-12-09T10:17:10.34777Z"},"papermill":{"duration":0.348406,"end_time":"2020-12-09T10:17:10.348378","exception":false,"start_time":"2020-12-09T10:17:09.999972","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"model.save('./Xception_best_weights.h5')","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.332244,"end_time":"2020-12-09T10:17:11.038955","exception":false,"start_time":"2020-12-09T10:17:10.706711","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Load Model"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:17:11.748018Z","iopub.status.busy":"2020-12-09T10:17:11.745626Z","iopub.status.idle":"2020-12-09T10:17:17.742256Z","shell.execute_reply":"2020-12-09T10:17:17.741264Z"},"papermill":{"duration":6.361498,"end_time":"2020-12-09T10:17:17.742373","exception":false,"start_time":"2020-12-09T10:17:11.380875","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"model = keras.models.load_model('./Xception_best_weights.h5')","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.331287,"end_time":"2020-12-09T10:17:18.410104","exception":false,"start_time":"2020-12-09T10:17:18.078817","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Submission"},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:17:19.126092Z","iopub.status.busy":"2020-12-09T10:17:19.125436Z","iopub.status.idle":"2020-12-09T10:17:19.138782Z","shell.execute_reply":"2020-12-09T10:17:19.139259Z"},"papermill":{"duration":0.39115,"end_time":"2020-12-09T10:17:19.139393","exception":false,"start_time":"2020-12-09T10:17:18.748243","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"submission_file = pd.read_csv(os.path.join('../input/cassava-leaf-disease-classification/sample_submission.csv'))\nsubmission_file","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:17:19.845625Z","iopub.status.busy":"2020-12-09T10:17:19.840592Z","iopub.status.idle":"2020-12-09T10:17:23.468692Z","shell.execute_reply":"2020-12-09T10:17:23.469446Z"},"papermill":{"duration":3.97227,"end_time":"2020-12-09T10:17:23.469614","exception":false,"start_time":"2020-12-09T10:17:19.497344","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"preds = []\n\nfor image_id in submission_file.image_id:\n    image = Image.open(os.path.join(f'../input/cassava-leaf-disease-classification/test_images/{image_id}'))\n    image = image.resize((TARGET_SIZE, TARGET_SIZE))\n    image = np.expand_dims(image, axis = 0)\n    preds.append(np.argmax(model.predict(image)))\n\nsubmission_file['label'] = preds\nsubmission_file","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-12-09T10:17:24.263631Z","iopub.status.busy":"2020-12-09T10:17:24.262671Z","iopub.status.idle":"2020-12-09T10:17:24.439201Z","shell.execute_reply":"2020-12-09T10:17:24.438096Z"},"papermill":{"duration":0.537111,"end_time":"2020-12-09T10:17:24.439322","exception":false,"start_time":"2020-12-09T10:17:23.902211","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"submission_file.to_csv('submission.csv', index = False)","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}