{"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":"none","dataSources":[{"sourceId":99552,"databundleVersionId":13190393,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!ls -la /kaggle/input/rsna-intracranial-aneurysm-detection","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T04:48:47.636528Z","iopub.execute_input":"2025-07-29T04:48:47.636745Z","iopub.status.idle":"2025-07-29T04:48:47.766745Z","shell.execute_reply.started":"2025-07-29T04:48:47.636719Z","shell.execute_reply":"2025-07-29T04:48:47.765673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport torch\nimport pandas as pd\nfrom skimage import io, transform\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms, utils\n\nimport pandas as pd\nfrom pathlib import Path\n\n# Ignore warnings\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nplt.ion()   # interactive mode","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T05:06:04.558863Z","iopub.execute_input":"2025-07-29T05:06:04.559213Z","iopub.status.idle":"2025-07-29T05:06:13.714101Z","shell.execute_reply.started":"2025-07-29T05:06:04.55919Z","shell.execute_reply":"2025-07-29T05:06:13.713162Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TRAIN = True                     # ← set to True when you want to train\nRAW_DIR = Path(\"/kaggle/input/rsna-intracranial-aneurysm-detection\")\nPRETRAINED_DIR = Path(\"/kaggle/input/\")# used when TRAIN=False\nSERIES_DIR = RAW_DIR / \"series\"\nEXPORT_DIR = Path(\"./\")                                    # artefacts will be saved here\nBATCH_SIZE = 64\nPAD_PERCENTILE = 95\nLR_INIT = 5e-4\nWD = 3e-3\nMIXUP_ALPHA = 0.4\nEPOCHS = 160\nPATIENCE = 40","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T05:52:49.336579Z","iopub.execute_input":"2025-07-29T05:52:49.336928Z","iopub.status.idle":"2025-07-29T05:52:49.342127Z","shell.execute_reply.started":"2025-07-29T05:52:49.336904Z","shell.execute_reply":"2025-07-29T05:52:49.341167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport numpy as np\n\nclass RSNADataset(Dataset):\n    \"\"\"Face Landmarks dataset.\"\"\"\n\n    def __init__(self, csv_file, series_dir = SERIES_DIR, transform=None):\n        \"\"\"\n        Arguments:\n            csv_file (string): Path to the csv file with annotations.\n             (string): Directory with all the images.\n            transform (callable, optional): Optional transform to be applied\n                on a sample.\n        \"\"\"\n        self.train_df = pd.read_csv(csv_file)\n        self.series_dir = SERIES_DIR\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.train_df)\n\n    def __getitem__(self, idx):\n        if torch.is_tensor(idx):\n            idx = idx.tolist()\n\n        series_path = self.series_dir / self.train_df.iloc[idx, 0]\n        images = list(series_path.glob('**/*.dcm')) \n        imgs = [pydicom.dcmread(str(f)).pixel_array for f in sorted(images)]\n        volume = np.stack(imgs) \n        # image = (img_name)\n        # landmarks = self.landmarks_frame.iloc[idx, 1:]\n        # landmarks = np.array([landmarks], dtype=float).reshape(-1, 2)\n        # sample = {'image': image, 'landmarks': landmarks}\n\n        # if self.transform:\n        #     sample = self.transform(sample)\n        labels = self.train_df.iloc[idx, 4:-1]\n        label = self.train_df.iloc[idx, -1]\n        sample = {'images': volume, 'labels': labels, 'label': label}\n        # return sample\n        return sample","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T05:52:51.572384Z","iopub.execute_input":"2025-07-29T05:52:51.57292Z","iopub.status.idle":"2025-07-29T05:52:51.580292Z","shell.execute_reply.started":"2025-07-29T05:52:51.572889Z","shell.execute_reply":"2025-07-29T05:52:51.57941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds = RSNADataset(RAW_DIR / \"train.csv\")\nsample = train_ds[0]\nprint(sample['images'].shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T05:52:53.926647Z","iopub.execute_input":"2025-07-29T05:52:53.92696Z","iopub.status.idle":"2025-07-29T05:52:57.104329Z","shell.execute_reply.started":"2025-07-29T05:52:53.926938Z","shell.execute_reply":"2025-07-29T05:52:57.103406Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = plt.figure(figsize=(15, 10))\ncolumns = 5; rows = 4\nfor i in range(20):\n    fig.add_subplot(rows, columns, i + 1)\n    plt.imshow(sample['images'][i], cmap='gray')\n    plt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T05:44:52.242229Z","iopub.execute_input":"2025-07-29T05:44:52.24257Z","iopub.status.idle":"2025-07-29T05:44:53.374809Z","shell.execute_reply.started":"2025-07-29T05:44:52.24254Z","shell.execute_reply":"2025-07-29T05:44:53.373741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dl = DataLoader(train_ds, batch_size=4,\n                        shuffle=True, num_workers=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T05:48:27.130438Z","iopub.execute_input":"2025-07-29T05:48:27.130852Z","iopub.status.idle":"2025-07-29T05:48:27.136007Z","shell.execute_reply.started":"2025-07-29T05:48:27.130826Z","shell.execute_reply":"2025-07-29T05:48:27.13484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}