{"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-14T17:29:31.633392Z","iopub.execute_input":"2021-11-14T17:29:31.633949Z","iopub.status.idle":"2021-11-14T17:29:37.106797Z","shell.execute_reply.started":"2021-11-14T17:29:31.633849Z","shell.execute_reply":"2021-11-14T17:29:37.106019Z"},"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-14T17:29:37.108183Z","iopub.execute_input":"2021-11-14T17:29:37.108692Z","iopub.status.idle":"2021-11-14T17:29:37.306460Z","shell.execute_reply.started":"2021-11-14T17:29:37.108654Z","shell.execute_reply":"2021-11-14T17:29:37.305802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:29:37.309296Z","iopub.execute_input":"2021-11-14T17:29:37.312049Z","iopub.status.idle":"2021-11-14T17:29:37.334192Z","shell.execute_reply.started":"2021-11-14T17:29:37.312009Z","shell.execute_reply":"2021-11-14T17:29:37.333527Z"},"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-14T17:29:37.338021Z","iopub.execute_input":"2021-11-14T17:29:37.340694Z","iopub.status.idle":"2021-11-14T17:29:37.385280Z","shell.execute_reply.started":"2021-11-14T17:29:37.340657Z","shell.execute_reply":"2021-11-14T17:29:37.384661Z"},"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-14T17:29:37.388010Z","iopub.execute_input":"2021-11-14T17:29:37.388471Z","iopub.status.idle":"2021-11-14T17:29:38.330113Z","shell.execute_reply.started":"2021-11-14T17:29:37.388407Z","shell.execute_reply":"2021-11-14T17:29:38.329335Z"},"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-14T17:29:38.331738Z","iopub.execute_input":"2021-11-14T17:29:38.332155Z","iopub.status.idle":"2021-11-14T17:31:52.943037Z","shell.execute_reply.started":"2021-11-14T17:29:38.332115Z","shell.execute_reply":"2021-11-14T17:31:52.942261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load model","metadata":{}},{"cell_type":"code","source":"cnn_model = keras.models.load_model('../input/lk-cancer-detection-model-v01revised/cancer_model_v08.h5')\ncnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:31:52.944204Z","iopub.execute_input":"2021-11-14T17:31:52.945024Z","iopub.status.idle":"2021-11-14T17:31:56.349045Z","shell.execute_reply.started":"2021-11-14T17:31:52.944982Z","shell.execute_reply":"2021-11-14T17:31:56.348297Z"},"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-14T17:31:56.350372Z","iopub.execute_input":"2021-11-14T17:31:56.350721Z","iopub.status.idle":"2021-11-14T17:37:14.773552Z","shell.execute_reply.started":"2021-11-14T17:31:56.350681Z","shell.execute_reply":"2021-11-14T17:37:14.772799Z"},"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-14T17:37:14.775307Z","iopub.execute_input":"2021-11-14T17:37:14.775568Z","iopub.status.idle":"2021-11-14T17:37:14.783617Z","shell.execute_reply.started":"2021-11-14T17:37:14.775531Z","shell.execute_reply":"2021-11-14T17:37:14.782868Z"},"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-14T17:37:14.785099Z","iopub.execute_input":"2021-11-14T17:37:14.785422Z","iopub.status.idle":"2021-11-14T17:37:14.839202Z","shell.execute_reply.started":"2021-11-14T17:37:14.785371Z","shell.execute_reply":"2021-11-14T17:37:14.838341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:37:14.840690Z","iopub.execute_input":"2021-11-14T17:37:14.840967Z","iopub.status.idle":"2021-11-14T17:37:14.849660Z","shell.execute_reply.started":"2021-11-14T17:37:14.840933Z","shell.execute_reply":"2021-11-14T17:37:14.848944Z"},"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-14T17:37:55.219249Z","iopub.execute_input":"2021-11-14T17:37:55.219619Z","iopub.status.idle":"2021-11-14T17:37:55.507375Z","shell.execute_reply.started":"2021-11-14T17:37:55.219572Z","shell.execute_reply":"2021-11-14T17:37:55.506441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}