{"cells":[{"metadata":{},"cell_type":"markdown","source":"[Check out this notebook](https://www.kaggle.com/ayuraj/baseline-efficientnet-using-tf-and-w-b) which is used to train the model. I am continuously working to improve the score.\n\nThis notebook will be used to run inference and create the `sample_submission.csv` file."},{"metadata":{"trusted":true},"cell_type":"code","source":"%%capture\n!pip install ../input/keras-efficientnet-whl/Keras_Applications-1.0.8-py3-none-any.whl\n!pip install ../input/keras-efficientnet-whl/efficientnet-1.1.1-py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.models import *\n\nimport efficientnet.keras as efn\n\nimport os\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"MODEL_PATH_ROOT = '../input/cassava-model/'\nMODEL_PATHS = [MODEL_PATH_ROOT+file for file in os.listdir(MODEL_PATH_ROOT) if file.endswith('.h5')]\n\nprint(MODEL_PATHS)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tf.keras.backend.clear_session()\n\nmodel = tf.keras.models.load_model(MODEL_PATHS[0])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_SIZE = 380\nBATCH_SIZE = 1\nAUTOTUNE = tf.data.experimental.AUTOTUNE\nCLASS_NUMS = 5\n\nTEST_DIR = '../input/cassava-leaf-disease-classification/test_images/'\ntest_images = os.listdir(TEST_DIR)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_paths = [os.path.join(TEST_DIR, img_path) for img_path in test_images]\n\nprint(image_paths)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def decode_image(img_path):\n    img = tf.io.read_file(img_path)\n    img = tf.image.decode_jpeg(img)\n    img = tf.cast(img, tf.float32) / 255.0\n    img = tf.image.resize(img, (IMAGE_SIZE,IMAGE_SIZE))\n    \n    return img\n\ndef get_dataloader():\n    testloader = tf.data.Dataset.from_tensor_slices((image_paths)).map(decode_image, num_parallel_calls=AUTOTUNE).batch(BATCH_SIZE).prefetch(AUTOTUNE)\n    \n    return testloader","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testloader = get_dataloader()\n\nimg = next(iter(testloader))\nprint(img.shape)\nplt.imshow(img[0]);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_size = len(image_paths)\ntest_preds = np.zeros((test_size, CLASS_NUMS))\n\nfor model_path in MODEL_PATHS:\n    print(f'Loading model from: {model_path}')\n    \n    # Load trained model\n    tf.keras.backend.clear_session()\n    model = tf.keras.models.load_model(model_path)\n    \n    # Get dataloader\n    testloader = get_dataloader()\n    \n    # Perform inference\n    predictions = []\n    preds = model.predict(testloader)\n    predictions.extend(preds)\n\n    test_preds += np.array(predictions) / len(MODEL_PATHS)\n    \nfinal_test_preds = np.argmax(test_preds, axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_test_preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.DataFrame({'image_id': test_images, 'label': final_test_preds})\ndisplay(sub)\nsub.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}