{"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-12T23:07:57.89885Z","iopub.execute_input":"2021-11-12T23:07:57.899195Z","iopub.status.idle":"2021-11-12T23:08:03.766581Z","shell.execute_reply.started":"2021-11-12T23:07:57.899106Z","shell.execute_reply":"2021-11-12T23:08:03.765813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load DataFrame\n","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-12T23:08:03.768326Z","iopub.execute_input":"2021-11-12T23:08:03.768633Z","iopub.status.idle":"2021-11-12T23:08:03.941501Z","shell.execute_reply.started":"2021-11-12T23:08:03.768592Z","shell.execute_reply":"2021-11-12T23:08:03.940664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-12T23:08:03.9427Z","iopub.execute_input":"2021-11-12T23:08:03.943042Z","iopub.status.idle":"2021-11-12T23:08:03.963406Z","shell.execute_reply.started":"2021-11-12T23:08:03.943007Z","shell.execute_reply":"2021-11-12T23:08:03.962794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extract Test 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-12T23:08:03.965459Z","iopub.execute_input":"2021-11-12T23:08:03.966184Z","iopub.status.idle":"2021-11-12T23:08:08.638392Z","shell.execute_reply.started":"2021-11-12T23:08:03.966138Z","shell.execute_reply":"2021-11-12T23:08:08.637708Z"},"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-12T23:08:08.639647Z","iopub.execute_input":"2021-11-12T23:08:08.640059Z","iopub.status.idle":"2021-11-12T23:08:08.643766Z","shell.execute_reply.started":"2021-11-12T23:08:08.640014Z","shell.execute_reply":"2021-11-12T23:08:08.643054Z"},"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-12T23:08:08.645177Z","iopub.execute_input":"2021-11-12T23:08:08.64559Z","iopub.status.idle":"2021-11-12T23:08:08.695439Z","shell.execute_reply.started":"2021-11-12T23:08:08.645546Z","shell.execute_reply":"2021-11-12T23:08:08.69489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Generator","metadata":{}},{"cell_type":"code","source":"BATCH_SIZE = 128\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-12T23:08:08.696931Z","iopub.execute_input":"2021-11-12T23:08:08.697378Z","iopub.status.idle":"2021-11-12T23:08:59.644972Z","shell.execute_reply.started":"2021-11-12T23:08:08.697344Z","shell.execute_reply":"2021-11-12T23:08:59.643209Z"},"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-crop-models/cancer_detection_model_crop_v07.h5')\n                               \ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-12T23:08:59.646151Z","iopub.status.idle":"2021-11-12T23:08:59.646628Z","shell.execute_reply.started":"2021-11-12T23:08:59.646373Z","shell.execute_reply":"2021-11-12T23:08:59.646399Z"},"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-12T23:08:59.648464Z","iopub.status.idle":"2021-11-12T23:08:59.649507Z","shell.execute_reply.started":"2021-11-12T23:08:59.649197Z","shell.execute_reply":"2021-11-12T23:08:59.649227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_probs[:10,].round(2))","metadata":{"execution":{"iopub.status.busy":"2021-11-12T23:08:59.650987Z","iopub.status.idle":"2021-11-12T23:08:59.651677Z","shell.execute_reply.started":"2021-11-12T23:08:59.651406Z","shell.execute_reply":"2021-11-12T23:08:59.651434Z"},"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-12T23:08:59.653066Z","iopub.status.idle":"2021-11-12T23:08:59.653767Z","shell.execute_reply.started":"2021-11-12T23:08:59.653499Z","shell.execute_reply":"2021-11-12T23:08:59.653526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_probs[:,1]\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-12T23:08:59.65515Z","iopub.status.idle":"2021-11-12T23:08:59.65587Z","shell.execute_reply.started":"2021-11-12T23:08:59.655581Z","shell.execute_reply":"2021-11-12T23:08:59.65561Z"},"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-12T23:08:59.657223Z","iopub.status.idle":"2021-11-12T23:08:59.657941Z","shell.execute_reply.started":"2021-11-12T23:08:59.657642Z","shell.execute_reply":"2021-11-12T23:08:59.657669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}