{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"none","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"colab":{"provenance":[{"file_id":"1YRkkr_CPj2qt3DKvzPlICPVLxxcvFQLn","timestamp":1761940047508},{"file_id":"https://storage.googleapis.com/kaggle-colab-exported-notebooks/johnogilvie/jwo-histocancerdetect-eda-select-v1.13c88209-26c2-4563-92f1-33e515127713.ipynb?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=gcp-kaggle-com%40kaggle-161607.iam.gserviceaccount.com/20251031/auto/storage/goog4_request&X-Goog-Date=20251031T121253Z&X-Goog-Expires=259200&X-Goog-SignedHeaders=host&X-Goog-Signature=6f05ee53bf61dda38fef46b19446637a4eb89330b69b0ad8027c1f188640f1632a774c603b78aea7d638cb73bb147015a8cbba24035e4dcbb527db39b028f19bee508196315e19e5b8159c23028a474fbd9a9252937913da12f727dbf874bc22ee1083c60bce52f35e908d91f36437980e6634f9d620c6368fb078700781e463a61fafa9842e9a61e9e7a17e0562710c27335a5e5fb442108d331cf81555aa31a21a7adfb8bee45c93a9f402c60fbfcf85b8f6c02151b4bbbe066d0bbe2190139c90c094e92bdff2a0d6ae8dd2cd3e00f5f3feff253a888effe6c366d6ca40e0a5041f9948f7f833f68868a5f86be248b6e4ac4532f827fec1ee9676e0b6f37f","timestamp":1761912807663}],"gpuType":"T4"},"accelerator":"GPU","widgets":{"application/vnd.jupyter.widget-state+json":{"6136f0fedcfa442a9ddae185d1a61c0c":{"model_module":"@jupyter-widgets/controls","model_name":"VBoxModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"VBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"VBoxView","box_style":"","children":["IPY_MODEL_065e060bf8f7417fa71caf590fe12bac"],"layout":"IPY_MODEL_be1ee54e8e7843a3a5d163709c8c68fe"}},"73a7a2d7b9574a448dcfe403bf4461a4":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_89779cae662c46f587ef7a6e00758297","placeholder":"​","style":"IPY_MODEL_c2c03e4ceb1049a9ab9f4e2170137237","value":"<center> <img\nsrc=https://www.kaggle.com/static/images/site-logo.png\nalt='Kaggle'> <br> Create an API token from <a\nhref=\"https://www.kaggle.com/settings/account\" target=\"_blank\">your Kaggle\nsettings page</a> and paste it below along with your Kaggle username. <br> </center>"}},"f3976e25c7c94c06837cc20c8e650458":{"model_module":"@jupyter-widgets/controls","model_name":"TextModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"TextModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"TextView","continuous_update":true,"description":"Username:","description_tooltip":null,"disabled":false,"layout":"IPY_MODEL_9a24903ccbad43e2ac1d4e791a70901b","placeholder":"​","style":"IPY_MODEL_3225a9d0dde44f25ac7f95b673030e30","value":"johnogilvie"}},"62b3ee7bf120496a8bdd6c90ad04b0c8":{"model_module":"@jupyter-widgets/controls","model_name":"PasswordModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"PasswordModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"PasswordView","continuous_update":true,"description":"Token:","description_tooltip":null,"disabled":false,"layout":"IPY_MODEL_04316722782b4b8d9109e4b835aca24b","placeholder":"​","style":"IPY_MODEL_00e2fdcd2ac540edaae8f56990b7fe1d","value":""}},"84884b1080784e119ac3799648b8d749":{"model_module":"@jupyter-widgets/controls","model_name":"ButtonModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ButtonModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"ButtonView","button_style":"","description":"Login","disabled":false,"icon":"","layout":"IPY_MODEL_655c1dcf772044b19c22eba1f8ae74e7","style":"IPY_MODEL_960dc8a588864a59ba89ec53c05d7d99","tooltip":""}},"c23de7c9f6bc4e6cb719c89ba0c8349f":{"model_module":"@jupyter-widgets/controls","model_name":"HTMLModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_78dd6618a3d24969b95ea5521bea58aa","placeholder":"​","style":"IPY_MODEL_5cb8bf4f14ca49ee94bf836257f4b7fd","value":"\n<b>Thank You</b></center>"}},"be1ee54e8e7843a3a5d163709c8c68fe":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":"center","align_self":null,"border":null,"bottom":null,"display":"flex","flex":null,"flex_flow":"column","grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":"50%"}},"89779cae662c46f587ef7a6e00758297":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"c2c03e4ceb1049a9ab9f4e2170137237":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"9a24903ccbad43e2ac1d4e791a70901b":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"3225a9d0dde44f25ac7f95b673030e30":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"04316722782b4b8d9109e4b835aca24b":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"00e2fdcd2ac540edaae8f56990b7fe1d":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"655c1dcf772044b19c22eba1f8ae74e7":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"960dc8a588864a59ba89ec53c05d7d99":{"model_module":"@jupyter-widgets/controls","model_name":"ButtonStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ButtonStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","button_color":null,"font_weight":""}},"78dd6618a3d24969b95ea5521bea58aa":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"5cb8bf4f14ca49ee94bf836257f4b7fd":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"3fbcfcdced6543bab0aa513f076a1561":{"model_module":"@jupyter-widgets/controls","model_name":"LabelModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"LabelModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"LabelView","description":"","description_tooltip":null,"layout":"IPY_MODEL_d633a333bcb9428781f1e181fa969f9c","placeholder":"​","style":"IPY_MODEL_20af3e9766074550a8b7890221582b51","value":"Connecting..."}},"d633a333bcb9428781f1e181fa969f9c":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"20af3e9766074550a8b7890221582b51":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"065e060bf8f7417fa71caf590fe12bac":{"model_module":"@jupyter-widgets/controls","model_name":"LabelModel","model_module_version":"1.5.0","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"LabelModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"LabelView","description":"","description_tooltip":null,"layout":"IPY_MODEL_6333c36b4b3e41aea0372d14e237c3b3","placeholder":"​","style":"IPY_MODEL_a3414203c43a4a6a838405f2370570dd","value":"Kaggle credentials successfully validated."}},"6333c36b4b3e41aea0372d14e237c3b3":{"model_module":"@jupyter-widgets/base","model_name":"LayoutModel","model_module_version":"1.2.0","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"a3414203c43a4a6a838405f2370570dd":{"model_module":"@jupyter-widgets/controls","model_name":"DescriptionStyleModel","model_module_version":"1.5.0","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}}}}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## This notebook explores the impact of different kinds of data augmentation","metadata":{"id":"-n0UKopcL3vT"}},{"cell_type":"code","source":"#Histopathological Cancer Detection Project\n#Copy & Edit from JWO HistoCancerDetect img-sizes select v1, which is from JWO HistoCancerDetect EDA select v1, which is from MWV Final Project Training Model 5","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-31T21:11:03.961619Z","iopub.execute_input":"2025-10-31T21:11:03.961827Z","iopub.status.idle":"2025-10-31T21:11:03.967036Z","shell.execute_reply.started":"2025-10-31T21:11:03.961810Z","shell.execute_reply":"2025-10-31T21:11:03.965761Z"},"id":"2vEW5Qu-L3vS","executionInfo":{"status":"ok","timestamp":1761941956386,"user_tz":360,"elapsed":13,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# IMPORTANT: SOME KAGGLE DATA SOURCES ARE PRIVATE\n# RUN THIS CELL IN ORDER TO IMPORT YOUR KAGGLE DATA SOURCES.\nimport kagglehub\nkagglehub.login()\n","metadata":{"id":"0qBIi9aCL3vQ","executionInfo":{"status":"ok","timestamp":1761941956781,"user_tz":360,"elapsed":392,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"3105cc7a-21f6-4e96-cbe1-0fd1a3c93548"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# IMPORTANT: RUN THIS CELL IN ORDER TO IMPORT YOUR KAGGLE DATA SOURCES,\n# THEN FEEL FREE TO DELETE THIS CELL.\n# NOTE: THIS NOTEBOOK ENVIRONMENT DIFFERS FROM KAGGLE'S PYTHON\n# ENVIRONMENT SO THERE MAY BE MISSING LIBRARIES USED BY YOUR\n# NOTEBOOK.\n\nhistopathologic_cancer_detection_path = kagglehub.competition_download('histopathologic-cancer-detection')\n\nprint('Data source import complete.')\n","metadata":{"id":"XhnpYVP8L3vS","executionInfo":{"status":"ok","timestamp":1761942219213,"user_tz":360,"elapsed":183606,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"9d49ae05-492a-408c-fc76-a7022c9fda92"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(histopathologic_cancer_detection_path)","metadata":{"id":"ggFh-fPfR_AK","executionInfo":{"status":"ok","timestamp":1761942660700,"user_tz":360,"elapsed":12,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"95754ad2-e073-4607-c90c-1f5e54673ac8"},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Import packages","metadata":{"id":"_NWHdwW2L3vT"}},{"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.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-10-30T19:17:19.578822Z","iopub.execute_input":"2025-10-30T19:17:19.579496Z","iopub.status.idle":"2025-10-30T19:17:32.826462Z","shell.execute_reply.started":"2025-10-30T19:17:19.579461Z","shell.execute_reply":"2025-10-30T19:17:32.825931Z"},"id":"OHh31zh4L3vU","executionInfo":{"status":"ok","timestamp":1761942660704,"user_tz":360,"elapsed":2,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## View Data, Distributions, and Images with labels","metadata":{"id":"1GZ3moZ1L3vU"}},{"cell_type":"markdown","source":"colab train = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv')","metadata":{"id":"G-qj2mAtSKu1"}},{"cell_type":"code","source":"train = pd.read_csv(f'{histopathologic_cancer_detection_path}/train_labels.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:32.82716Z","iopub.execute_input":"2025-10-30T19:17:32.827616Z","iopub.status.idle":"2025-10-30T19:17:33.166373Z","shell.execute_reply.started":"2025-10-30T19:17:32.827597Z","shell.execute_reply":"2025-10-30T19:17:33.165816Z"},"id":"XNNWavytL3vU","executionInfo":{"status":"ok","timestamp":1761942660961,"user_tz":360,"elapsed":256,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:33.187585Z","iopub.execute_input":"2025-10-30T19:17:33.187756Z","iopub.status.idle":"2025-10-30T19:17:33.203536Z","shell.execute_reply.started":"2025-10-30T19:17:33.187742Z","shell.execute_reply":"2025-10-30T19:17:33.202912Z"},"id":"ylE1jQPfL3vV","outputId":"5d8ae42a-2c8c-4476-a9d1-a221ff8e1eb4","executionInfo":{"status":"ok","timestamp":1761942660968,"user_tz":360,"elapsed":6,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(train.label.value_counts()/len(train.label)).to_frame().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:33.204184Z","iopub.execute_input":"2025-10-30T19:17:33.204504Z","iopub.status.idle":"2025-10-30T19:17:33.221834Z","shell.execute_reply.started":"2025-10-30T19:17:33.204486Z","shell.execute_reply":"2025-10-30T19:17:33.221217Z"},"id":"3J0-N4ESL3vV","outputId":"a4a88f48-1b8a-4bcb-85b5-7924c3386e25","executionInfo":{"status":"ok","timestamp":1761942660973,"user_tz":360,"elapsed":8,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Challenge: The image ids are used as filenames for the images, but the ids are missing the \".tif\" extension.\n#You will need to add a copy to the DataFrame to store the complete filename rather than just the id\ntrain['filenames'] = train['id']+'.tif'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:33.222557Z","iopub.execute_input":"2025-10-30T19:17:33.222811Z","iopub.status.idle":"2025-10-30T19:17:33.260522Z","shell.execute_reply.started":"2025-10-30T19:17:33.222788Z","shell.execute_reply":"2025-10-30T19:17:33.259871Z"},"id":"zeRFj7mRL3vV","executionInfo":{"status":"ok","timestamp":1761942661009,"user_tz":360,"elapsed":36,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:33.261192Z","iopub.execute_input":"2025-10-30T19:17:33.261422Z","iopub.status.idle":"2025-10-30T19:17:33.269709Z","shell.execute_reply.started":"2025-10-30T19:17:33.261405Z","shell.execute_reply":"2025-10-30T19:17:33.268942Z"},"id":"9gmbFJy9L3vV","outputId":"77aa8c7b-d7af-4b1c-b633-bc974b86f7e7","executionInfo":{"status":"ok","timestamp":1761942661027,"user_tz":360,"elapsed":3,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Sample the Data to make training more efficient","metadata":{"id":"Ib4cGkfNL3vV"}},{"cell_type":"code","source":"SS = 50000\nRS = 10\n\npositives = train[train['label']==1].sample(SS, random_state = RS)\nnegatives = train[train['label']==0].sample(SS, random_state = SS)\n\nnew_train = pd.concat([positives,negatives], axis = 0).reset_index(drop = True)\nnew_train = shuffle(new_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:34.277308Z","iopub.execute_input":"2025-10-30T19:17:34.277497Z","iopub.status.idle":"2025-10-30T19:17:34.351229Z","shell.execute_reply.started":"2025-10-30T19:17:34.277483Z","shell.execute_reply":"2025-10-30T19:17:34.350431Z"},"id":"TCkgf56vL3vW","executionInfo":{"status":"ok","timestamp":1761942661163,"user_tz":360,"elapsed":100,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(new_train.label.value_counts()/len(new_train)).to_frame().T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:34.360326Z","iopub.execute_input":"2025-10-30T19:17:34.360807Z","iopub.status.idle":"2025-10-30T19:17:34.375092Z","shell.execute_reply.started":"2025-10-30T19:17:34.36079Z","shell.execute_reply":"2025-10-30T19:17:34.37441Z"},"id":"Vntm_fgEL3vW","outputId":"12fea4a4-c40b-4c56-ceec-86a5295214d5","executionInfo":{"status":"ok","timestamp":1761942661164,"user_tz":360,"elapsed":6,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"new_train = new_train.sample(frac=0.5)  # use only a fraction of the dataset","metadata":{"id":"amj71hxIOFFl","executionInfo":{"status":"ok","timestamp":1761942661165,"user_tz":360,"elapsed":4,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Train_Test_Split","metadata":{"id":"GC7i2pmRL3vW"}},{"cell_type":"code","source":"train_df, val_df = train_test_split(new_train, test_size = .2, random_state = 10, stratify = new_train.label)\n\nprint(train_df.shape)\nprint(val_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:34.375866Z","iopub.execute_input":"2025-10-30T19:17:34.376167Z","iopub.status.idle":"2025-10-30T19:17:34.425624Z","shell.execute_reply.started":"2025-10-30T19:17:34.376149Z","shell.execute_reply":"2025-10-30T19:17:34.424839Z"},"id":"rOI9cOpzL3vW","outputId":"b4a80a4a-0114-42b6-a87a-c1fb85ef127d","executionInfo":{"status":"ok","timestamp":1761942661193,"user_tz":360,"elapsed":31,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Rescale Images with ImageDataGenerator","metadata":{"id":"dHoqzJJOL3vW"}},{"cell_type":"markdown","source":"train_images_path = '/kaggle/input/histopathologic-cancer-detection/train'\n","metadata":{"id":"Uo1bWd6kXohE"}},{"cell_type":"code","source":"train_images_path = f'{histopathologic_cancer_detection_path}/train'","metadata":{"id":"H1JLG_QdXkf1","executionInfo":{"status":"ok","timestamp":1761942661213,"user_tz":360,"elapsed":18,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Challenge: You will need to use an image data generator to load the files from disk.\ntrain_datagen = ImageDataGenerator(rescale = 1/255)\nval_datagen = ImageDataGenerator(rescale = 1/255)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:34.426412Z","iopub.execute_input":"2025-10-30T19:17:34.426749Z","iopub.status.idle":"2025-10-30T19:17:34.430143Z","shell.execute_reply.started":"2025-10-30T19:17:34.426726Z","shell.execute_reply":"2025-10-30T19:17:34.42949Z"},"id":"FDz3IG5AL3vW","executionInfo":{"status":"ok","timestamp":1761942661214,"user_tz":360,"elapsed":2,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Make labels into strings","metadata":{"id":"pqwUGF4HL3vW"}},{"cell_type":"code","source":"train_df['label'] = train_df['label'].astype(str)\nval_df['label'] = val_df['label'].astype(str)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:34.430772Z","iopub.execute_input":"2025-10-30T19:17:34.431034Z","iopub.status.idle":"2025-10-30T19:17:34.463712Z","shell.execute_reply.started":"2025-10-30T19:17:34.431009Z","shell.execute_reply":"2025-10-30T19:17:34.463051Z"},"id":"q92zAb16L3vW","executionInfo":{"status":"ok","timestamp":1761942661240,"user_tz":360,"elapsed":26,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Create loaders for respective kinds of data augmentation\n* flips only -> f\n* rotation only -> r\n* shifts (aka translate) only -> t\n* no data augmentation -> n\n* Image sizes are all 64x64","metadata":{"id":"YBZ6_18ML3vW"}},{"cell_type":"code","source":"%%time\nbatch_size = 64\n\ntrain_datagen = ImageDataGenerator(rescale = 1/255)\nval_datagen = ImageDataGenerator(rescale = 1/255)\n\nprint('Creating training data loader for images with no data augmentation\\n')\ntrain_loadern = train_datagen.flow_from_dataframe(\n    dataframe = train_df,\n    directory = train_images_path,\n    x_col = 'filenames',\n    y_col = 'label',\n    batch_size = batch_size,\n    seed = 10,\n    shuffle = True,\n    class_mode = 'binary',\n    horizontal_flip = False,\n    vertical_flip = False,\n    height_shift_range = 0,\n    width_shift_range = 0,\n    rotation_range = 0,\n    target_size = (64,64),\n    color_mode = 'grayscale'\n)\n\nprint('Creating training data loader for images with only flips as data augmentation\\n')\ntrain_loaderf = train_datagen.flow_from_dataframe(\n    dataframe = train_df,\n    directory = train_images_path,\n    x_col = 'filenames',\n    y_col = 'label',\n    batch_size = batch_size,\n    seed = 10,\n    shuffle = True,\n    class_mode = 'binary',\n    horizontal_flip = True,\n    vertical_flip = True,\n    height_shift_range = 0,\n    width_shift_range = 0,\n    rotation_range = 0,\n    target_size = (64,64),\n    color_mode = 'grayscale'\n)\n\nprint('Creating training data loader for images with only shifts as data augmentation\\n')\ntrain_loadert = train_datagen.flow_from_dataframe(\n    dataframe = train_df,\n    directory = train_images_path,\n    x_col = 'filenames',\n    y_col = 'label',\n    batch_size = batch_size,\n    seed = 10,\n    shuffle = True,\n    class_mode = 'binary',\n    horizontal_flip = False,\n    vertical_flip = False,\n    height_shift_range = 0.12,\n    width_shift_range = 0.12,\n    rotation_range = 0,\n    target_size = (64,64),\n    color_mode = 'grayscale'\n)\n\nprint('Creating training data loader for images with only rotation as data augmentation\\n')\ntrain_loaderr = train_datagen.flow_from_dataframe(\n    dataframe = train_df,\n    directory = train_images_path,\n    x_col = 'filenames',\n    y_col = 'label',\n    batch_size = batch_size,\n    seed = 10,\n    shuffle = True,\n    class_mode = 'binary',\n    horizontal_flip = False,\n    vertical_flip = False,\n    height_shift_range = 0,\n    width_shift_range = 0,\n    rotation_range = 21,\n    target_size = (64,64),\n    color_mode = 'grayscale'\n)\n\nprint('Creating validation data loader\\n')\nval_loader = val_datagen.flow_from_dataframe(\n    dataframe = val_df,\n    directory = train_images_path,\n    x_col = 'filenames',\n    y_col = 'label',\n    batch_size = batch_size,\n    seed = 10,\n    shuffle = True,\n    class_mode = 'binary',\n    target_size = (64,64),\n    color_mode = 'grayscale'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:17:34.464339Z","iopub.execute_input":"2025-10-30T19:17:34.464552Z","iopub.status.idle":"2025-10-30T19:20:25.492246Z","shell.execute_reply.started":"2025-10-30T19:17:34.464529Z","shell.execute_reply":"2025-10-30T19:20:25.491663Z"},"id":"JecftVF_L3vW","outputId":"2148af37-0823-4f0c-d43e-88ebb2b3155a","executionInfo":{"status":"ok","timestamp":1761942663365,"user_tz":360,"elapsed":2124,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TR_STEPSN = len(train_loadern)\nTR_STEPSF = len(train_loaderf)\nTR_STEPST = len(train_loadert)\nTR_STEPSR = len(train_loaderr)\nVAL_STEPS = len(val_loader)\n\nprint(TR_STEPSN)\nprint(TR_STEPSF)\nprint(TR_STEPST)\nprint(TR_STEPSR)\nprint(VAL_STEPS)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T19:20:25.493001Z","iopub.execute_input":"2025-10-30T19:20:25.493248Z","iopub.status.idle":"2025-10-30T19:20:25.497344Z","shell.execute_reply.started":"2025-10-30T19:20:25.493222Z","shell.execute_reply":"2025-10-30T19:20:25.496661Z"},"id":"tfFFqdp-L3vW","outputId":"79870d1a-7260-47eb-a29f-67b7d4faeb4c","executionInfo":{"status":"ok","timestamp":1761942663393,"user_tz":360,"elapsed":5,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Create simple CNN models for respective data augmentation choices","metadata":{"id":"hEl3P20ntnxv"}},{"cell_type":"code","source":"# use same batch size, epochs for all models\ntrain_batch_size = 64\nnum_epochs = 10","metadata":{"id":"NHIZnpXQuGMo","executionInfo":{"status":"ok","timestamp":1761942663398,"user_tz":360,"elapsed":4,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# use same callback definitions for all models\nfrom tensorflow import keras\n\nearly_stopping_callback = keras.callbacks.EarlyStopping(\n    monitor='val_AUC',\n    patience=10,\n    restore_best_weights=True,\n    mode='max',\n    verbose=1\n)\n\nlr_scheduler_callback = keras.callbacks.ReduceLROnPlateau(\n    monitor='val_AUC',\n    factor=0.5,\n    patience=5,\n    min_lr=1e-8,\n    mode='max',\n    verbose=1\n)\n\ncallbacks_list = [early_stopping_callback, lr_scheduler_callback]","metadata":{"id":"icTpVR3id5PP","executionInfo":{"status":"ok","timestamp":1761942663405,"user_tz":360,"elapsed":2,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modeln = models.Sequential([\n    layers.Conv2D(16, (3, 3), activation='relu', input_shape=(64, 64, 1)),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(32, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Flatten(),\n    layers.Dense(32, activation='relu'),\n    layers.Dense(1, activation='sigmoid')  # Binary classification\n])","metadata":{"id":"locn5f3AcHsh","executionInfo":{"status":"ok","timestamp":1761942663463,"user_tz":360,"elapsed":16,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"bfcedd1f-8cb0-4eac-c5ee-4e3e13f205c8"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modeln.compile(optimizer='adam',\n              loss='binary_crossentropy',\n              metrics=['AUC'])","metadata":{"id":"t0LWViOxcfDq","executionInfo":{"status":"ok","timestamp":1761942663465,"user_tz":360,"elapsed":1,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\n\nhistoryn = modeln.fit(\n    x = train_loadern,\n    steps_per_epoch = TR_STEPSN,\n    epochs = num_epochs,\n    validation_data = val_loader,\n    validation_steps = VAL_STEPS,\n    verbose = 1,\n    callbacks = callbacks_list\n)","metadata":{"id":"rRY73mFtdfKX","executionInfo":{"status":"ok","timestamp":1761943045053,"user_tz":360,"elapsed":381587,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"cb3a4a32-1a21-43fd-b343-b76c4a875d41"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lossn, aucn = modeln.evaluate(val_loader, verbose=0)\nprint(f\"No data aug Test AUC: {aucn:.4f}\")","metadata":{"id":"2KWtS16hcl3n","executionInfo":{"status":"ok","timestamp":1761943050739,"user_tz":360,"elapsed":5683,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"c9b96be7-f898-4829-e53f-ce54e8927be4"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on new data\n# Get a sample batch from the validation loader\nsample_batch = next(iter(val_loader))\nsample_images, sample_labels = sample_batch\nsample = sample_images[0:1] # Take the first image in the batch\n\npredictionn = modeln.predict(sample)\nprint(f\"No data aug predicted probability of class 1: {predictionn[0][0]:.4f}\")","metadata":{"id":"N-a7MkUDcoKv","executionInfo":{"status":"ok","timestamp":1761943051137,"user_tz":360,"elapsed":400,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"62255ca9-1d52-4999-ed96-d7c0d44c94e8"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelf = models.Sequential([\n    layers.Conv2D(16, (3, 3), activation='relu', input_shape=(64, 64, 1)),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(32, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Flatten(),\n    layers.Dense(32, activation='relu'),\n    layers.Dense(1, activation='sigmoid')  # Binary classification\n])","metadata":{"executionInfo":{"status":"ok","timestamp":1761943051156,"user_tz":360,"elapsed":19,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"id":"jqGSB1-dwW0Y"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelf.compile(optimizer='adam',\n              loss='binary_crossentropy',\n              metrics=['AUC'])","metadata":{"executionInfo":{"status":"ok","timestamp":1761943051169,"user_tz":360,"elapsed":11,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"id":"R-b6kL7GwW0a"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\n\nhistoryf = modelf.fit(\n    x = train_loaderf,\n    steps_per_epoch = TR_STEPSF,\n    epochs = num_epochs,\n    validation_data = val_loader,\n    validation_steps = VAL_STEPS,\n    verbose = 1,\n    callbacks = callbacks_list\n)","metadata":{"executionInfo":{"status":"ok","timestamp":1761943376910,"user_tz":360,"elapsed":325740,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"37c8fbdb-eebb-452d-8454-9adad463056e","id":"KAJb7B8kwW0a"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lossf, aucf = modelf.evaluate(val_loader, verbose=0)\nprint(f\"Flips only Test AUC: {aucf:.4f}\")","metadata":{"executionInfo":{"status":"ok","timestamp":1761943382692,"user_tz":360,"elapsed":5780,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"6a68a67f-aa8a-4228-bab3-004c4c2c6845","id":"38D5y2A3wW0b"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on new data\npredictionf = modelf.predict(sample)\nprint(f\"Flips only predicted probability of class 1: {predictionf[0][0]:.4f}\")","metadata":{"executionInfo":{"status":"ok","timestamp":1761943383078,"user_tz":360,"elapsed":385,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"eae179c4-d8d3-4096-db30-43b38c808e32","id":"1pPrkSm0wW0b"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelt = models.Sequential([\n    layers.Conv2D(16, (3, 3), activation='relu', input_shape=(64, 64, 1)),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(32, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Flatten(),\n    layers.Dense(32, activation='relu'),\n    layers.Dense(1, activation='sigmoid')  # Binary classification\n])","metadata":{"executionInfo":{"status":"ok","timestamp":1761943383106,"user_tz":360,"elapsed":27,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"id":"FWdEmbgOwXS8"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelt.compile(optimizer='adam',\n              loss='binary_crossentropy',\n              metrics=['AUC'])","metadata":{"executionInfo":{"status":"ok","timestamp":1761943383134,"user_tz":360,"elapsed":23,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"id":"Xe-m6IxkwXS9"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\n\nhistoryt = modelt.fit(\n    x = train_loadert,\n    steps_per_epoch = TR_STEPST,\n    epochs = num_epochs,\n    validation_data = val_loader,\n    validation_steps = VAL_STEPS,\n    verbose = 1,\n    callbacks = callbacks_list\n)","metadata":{"executionInfo":{"status":"ok","timestamp":1761943729114,"user_tz":360,"elapsed":345978,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"19199d7c-3684-497b-9bf3-fbde536c3e1d","id":"Uww2ZlxYwXS9"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss, auct = modelt.evaluate(val_loader, verbose=0)\nprint(f\"Shifts only Test AUC: {auct:.4f}\")","metadata":{"executionInfo":{"status":"ok","timestamp":1761943734855,"user_tz":360,"elapsed":5739,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"a496f1d1-8b3f-4841-beb5-c65c3e00a300","id":"mYYITWgcwXS9"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on new data\npredictiont = modelt.predict(sample)\nprint(f\"Shifts only predicted probability of class 1: {predictiont[0][0]:.4f}\")","metadata":{"executionInfo":{"status":"ok","timestamp":1761943735207,"user_tz":360,"elapsed":336,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"0795af91-447b-47ad-946a-96148a7d5e8c","id":"VtSlLVYowXS9"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelr = models.Sequential([\n    layers.Conv2D(16, (3, 3), activation='relu', input_shape=(64, 64, 1)),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(32, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Flatten(),\n    layers.Dense(32, activation='relu'),\n    layers.Dense(1, activation='sigmoid')  # Binary classification\n])","metadata":{"executionInfo":{"status":"ok","timestamp":1761943735281,"user_tz":360,"elapsed":45,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"id":"RldZP5q1wXpV"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"modelr.compile(optimizer='adam',\n              loss='binary_crossentropy',\n              metrics=['AUC'])","metadata":{"executionInfo":{"status":"ok","timestamp":1761943735310,"user_tz":360,"elapsed":26,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"id":"7GHRWyoWwXpW"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\n\nhistoryr = modelr.fit(\n    x = train_loaderr,\n    steps_per_epoch = TR_STEPSR,\n    epochs = num_epochs,\n    validation_data = val_loader,\n    validation_steps = VAL_STEPS,\n    verbose = 1,\n    callbacks = callbacks_list\n)","metadata":{"executionInfo":{"status":"ok","timestamp":1761944060748,"user_tz":360,"elapsed":325450,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"ccac507b-9658-4726-d67c-ecf39513cbeb","id":"7YReuPanwXpW"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lossr, aucr = modelr.evaluate(val_loader, verbose=0)\nprint(f\"Rotations only Test AUC: {aucr:.4f}\")","metadata":{"executionInfo":{"status":"ok","timestamp":1761944066338,"user_tz":360,"elapsed":5580,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"92c24908-7386-4e6a-80f0-5a88e1ec3c5e","id":"g6Z9arKwwXpX"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on new data\npredictionr = modelr.predict(sample)\nprint(f\"Rotations only predicted probability of class 1: {predictionr[0][0]:.4f}\")","metadata":{"executionInfo":{"status":"ok","timestamp":1761944066719,"user_tz":360,"elapsed":378,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"38a04f39-ae83-443c-ae16-39270f8759fe","id":"Yp2FpPcgwXpX"},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Display the models' respective performances\n","metadata":{"id":"q8uME84cL3vX"}},{"cell_type":"code","source":"epoch_range = range(1, len(historyn.history['loss'])+1)\nplt.figure(figsize = [12,5]); plt.subplot(1,2,1)\nplt.plot(epoch_range, historyn.history['loss'], label = 'Training')\nplt.plot(epoch_range, historyn.history['val_loss'], label = 'Validation')\nplt.xlabel('Epoch');plt.ylabel('Loss');plt.title(\"Loss No Data Aug\")\n\nplt.subplot(1,2,2)\nplt.plot(epoch_range, historyn.history['AUC'], label = 'Training')\nplt.plot(epoch_range, historyn.history['val_AUC'], label = 'Validation')\nplt.xlabel(\"Epoch\");plt.ylabel(\"AUC\");plt.title(\"AUC No Data Aug\")\nplt.legend()\nplt.show()\n\nepoch_range = range(1, len(historyf.history['loss'])+1)\nplt.figure(figsize = [12,5]); plt.subplot(1,2,1)\nplt.plot(epoch_range, historyf.history['loss'], label = 'Training')\nplt.plot(epoch_range, historyf.history['val_loss'], label = 'Validation')\nplt.xlabel('Epoch');plt.ylabel('Loss');plt.title(\"Loss Flips Only\")\n\nplt.subplot(1,2,2)\nplt.plot(epoch_range, historyf.history['AUC'], label = 'Training')\nplt.plot(epoch_range, historyf.history['val_AUC'], label = 'Validation')\nplt.xlabel(\"Epoch\");plt.ylabel(\"AUC\");plt.title(\"AUC Flips Only\")\nplt.legend()\nplt.show()\n\nepoch_range = range(1, len(historyt.history['loss'])+1)\nplt.figure(figsize = [12,5]); plt.subplot(1,2,1)\nplt.plot(epoch_range, historyt.history['loss'], label = 'Training')\nplt.plot(epoch_range, historyt.history['val_loss'], label = 'Validation')\nplt.xlabel('Epoch');plt.ylabel('Loss');plt.title(\"Loss Shifts Only\")\n\nplt.subplot(1,2,2)\nplt.plot(epoch_range, historyt.history['AUC'], label = 'Training')\nplt.plot(epoch_range, historyt.history['val_AUC'], label = 'Validation')\nplt.xlabel(\"Epoch\");plt.ylabel(\"AUC\");plt.title(\"AUC Shifts Only\")\nplt.legend()\nplt.show()\n\nepoch_range = range(1, len(historyr.history['loss'])+1)\nplt.figure(figsize = [12,5]); plt.subplot(1,2,1)\nplt.plot(epoch_range, historyr.history['loss'], label = 'Training')\nplt.plot(epoch_range, historyr.history['val_loss'], label = 'Validation')\nplt.xlabel('Epoch');plt.ylabel('Loss');plt.title(\"Loss Rotations Only\")\n\nplt.subplot(1,2,2)\nplt.plot(epoch_range, historyr.history['AUC'], label = 'Training')\nplt.plot(epoch_range, historyr.history['val_AUC'], label = 'Validation')\nplt.xlabel(\"Epoch\");plt.ylabel(\"AUC\");plt.title(\"AUC Rotations Only\")\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T20:37:56.041209Z","iopub.execute_input":"2025-10-30T20:37:56.041474Z","iopub.status.idle":"2025-10-30T20:37:56.343301Z","shell.execute_reply.started":"2025-10-30T20:37:56.041451Z","shell.execute_reply":"2025-10-30T20:37:56.342759Z"},"id":"-fQMCg6zL3vX","outputId":"10acd06d-7aa1-4a25-a1f2-ce54e90edf4f","executionInfo":{"status":"ok","timestamp":1761944067602,"user_tz":360,"elapsed":847,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Save the models","metadata":{"id":"25U8oiZGL3vX"}},{"cell_type":"code","source":"# save models in Google Drive\nfrom google.colab import drive\ndrive.mount('/content/drive')\nmodel_save_path = '/content/drive/MyDrive/Cancer_Detection_Models/'","metadata":{"id":"7f07Zs3z-A3B","executionInfo":{"status":"ok","timestamp":1761944068173,"user_tz":360,"elapsed":569,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}},"outputId":"e5af76fc-e6f7-4c6c-af3b-56eb1add4543"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pickle\nimport os\n\n# Create the directory if it doesn't exist\nos.makedirs(model_save_path, exist_ok=True)\n\nmodeln.save(f'{model_save_path}/Cancer_Detection_model_n.keras')\npickle.dump(historyn.history, open(f'{model_save_path}/Cancer_Detection_model_n.pk1', 'wb'))\n\nmodelf.save(f'{model_save_path}/Cancer_Detection_model_f.keras')\npickle.dump(historyf.history, open(f'{model_save_path}/Cancer_Detection_model_f.pk1', 'wb'))\n\nmodelt.save(f'{model_save_path}/Cancer_Detection_model_t.keras')\npickle.dump(historyt.history, open(f'{model_save_path}/Cancer_Detection_model_t.pk1', 'wb'))\n\nmodelr.save(f'{model_save_path}/Cancer_Detection_model_r.keras')\npickle.dump(historyr.history, open(f'{model_save_path}/Cancer_Detection_model_r.pk1', 'wb'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T20:37:56.345881Z","iopub.execute_input":"2025-10-30T20:37:56.346326Z","iopub.status.idle":"2025-10-30T20:37:57.325314Z","shell.execute_reply.started":"2025-10-30T20:37:56.346308Z","shell.execute_reply":"2025-10-30T20:37:57.324618Z"},"id":"C4MF9eVrL3va","executionInfo":{"status":"ok","timestamp":1761944068381,"user_tz":360,"elapsed":207,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"id":"Ok_oIDLVL3vb","executionInfo":{"status":"ok","timestamp":1761944068385,"user_tz":360,"elapsed":2,"user":{"displayName":"John Ogilvie","userId":"15535298248975448890"}}},"outputs":[],"execution_count":null}]}