{"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":"","metadata":{}},{"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-28T19:27:12.059519Z","iopub.execute_input":"2021-11-28T19:27:12.060176Z","iopub.status.idle":"2021-11-28T19:27:13.815191Z","shell.execute_reply.started":"2021-11-28T19:27:12.060061Z","shell.execute_reply":"2021-11-28T19:27:13.814421Z"},"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-28T19:27:20.126648Z","iopub.execute_input":"2021-11-28T19:27:20.127269Z","iopub.status.idle":"2021-11-28T19:27:20.183522Z","shell.execute_reply.started":"2021-11-28T19:27:20.127227Z","shell.execute_reply":"2021-11-28T19:27:20.182783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T19:27:35.937289Z","iopub.execute_input":"2021-11-28T19:27:35.938111Z","iopub.status.idle":"2021-11-28T19:27:35.953079Z","shell.execute_reply.started":"2021-11-28T19:27:35.938063Z","shell.execute_reply":"2021-11-28T19:27:35.951971Z"},"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-28T19:27:40.715412Z","iopub.execute_input":"2021-11-28T19:27:40.715664Z","iopub.status.idle":"2021-11-28T19:27:40.746739Z","shell.execute_reply.started":"2021-11-28T19:27:40.715634Z","shell.execute_reply":"2021-11-28T19:27:40.746068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extract 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-28T19:27:45.888298Z","iopub.execute_input":"2021-11-28T19:27:45.888568Z","iopub.status.idle":"2021-11-28T19:27:45.924637Z","shell.execute_reply.started":"2021-11-28T19:27:45.888538Z","shell.execute_reply":"2021-11-28T19:27:45.92367Z"},"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 = (96,96)\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T19:28:06.036325Z","iopub.execute_input":"2021-11-28T19:28:06.036597Z","iopub.status.idle":"2021-11-28T19:29:23.339091Z","shell.execute_reply.started":"2021-11-28T19:28:06.036567Z","shell.execute_reply":"2021-11-28T19:29:23.338328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load label","metadata":{}},{"cell_type":"code","source":"cnn = keras.models.load_model('../input/cancer-detection-final2/cancer_model_v12.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T19:31:29.405218Z","iopub.execute_input":"2021-11-28T19:31:29.405857Z","iopub.status.idle":"2021-11-28T19:31:33.398209Z","shell.execute_reply.started":"2021-11-28T19:31:29.405796Z","shell.execute_reply":"2021-11-28T19:31:33.397449Z"},"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-28T19:31:41.974631Z","iopub.execute_input":"2021-11-28T19:31:41.975324Z","iopub.status.idle":"2021-11-28T19:36:18.011632Z","shell.execute_reply.started":"2021-11-28T19:31:41.975277Z","shell.execute_reply":"2021-11-28T19:36:18.01086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_pred = np.argmax(test_probs, axis=1)\n#print(test_pred[:10]) \nprint(test_probs[:10,].round(2))\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-28T19:40:41.305415Z","iopub.execute_input":"2021-11-28T19:40:41.305912Z","iopub.status.idle":"2021-11-28T19:40:41.31264Z","shell.execute_reply.started":"2021-11-28T19:40:41.305878Z","shell.execute_reply":"2021-11-28T19:40:41.311761Z"},"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-28T19:40:47.305278Z","iopub.execute_input":"2021-11-28T19:40:47.305532Z","iopub.status.idle":"2021-11-28T19:40:47.358861Z","shell.execute_reply.started":"2021-11-28T19:40:47.305504Z","shell.execute_reply":"2021-11-28T19:40:47.357868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission.label = test_pred\n#submission.head()\nsubmission.label = test_probs[:,1]\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T19:40:52.413859Z","iopub.execute_input":"2021-11-28T19:40:52.41497Z","iopub.status.idle":"2021-11-28T19:40:52.433219Z","shell.execute_reply.started":"2021-11-28T19:40:52.414926Z","shell.execute_reply":"2021-11-28T19:40:52.432583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission_final2.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T19:41:01.304091Z","iopub.execute_input":"2021-11-28T19:41:01.304383Z","iopub.status.idle":"2021-11-28T19:41:01.469998Z","shell.execute_reply.started":"2021-11-28T19:41:01.304351Z","shell.execute_reply":"2021-11-28T19:41:01.469246Z"},"trusted":true},"execution_count":null,"outputs":[]}]}