{"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-17T04:15:02.805472Z","iopub.execute_input":"2021-11-17T04:15:02.805908Z","iopub.status.idle":"2021-11-17T04:15:04.705876Z","shell.execute_reply.started":"2021-11-17T04:15:02.805804Z","shell.execute_reply":"2021-11-17T04:15:04.705098Z"},"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-17T04:15:04.710777Z","iopub.execute_input":"2021-11-17T04:15:04.711145Z","iopub.status.idle":"2021-11-17T04:15:04.774815Z","shell.execute_reply.started":"2021-11-17T04:15:04.711081Z","shell.execute_reply":"2021-11-17T04:15:04.773785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T04:15:04.776425Z","iopub.execute_input":"2021-11-17T04:15:04.776781Z","iopub.status.idle":"2021-11-17T04:15:04.793068Z","shell.execute_reply.started":"2021-11-17T04:15:04.776741Z","shell.execute_reply":"2021-11-17T04:15:04.792066Z"},"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-17T04:15:04.796101Z","iopub.execute_input":"2021-11-17T04:15:04.797170Z","iopub.status.idle":"2021-11-17T04:15:04.833767Z","shell.execute_reply.started":"2021-11-17T04:15:04.797133Z","shell.execute_reply":"2021-11-17T04:15:04.832883Z"},"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-17T04:15:04.835397Z","iopub.execute_input":"2021-11-17T04:15:04.835882Z","iopub.status.idle":"2021-11-17T04:15:04.875718Z","shell.execute_reply.started":"2021-11-17T04:15:04.835839Z","shell.execute_reply":"2021-11-17T04:15:04.874585Z"},"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 = (64,64)\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T04:15:04.877616Z","iopub.execute_input":"2021-11-17T04:15:04.878232Z","iopub.status.idle":"2021-11-17T04:15:21.652370Z","shell.execute_reply.started":"2021-11-17T04:15:04.878195Z","shell.execute_reply":"2021-11-17T04:15:21.651600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load model","metadata":{}},{"cell_type":"code","source":"cnn_model = keras.models.load_model('../input/cancer-detection-model-v03-1116/cancer_model_v03.h5')\ncnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T04:15:21.653674Z","iopub.execute_input":"2021-11-17T04:15:21.654122Z","iopub.status.idle":"2021-11-17T04:15:23.432246Z","shell.execute_reply.started":"2021-11-17T04:15:21.654085Z","shell.execute_reply":"2021-11-17T04:15:23.431432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test predictions","metadata":{}},{"cell_type":"code","source":"test_probs = cnn_model.predict(test_loader)\nprint(test_probs.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T04:15:23.433654Z","iopub.execute_input":"2021-11-17T04:15:23.434110Z","iopub.status.idle":"2021-11-17T04:18:34.650974Z","shell.execute_reply.started":"2021-11-17T04:15:23.434070Z","shell.execute_reply":"2021-11-17T04:18:34.648604Z"},"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-17T04:18:34.652656Z","iopub.execute_input":"2021-11-17T04:18:34.652930Z","iopub.status.idle":"2021-11-17T04:18:34.664916Z","shell.execute_reply.started":"2021-11-17T04:18:34.652894Z","shell.execute_reply":"2021-11-17T04:18:34.663613Z"},"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-17T04:18:34.666283Z","iopub.execute_input":"2021-11-17T04:18:34.666510Z","iopub.status.idle":"2021-11-17T04:18:34.755870Z","shell.execute_reply.started":"2021-11-17T04:18:34.666478Z","shell.execute_reply":"2021-11-17T04:18:34.754858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T04:18:34.757398Z","iopub.execute_input":"2021-11-17T04:18:34.757666Z","iopub.status.idle":"2021-11-17T04:18:34.775540Z","shell.execute_reply.started":"2021-11-17T04:18:34.757630Z","shell.execute_reply":"2021-11-17T04:18:34.774837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T04:19:06.680059Z","iopub.execute_input":"2021-11-17T04:19:06.680746Z","iopub.status.idle":"2021-11-17T04:19:06.855145Z","shell.execute_reply.started":"2021-11-17T04:19:06.680712Z","shell.execute_reply":"2021-11-17T04:19:06.854279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}