{"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\n\nThe notebook where this model is created can be found [here](https://www.kaggle.com/akashsdas/cassava-leaf-disease-classification/) and older submission for models of that notebook can be [here](https://www.kaggle.com/akashsdas/cassava-leaf-disease-classification-old-submission/)","metadata":{}},{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The model used here is `model-without-xla-and-mixed-precision.h5` because while using the other model `model.h5`, I was getting the below error as it was trained using `TPU` with `XLA` and `Mixed Precision` enabled.\n\n```bash\nInvalidArgumentError: No OpKernel was registered to support Op 'Conv2D' used by {{node sequential/inception_v3/conv2d/Conv2D}} with these attrs: [dilations=[1, 1, 1, 1], T=DT_BFLOAT16, strides=[1, 2, 2, 1], data_format=\"NHWC\", explicit_paddings=[], use_cudnn_on_gpu=true, padding=\"VALID\"]\nRegistered devices: [CPU, GPU]\nRegistered kernels:\n  device='XLA_CPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_BFLOAT16, DT_HALF]\n  device='XLA_GPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_BFLOAT16, DT_HALF]\n  device='GPU'; T in [DT_INT32]\n  device='GPU'; T in [DT_DOUBLE]\n  device='GPU'; T in [DT_FLOAT]\n  device='GPU'; T in [DT_HALF]\n  device='CPU'; T in [DT_INT32]\n  device='CPU'; T in [DT_DOUBLE]\n  device='CPU'; T in [DT_FLOAT]\n  device='CPU'; T in [DT_HALF]\n\n\t [[sequential/inception_v3/conv2d/Conv2D]] [Op:__inference_predict_function_11991]\n```","metadata":{}},{"cell_type":"code","source":"# constants\n\nIMAGE_SIZE = [512, 512]\n\nMODEL_DIR = '../input/cassava-leaf-disease-classification-model/model-without-xla-and-mixed-precision.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":{}}]}