{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"},{"sourceId":12535942,"sourceType":"datasetVersion","datasetId":7913969},{"sourceId":12535958,"sourceType":"datasetVersion","datasetId":7913981}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# CNN Cancer Detection Submission: Transfer learning, Data augmentation","metadata":{}},{"cell_type":"markdown","source":"## Import Packages","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import shuffle\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import models, layers, datasets","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:14:47.703473Z","iopub.execute_input":"2025-07-21T15:14:47.703666Z","iopub.status.idle":"2025-07-21T15:15:03.932849Z","shell.execute_reply.started":"2025-07-21T15:14:47.703647Z","shell.execute_reply":"2025-07-21T15:15:03.932016Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## View Submission CSV and Shape","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/sample_submission.csv')\ntest_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:15:03.933725Z","iopub.execute_input":"2025-07-21T15:15:03.934189Z","iopub.status.idle":"2025-07-21T15:15:04.040903Z","shell.execute_reply.started":"2025-07-21T15:15:03.934168Z","shell.execute_reply":"2025-07-21T15:15:04.040131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:15:04.042601Z","iopub.execute_input":"2025-07-21T15:15:04.042840Z","iopub.status.idle":"2025-07-21T15:15:04.046912Z","shell.execute_reply.started":"2025-07-21T15:15:04.042822Z","shell.execute_reply":"2025-07-21T15:15:04.046213Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Add tif extension","metadata":{}},{"cell_type":"code","source":"test_df['filenames'] = test_df['id']+'.tif'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:15:04.047605Z","iopub.execute_input":"2025-07-21T15:15:04.047864Z","iopub.status.idle":"2025-07-21T15:15:04.069344Z","shell.execute_reply.started":"2025-07-21T15:15:04.047838Z","shell.execute_reply":"2025-07-21T15:15:04.068757Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Create the test image path and its length","metadata":{}},{"cell_type":"code","source":"test_ref = '/kaggle/input/histopathologic-cancer-detection/test'\nprint(f'Test Images Shape {len(test_ref)}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:15:04.070166Z","iopub.execute_input":"2025-07-21T15:15:04.070432Z","iopub.status.idle":"2025-07-21T15:15:04.083936Z","shell.execute_reply.started":"2025-07-21T15:15:04.070390Z","shell.execute_reply":"2025-07-21T15:15:04.083183Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Use ImageDataGenerator and create the loader","metadata":{}},{"cell_type":"code","source":"batch_size = 25\ntest_datagen = ImageDataGenerator(rescale = 1/255)\n\ntest_loader = test_datagen.flow_from_dataframe(\n    dataframe = test_df,\n    directory = test_ref,\n    x_col = 'filenames',\n    batch_size = batch_size,\n    shuffle = False,\n    class_mode = None,\n    target_size = (64,64)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:19:14.547949Z","iopub.execute_input":"2025-07-21T15:19:14.548253Z","iopub.status.idle":"2025-07-21T15:19:45.110490Z","shell.execute_reply.started":"2025-07-21T15:19:14.548230Z","shell.execute_reply":"2025-07-21T15:19:45.109847Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Make sure the model is the same from the prior notebook\n#### print the summary","metadata":{}},{"cell_type":"code","source":"cnn_model = keras.models.load_model('/kaggle/input/cnn-model-3/Cancer_detection_cnn_model_3.h5')\ncnn_model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:19:45.111582Z","iopub.execute_input":"2025-07-21T15:19:45.111824Z","iopub.status.idle":"2025-07-21T15:19:45.527628Z","shell.execute_reply.started":"2025-07-21T15:19:45.111797Z","shell.execute_reply":"2025-07-21T15:19:45.527044Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Use the model to make predictions","metadata":{}},{"cell_type":"code","source":"test_preds = cnn_model.predict(test_loader)\nprint(test_preds.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:19:45.528279Z","iopub.execute_input":"2025-07-21T15:19:45.528505Z","iopub.status.idle":"2025-07-21T15:26:45.778572Z","shell.execute_reply.started":"2025-07-21T15:19:45.528488Z","shell.execute_reply":"2025-07-21T15:26:45.777757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_preds[:10].round(2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:26:45.780220Z","iopub.execute_input":"2025-07-21T15:26:45.780509Z","iopub.status.idle":"2025-07-21T15:26:45.784937Z","shell.execute_reply.started":"2025-07-21T15:26:45.780482Z","shell.execute_reply":"2025-07-21T15:26:45.784219Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Change labels to the predicted labels","metadata":{}},{"cell_type":"code","source":"cancer_submission = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/sample_submission.csv')\ncancer_submission.label = test_preds\ncancer_submission.tail(40)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:26:45.785563Z","iopub.execute_input":"2025-07-21T15:26:45.785827Z","iopub.status.idle":"2025-07-21T15:26:45.854088Z","shell.execute_reply.started":"2025-07-21T15:26:45.785805Z","shell.execute_reply":"2025-07-21T15:26:45.853281Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cancer_submission.to_csv('cancer_detection_submission_data_aug.csv', index = False, header = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-21T15:26:45.854922Z","iopub.execute_input":"2025-07-21T15:26:45.855467Z","iopub.status.idle":"2025-07-21T15:26:46.004686Z","shell.execute_reply.started":"2025-07-21T15:26:45.855439Z","shell.execute_reply":"2025-07-21T15:26:46.003643Z"}},"outputs":[],"execution_count":null}]}