{"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":"","metadata":{}},{"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-27T05:07:53.670477Z","iopub.execute_input":"2021-11-27T05:07:53.670812Z","iopub.status.idle":"2021-11-27T05:07:59.093309Z","shell.execute_reply.started":"2021-11-27T05:07:53.670717Z","shell.execute_reply":"2021-11-27T05:07:59.092528Z"},"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-27T05:07:59.096466Z","iopub.execute_input":"2021-11-27T05:07:59.097076Z","iopub.status.idle":"2021-11-27T05:07:59.239616Z","shell.execute_reply.started":"2021-11-27T05:07:59.097044Z","shell.execute_reply":"2021-11-27T05:07:59.238869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T05:07:59.240991Z","iopub.execute_input":"2021-11-27T05:07:59.241272Z","iopub.status.idle":"2021-11-27T05:07:59.258796Z","shell.execute_reply.started":"2021-11-27T05:07:59.241234Z","shell.execute_reply":"2021-11-27T05:07:59.258050Z"},"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-27T05:07:59.260926Z","iopub.execute_input":"2021-11-27T05:07:59.261204Z","iopub.status.idle":"2021-11-27T05:07:59.295629Z","shell.execute_reply.started":"2021-11-27T05:07:59.261153Z","shell.execute_reply":"2021-11-27T05:07:59.294965Z"},"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-27T05:07:59.296955Z","iopub.execute_input":"2021-11-27T05:07:59.297241Z","iopub.status.idle":"2021-11-27T05:08:00.148653Z","shell.execute_reply.started":"2021-11-27T05:07:59.297173Z","shell.execute_reply":"2021-11-27T05:08:00.147881Z"},"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-27T05:08:00.150008Z","iopub.execute_input":"2021-11-27T05:08:00.150284Z","iopub.status.idle":"2021-11-27T05:08:30.425037Z","shell.execute_reply.started":"2021-11-27T05:08:00.150245Z","shell.execute_reply":"2021-11-27T05:08:30.424253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load label","metadata":{}},{"cell_type":"code","source":"cnn = keras.models.load_model('../input/cancerdetection-tl-v02/cancer_model_v01.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T05:08:30.426367Z","iopub.execute_input":"2021-11-27T05:08:30.426782Z","iopub.status.idle":"2021-11-27T05:08:34.953529Z","shell.execute_reply.started":"2021-11-27T05:08:30.426742Z","shell.execute_reply":"2021-11-27T05:08:34.952801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test predictions","metadata":{}},{"cell_type":"code","source":"test_probs = cnn.predict(test_loader)\nprint(test_probs.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T05:08:34.954782Z","iopub.execute_input":"2021-11-27T05:08:34.955040Z","iopub.status.idle":"2021-11-27T05:13:09.430717Z","shell.execute_reply.started":"2021-11-27T05:08:34.955004Z","shell.execute_reply":"2021-11-27T05:13:09.429875Z"},"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-27T05:13:09.432889Z","iopub.execute_input":"2021-11-27T05:13:09.433149Z","iopub.status.idle":"2021-11-27T05:13:09.437980Z","shell.execute_reply.started":"2021-11-27T05:13:09.433114Z","shell.execute_reply":"2021-11-27T05:13:09.437303Z"},"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-27T05:13:09.440674Z","iopub.execute_input":"2021-11-27T05:13:09.441135Z","iopub.status.idle":"2021-11-27T05:13:09.498431Z","shell.execute_reply.started":"2021-11-27T05:13:09.441091Z","shell.execute_reply":"2021-11-27T05:13:09.497610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-27T05:13:09.499876Z","iopub.execute_input":"2021-11-27T05:13:09.500139Z","iopub.status.idle":"2021-11-27T05:13:09.508975Z","shell.execute_reply.started":"2021-11-27T05:13:09.500103Z","shell.execute_reply":"2021-11-27T05:13:09.508240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submissionTLV2.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-27T05:13:09.510587Z","iopub.execute_input":"2021-11-27T05:13:09.511226Z","iopub.status.idle":"2021-11-27T05:13:09.677912Z","shell.execute_reply.started":"2021-11-27T05:13:09.511172Z","shell.execute_reply":"2021-11-27T05:13:09.677228Z"},"trusted":true},"execution_count":null,"outputs":[]}]}