{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","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"}],"dockerImageVersionId":31236,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-24T17:36:57.119247Z","iopub.execute_input":"2025-12-24T17:36:57.119980Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"full_train_df=pd.read_csv(\"/kaggle/input/histopathologic-cancer-detection/train_labels.csv\")\nfull_train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T18:12:30.160590Z","iopub.execute_input":"2025-12-24T18:12:30.161197Z","iopub.status.idle":"2025-12-24T18:12:30.372008Z","shell.execute_reply.started":"2025-12-24T18:12:30.161167Z","shell.execute_reply":"2025-12-24T18:12:30.371412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Train Size: {}\".format(len(os.listdir('/kaggle/input/histopathologic-cancer-detection/train'))))\nprint(\"Test Size: {}\".format(len(os.listdir('/kaggle/input/histopathologic-cancer-detection/test'))))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T18:12:36.794912Z","iopub.execute_input":"2025-12-24T18:12:36.795172Z","iopub.status.idle":"2025-12-24T18:12:40.787553Z","shell.execute_reply.started":"2025-12-24T18:12:36.795151Z","shell.execute_reply":"2025-12-24T18:12:40.786870Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import sklearn.utils\n#SAMPLING\nSAMPLE_SIZE = 80000\n\n# DATA_PATHS\ntrain_path = '/kaggle/input/histopathologic-cancer-detection/train'\ntest_path = '/kaggle/input/histopathologic-cancer-detection/test'\n\n# use 80000 P & N \ndf_negatives = full_train_df[full_train_df['label'] == 0].sample(SAMPLE_SIZE ,random_state = 42)\n\ndf_positives = full_train_df[full_train_df['label'] == 1].sample(SAMPLE_SIZE ,random_state = 42)\n\n# Concatenate the two dfs and shuffle them up\ntrain_df = sklearn.utils.shuffle(pd.concat([df_positives, df_negatives], axis=0).reset_index(drop=True))\n\ntrain_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T18:12:45.745996Z","iopub.execute_input":"2025-12-24T18:12:45.746703Z","iopub.status.idle":"2025-12-24T18:12:45.804129Z","shell.execute_reply.started":"2025-12-24T18:12:45.746675Z","shell.execute_reply":"2025-12-24T18:12:45.803376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import sklearn.utils\nimport torch\nfrom torch.utils.data import Dataset, DataLoader,SubsetRandomSampler\nimport torch.nn as nn \nimport torch.optim as optim","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T18:30:14.634013Z","iopub.execute_input":"2025-12-24T18:30:14.634315Z","iopub.status.idle":"2025-12-24T18:30:14.638501Z","shell.execute_reply.started":"2025-12-24T18:30:14.634291Z","shell.execute_reply":"2025-12-24T18:30:14.637613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Our own custom class for datasets\nclass CreateDataset(Dataset):\n    def __init__(self, df_data, data_dir = './', transform=None):\n        super().__init__()\n        self.df = df_data.values\n        self.data_dir = data_dir\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_name,label = self.df[index]\n        img_path = os.path.join(self.data_dir, img_name+'.tif')\n        image = cv2.imread(img_path)\n        if self.transform is not None:\n            image = self.transform(image)\n        return image, label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T18:30:18.141273Z","iopub.execute_input":"2025-12-24T18:30:18.141778Z","iopub.status.idle":"2025-12-24T18:30:18.146872Z","shell.execute_reply.started":"2025-12-24T18:30:18.141749Z","shell.execute_reply":"2025-12-24T18:30:18.146085Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torchvision import transforms","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T18:37:24.961076Z","iopub.execute_input":"2025-12-24T18:37:24.961689Z","iopub.status.idle":"2025-12-24T18:37:28.352897Z","shell.execute_reply.started":"2025-12-24T18:37:24.961660Z","shell.execute_reply":"2025-12-24T18:37:28.352331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transforms_train = transforms.Compose([\n    transforms.ToPILImage(),\n    transforms.RandomHorizontalFlip(p=0.4),\n    transforms.RandomVerticalFlip(p=0.4),\n    transforms.RandomRotation(20),\n    transforms.ToTensor(),\n    # We the get the following mean and std for the channels of all the images\n    #transforms.Normalize((0.70244707, 0.54624322, 0.69645334), (0.23889325, 0.28209431, 0.21625058))\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))\n])\n\ntrain_data = CreateDataset(df_data=train_df, data_dir=train_path, transform=transforms_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T18:37:59.957026Z","iopub.execute_input":"2025-12-24T18:37:59.957331Z","iopub.status.idle":"2025-12-24T18:37:59.975056Z","shell.execute_reply.started":"2025-12-24T18:37:59.957305Z","shell.execute_reply":"2025-12-24T18:37:59.974319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Set Batch Size\nbatch_size = 128\n\n# Percentage of training set to use as validation\nvalid_size = 0.1\n\n# obtain training indices that will be used for validation\nnum_train = len(train_data)\nindices = list(range(num_train))\n# np.random.shuffle(indices)\nsplit = int(np.floor(valid_size * num_train))\ntrain_idx, valid_idx = indices[split:], indices[:split]\n\n# Create Samplers\ntrain_sampler = SubsetRandomSampler(train_idx)\nvalid_sampler = SubsetRandomSampler(valid_idx)\n\n# prepare data loaders (combine dataset and sampler)\ntrain_loader = DataLoader(train_data, batch_size=batch_size, sampler=train_sampler)\nvalid_loader = DataLoader(train_data, batch_size=batch_size, sampler=valid_sampler)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T18:48:36.399491Z","iopub.execute_input":"2025-12-24T18:48:36.400263Z","iopub.status.idle":"2025-12-24T18:48:36.410720Z","shell.execute_reply.started":"2025-12-24T18:48:36.400232Z","shell.execute_reply":"2025-12-24T18:48:36.410026Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transforms_test = transforms.Compose([\n    transforms.ToPILImage(),\n    transforms.ToTensor(),\n    #transforms.Normalize((0.70244707, 0.54624322, 0.69645334), (0.23889325, 0.28209431, 0.21625058))\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))\n])\n\n# creating test data\nsample_sub = pd.read_csv(\"/kaggle/input/histopathologic-cancer-detection/sample_submission.csv\")\ntest_data = CreateDataset(df_data=sample_sub, data_dir=test_path, transform=transforms_test)\n\n# prepare the test loader\ntest_loader = DataLoader(test_data, batch_size=batch_size, shuffle=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-24T18:51:11.558470Z","iopub.execute_input":"2025-12-24T18:51:11.559036Z","iopub.status.idle":"2025-12-24T18:51:11.725220Z","shell.execute_reply.started":"2025-12-24T18:51:11.559008Z","shell.execute_reply":"2025-12-24T18:51:11.724424Z"}},"outputs":[],"execution_count":null}]}