{"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-28T20:35:39.930089Z","iopub.execute_input":"2021-11-28T20:35:39.930781Z","iopub.status.idle":"2021-11-28T20:35:45.889350Z","shell.execute_reply.started":"2021-11-28T20:35:39.930652Z","shell.execute_reply":"2021-11-28T20:35:45.888373Z"},"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-28T20:35:45.891115Z","iopub.execute_input":"2021-11-28T20:35:45.891341Z","iopub.status.idle":"2021-11-28T20:35:46.042882Z","shell.execute_reply.started":"2021-11-28T20:35:45.891314Z","shell.execute_reply":"2021-11-28T20:35:46.042024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T20:35:46.044420Z","iopub.execute_input":"2021-11-28T20:35:46.044894Z","iopub.status.idle":"2021-11-28T20:35:46.063919Z","shell.execute_reply.started":"2021-11-28T20:35:46.044850Z","shell.execute_reply":"2021-11-28T20:35:46.063072Z"},"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-28T20:35:46.065941Z","iopub.execute_input":"2021-11-28T20:35:46.066336Z","iopub.status.idle":"2021-11-28T20:35:47.071816Z","shell.execute_reply.started":"2021-11-28T20:35:46.066291Z","shell.execute_reply":"2021-11-28T20:35:47.070931Z"},"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-28T20:35:47.074097Z","iopub.execute_input":"2021-11-28T20:35:47.074573Z","iopub.status.idle":"2021-11-28T20:35:47.079158Z","shell.execute_reply.started":"2021-11-28T20:35:47.074528Z","shell.execute_reply":"2021-11-28T20:35:47.078346Z"},"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-28T20:35:47.080536Z","iopub.execute_input":"2021-11-28T20:35:47.081561Z","iopub.status.idle":"2021-11-28T20:35:47.129064Z","shell.execute_reply.started":"2021-11-28T20:35:47.081516Z","shell.execute_reply":"2021-11-28T20:35:47.128181Z"},"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-28T20:35:47.130658Z","iopub.execute_input":"2021-11-28T20:35:47.131107Z","iopub.status.idle":"2021-11-28T20:37:21.412740Z","shell.execute_reply.started":"2021-11-28T20:35:47.131060Z","shell.execute_reply":"2021-11-28T20:37:21.411709Z"},"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_model.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T20:37:21.414202Z","iopub.execute_input":"2021-11-28T20:37:21.414500Z","iopub.status.idle":"2021-11-28T20:37:21.956398Z","shell.execute_reply.started":"2021-11-28T20:37:21.414467Z","shell.execute_reply":"2021-11-28T20:37:21.955600Z"},"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-28T20:37:21.957422Z","iopub.execute_input":"2021-11-28T20:37:21.958054Z","iopub.status.idle":"2021-11-28T20:42:22.811674Z","shell.execute_reply.started":"2021-11-28T20:37:21.958020Z","shell.execute_reply":"2021-11-28T20:42:22.810775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_probs[:10,].round(2))","metadata":{"execution":{"iopub.status.busy":"2021-11-28T20:42:22.814678Z","iopub.execute_input":"2021-11-28T20:42:22.815104Z","iopub.status.idle":"2021-11-28T20:42:22.824621Z","shell.execute_reply.started":"2021-11-28T20:42:22.815059Z","shell.execute_reply":"2021-11-28T20:42:22.823048Z"},"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-28T20:42:22.826215Z","iopub.execute_input":"2021-11-28T20:42:22.826671Z","iopub.status.idle":"2021-11-28T20:42:22.899353Z","shell.execute_reply.started":"2021-11-28T20:42:22.826625Z","shell.execute_reply":"2021-11-28T20:42:22.898659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_probs[:,1]\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T20:42:22.900729Z","iopub.execute_input":"2021-11-28T20:42:22.901236Z","iopub.status.idle":"2021-11-28T20:42:22.912680Z","shell.execute_reply.started":"2021-11-28T20:42:22.901192Z","shell.execute_reply":"2021-11-28T20:42:22.911765Z"},"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-28T20:42:22.914024Z","iopub.execute_input":"2021-11-28T20:42:22.914334Z","iopub.status.idle":"2021-11-28T20:42:23.188941Z","shell.execute_reply.started":"2021-11-28T20:42:22.914292Z","shell.execute_reply":"2021-11-28T20:42:23.188032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}