{"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-21T09:20:24.474704Z","iopub.execute_input":"2021-11-21T09:20:24.475151Z","iopub.status.idle":"2021-11-21T09:20:29.746523Z","shell.execute_reply.started":"2021-11-21T09:20:24.475114Z","shell.execute_reply":"2021-11-21T09:20:29.745735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load dataframe test","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/hcd-cropped/sample_submission.csv')\n\nprint('Test Set Size:', test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:20:32.713738Z","iopub.execute_input":"2021-11-21T09:20:32.714455Z","iopub.status.idle":"2021-11-21T09:20:32.878461Z","shell.execute_reply.started":"2021-11-21T09:20:32.714413Z","shell.execute_reply":"2021-11-21T09:20:32.877725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:20:35.633592Z","iopub.execute_input":"2021-11-21T09:20:35.634315Z","iopub.status.idle":"2021-11-21T09:20:35.655737Z","shell.execute_reply.started":"2021-11-21T09:20:35.634279Z","shell.execute_reply":"2021-11-21T09:20:35.655087Z"},"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-21T09:20:38.544123Z","iopub.execute_input":"2021-11-21T09:20:38.54441Z","iopub.status.idle":"2021-11-21T09:20:38.578766Z","shell.execute_reply.started":"2021-11-21T09:20:38.544358Z","shell.execute_reply":"2021-11-21T09:20:38.578039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extract images","metadata":{}},{"cell_type":"code","source":"test_path = \"../input/hcd-cropped/test\"\nprint('Test Images:', len(os.listdir(test_path)))","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:20:41.380904Z","iopub.execute_input":"2021-11-21T09:20:41.381153Z","iopub.status.idle":"2021-11-21T09:20:44.040651Z","shell.execute_reply.started":"2021-11-21T09:20:41.381125Z","shell.execute_reply":"2021-11-21T09:20:44.039795Z"},"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 = (32,32)\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:21:53.053899Z","iopub.execute_input":"2021-11-21T09:21:53.054156Z","iopub.status.idle":"2021-11-21T09:24:18.917166Z","shell.execute_reply.started":"2021-11-21T09:21:53.054127Z","shell.execute_reply":"2021-11-21T09:24:18.916423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load model","metadata":{}},{"cell_type":"code","source":"cnn = keras.models.load_model('../input/cancerdetection-cropped-model/cancer_model_v03.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:24:29.20472Z","iopub.execute_input":"2021-11-21T09:24:29.205275Z","iopub.status.idle":"2021-11-21T09:24:31.998901Z","shell.execute_reply.started":"2021-11-21T09:24:29.205233Z","shell.execute_reply":"2021-11-21T09:24:31.998171Z"},"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-21T09:24:38.963621Z","iopub.execute_input":"2021-11-21T09:24:38.96389Z","iopub.status.idle":"2021-11-21T09:30:38.397712Z","shell.execute_reply.started":"2021-11-21T09:24:38.963861Z","shell.execute_reply":"2021-11-21T09:30:38.396965Z"},"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-21T09:31:22.477262Z","iopub.execute_input":"2021-11-21T09:31:22.477546Z","iopub.status.idle":"2021-11-21T09:31:22.48266Z","shell.execute_reply.started":"2021-11-21T09:31:22.477515Z","shell.execute_reply":"2021-11-21T09:31:22.481924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare submission","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/hcd-cropped/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:31:31.864366Z","iopub.execute_input":"2021-11-21T09:31:31.864665Z","iopub.status.idle":"2021-11-21T09:31:31.920763Z","shell.execute_reply.started":"2021-11-21T09:31:31.864636Z","shell.execute_reply":"2021-11-21T09:31:31.919942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:31:46.647261Z","iopub.execute_input":"2021-11-21T09:31:46.647564Z","iopub.status.idle":"2021-11-21T09:31:46.662024Z","shell.execute_reply.started":"2021-11-21T09:31:46.647516Z","shell.execute_reply":"2021-11-21T09:31:46.661292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission1.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:41:31.995366Z","iopub.execute_input":"2021-11-21T09:41:31.995961Z","iopub.status.idle":"2021-11-21T09:41:32.162242Z","shell.execute_reply.started":"2021-11-21T09:41:31.995924Z","shell.execute_reply":"2021-11-21T09:41:32.161513Z"},"trusted":true},"execution_count":null,"outputs":[]}]}