{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Cassava Leaf Disease Classification Submission","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport cv2\n\nfrom tensorflow.keras import preprocessing\nfrom tensorflow.keras.models import load_model","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# constants\n\nIMAGE_SIZE = [512, 512]\nMODEL_DIR = '../input/cassava-leaf-disease-classification-model/model.h5'\nTEST_DIR = '../input/cassava-leaf-disease-classification/sample_submission.csv'\nTEST_IMG_DIR = '../input/cassava-leaf-disease-classification/test_images/'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data preparation\n\ntest_df = pd.read_csv(TEST_DIR)\ntest_df['label'] = test_df['label'].astype('str')\n\ntest_data_gen = preprocessing.image.ImageDataGenerator(rescale=1./255)\ntest_gen = test_data_gen.flow_from_dataframe(\n    test_df,\n    directory=TEST_IMG_DIR,\n    x_col='image_id',\n    y_col='label',\n    target_size=IMAGE_SIZE,\n    class_mode='sparse'\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading the model trained using tpu\n\nmodel = load_model(MODEL_DIR)\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prediction\n\npredictions = model.predict(test_gen)\npred_labels = [] # [1, 2, 0, 4, 2, ...]\n\nfor pred in predictions:\n    pred_labels.append(np.argmax(pred))\n    \npred_labels","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission\n\nsubmission = pd.DataFrame({'image_id': test_df.image_id, 'label': pred_labels})\nsubmission.to_csv('submission.csv', index=False) \nsubmission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}}]}