{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom tensorflow.keras.applications.inception_resnet_v2 import preprocess_input\nfrom keras import optimizers, losses, activations\nfrom keras.layers import Dense, Dropout, MaxPooling2D, BatchNormalization, GlobalAveragePooling2D\nfrom keras.callbacks import ModelCheckpoint","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Set Parameters"},{"metadata":{"trusted":true},"cell_type":"code","source":"Working_dir = '../input/cassava-leaf-disease-classification/'\nimage_size = 299\nIMAGE_S = [512, 512]\nclasses = 5","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Load the data file"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.label = df.label.astype('str')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data augmentation using ImageDataGenerator"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_gen = ImageDataGenerator(rotation_range=270, width_shift_range=0.2, \n                               height_shift_range=0.2, brightness_range=[0.1,0.6], \n                               shear_range=25, zoom_range=0.3, channel_shift_range=0.2, \n                               fill_mode=\"nearest\", horizontal_flip=True, vertical_flip=True, \n                               validation_split=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_gen = train_gen.flow_from_dataframe(df, directory=Working_dir+\"train_images\", \n                                          x_col='image_id', y_col='label', \n                                          target_size=(image_size, image_size), \n                                          class_mode='sparse', batch_size=64, \n                                          shuffle=True,subset='training')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_gen = ImageDataGenerator(validation_split=0.2)\n\nvalid_gen = valid_gen.flow_from_dataframe(df, directory=Working_dir+\"train_images\", \n                                          x_col='image_id', y_col='label', \n                                          target_size=(image_size, image_size), \n                                          class_mode='sparse', batch_size=64, \n                                          shuffle=True, subset='validation')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr_scheduler = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate=0.0001, \n    decay_steps=1000, \n    decay_rate=0.9)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_check = ModelCheckpoint(\n                            \"./saved_model.h5\",\n                            monitor = \"val_loss\",\n                            verbose = 1,\n                            save_best_only = True,\n                            save_weights_only = False,\n                            mode = \"min\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_adjust_layer = tf.keras.layers.Lambda(tf.keras.applications.inception_resnet_v2.preprocess_input, input_shape=[*IMAGE_S, 3])\n    \nbase_model = tf.keras.applications.InceptionResNetV2(weights=None, include_top=False)\nbase_model.trainable = True\n    \nmodel_incept = tf.keras.Sequential([\n    tf.keras.layers.BatchNormalization(renorm=True),\n    img_adjust_layer,\n    base_model,\n    tf.keras.layers.GlobalAveragePooling2D(),\n    tf.keras.layers.Dense(1024, activation='relu'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.BatchNormalization(renorm=True),\n    tf.keras.layers.Dense(1024, activation='relu'),\n    tf.keras.layers.Dense(classes, activation='softmax')])\n    \nmodel_incept.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=lr_scheduler, epsilon=0.001),\n    loss='sparse_categorical_crossentropy',  \n    metrics=['sparse_categorical_accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model_incept.fit(train_gen,  \n                    epochs=20,\n                    validation_data=valid_gen,callbacks = [model_check])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['sparse_categorical_accuracy', 'val_sparse_categorical_accuracy']].plot();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = []\nsubmission = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\nfor i in submission.image_id:\n    img = tf.keras.preprocessing.image.load_img(Working_dir+'test_images/' + i)\n    img = tf.keras.preprocessing.image.img_to_array(img)\n    img = tf.keras.preprocessing.image.smart_resize(img, (IMAGE_S[0], IMAGE_S[0]))\n    img = tf.keras.applications.inception_resnet_v2.preprocess_input(img)\n    img = tf.reshape(img, (-1, 512, 512, 3))\n    pred = model_incept.predict(img)\n    pred = pred.argmax()\n    predictions.append(pred)\n\nsubmission['label'] = predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('submission.csv', index=False) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission","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}