{"cells":[{"metadata":{},"cell_type":"markdown","source":"# **Objective**\nThis is to show how to submit the trained model with a sample trained model (>83%).\nRefer to the following notebooks for better models. I learned a lot from them.\n\n> **Reference**\n> >   Cassava Disease 🌿 Keras + TF + EfficentNet (90%)<br>\n> >     https://www.kaggle.com/andreshg/cassava-disease-keras-tf-efficentnet-90\n> \n> >   Cassava EfficientNetB3 Beginner (88%) <br>\n> >     https://www.kaggle.com/khanrahim/cassava-efficientnetb3-beginner-88\n\n# **Prerequisite**\nDownload \"model_sample.h5\" from https://www.kaggle.com/matsumuranaoki/samplemodel <br>\nand upload by clicking \"Add data\" in the \"Data\" section on the top right of your notebook. \n\n# **Instruction**\n1. In the code, modify the following file path accordingly.<br>\n   final_model = models.load_model('../input/samplemodel/model_sample.h5')\n2. Turn off \"Internet\" in the \"Setting\" section on the right of your notebook.\n3. Save & Run All (Commit) the code on your notebook. \"submission.csv\" will be created in \"output\" folder in \"Data\" section.\n4. On the \"Cassava Leaf Disease Classification\" page, click \"Submit Predictions\" and select your notebook, version and output file (submission.csv).\n\nPlease note that submission would take ~2 hours as it uses different test set which contains many data.\n\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from keras import models\nfrom keras_preprocessing.image import ImageDataGenerator\nimport pandas as pd\nimport numpy as np\nimport os\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\n\ntrain_csv_location = '../input/cassava-leaf-disease-classification/train.csv'\ntrain_image_location = '../input/cassava-leaf-disease-classification/train_images'\ntest_image_location = '../input/cassava-leaf-disease-classification/test_images'\n\ntraindf = pd.read_csv(train_csv_location, dtype=str)\ntest_image_dir = os.listdir(test_image_location)\ntestdf = pd.DataFrame(test_image_dir, columns=['image_id'])\n\ntrain, valid = train_test_split(traindf, test_size = 0.05, \n    random_state = 42, stratify = traindf['label']\n    )\n\nIMG_SIZE = 456\nsize = (IMG_SIZE,IMG_SIZE)\nn_CLASS = 5\nBATCH_SIZE = 15\n\ndatagen_train = ImageDataGenerator(\n    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)\n\ntest_datagen=ImageDataGenerator(\n    preprocessing_function= tf.keras.applications.efficientnet.preprocess_input,\n)\n\ntrain_generator = datagen_train.flow_from_dataframe(\n    dataframe = train,\n    directory = train_image_location,\n    seed= 42,\n    x_col = 'image_id',\n    y_col = 'label',\n    target_size= size,\n    class_mode= 'categorical',\n    interpolation= 'nearest',\n    shuffle= True,\n    batch_size= BATCH_SIZE\n)\n\ntest_generator=test_datagen.flow_from_dataframe(\n    dataframe=testdf,\n    directory= test_image_location,\n    seed=42,\n    x_col = 'image_id',\n    y_col = None,\n    target_size= size,\n    interpolation= 'nearest',\n    shuffle= False,\n    batch_size=1,\n    class_mode=None,\n)\nSTEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size\nSTEP_SIZE_TEST=test_generator.n//test_generator.batch_size\n\n# evaluate the best model\nfinal_model = models.load_model('../input/samplemodel/model_sample.h5')\n\n# predict output\ntest_generator.reset()\npred = final_model.predict(test_generator, steps = STEP_SIZE_TEST, verbose = 1)\n\npredicted_class_indices=np.argmax(pred,axis=1)\n\nlabels = (train_generator.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npredictions = [labels[k] for k in predicted_class_indices]\n\n# export to csv\nfilenames=test_generator.filenames\nresults=pd.DataFrame({\"image_id\":filenames, \"label\":predictions})\nresults.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}