{"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":"# Import packages","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nimport pickle\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import *\n\nimport zipfile \nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:08:59.134263Z","iopub.execute_input":"2021-11-21T10:08:59.135112Z","iopub.status.idle":"2021-11-21T10:09:05.222568Z","shell.execute_reply.started":"2021-11-21T10:08:59.135006Z","shell.execute_reply":"2021-11-21T10:09:05.221776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load dataframe test","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/histopathologic-cancer-detection/sample_submission.csv')\n\nprint('Test Set Size:', test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:09:05.224172Z","iopub.execute_input":"2021-11-21T10:09:05.225391Z","iopub.status.idle":"2021-11-21T10:09:05.372969Z","shell.execute_reply.started":"2021-11-21T10:09:05.225347Z","shell.execute_reply":"2021-11-21T10:09:05.371945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:09:05.374914Z","iopub.execute_input":"2021-11-21T10:09:05.375171Z","iopub.status.idle":"2021-11-21T10:09:05.394115Z","shell.execute_reply.started":"2021-11-21T10:09:05.375142Z","shell.execute_reply":"2021-11-21T10:09:05.393247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The id in the csv file does not have .tif extension, therefore we need to add it here.\ntest['id'] = test['id'].apply(lambda x:f'{x}.tif')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:09:05.395352Z","iopub.execute_input":"2021-11-21T10:09:05.395678Z","iopub.status.idle":"2021-11-21T10:09:05.429528Z","shell.execute_reply.started":"2021-11-21T10:09:05.395641Z","shell.execute_reply":"2021-11-21T10:09:05.428819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extract images","metadata":{}},{"cell_type":"code","source":"test_path = \"../input/histopathologic-cancer-detection/test\"\nprint('Test Images:', len(os.listdir(test_path)))","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:09:05.431621Z","iopub.execute_input":"2021-11-21T10:09:05.431861Z","iopub.status.idle":"2021-11-21T10:09:06.497317Z","shell.execute_reply.started":"2021-11-21T10:09:05.431828Z","shell.execute_reply":"2021-11-21T10:09:06.496484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data generator","metadata":{}},{"cell_type":"code","source":"BATCH_SIZE = 64\n\ntest_datagen = ImageDataGenerator(rescale=1/255)\n\ntest_loader = test_datagen.flow_from_dataframe(\n    dataframe = test,\n    directory = test_path,\n    x_col = 'id',\n    batch_size = BATCH_SIZE,\n    shuffle = False,\n    class_mode = None,\n    target_size = (96,96)\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:09:06.498883Z","iopub.execute_input":"2021-11-21T10:09:06.499431Z","iopub.status.idle":"2021-11-21T10:10:02.328634Z","shell.execute_reply.started":"2021-11-21T10:09:06.499379Z","shell.execute_reply":"2021-11-21T10:10:02.327863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load model","metadata":{}},{"cell_type":"code","source":"cnn_model = keras.models.load_model('../input/cancerdetection-full-model/cancer_model_v01.h5')\ncnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:10:02.330018Z","iopub.execute_input":"2021-11-21T10:10:02.330461Z","iopub.status.idle":"2021-11-21T10:10:05.652816Z","shell.execute_reply.started":"2021-11-21T10:10:02.330421Z","shell.execute_reply":"2021-11-21T10:10:05.652043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# test prediction","metadata":{}},{"cell_type":"code","source":"test_probs = cnn_model.predict(test_loader)\nprint(test_probs.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:10:05.654138Z","iopub.execute_input":"2021-11-21T10:10:05.654400Z","iopub.status.idle":"2021-11-21T10:14:56.791872Z","shell.execute_reply.started":"2021-11-21T10:10:05.654363Z","shell.execute_reply":"2021-11-21T10:14:56.791133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = np.argmax(test_probs, axis=1)\nprint(test_pred[:10]) ","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:14:56.795166Z","iopub.execute_input":"2021-11-21T10:14:56.795367Z","iopub.status.idle":"2021-11-21T10:14:56.803138Z","shell.execute_reply.started":"2021-11-21T10:14:56.795340Z","shell.execute_reply":"2021-11-21T10:14:56.802468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare submission","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/histopathologic-cancer-detection/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:14:56.804286Z","iopub.execute_input":"2021-11-21T10:14:56.804959Z","iopub.status.idle":"2021-11-21T10:14:56.859079Z","shell.execute_reply.started":"2021-11-21T10:14:56.804907Z","shell.execute_reply":"2021-11-21T10:14:56.858267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:14:56.860285Z","iopub.execute_input":"2021-11-21T10:14:56.860594Z","iopub.status.idle":"2021-11-21T10:14:56.869968Z","shell.execute_reply.started":"2021-11-21T10:14:56.860559Z","shell.execute_reply":"2021-11-21T10:14:56.869294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission1.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T10:14:56.871301Z","iopub.execute_input":"2021-11-21T10:14:56.872019Z","iopub.status.idle":"2021-11-21T10:14:57.039835Z","shell.execute_reply.started":"2021-11-21T10:14:56.871966Z","shell.execute_reply":"2021-11-21T10:14:57.039134Z"},"trusted":true},"execution_count":null,"outputs":[]}]}