{"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 Statements","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport zipfile\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:43:02.034014Z","iopub.execute_input":"2021-11-07T22:43:02.034607Z","iopub.status.idle":"2021-11-07T22:43:09.158692Z","shell.execute_reply.started":"2021-11-07T22:43:02.034504Z","shell.execute_reply":"2021-11-07T22:43:09.157779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load DataFrame\n","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-07T22:43:09.160443Z","iopub.execute_input":"2021-11-07T22:43:09.160683Z","iopub.status.idle":"2021-11-07T22:43:09.321393Z","shell.execute_reply.started":"2021-11-07T22:43:09.160656Z","shell.execute_reply":"2021-11-07T22:43:09.320189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:43:09.322636Z","iopub.execute_input":"2021-11-07T22:43:09.322864Z","iopub.status.idle":"2021-11-07T22:43:09.345986Z","shell.execute_reply.started":"2021-11-07T22:43:09.322837Z","shell.execute_reply":"2021-11-07T22:43:09.344837Z"},"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-07T22:43:09.348307Z","iopub.execute_input":"2021-11-07T22:43:09.348552Z","iopub.status.idle":"2021-11-07T22:43:10.396017Z","shell.execute_reply.started":"2021-11-07T22:43:09.348525Z","shell.execute_reply":"2021-11-07T22:43:10.395026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".tif\"","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:43:10.398586Z","iopub.execute_input":"2021-11-07T22:43:10.398897Z","iopub.status.idle":"2021-11-07T22:43:10.402928Z","shell.execute_reply.started":"2021-11-07T22:43:10.398867Z","shell.execute_reply":"2021-11-07T22:43:10.402239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['id'] = test['id'].apply(append_ext)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:43:10.404087Z","iopub.execute_input":"2021-11-07T22:43:10.404676Z","iopub.status.idle":"2021-11-07T22:43:10.459555Z","shell.execute_reply.started":"2021-11-07T22:43:10.404602Z","shell.execute_reply":"2021-11-07T22:43:10.458587Z"},"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-07T22:43:10.461018Z","iopub.execute_input":"2021-11-07T22:43:10.461318Z","iopub.status.idle":"2021-11-07T22:44:55.421661Z","shell.execute_reply.started":"2021-11-07T22:43:10.461278Z","shell.execute_reply":"2021-11-07T22:44:55.420438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Model","metadata":{}},{"cell_type":"code","source":"cnn = keras.models.load_model('../input/cancer-detection-models/cancer_detection_model_v10.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:44:55.423508Z","iopub.execute_input":"2021-11-07T22:44:55.425297Z","iopub.status.idle":"2021-11-07T22:44:55.978794Z","shell.execute_reply.started":"2021-11-07T22:44:55.425248Z","shell.execute_reply":"2021-11-07T22:44:55.977434Z"},"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-07T22:44:55.980288Z","iopub.execute_input":"2021-11-07T22:44:55.980576Z","iopub.status.idle":"2021-11-07T22:51:18.746758Z","shell.execute_reply.started":"2021-11-07T22:44:55.980543Z","shell.execute_reply":"2021-11-07T22:51:18.745404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_probs[:10,].round(2))","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:51:18.749738Z","iopub.execute_input":"2021-11-07T22:51:18.750715Z","iopub.status.idle":"2021-11-07T22:51:18.757034Z","shell.execute_reply.started":"2021-11-07T22:51:18.75066Z","shell.execute_reply":"2021-11-07T22:51:18.756409Z"},"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-07T22:52:02.211814Z","iopub.execute_input":"2021-11-07T22:52:02.212206Z","iopub.status.idle":"2021-11-07T22:52:02.303798Z","shell.execute_reply.started":"2021-11-07T22:52:02.212167Z","shell.execute_reply":"2021-11-07T22:52:02.302747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_probs[:,1]\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-07T22:52:21.851811Z","iopub.execute_input":"2021-11-07T22:52:21.852137Z","iopub.status.idle":"2021-11-07T22:52:21.865961Z","shell.execute_reply.started":"2021-11-07T22:52:21.852107Z","shell.execute_reply":"2021-11-07T22:52:21.864923Z"},"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-07T22:52:40.801389Z","iopub.execute_input":"2021-11-07T22:52:40.802539Z","iopub.status.idle":"2021-11-07T22:52:41.086261Z","shell.execute_reply.started":"2021-11-07T22:52:40.802462Z","shell.execute_reply":"2021-11-07T22:52:41.085112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}