{"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\n","metadata":{"execution":{"iopub.status.busy":"2021-11-12T12:27:50.288466Z","iopub.execute_input":"2021-11-12T12:27:50.28883Z","iopub.status.idle":"2021-11-12T12:27:52.430805Z","shell.execute_reply.started":"2021-11-12T12:27:50.288739Z","shell.execute_reply":"2021-11-12T12:27:52.42976Z"},"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-12T12:27:52.433023Z","iopub.execute_input":"2021-11-12T12:27:52.433381Z","iopub.status.idle":"2021-11-12T12:27:52.495831Z","shell.execute_reply.started":"2021-11-12T12:27:52.433338Z","shell.execute_reply":"2021-11-12T12:27:52.494608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-12T12:27:52.497617Z","iopub.execute_input":"2021-11-12T12:27:52.498016Z","iopub.status.idle":"2021-11-12T12:27:52.516414Z","shell.execute_reply.started":"2021-11-12T12:27:52.497963Z","shell.execute_reply":"2021-11-12T12:27:52.515406Z"},"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-12T12:27:52.518123Z","iopub.execute_input":"2021-11-12T12:27:52.518902Z","iopub.status.idle":"2021-11-12T12:27:52.553841Z","shell.execute_reply.started":"2021-11-12T12:27:52.518856Z","shell.execute_reply":"2021-11-12T12:27:52.552702Z"},"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-12T12:27:52.55686Z","iopub.execute_input":"2021-11-12T12:27:52.557604Z","iopub.status.idle":"2021-11-12T12:27:52.616281Z","shell.execute_reply.started":"2021-11-12T12:27:52.55756Z","shell.execute_reply":"2021-11-12T12:27:52.615085Z"},"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-12T12:27:52.618169Z","iopub.execute_input":"2021-11-12T12:27:52.61979Z","iopub.status.idle":"2021-11-12T12:28:20.904295Z","shell.execute_reply.started":"2021-11-12T12:27:52.619711Z","shell.execute_reply":"2021-11-12T12:28:20.903256Z"},"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-model-v01/cancer_model_v02.h5')\ncnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-12T12:28:20.906077Z","iopub.execute_input":"2021-11-12T12:28:20.906643Z","iopub.status.idle":"2021-11-12T12:28:23.425453Z","shell.execute_reply.started":"2021-11-12T12:28:20.906585Z","shell.execute_reply":"2021-11-12T12:28:23.424439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test prediction","metadata":{}},{"cell_type":"code","source":"test_probs = cnn_model.predict(test_loader)\nprint(test_probs.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-12T12:28:23.427913Z","iopub.execute_input":"2021-11-12T12:28:23.428572Z","iopub.status.idle":"2021-11-12T12:29:45.773265Z","shell.execute_reply.started":"2021-11-12T12:28:23.428512Z","shell.execute_reply":"2021-11-12T12:29:45.772399Z"},"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-12T12:29:45.776562Z","iopub.execute_input":"2021-11-12T12:29:45.776789Z","iopub.status.idle":"2021-11-12T12:29:45.784325Z","shell.execute_reply.started":"2021-11-12T12:29:45.776759Z","shell.execute_reply":"2021-11-12T12:29:45.783056Z"},"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-12T12:29:45.786281Z","iopub.execute_input":"2021-11-12T12:29:45.787412Z","iopub.status.idle":"2021-11-12T12:29:45.850673Z","shell.execute_reply.started":"2021-11-12T12:29:45.78735Z","shell.execute_reply":"2021-11-12T12:29:45.849717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-12T12:29:45.852233Z","iopub.execute_input":"2021-11-12T12:29:45.852564Z","iopub.status.idle":"2021-11-12T12:29:45.86445Z","shell.execute_reply.started":"2021-11-12T12:29:45.852523Z","shell.execute_reply":"2021-11-12T12:29:45.863129Z"},"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-12T12:29:45.866037Z","iopub.execute_input":"2021-11-12T12:29:45.867346Z","iopub.status.idle":"2021-11-12T12:29:46.037467Z","shell.execute_reply.started":"2021-11-12T12:29:45.867299Z","shell.execute_reply":"2021-11-12T12:29:46.036529Z"},"trusted":true},"execution_count":null,"outputs":[]}]}