{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Imports\nimport pandas as pd\nimport numpy as np\nfrom PIL import Image\n\nimport tensorflow as tf       \nimport tensorflow.keras as keras\nimport tensorflow.keras.layers as layers      \nimport tensorflow.keras.layers.experimental.preprocessing as preprocessing     \nimport seaborn as sns\nimport os, cv2, json, warnings           \nwarnings.simplefilter(\"ignore\")        \nimport matplotlib.pyplot as plt\nfrom matplotlib import gridspec    \nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator \nfrom tensorflow.python.keras import optimizers    \nfrom tensorflow.python.keras.models import Sequential \nfrom tensorflow.keras.applications import MobileNet, EfficientNetB4\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.python.keras.layers import  Convolution2D, MaxPooling2D\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Input, GlobalAveragePooling2D\nfrom tensorflow.python.keras import backend as K\nK.clear_session()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df_train = pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")\ndf_train.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train[\"label\"] = df_train[\"label\"].astype(str)\ndf_train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.set_style(\"whitegrid\")\nfig, ax = plt.subplots(figsize = (10,8))\n\nfor i in ['top', 'right', 'left']:\n    ax.spines[i].set_visible(False)\nax.spines['bottom'].set_color('black')\n\nsns.countplot(df_train[\"label\"], edgecolor = \"black\", palette = reversed(sns.color_palette(\"Spectral\", 5)))\nplt.xlabel('Classes', fontfamily = 'serif', size = 15)\nplt.ylabel('Counts', fontfamily = 'serif', size = 15)\nplt.xticks(fontfamily = 'serif', size = 12)\nplt.yticks(fontfamily = 'serif', size = 12)\nax.grid(axis = 'y', linestyle = '--', alpha = 0.9)\nplt.show()","execution_count":null,"outputs":[]},{"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\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df0 = df_train[df_train[\"label\"] == \"4\"]\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\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BATCH_SIZE = 16\nIMG_SIZE = 380\nEPOCHS = 20\ninput_shape = (IMG_SIZE, IMG_SIZE, 3)\ntarget_size = (IMG_SIZE, IMG_SIZE)\nSTEPS_PER_EPOCHS = len(df_train)*0.8//BATCH_SIZE\nVALIDATION_STEPS = len(df_train)*0.2//BATCH_SIZE","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(validation_split = 0.2,\n                                    rotation_range = 45,\n                                    zoom_range = 0.2,\n                                    horizontal_flip = True,\n                                    vertical_flip = True,\n                                    fill_mode = 'nearest',\n                                    height_shift_range = 0.2,\n                                    width_shift_range = 0.2,\n                                  )\n\ntrain_generator = train_datagen.flow_from_dataframe(df_train, \n                                                    directory = path, \n                                                    x_col = 'image_id', \n                                                    y_col = 'label', \n                                                    subset = \"training\",\n                                                    batch_size = BATCH_SIZE,\n                                                    class_mode = 'sparse',\n                                                    seed = 2020,\n                                                    shuffle = True,\n                                                    target_size = (IMG_SIZE, IMG_SIZE))\n\n\nval_datagen = ImageDataGenerator(validation_split = 0.2)\n\nval_generator = val_datagen.flow_from_dataframe(df_train, \n                                                directory = path,\n                                                x_col = 'image_id',\n                                                y_col = 'label', \n                                                subset = \"validation\",\n                                                batch_size = BATCH_SIZE,\n                                                class_mode = 'sparse',\n                                                seed = 2020,\n                                                shuffle = True,\n                                                target_size = (IMG_SIZE, IMG_SIZE))\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.models import Model, load_model\n\n\ndef create_Inception():\n    base_model = EfficientNetB4(include_top=False, weights=\"imagenet\", input_shape=input_shape)\n\n    # Rebuild top\n    inputs = Input(shape=input_shape)\n\n    model = base_model(inputs)\n    pooling = GlobalAveragePooling2D()(model)\n    dropout = Dropout(0.2)(pooling)\n\n    outputs = Dense(5, activation=\"softmax\", name=\"dense\", dtype='float32')(dropout)\n    # Compile  \n    inception = Model(inputs=inputs, outputs=outputs)\n    #optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9, nesterov=True)\n    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3)\n    #loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.2, from_logits=True)\n    loss = \"sparse_categorical_crossentropy\"\n    \n    inception.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])\n    return inception\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = create_Inception()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_save = ModelCheckpoint('best_weights.h5',\n                              save_best_only = True,\n                              monitor = 'val_loss',\n                              mode = 'min', verbose = 1)\n\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_loss', factor = 0.3, \n                              patience = 2, min_delta = 1e-6, \n                              mode = 'min', verbose = 1)\n\nearly_stop = EarlyStopping(monitor = 'val_loss',\n                           patience = 3, mode = 'min',\n                           verbose = 1, restore_best_weights = True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(train_generator, steps_per_epoch = STEPS_PER_EPOCHS ,epochs = EPOCHS, validation_data = val_generator,\n                             validation_steps = VALIDATION_STEPS, callbacks = [model_save, reduce_lr, early_stop])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])         \nplt.title('Model accuracy')   \nplt.xlabel('Epoch')    \nplt.ylabel('Accuracy')             \nplt.legend(['Train', 'Val'], loc = 'upper left')            \nplt.show()\n       \n# plot training and validation loss values\nplt.plot(history.history['loss'])   \nplt.plot(history.history['val_loss'])    \nplt.title('Model loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(['Train', 'Val'], loc = 'upper left')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ss = pd.read_csv(os.path.join('../input/cassava-leaf-disease-classification', \"sample_submission.csv\"))\npreds = [] \nresults = []  \n  \nfor image_id in ss.image_id:         \n    image = Image.open(os.path.join('../input/cassava-leaf-disease-classification', \"test_images\", image_id))\n    image = image.resize((IMG_SIZE, IMG_SIZE)) \n    image = np.expand_dims(image, axis = 0)             \n    preds.append(np.argmax(model.predict(image)))        \n    res = max(set(preds), key = preds.count)                   \n    results.append(res)                \n                       \nss['label'] = results   \nss.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}