{"cells":[{"metadata":{},"cell_type":"markdown","source":"**IMPORTING NECESSARY LIBRARIES**"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport torch\nimport math\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n\nimport os\nimport seaborn as sn\nimport json\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential, Model \nfrom tensorflow.keras import layers\nfrom tensorflow.keras.layers import Conv2D, GlobalAveragePooling2D, Dense, Flatten, Dropout, BatchNormalization\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**UNDERSTANDING THE DATA**"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#credits - https://www.kaggle.com/ihelon/cassava-leaf-disease-exploratory-data-analysis\n\ndirec = \"../input/cassava-leaf-disease-classification/\"\n\nwith open(os.path.join(direc, \"label_num_to_disease_map.json\")) as file:\n    map_classes = json.loads(file.read())\n    map_classes = {int(k) : v for k, v in map_classes.items()}\n\ninput_files = os.listdir(os.path.join(direc, \"train_images\"))\nprint(\"Number of train images:\",len(input_files))\n\ndf_train = pd.read_csv(os.path.join(direc, \"train.csv\"))\n\ndf_train[\"class_name\"] = df_train[\"label\"].map(map_classes)\nprint(map_classes)\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(8, 4))\nsn.countplot(y=\"class_name\", data=df_train);\n\ndf_train.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**LOADING DATASETS AND RUNNING AUGMENTATIONS**"},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ndata_gen = ImageDataGenerator(\n                                rotation_range=270,\n                                width_shift_range=0.2,\n                                height_shift_range=0.2,\n                                brightness_range=[0.1,0.9],\n                                shear_range=25,\n                                zoom_range=0.3,\n                                channel_shift_range=0.1,\n                                horizontal_flip=True,\n                                vertical_flip=True,\n                                rescale=1/255,\n                                validation_split=0.2\n)\n\nvalid_gen = ImageDataGenerator(\n                               rescale=1/255,\n                               validation_split = 0.2\n)\n\nIMG_SIZE, BATCH_SIZE = 312, 32\ndf_train[\"label\"] = df_train[\"label\"].astype(str)\ntr_dataset = data_gen.flow_from_dataframe(\n                            dataframe=df_train,\n                            directory = direc + \"train_images\",\n                            x_col = \"image_id\",\n                            y_col = \"label\",\n                            target_size = (IMG_SIZE, IMG_SIZE),\n                            class_mode = \"categorical\",\n                            batch_size = BATCH_SIZE,\n                            shuffle = True,\n                            subset = \"training\",\n)\n\nvalid_dataset = valid_gen.flow_from_dataframe(\n                            dataframe=df_train,\n                            directory = direc + \"train_images\",\n                            x_col = \"image_id\",\n                            y_col = \"label\",\n                            target_size = (IMG_SIZE, IMG_SIZE),\n                            class_mode = \"categorical\",\n                            batch_size = BATCH_SIZE,\n                            shuffle = False,\n                            subset = \"validation\"\n)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**DATA VISUALIZATION**"},{"metadata":{"trusted":true},"cell_type":"code","source":"for i,(images,labels) in enumerate(tr_dataset):\n    plt.figure(figsize = (16,12))\n    for j in range(16):\n        plt.subplot(4,4,j+1)\n        plt.axis(\"off\")\n        plt.imshow(images[j])\n        plt.title(map_classes[np.argmax(labels[j])])\n    break","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**CREATING A MODEL**"},{"metadata":{"trusted":true},"cell_type":"code","source":"def customModel(weights = None):\n    \n    base_model = keras.applications.InceptionResNetV2(include_top = False,weights = weights, input_shape = [IMG_SIZE,IMG_SIZE,3])\n    model = Sequential()\n    model.add(base_model)\n    model.add(BatchNormalization(axis=-1))\n    model.add(GlobalAveragePooling2D())\n    model.add(Dropout(0.5))\n    model.add(Dense(256,activation = \"relu\"))\n    model.add(Dense(5, activation = \"softmax\"))\n    \n    model.compile(loss = \"categorical_crossentropy\", optimizer = Adam(lr = 0.001), metrics = [\"acc\"])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = customModel(weights = \"imagenet\")\n#model.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**TRAINING THE MODEL**"},{"metadata":{"trusted":true},"cell_type":"code","source":"#LEARNING RATE SCHEDULER\ndef lr_scheduler(epoch,lr):\n    if epoch > 3:\n        return lr/1.125\n    return lr\n\nearly_stopping = EarlyStopping(monitor = \"acc\", mode = \"max\",patience = 7)\nlr_scheduler = tf.keras.callbacks.LearningRateScheduler(lr_scheduler)\ncheck_point = tf.keras.callbacks.ModelCheckpoint(\"./CasavaLeafDiseaseDetectModel.h5\", \n                                               monitor='val_loss',save_best_only=True)\nhistory = model.fit(tr_dataset,\n                    validation_data = valid_dataset,\n                    epochs = 25,\n                    callbacks = [early_stopping, lr_scheduler, check_point]\n                   )","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**PLOTTING LEARNING CURVES**"},{"metadata":{"trusted":true},"cell_type":"code","source":"acc = history.history[\"acc\"]\nv_acc = history.history[\"val_acc\"]\n\nloss = history.history[\"loss\"]\nv_loss = history.history[\"loss\"]\n\nepochs = range(25)\n\nplt.figure(figsize = (8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs, acc, label = \"Training Set Accuracy\")\nplt.plot(epochs, v_acc, label = \"Validation Set Accuracy\")\nplt.legend(loc = \"lower right\")\nplt.title(\"Training and Validation Accuracy\")\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs, loss, label = \"Training Loss\")\nplt.plot(epochs, v_loss, label = \"Validation Loss\")\nplt.legend(loc = \"lower right\")\nplt.title(\"Training and Validation Loss\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = model = tf.keras.models.load_model(\"../input/casavaleafdiseasemodel-tf/CasavaLeafDiseaseModel_epoch_12_acc_85.h5\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**TEST IMAGES PREDICTION AND SUBMISSION**"},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = []\ntest_direc = direc + \"test_images/\"\nsample_sub_csv = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\n\nfor image in sample_sub_csv.image_id:\n    img = tf.keras.preprocessing.image.load_img('../input/cassava-leaf-disease-classification/test_images/' + image)\n    img = tf.keras.preprocessing.image.img_to_array(img)\n    img = tf.keras.preprocessing.image.smart_resize(img, (IMG_SIZE, IMG_SIZE))\n    img = tf.reshape(img, (-1, IMG_SIZE, IMG_SIZE, 3))\n    prediction = model.predict(img/255)\n    preds.append(np.argmax(prediction))\n\nfinal_submission = pd.DataFrame({'image_id': sample_sub_csv.image_id, 'label': preds})\nfinal_submission.to_csv('submission.csv', index=False) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(final_submission.head())","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}