{"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\nfrom sklearn.utils import shuffle\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:50:09.782836Z","iopub.execute_input":"2021-12-08T18:50:09.783814Z","iopub.status.idle":"2021-12-08T18:50:09.788782Z","shell.execute_reply.started":"2021-12-08T18:50:09.783771Z","shell.execute_reply":"2021-12-08T18:50:09.788183Z"},"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-12-08T18:28:37.903518Z","iopub.execute_input":"2021-12-08T18:28:37.903922Z","iopub.status.idle":"2021-12-08T18:28:37.97105Z","shell.execute_reply.started":"2021-12-08T18:28:37.903875Z","shell.execute_reply":"2021-12-08T18:28:37.970186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:28:37.972442Z","iopub.execute_input":"2021-12-08T18:28:37.973329Z","iopub.status.idle":"2021-12-08T18:28:37.98369Z","shell.execute_reply.started":"2021-12-08T18:28:37.973282Z","shell.execute_reply":"2021-12-08T18:28:37.982779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:28:37.984805Z","iopub.execute_input":"2021-12-08T18:28:37.985068Z","iopub.status.idle":"2021-12-08T18:28:38.30292Z","shell.execute_reply.started":"2021-12-08T18:28:37.985003Z","shell.execute_reply":"2021-12-08T18:28:38.302106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_data['label']\ntrain_df, valid_df = train_test_split(train_data, test_size=0.2, random_state=45, stratify=y)\n\nprint(train_df.shape)\nprint(valid_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:28:38.305021Z","iopub.execute_input":"2021-12-08T18:28:38.305238Z","iopub.status.idle":"2021-12-08T18:28:38.494585Z","shell.execute_reply.started":"2021-12-08T18:28:38.305211Z","shell.execute_reply":"2021-12-08T18:28:38.493608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".tif\"","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:28:38.496225Z","iopub.execute_input":"2021-12-08T18:28:38.496527Z","iopub.status.idle":"2021-12-08T18:28:38.500795Z","shell.execute_reply.started":"2021-12-08T18:28:38.496486Z","shell.execute_reply":"2021-12-08T18:28:38.499947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['id'] = train_df['id'].apply(append_ext)\nvalid_df['id'] = valid_df['id'].apply(append_ext)\nvalid_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:28:38.502442Z","iopub.execute_input":"2021-12-08T18:28:38.50275Z","iopub.status.idle":"2021-12-08T18:28:38.650019Z","shell.execute_reply.started":"2021-12-08T18:28:38.50271Z","shell.execute_reply":"2021-12-08T18:28:38.649153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_valid = valid_df['label']","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:28:38.651393Z","iopub.execute_input":"2021-12-08T18:28:38.651615Z","iopub.status.idle":"2021-12-08T18:28:38.655568Z","shell.execute_reply.started":"2021-12-08T18:28:38.651587Z","shell.execute_reply":"2021-12-08T18:28:38.654731Z"},"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-12-08T18:28:38.656499Z","iopub.execute_input":"2021-12-08T18:28:38.657269Z","iopub.status.idle":"2021-12-08T18:28:38.703568Z","shell.execute_reply.started":"2021-12-08T18:28:38.657235Z","shell.execute_reply":"2021-12-08T18:28:38.702703Z"},"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-12-08T18:28:38.704864Z","iopub.execute_input":"2021-12-08T18:28:38.705732Z","iopub.status.idle":"2021-12-08T18:28:38.738433Z","shell.execute_reply.started":"2021-12-08T18:28:38.705685Z","shell.execute_reply":"2021-12-08T18:28:38.737555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Generator","metadata":{}},{"cell_type":"code","source":"train_path = '../input/histopathologic-cancer-detection/train'\n","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:28:38.739956Z","iopub.execute_input":"2021-12-08T18:28:38.740452Z","iopub.status.idle":"2021-12-08T18:28:38.744641Z","shell.execute_reply.started":"2021-12-08T18:28:38.740409Z","shell.execute_reply":"2021-12-08T18:28:38.743835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 64\n\ntest_datagen = ImageDataGenerator(rescale=1/255)\nvalid_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)\n\n\nvalid_loader = valid_datagen.flow_from_dataframe(\n    dataframe = valid_df,\n    directory = train_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-12-08T18:28:38.746129Z","iopub.execute_input":"2021-12-08T18:28:38.746602Z","iopub.status.idle":"2021-12-08T18:30:04.155502Z","shell.execute_reply.started":"2021-12-08T18:28:38.74656Z","shell.execute_reply":"2021-12-08T18:30:04.154667Z"},"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_v25.h5')\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:30:04.158098Z","iopub.execute_input":"2021-12-08T18:30:04.158393Z","iopub.status.idle":"2021-12-08T18:30:04.88735Z","shell.execute_reply.started":"2021-12-08T18:30:04.158361Z","shell.execute_reply":"2021-12-08T18:30:04.886427Z"},"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-12-08T18:30:04.888642Z","iopub.execute_input":"2021-12-08T18:30:04.888877Z","iopub.status.idle":"2021-12-08T18:34:58.428786Z","shell.execute_reply.started":"2021-12-08T18:30:04.888849Z","shell.execute_reply":"2021-12-08T18:34:58.428109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_probs[:10,].round(2))","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:34:58.429773Z","iopub.execute_input":"2021-12-08T18:34:58.430535Z","iopub.status.idle":"2021-12-08T18:34:58.436998Z","shell.execute_reply.started":"2021-12-08T18:34:58.430483Z","shell.execute_reply":"2021-12-08T18:34:58.436173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_probs = cnn.predict(valid_loader)\nprint(valid_probs.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:34:58.438266Z","iopub.execute_input":"2021-12-08T18:34:58.438652Z","iopub.status.idle":"2021-12-08T18:39:08.453493Z","shell.execute_reply.started":"2021-12-08T18:34:58.438621Z","shell.execute_reply":"2021-12-08T18:39:08.452545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_pred = np.argmax(valid_probs, axis=1)\nprint(valid_pred[:10])","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:39:08.45483Z","iopub.execute_input":"2021-12-08T18:39:08.455118Z","iopub.status.idle":"2021-12-08T18:39:08.462579Z","shell.execute_reply.started":"2021-12-08T18:39:08.45508Z","shell.execute_reply":"2021-12-08T18:39:08.461709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_valid, valid_pred)\ncm_df = pd.DataFrame(cm)\ncm_df","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:41:47.214195Z","iopub.execute_input":"2021-12-08T18:41:47.214501Z","iopub.status.idle":"2021-12-08T18:41:47.290475Z","shell.execute_reply.started":"2021-12-08T18:41:47.214468Z","shell.execute_reply":"2021-12-08T18:41:47.289607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(cm, index=['True No', 'True Has'], \n            columns=['Pred No', 'Pred Has'] )","metadata":{"execution":{"iopub.status.busy":"2021-12-08T19:12:00.068641Z","iopub.execute_input":"2021-12-08T19:12:00.069359Z","iopub.status.idle":"2021-12-08T19:12:00.081204Z","shell.execute_reply.started":"2021-12-08T19:12:00.069316Z","shell.execute_reply":"2021-12-08T19:12:00.08011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- We can see 20432 sample is correctly predicted as No cancer, and 16163 samples is correctly predicted as Has Cancers. \n- There are 5750 samples predicted as has cancer, but actually dont't have cancer.\n- Ther are 1660 samples predicted as has no-cancer, but actually have cancer.","metadata":{}},{"cell_type":"code","source":"print(classification_report(y_valid, valid_pred))","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:50:17.437304Z","iopub.execute_input":"2021-12-08T18:50:17.437591Z","iopub.status.idle":"2021-12-08T18:50:17.523953Z","shell.execute_reply.started":"2021-12-08T18:50:17.437561Z","shell.execute_reply":"2021-12-08T18:50:17.522979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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-12-08T18:39:08.500453Z","iopub.status.idle":"2021-12-08T18:39:08.500835Z","shell.execute_reply.started":"2021-12-08T18:39:08.500652Z","shell.execute_reply":"2021-12-08T18:39:08.500671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_probs[:,1]\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-08T18:39:08.502256Z","iopub.status.idle":"2021-12-08T18:39:08.502571Z","shell.execute_reply.started":"2021-12-08T18:39:08.502409Z","shell.execute_reply":"2021-12-08T18:39:08.502426Z"},"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-12-08T18:39:08.503666Z","iopub.status.idle":"2021-12-08T18:39:08.504371Z","shell.execute_reply.started":"2021-12-08T18:39:08.504184Z","shell.execute_reply":"2021-12-08T18:39:08.504204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}