{"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":12520094,"sourceType":"datasetVersion","datasetId":7902983}],"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-19T21:39:26.688386Z","iopub.execute_input":"2025-07-19T21:39:26.688632Z","iopub.status.idle":"2025-07-19T21:39:42.627039Z","shell.execute_reply.started":"2025-07-19T21:39:26.688607Z","shell.execute_reply":"2025-07-19T21:39:42.626428Z"}},"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-19T21:39:42.628813Z","iopub.execute_input":"2025-07-19T21:39:42.629680Z","iopub.status.idle":"2025-07-19T21:39:42.718691Z","shell.execute_reply.started":"2025-07-19T21:39:42.629660Z","shell.execute_reply":"2025-07-19T21:39:42.718041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T21:39:42.719296Z","iopub.execute_input":"2025-07-19T21:39:42.719506Z","iopub.status.idle":"2025-07-19T21:39:42.723258Z","shell.execute_reply.started":"2025-07-19T21:39:42.719490Z","shell.execute_reply":"2025-07-19T21:39:42.722637Z"}},"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},"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-19T21:39:42.772493Z","iopub.execute_input":"2025-07-19T21:39:42.772720Z","iopub.status.idle":"2025-07-19T21:39:42.786326Z","shell.execute_reply.started":"2025-07-19T21:39:42.772703Z","shell.execute_reply":"2025-07-19T21:39:42.785819Z"}},"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 = (96,96)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T21:39:42.787842Z","iopub.execute_input":"2025-07-19T21:39:42.788098Z","iopub.status.idle":"2025-07-19T21:41:05.659158Z","shell.execute_reply.started":"2025-07-19T21:39:42.788077Z","shell.execute_reply":"2025-07-19T21:41:05.658624Z"}},"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/cancer-detection-training-model/Cancer_Detection_cnn_model_2.h5')\ncnn_model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T21:41:05.659857Z","iopub.execute_input":"2025-07-19T21:41:05.660067Z","iopub.status.idle":"2025-07-19T21:41:13.334361Z","shell.execute_reply.started":"2025-07-19T21:41:05.660048Z","shell.execute_reply":"2025-07-19T21:41:13.333825Z"}},"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-19T21:41:13.335065Z","iopub.execute_input":"2025-07-19T21:41:13.335272Z","iopub.status.idle":"2025-07-19T21:48:40.917294Z","shell.execute_reply.started":"2025-07-19T21:41:13.335255Z","shell.execute_reply":"2025-07-19T21:48:40.916641Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_preds[:10].round(2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T21:48:40.918208Z","iopub.execute_input":"2025-07-19T21:48:40.918849Z","iopub.status.idle":"2025-07-19T21:48:40.923177Z","shell.execute_reply.started":"2025-07-19T21:48:40.918820Z","shell.execute_reply":"2025-07-19T21:48:40.922618Z"}},"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-19T21:48:40.923872Z","iopub.execute_input":"2025-07-19T21:48:40.924035Z","iopub.status.idle":"2025-07-19T21:48:40.949891Z","shell.execute_reply.started":"2025-07-19T21:48:40.924022Z","shell.execute_reply":"2025-07-19T21:48:40.949016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cancer_submission.to_csv('cancer_detection_submission_TL.csv', index = False, header = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-19T21:48:40.950693Z","iopub.execute_input":"2025-07-19T21:48:40.950961Z","iopub.status.idle":"2025-07-19T21:48:41.179820Z","shell.execute_reply.started":"2025-07-19T21:48:40.950937Z","shell.execute_reply":"2025-07-19T21:48:41.179003Z"}},"outputs":[],"execution_count":null}]}