{"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":12611338,"sourceType":"datasetVersion","datasetId":7966524},{"sourceId":12611353,"sourceType":"datasetVersion","datasetId":7966537}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Submission using EfficientNetV2B2 Transfer learning","metadata":{}},{"cell_type":"markdown","source":"## Import Packages","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\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-29T14:36:44.211494Z","iopub.execute_input":"2025-07-29T14:36:44.212259Z","iopub.status.idle":"2025-07-29T14:37:00.258107Z","shell.execute_reply.started":"2025-07-29T14:36:44.212239Z","shell.execute_reply":"2025-07-29T14:37:00.257394Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### View Dataframe 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-29T14:37:00.258960Z","iopub.execute_input":"2025-07-29T14:37:00.259560Z","iopub.status.idle":"2025-07-29T14:37:00.426060Z","shell.execute_reply.started":"2025-07-29T14:37:00.259533Z","shell.execute_reply":"2025-07-29T14:37:00.425354Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:37:00.427542Z","iopub.execute_input":"2025-07-29T14:37:00.427750Z","iopub.status.idle":"2025-07-29T14:37:00.431764Z","shell.execute_reply.started":"2025-07-29T14:37:00.427734Z","shell.execute_reply":"2025-07-29T14:37:00.431123Z"}},"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-29T14:37:00.432550Z","iopub.execute_input":"2025-07-29T14:37:00.432783Z","iopub.status.idle":"2025-07-29T14:37:00.458613Z","shell.execute_reply.started":"2025-07-29T14:37:00.432763Z","shell.execute_reply":"2025-07-29T14:37:00.457889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:37:00.459400Z","iopub.execute_input":"2025-07-29T14:37:00.459677Z","iopub.status.idle":"2025-07-29T14:37:00.476705Z","shell.execute_reply.started":"2025-07-29T14:37:00.459653Z","shell.execute_reply":"2025-07-29T14:37:00.475979Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Create test image path and print its shape","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-29T14:37:00.477460Z","iopub.execute_input":"2025-07-29T14:37:00.477750Z","iopub.status.idle":"2025-07-29T14:37:00.491466Z","shell.execute_reply.started":"2025-07-29T14:37:00.477728Z","shell.execute_reply":"2025-07-29T14:37:00.490760Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Use ImageDataGenerator and create the Test 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 = (260,260)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:37:00.492198Z","iopub.execute_input":"2025-07-29T14:37:00.492413Z","iopub.status.idle":"2025-07-29T14:39:37.846664Z","shell.execute_reply.started":"2025-07-29T14:37:00.492398Z","shell.execute_reply":"2025-07-29T14:39:37.845915Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"View the imported model","metadata":{}},{"cell_type":"code","source":"cnn_model = keras.models.load_model('/kaggle/input/cnn-model-5/Cancer_Detection_cnn_model_5.h5')\ncnn_model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:39:37.847374Z","iopub.execute_input":"2025-07-29T14:39:37.847600Z","iopub.status.idle":"2025-07-29T14:39:42.944706Z","shell.execute_reply.started":"2025-07-29T14:39:37.847581Z","shell.execute_reply":"2025-07-29T14:39:42.944111Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Make a prediction using the model","metadata":{}},{"cell_type":"code","source":"test_pred = cnn_model.predict(test_loader)\nprint(test_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:39:42.946374Z","iopub.execute_input":"2025-07-29T14:39:42.946585Z","iopub.status.idle":"2025-07-29T14:46:15.042909Z","shell.execute_reply.started":"2025-07-29T14:39:42.946570Z","shell.execute_reply":"2025-07-29T14:46:15.042255Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Print first 10 test predictions","metadata":{}},{"cell_type":"code","source":"print(test_pred[:10].round(2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:46:15.043713Z","iopub.execute_input":"2025-07-29T14:46:15.043982Z","iopub.status.idle":"2025-07-29T14:46:15.048564Z","shell.execute_reply.started":"2025-07-29T14:46:15.043957Z","shell.execute_reply":"2025-07-29T14:46:15.047828Z"}},"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_pred\ncancer_submission.head(25)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:46:15.051722Z","iopub.execute_input":"2025-07-29T14:46:15.052349Z","iopub.status.idle":"2025-07-29T14:46:15.119695Z","shell.execute_reply.started":"2025-07-29T14:46:15.052330Z","shell.execute_reply":"2025-07-29T14:46:15.119112Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cancer_submission.to_csv('cancer_detection_submission_tl_efficientnet.csv', index = False, header = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:46:15.120470Z","iopub.execute_input":"2025-07-29T14:46:15.120745Z","iopub.status.idle":"2025-07-29T14:46:15.264237Z","shell.execute_reply.started":"2025-07-29T14:46:15.120717Z","shell.execute_reply":"2025-07-29T14:46:15.263685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}