{"cells":[{"metadata":{},"cell_type":"markdown","source":"<h1><center><b> Cassava prediction notebook </b></center></h1>"},{"metadata":{},"cell_type":"markdown","source":"This notebook contains the prediction part using the weights obtained from the baseline notebook.\n\nPlease go through the baseline notebook to understand how the best weights are obtained.\n\n**Link for baseline notebook:** https://www.kaggle.com/pndeepak/cassava-xception-baseline\n\nPS : This notebook does not require GPU to run."},{"metadata":{},"cell_type":"markdown","source":"**Outcomes from this notebook:**\n\n1. Understanding how you can use pretrained weights from a base model and use it to implement solutions faster.\n2. Understanding the hacks to develop a inference notebook."},{"metadata":{},"cell_type":"markdown","source":"# Importing the necessary libraries"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom tensorflow import keras","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Preparing our Deep Learning model\n\nSince we already have our pretrained weights we will be passing **weights=None** for our **Xception** model and then later load our weights and assign to the model.\n\n**Remember**: We have to do this because the notebook rules are that the internet connection must be off but transfer learning techniques require internet connection to load the model weights in real time."},{"metadata":{"trusted":true},"cell_type":"code","source":"model = keras.models.Sequential()\nmodel.add(keras.applications.Xception(input_shape=(300, 300, 3), weights=None, include_top=False))\nmodel.add(keras.layers.GlobalAveragePooling2D())\nmodel.add(keras.layers.Dense(5, activation='softmax'))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading our best weights"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_weights(\"../input/cassava-xception-try/best_weights_xception.h5\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Predicting on test dataset"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"preds = []\nss = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\n\nfor image in ss.image_id:\n    img = keras.preprocessing.image.load_img('../input/cassava-leaf-disease-classification/test_images/' + image)\n    img = keras.preprocessing.image.img_to_array(img)\n    img = keras.preprocessing.image.smart_resize(img, (300, 300))\n    img = np.expand_dims(img, 0)\n    prediction = model.predict(img)\n    preds.append(np.argmax(prediction))\n\nmy_submission = pd.DataFrame({'image_id': ss.image_id, 'label': preds})\nmy_submission.to_csv('submission.csv', index=False) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualizing our submission file"},{"metadata":{"trusted":true},"cell_type":"code","source":"my_submission","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}