{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"\nimport os\nimport glob\nimport shutil\nimport json\nimport keras\nimport itertools\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport tensorflow as tf\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom collections import Counter\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import GlobalAveragePooling2D, Flatten, Dense, Dropout\nfrom keras.optimizers import RMSprop, Adam\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\nfrom keras.layers import Dense, Flatten, Dropout, Conv2D, Activation, MaxPooling2D, BatchNormalization\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"work_dir = '../input/cassava-leaf-disease-classification/'\nos.listdir(work_dir) \ntrain_path = '/kaggle/input/cassava-leaf-disease-classification/train_images'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data = pd.read_csv(work_dir + 'train.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(Counter(data['label'])) # checking labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir(work_dir))\ndata_dir = work_dir\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.set_style(\"dark\")\nplt.figure(figsize=(10,8))\nsns.countplot(data[\"label\"], edgecolor=\"black\", palette=\"mako\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")\ndf_train.head()\ndf_train[\"label\"] = df_train[\"label\"].astype(str) #convert to str as we want to use Categorical Cross Entropy (CCE) later on\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **0: Cassava Bacterial Blight**"},{"metadata":{"trusted":true},"cell_type":"code","source":"path = \"../input/cassava-leaf-disease-classification/train_images/\"\ndf0 = df_train[df_train[\"label\"] == \"0\"]\nfiles = df0[\"image_id\"].sample(3).tolist()\n\nplt.figure(figsize=(15,5))\nindex = 0\nfor file in files:\n    image = Image.open(path + file)\n    plt.subplot(1, 3, index + 1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    index += 1\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#  **1: Cassava Brown Streak Disease**"},{"metadata":{"trusted":true},"cell_type":"code","source":"df1 = df_train[df_train[\"label\"] == \"1\"]\nfiles = df1[\"image_id\"].sample(3).tolist()\n\nplt.figure(figsize=(15,5))\nindex = 0\nfor file in files:\n    image = Image.open(path + file)\n    plt.subplot(1, 3, index + 1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    index += 1\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **2: Cassava Green Mottle**"},{"metadata":{"trusted":true},"cell_type":"code","source":"df2 = df_train[df_train[\"label\"] == \"2\"]\nfiles = df2[\"image_id\"].sample(3).tolist()\n\nplt.figure(figsize=(15,5))\nindex = 0\nfor file in files:\n    image = Image.open(path + file)\n    plt.subplot(1, 3, index + 1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    index += 1\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **3: Cassava Mosiac Disease**"},{"metadata":{"trusted":true},"cell_type":"code","source":"df3 = df_train[df_train[\"label\"] == \"3\"]\nfiles = df3[\"image_id\"].sample(3).tolist()\n\nplt.figure(figsize=(15,5))\nindex = 0\nfor file in files:\n    image = Image.open(path + file)\n    plt.subplot(1, 3, index + 1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    index += 1\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **4: Healthy**"},{"metadata":{"trusted":true},"cell_type":"code","source":"df3 = df_train[df_train[\"label\"] == \"3\"]\nfiles = df3[\"image_id\"].sample(3).tolist()\n\nplt.figure(figsize=(15,5))\nindex = 0\nfor file in files:\n    image = Image.open(path + file)\n    plt.subplot(1, 3, index + 1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    index += 1\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.read_csv(work_dir + 'train.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Importing the json file with labels\n\nf = open(work_dir + 'label_num_to_disease_map.json')\nreal_labels = json.load(f)\nreal_labels = {int(k):v for k,v in real_labels.items()}\n\ndata['class_name'] = data.label.map(real_labels)\n\nfrom sklearn.model_selection import train_test_split\n\ntrain,val = train_test_split(data, test_size = 0.2, random_state = 2, stratify = data['class_name'])\n\nIMG_SIZE = 224\nsize = (IMG_SIZE,IMG_SIZE)\nn_CLASS = 5\n\ndatagen = ImageDataGenerator(preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,\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_set = datagen.flow_from_dataframe(train,directory = train_path,\n                         seed=42,\n                         x_col = 'image_id',\n                         y_col = 'class_name',\n                         target_size = size,\n                         #color_mode=\"rgb\",\n                         class_mode = 'categorical',\n                         interpolation = 'nearest',\n                         shuffle = True,\n                         batch_size = 20)\n\nval_set = datagen.flow_from_dataframe(val,directory = train_path,\n                         seed=42,\n                         x_col = 'image_id',\n                         y_col = 'class_name',\n                         target_size = size,\n                         #color_mode=\"rgb\",\n                         class_mode = 'categorical',\n                         interpolation = 'nearest',\n                         shuffle = True,\n                         batch_size = 20)\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.layers import ZeroPadding2D, Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Input\nfrom keras.layers import GlobalAveragePooling2D, MaxPooling2D\nfrom keras.models import Model, Sequential\nfrom keras.callbacks import ModelCheckpoint\nfrom keras import regularizers\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def main_block(x, filters, n, strides, dropout):\n    # Normal part\n    x_res = Conv2D(filters, (3,3), strides=strides, padding=\"same\")(x)# , kernel_regularizer=l2(5e-4)\n    x_res = BatchNormalization()(x_res)\n    x_res = Activation('relu')(x_res)\n    x_res = Conv2D(filters, (3,3), padding=\"same\")(x_res)\n    # Alternative branch\n    x = Conv2D(filters, (1,1), strides=strides)(x)\n    # Merge Branches\n    x = Add()([x_res, x])\n\n    for i in range(n-1):\n        # Residual conection\n        x_res = BatchNormalization()(x)\n        x_res = Activation('relu')(x_res)\n        x_res = Conv2D(filters, (3,3), padding=\"same\")(x_res)\n        # Apply dropout if given\n        if dropout: x_res = Dropout(dropout)(x)\n        # Second part\n        x_res = BatchNormalization()(x_res)\n        x_res = Activation('relu')(x_res)\n        x_res = Conv2D(filters, (3,3), padding=\"same\")(x_res)\n        # Merge branches\n        x = Add()([x, x_res])\n\n    # Inter block part\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n    return x\n\ndef build_model(input_dims, output_dim, n, k, act= \"relu\", dropout=None):\n    \"\"\" Builds the model. Params:\n        - n: number of layers. WRNs are of the form WRN-N-K\n             It must satisfy that (N-4)%6 = 0\n        - k: Widening factor. WRNs are of the form WRN-N-K\n             It must satisfy that K%2 = 0\n        - input_dims: input dimensions for the model\n        - output_dim: output dimensions for the model\n        - dropout: dropout rate - default=0 (not recomended >0.3)\n        - act: activation function - default=relu. Build your custom\n     one with keras.backend (ex: swish, e-swish)\n    \"\"\"\n    # Ensure n & k are correct\n    assert (n-4)%6 == 0\n    assert k%2 == 0\n    n = (n-4)//6 \n    # This returns a tensor input to the model\n    inputs = Input(shape=(input_dims))\n\n    # Head of the model\n    x = Conv2D(16, (3,3), padding=\"same\")(inputs)\n    x = BatchNormalization()(x)\n    x = Activation('relu')(x)\n\n    # 3 Blocks (normal-residual)\n    x = main_block(x, 16*k, n, (1,1), dropout) # 0\n    x = main_block(x, 32*k, n, (2,2), dropout) # 1\n    x = main_block(x, 64*k, n, (2,2), dropout) # 2\n\n    # Final part of the model\n    x = AveragePooling2D((8,8))(x)\n    x = Flatten()(x)\n    outputs = Dense(output_dim, activation=\"softmax\")(x)\n\n    model = Model(inputs=inputs, outputs=outputs)\n    return model\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = build_model((224,224,3), 11,16,4)\nmodel.compile(\"adam\",\"categorical_crossentropy\", ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = model\nfilepath = \"best_model_1.hdf5\"\n\ncheckpoint = ModelCheckpoint(filepath, verbose=1, save_best_only=True)\ncallbacks_list = [checkpoint]\n    \n    \nhistory = model.fit_generator(\n           train_set,\n           steps_per_epoch=train_set.n//train_set.batch_size,\n           epochs=1,\n           validation_data=val_set,\n           validation_steps=val_set.n//val_set.batch_size,  callbacks=callbacks_list)\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import load_model\nimport h5py\n","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":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"keras.utils.plot_model(model)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = model\nfilepath = \"EffNetB0_512_8.h5\"\n\ncheckpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')\ncallbacks_list = [checkpoint]\n    \n    \nhistory = model.fit_generator(\n           train_set,\n           steps_per_epoch=train_set.n//train_set.batch_size,\n           epochs=20,\n           validation_data=val_set,\n           validation_steps=val_set.n//val_set.batch_size,  callbacks=callbacks_list)\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot results\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc)+1)\n\nplt.plot(epochs, acc, 'g', label='Training accuracy')\nplt.plot(epochs, val_acc, 'r', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'g', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TEST_DIR = '../input/cassava-leaf-disease-classification/test_images/'\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images = os.listdir(TEST_DIR)\npredictions = []\n\nfor image in test_images:\n    img = Image.open(TEST_DIR + image)\n    img = img.resize(size)\n    img = np.expand_dims(img, axis=0)\n    predictions.extend(model.predict(img).argmax(axis = 1))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# Creating the CSV for final submission\n\nsub = pd.DataFrame({'image_id': test_images, 'label': predictions})\ndisplay(sub)\nsub.to_csv('submission.csv', index = False)\n","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}