{"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},{"sourceId":251349073,"sourceType":"kernelVersion"}],"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-08-04T19:41:55.881205Z","iopub.execute_input":"2025-08-04T19:41:55.881496Z","iopub.status.idle":"2025-08-04T19:42:13.560218Z","shell.execute_reply.started":"2025-08-04T19:41:55.881472Z","shell.execute_reply":"2025-08-04T19:42:13.559666Z"}},"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-08-04T19:42:13.561312Z","iopub.execute_input":"2025-08-04T19:42:13.561866Z","iopub.status.idle":"2025-08-04T19:42:13.673691Z","shell.execute_reply.started":"2025-08-04T19:42:13.561839Z","shell.execute_reply":"2025-08-04T19:42:13.672964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T19:42:31.364736Z","iopub.execute_input":"2025-08-04T19:42:31.365020Z","iopub.status.idle":"2025-08-04T19:42:31.369243Z","shell.execute_reply.started":"2025-08-04T19:42:31.364999Z","shell.execute_reply":"2025-08-04T19:42:31.368441Z"}},"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-08-04T19:42:31.712380Z","iopub.execute_input":"2025-08-04T19:42:31.712671Z","iopub.status.idle":"2025-08-04T19:42:31.727105Z","shell.execute_reply.started":"2025-08-04T19:42:31.712649Z","shell.execute_reply":"2025-08-04T19:42:31.726502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T19:42:31.891926Z","iopub.execute_input":"2025-08-04T19:42:31.892202Z","iopub.status.idle":"2025-08-04T19:42:31.900209Z","shell.execute_reply.started":"2025-08-04T19:42:31.892178Z","shell.execute_reply":"2025-08-04T19:42:31.899402Z"}},"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-08-04T19:42:33.691925Z","iopub.execute_input":"2025-08-04T19:42:33.692608Z","iopub.status.idle":"2025-08-04T19:42:33.696402Z","shell.execute_reply.started":"2025-08-04T19:42:33.692584Z","shell.execute_reply":"2025-08-04T19:42:33.695581Z"}},"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_1 = 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)\n\ntest_loader_2 = 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-08-04T19:44:14.689184Z","iopub.execute_input":"2025-08-04T19:44:14.689468Z","iopub.status.idle":"2025-08-04T19:46:38.881951Z","shell.execute_reply.started":"2025-08-04T19:44:14.689448Z","shell.execute_reply":"2025-08-04T19:46:38.881304Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"View the imported model","metadata":{}},{"cell_type":"code","source":"cnn_model_1 = keras.models.load_model('/kaggle/input/cnn-model-5/Cancer_Detection_cnn_model_5.h5')\ncnn_model_1.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T19:46:38.883323Z","iopub.execute_input":"2025-08-04T19:46:38.883620Z","iopub.status.idle":"2025-08-04T19:46:44.220164Z","shell.execute_reply.started":"2025-08-04T19:46:38.883593Z","shell.execute_reply":"2025-08-04T19:46:44.219440Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cnn_model_2 = keras.models.load_model('/kaggle/input/mwv-final-project-training-model-3/Cancer_Detection_cnn_model_2.h5')\ncnn_model_2.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T19:46:44.220965Z","iopub.execute_input":"2025-08-04T19:46:44.221261Z","iopub.status.idle":"2025-08-04T19:46:47.878356Z","shell.execute_reply.started":"2025-08-04T19:46:44.221237Z","shell.execute_reply":"2025-08-04T19:46:47.877797Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Make a prediction using two of the models created (both transfer learning models that performed the best)","metadata":{}},{"cell_type":"code","source":"test_pred_1 = cnn_model_1.predict(test_loader_1)\nprint(test_pred_1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T19:46:47.879641Z","iopub.execute_input":"2025-08-04T19:46:47.879869Z","iopub.status.idle":"2025-08-04T19:54:42.809164Z","shell.execute_reply.started":"2025-08-04T19:46:47.879852Z","shell.execute_reply":"2025-08-04T19:54:42.808392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_pred_2 = cnn_model_2.predict(test_loader_2)\nprint(test_pred_2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T19:54:42.810038Z","iopub.execute_input":"2025-08-04T19:54:42.810323Z","iopub.status.idle":"2025-08-04T19:56:10.771692Z","shell.execute_reply.started":"2025-08-04T19:54:42.810294Z","shell.execute_reply":"2025-08-04T19:56:10.770726Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Print first 10 test predictions","metadata":{}},{"cell_type":"code","source":"print(test_pred_1[:10].round(2))\nprint(test_pred_1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T19:56:10.772695Z","iopub.execute_input":"2025-08-04T19:56:10.773305Z","iopub.status.idle":"2025-08-04T19:56:10.778367Z","shell.execute_reply.started":"2025-08-04T19:56:10.773284Z","shell.execute_reply":"2025-08-04T19:56:10.777654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_pred_2[:10].round(2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T20:11:07.030383Z","iopub.execute_input":"2025-08-04T20:11:07.030641Z","iopub.status.idle":"2025-08-04T20:11:07.035428Z","shell.execute_reply.started":"2025-08-04T20:11:07.030623Z","shell.execute_reply":"2025-08-04T20:11:07.034688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Combine the scores and divide them. See if the average of the two models together performs better than the best model","metadata":{}},{"cell_type":"code","source":"ensemble_prediction = (test_pred_1+test_pred_2)/2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T20:11:21.225792Z","iopub.execute_input":"2025-08-04T20:11:21.226273Z","iopub.status.idle":"2025-08-04T20:11:21.230267Z","shell.execute_reply.started":"2025-08-04T20:11:21.226248Z","shell.execute_reply":"2025-08-04T20:11:21.229398Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Change labels to the ensemble predictions","metadata":{}},{"cell_type":"code","source":"\n\ncancer_submission = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/sample_submission.csv')\ncancer_submission.label = ensemble_prediction\ncancer_submission.head(25)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T20:11:31.026897Z","iopub.execute_input":"2025-08-04T20:11:31.027450Z","iopub.status.idle":"2025-08-04T20:11:31.089685Z","shell.execute_reply.started":"2025-08-04T20:11:31.027426Z","shell.execute_reply":"2025-08-04T20:11:31.088958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cancer_submission.to_csv('cancer_detection_submission_ensemble.csv', index = False, header = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-04T20:11:53.816209Z","iopub.execute_input":"2025-08-04T20:11:53.816908Z","iopub.status.idle":"2025-08-04T20:11:53.964145Z","shell.execute_reply.started":"2025-08-04T20:11:53.816883Z","shell.execute_reply":"2025-08-04T20:11:53.963566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"https://www.kaggle.com/code/mattvierheller/mwv-final-project-training-model-5\nhttps://www.kaggle.com/code/mattvierheller/mwv-final-project-training-model-3\n","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}