{"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":"none","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"},{"sourceId":2449781,"sourceType":"datasetVersion","datasetId":1482677}],"dockerImageVersionId":31234,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"IMPORTING LIBRARIES","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom plotly.subplots import make_subplots\nimport plotly.graph_objs as go\nimport copy\nimport os\nimport torch\nfrom PIL import Image \nfrom PIL import Image, ImageDraw\nfrom torch.utils.data import Dataset\nimport torchvision.transforms as transforms\nfrom torch.utils.data import random_split\nfrom torch.optim.lr_scheduler import ReduceLROnPlateau\nimport torch.nn as nn\nfrom torchvision import utils\n%matplotlib inline\nfrom IPython.display import HTML, display\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:46:32.991388Z","iopub.execute_input":"2025-12-31T16:46:32.991632Z","iopub.status.idle":"2025-12-31T16:46:42.993056Z","shell.execute_reply.started":"2025-12-31T16:46:32.991608Z","shell.execute_reply":"2025-12-31T16:46:42.992318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#library which allows us to view model summary like keras/tf \n!pip install torchsummary","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:47:06.248393Z","iopub.execute_input":"2025-12-31T16:47:06.248768Z","iopub.status.idle":"2025-12-31T16:47:10.948903Z","shell.execute_reply.started":"2025-12-31T16:47:06.248726Z","shell.execute_reply":"2025-12-31T16:47:10.947832Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_df = pd.read_csv( '/kaggle/input/histopathologic-cancer-detection/train_labels.csv')\nprint(labels_df.head().to_markdown())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:47:26.724253Z","iopub.execute_input":"2025-12-31T16:47:26.724581Z","iopub.status.idle":"2025-12-31T16:47:26.99849Z","shell.execute_reply.started":"2025-12-31T16:47:26.724553Z","shell.execute_reply":"2025-12-31T16:47:26.99751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir('/kaggle/input/histopathologic-cancer-detection/')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:47:32.477996Z","iopub.execute_input":"2025-12-31T16:47:32.478383Z","iopub.status.idle":"2025-12-31T16:47:32.486172Z","shell.execute_reply.started":"2025-12-31T16:47:32.478353Z","shell.execute_reply":"2025-12-31T16:47:32.485198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:47:38.799181Z","iopub.execute_input":"2025-12-31T16:47:38.799872Z","iopub.status.idle":"2025-12-31T16:47:38.804836Z","shell.execute_reply.started":"2025-12-31T16:47:38.799836Z","shell.execute_reply":"2025-12-31T16:47:38.80397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_df[labels_df.duplicated(keep=False)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:47:42.305434Z","iopub.execute_input":"2025-12-31T16:47:42.30583Z","iopub.status.idle":"2025-12-31T16:47:42.411444Z","shell.execute_reply.started":"2025-12-31T16:47:42.305795Z","shell.execute_reply":"2025-12-31T16:47:42.410558Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_df['label'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:47:46.356205Z","iopub.execute_input":"2025-12-31T16:47:46.356523Z","iopub.status.idle":"2025-12-31T16:47:46.3695Z","shell.execute_reply.started":"2025-12-31T16:47:46.356496Z","shell.execute_reply":"2025-12-31T16:47:46.368667Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"Dataset preview\n* non-malignant cases (0) (outlined with green colour)\n* malignant cases (1) (outlined with red colour)","metadata":{}},{"cell_type":"code","source":"imgpath = \"/kaggle/input/histopathologic-cancer-detection/train\"\n\nmalignant = labels_df.loc[labels_df['label']==1]['id'].values\n\nnormal = labels_df.loc[labels_df['label']==0]['id'].values\n\nprint('normal ids')\nprint(normal[0:3],'\\n')\n\nprint('malignant ids')\nprint(malignant [0:3])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:47:51.102712Z","iopub.execute_input":"2025-12-31T16:47:51.103062Z","iopub.status.idle":"2025-12-31T16:47:51.123201Z","shell.execute_reply.started":"2025-12-31T16:47:51.102998Z","shell.execute_reply":"2025-12-31T16:47:51.122184Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imgpath = \"/kaggle/input/histopathologic-cancer-detection/train\"\n\nmalignant = labels_df.loc[labels_df['label']==1]['id'].values\n\nnormal = labels_df.loc[labels_df['label']==0]['id'].values\n\nprint('normal ids')\nprint(normal[0:3],'\\n')\n\nprint('malignant ids')\nprint(malignant [0:3])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:47:55.310437Z","iopub.execute_input":"2025-12-31T16:47:55.310746Z","iopub.status.idle":"2025-12-31T16:47:55.334436Z","shell.execute_reply.started":"2025-12-31T16:47:55.31072Z","shell.execute_reply":"2025-12-31T16:47:55.33336Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imgpath = \"/kaggle/input/histopathologic-cancer-detection/train\"\n\nmalignant = labels_df.loc[labels_df['label']==1]['id'].values\n\nnormal = labels_df.loc[labels_df['label']==0]['id'].values\n\nprint('normal ids')\nprint(normal[0:3],'\\n')\n\nprint('malignant ids')\nprint(malignant [0:3])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:47:58.783167Z","iopub.execute_input":"2025-12-31T16:47:58.783857Z","iopub.status.idle":"2025-12-31T16:47:58.805489Z","shell.execute_reply.started":"2025-12-31T16:47:58.783816Z","shell.execute_reply":"2025-12-31T16:47:58.804455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_fig(ids,title,nrows=5,ncols=15):\n\n    fig,ax = plt.subplots(nrows,ncols,figsize=(18,6))\n    plt.subplots_adjust(wspace=0, hspace=0)\n    for i,j in enumerate(ids[:nrows*ncols]):\n        fname = os.path.join(imgpath,j+'.tif')\n        img = Image.open(fname)\n        idcol = ImageDraw.Draw(img)\n        idcol.rectangle(((0,0), (95,95)), outline='white')\n        plt.subplot(nrows, ncols, i+1)\n        plt.imshow(np.array(img))\n        plt.axis('off')\n\n    plt.suptitle(title, y=0.94)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:48:02.223416Z","iopub.execute_input":"2025-12-31T16:48:02.223712Z","iopub.status.idle":"2025-12-31T16:48:02.230044Z","shell.execute_reply.started":"2025-12-31T16:48:02.223687Z","shell.execute_reply":"2025-12-31T16:48:02.229175Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_fig(malignant,'Malignant Cases')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:48:07.363935Z","iopub.execute_input":"2025-12-31T16:48:07.36428Z","iopub.status.idle":"2025-12-31T16:48:09.466226Z","shell.execute_reply.started":"2025-12-31T16:48:07.364256Z","shell.execute_reply":"2025-12-31T16:48:09.465006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_fig(normal, 'Non-Malignant Cases')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:48:16.310851Z","iopub.execute_input":"2025-12-31T16:48:16.311215Z","iopub.status.idle":"2025-12-31T16:48:18.191307Z","shell.execute_reply.started":"2025-12-31T16:48:16.311185Z","shell.execute_reply":"2025-12-31T16:48:18.189415Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Data Preparation\n","metadata":{}},{"cell_type":"code","source":"torch.manual_seed(0) # fix random seed\nimport torch.nn as nn\n\n\nclass pytorch_data(Dataset):\n\n     def __init__(self,data_dir,transform,data_type=\"train\"):\n         self.data_dir = data_dir\n         self.transform = transform\n         self.data_type = data_type\n\n         cdm_data  = os.path.join(self.data__dir,self.data_type)\n\n         file_names = os.listdir(cdm_data)\n\n         idx_choose = np.random.choice(\n             np.arange(len(file_names)),\n             4000,\n             replace=False\n         ).tolist()\n         \n        file_names_sample = [file_names[x]for x in idx_choose]\n        self.full_filenames = [\n            os.path.join(cdm_data,f) for f in \n            file_names_\n            sample]\n\n        labels_path=os.path.join(data_dir,\"train_labels.csv\")\n        labels_df=pd.read_csv(labels_data)\n        labels_df.set_index(\"id\", inplace=True)\n        self.labels = [\n            labels_df.loc[filename[:-4]].values[0] \n            for filename in file_name_sample\n        ]\n        self.transform = transform\n\n        def  __len__(self):\n            return len(self.full_filenames)\n\n        def  __getitem__(self, idx):\n\n            image = Image.open(self.full_filenames[idx]).convert(\"RGB\")\n            image = self.transform(image)\n            return image, self.labels[idx]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:57:38.195317Z","iopub.execute_input":"2025-12-31T16:57:38.196186Z","iopub.status.idle":"2025-12-31T16:57:38.201908Z","shell.execute_reply.started":"2025-12-31T16:57:38.196153Z","shell.execute_reply":"2025-12-31T16:57:38.200797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torchvision.transforms as transforms\ndata_transformer = transforms.Compose([\n    transforms.Resize((46, 46,)),\n    transforms.ToTensor()\n])\n                                    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:52:54.336471Z","iopub.execute_input":"2025-12-31T16:52:54.336787Z","iopub.status.idle":"2025-12-31T16:52:54.341505Z","shell.execute_reply.started":"2025-12-31T16:52:54.33676Z","shell.execute_reply":"2025-12-31T16:52:54.340721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport os\n\nimport torch\n\n\ndata_dir    = '/kaggle/input/histopathologic-cancer-detection/'\nimg_dataset = HistopathologicDataset (data_dir, data_transformer,\"train\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T17:05:55.808771Z","iopub.execute_input":"2025-12-31T17:05:55.809103Z","iopub.status.idle":"2025-12-31T17:05:55.815721Z","shell.execute_reply.started":"2025-12-31T17:05:55.809075Z","shell.execute_reply":"2025-12-31T17:05:55.814656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img, label  = img_dataset[0]\nprint(img.shape, img.min(), img.max(), label)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T16:52:16.84744Z","iopub.execute_input":"2025-12-31T16:52:16.847802Z","iopub.status.idle":"2025-12-31T16:52:16.855637Z","shell.execute_reply.started":"2025-12-31T16:52:16.847772Z","shell.execute_reply":"2025-12-31T16:52:16.854524Z"}},"outputs":[],"execution_count":null}]}