{"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-26T04:22:47.852124Z","iopub.execute_input":"2021-11-26T04:22:47.852485Z","iopub.status.idle":"2021-11-26T04:22:53.395674Z","shell.execute_reply.started":"2021-11-26T04:22:47.852378Z","shell.execute_reply":"2021-11-26T04:22:53.394792Z"},"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-26T04:22:53.397593Z","iopub.execute_input":"2021-11-26T04:22:53.397923Z","iopub.status.idle":"2021-11-26T04:22:53.497199Z","shell.execute_reply.started":"2021-11-26T04:22:53.397818Z","shell.execute_reply":"2021-11-26T04:22:53.495783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:22:53.498696Z","iopub.execute_input":"2021-11-26T04:22:53.498974Z","iopub.status.idle":"2021-11-26T04:22:53.516996Z","shell.execute_reply.started":"2021-11-26T04:22:53.498939Z","shell.execute_reply":"2021-11-26T04:22:53.516224Z"},"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-26T04:22:53.519032Z","iopub.execute_input":"2021-11-26T04:22:53.519312Z","iopub.status.idle":"2021-11-26T04:22:53.552202Z","shell.execute_reply.started":"2021-11-26T04:22:53.519276Z","shell.execute_reply":"2021-11-26T04:22:53.551440Z"},"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-26T04:22:53.553721Z","iopub.execute_input":"2021-11-26T04:22:53.554217Z","iopub.status.idle":"2021-11-26T04:22:58.920462Z","shell.execute_reply.started":"2021-11-26T04:22:53.554178Z","shell.execute_reply":"2021-11-26T04:22:58.918327Z"},"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-26T04:22:58.921898Z","iopub.execute_input":"2021-11-26T04:22:58.922153Z","iopub.status.idle":"2021-11-26T04:24:26.074308Z","shell.execute_reply.started":"2021-11-26T04:22:58.922113Z","shell.execute_reply":"2021-11-26T04:24:26.073573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load label","metadata":{}},{"cell_type":"code","source":"cnn = keras.models.load_model('../input/cancer-detection-model-transfer-learning-1121/cancer_model_v00.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:24:26.075485Z","iopub.execute_input":"2021-11-26T04:24:26.076543Z","iopub.status.idle":"2021-11-26T04:24:30.168781Z","shell.execute_reply.started":"2021-11-26T04:24:26.076501Z","shell.execute_reply":"2021-11-26T04:24:30.168059Z"},"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-26T04:26:01.943299Z","iopub.execute_input":"2021-11-26T04:26:01.943862Z","iopub.status.idle":"2021-11-26T04:30:43.809044Z","shell.execute_reply.started":"2021-11-26T04:26:01.943824Z","shell.execute_reply":"2021-11-26T04:30:43.808052Z"},"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-26T04:31:15.895449Z","iopub.execute_input":"2021-11-26T04:31:15.896233Z","iopub.status.idle":"2021-11-26T04:31:15.902464Z","shell.execute_reply.started":"2021-11-26T04:31:15.896173Z","shell.execute_reply":"2021-11-26T04:31:15.901633Z"},"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-26T04:31:19.769244Z","iopub.execute_input":"2021-11-26T04:31:19.769792Z","iopub.status.idle":"2021-11-26T04:31:19.823416Z","shell.execute_reply.started":"2021-11-26T04:31:19.769753Z","shell.execute_reply":"2021-11-26T04:31:19.822586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:31:22.953243Z","iopub.execute_input":"2021-11-26T04:31:22.953533Z","iopub.status.idle":"2021-11-26T04:31:22.967000Z","shell.execute_reply.started":"2021-11-26T04:31:22.953502Z","shell.execute_reply":"2021-11-26T04:31:22.966265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submissionTL.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:31:46.217924Z","iopub.execute_input":"2021-11-26T04:31:46.218535Z","iopub.status.idle":"2021-11-26T04:31:46.385735Z","shell.execute_reply.started":"2021-11-26T04:31:46.218494Z","shell.execute_reply":"2021-11-26T04:31:46.384960Z"},"trusted":true},"execution_count":null,"outputs":[]}]}