{"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-14T05:10:08.829338Z","iopub.execute_input":"2021-11-14T05:10:08.829688Z","iopub.status.idle":"2021-11-14T05:10:14.884757Z","shell.execute_reply.started":"2021-11-14T05:10:08.829604Z","shell.execute_reply":"2021-11-14T05:10:14.883735Z"},"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-14T05:10:14.886782Z","iopub.execute_input":"2021-11-14T05:10:14.887536Z","iopub.status.idle":"2021-11-14T05:10:15.037813Z","shell.execute_reply.started":"2021-11-14T05:10:14.887492Z","shell.execute_reply":"2021-11-14T05:10:15.036817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T05:10:15.039585Z","iopub.execute_input":"2021-11-14T05:10:15.039918Z","iopub.status.idle":"2021-11-14T05:10:15.063215Z","shell.execute_reply.started":"2021-11-14T05:10:15.039875Z","shell.execute_reply":"2021-11-14T05:10:15.062337Z"},"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-14T05:10:15.065768Z","iopub.execute_input":"2021-11-14T05:10:15.066184Z","iopub.status.idle":"2021-11-14T05:10:15.105019Z","shell.execute_reply.started":"2021-11-14T05:10:15.066140Z","shell.execute_reply":"2021-11-14T05:10:15.104059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extract test 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-14T05:10:15.106375Z","iopub.execute_input":"2021-11-14T05:10:15.106603Z","iopub.status.idle":"2021-11-14T05:10:16.163803Z","shell.execute_reply.started":"2021-11-14T05:10:15.106576Z","shell.execute_reply":"2021-11-14T05:10:16.162794Z"},"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-14T05:10:16.165580Z","iopub.execute_input":"2021-11-14T05:10:16.165808Z","iopub.status.idle":"2021-11-14T05:11:34.735522Z","shell.execute_reply.started":"2021-11-14T05:10:16.165774Z","shell.execute_reply":"2021-11-14T05:11:34.733434Z"},"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-with-cropping-layers-model/cancer_model_v07.h5')\ncnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T05:11:34.737032Z","iopub.execute_input":"2021-11-14T05:11:34.738568Z","iopub.status.idle":"2021-11-14T05:11:38.278787Z","shell.execute_reply.started":"2021-11-14T05:11:34.738508Z","shell.execute_reply":"2021-11-14T05:11:38.277776Z"},"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-14T05:11:38.281793Z","iopub.execute_input":"2021-11-14T05:11:38.282202Z","iopub.status.idle":"2021-11-14T05:17:32.815829Z","shell.execute_reply.started":"2021-11-14T05:11:38.282126Z","shell.execute_reply":"2021-11-14T05:17:32.814800Z"},"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-14T05:17:32.818200Z","iopub.execute_input":"2021-11-14T05:17:32.818845Z","iopub.status.idle":"2021-11-14T05:17:32.826294Z","shell.execute_reply.started":"2021-11-14T05:17:32.818796Z","shell.execute_reply":"2021-11-14T05:17:32.825136Z"},"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-14T05:17:32.830714Z","iopub.execute_input":"2021-11-14T05:17:32.831634Z","iopub.status.idle":"2021-11-14T05:17:32.894974Z","shell.execute_reply.started":"2021-11-14T05:17:32.831589Z","shell.execute_reply":"2021-11-14T05:17:32.893993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T05:17:32.896585Z","iopub.execute_input":"2021-11-14T05:17:32.896883Z","iopub.status.idle":"2021-11-14T05:17:32.911071Z","shell.execute_reply.started":"2021-11-14T05:17:32.896843Z","shell.execute_reply":"2021-11-14T05:17:32.910061Z"},"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-14T05:18:30.419687Z","iopub.execute_input":"2021-11-14T05:18:30.420063Z","iopub.status.idle":"2021-11-14T05:18:30.592540Z","shell.execute_reply.started":"2021-11-14T05:18:30.420002Z","shell.execute_reply":"2021-11-14T05:18:30.591597Z"},"trusted":true},"execution_count":null,"outputs":[]}]}