{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":92399,"databundleVersionId":11038207,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":237522774,"sourceType":"kernelVersion"},{"sourceId":237522971,"sourceType":"kernelVersion"},{"sourceId":371743,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":307709,"modelId":328164},{"sourceId":371750,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":307716,"modelId":328171},{"sourceId":372052,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":307922,"modelId":328371}],"dockerImageVersionId":31011,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install pytorchvideo","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:07:24.531862Z","iopub.execute_input":"2025-05-03T22:07:24.532100Z","iopub.status.idle":"2025-05-03T22:07:38.510591Z","shell.execute_reply.started":"2025-05-03T22:07:24.532076Z","shell.execute_reply":"2025-05-03T22:07:38.509895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport h5py\nimport math\nimport cv2\nimport torch\nimport pandas as pd\nimport numpy as np\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom pytorchvideo.models.hub import slowfast_r50\nfrom sklearn.metrics import roc_auc_score\nimport matplotlib.pyplot as plt\n\n\nfrom tqdm import tqdm\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:07:38.511387Z","iopub.execute_input":"2025-05-03T22:07:38.511581Z","iopub.status.idle":"2025-05-03T22:07:52.587699Z","shell.execute_reply.started":"2025-05-03T22:07:38.511562Z","shell.execute_reply":"2025-05-03T22:07:52.587116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class SlowFastRGBD(nn.Module):\n    def __init__(self, pretrained=True, alpha=4):\n        super().__init__()\n        self.base = slowfast_r50(pretrained=pretrained)\n        self._update_stem(self.base.blocks[0].multipathway_blocks[0], 4)\n        self._update_stem(self.base.blocks[0].multipathway_blocks[1], 4)\n        in_features = self.base.blocks[-1].proj.in_features\n        self.base.blocks[-1].proj = nn.Linear(in_features, 1)\n        self.alpha = alpha\n\n    def _update_stem(self, stem, in_channels):\n        old_conv = stem.conv\n        new_conv = nn.Conv3d(\n            in_channels=in_channels,\n            out_channels=old_conv.out_channels,\n            kernel_size=old_conv.kernel_size,\n            stride=old_conv.stride,\n            padding=old_conv.padding,\n            bias= False\n        )\n        with torch.no_grad():\n            new_conv.weight[:, :3] = old_conv.weight\n            if in_channels > 3:\n                nn.init.kaiming_normal_(new_conv.weight[:, 3:])\n        stem.conv = new_conv\n\n    def pack_pathway_input(self, x):\n        # x: [batch, 4, T, H, W]\n        fast_pathway = x\n        slow_pathway = x[:, :, ::self.alpha, :, :]\n        return [slow_pathway, fast_pathway]\n\n    def forward(self, x):\n        x = self.pack_pathway_input(x)\n        return self.base(x)  # Output: (batch, 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:07:52.589467Z","iopub.execute_input":"2025-05-03T22:07:52.589870Z","iopub.status.idle":"2025-05-03T22:07:52.597144Z","shell.execute_reply.started":"2025-05-03T22:07:52.589844Z","shell.execute_reply":"2025-05-03T22:07:52.596385Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class H5DataLoader(Dataset):\n    def __init__(self,path,df,mode='Train',transform=False):\n        self.path = path\n        self.df = df\n        self.mode = mode\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        \n        file_name = int(self.df.loc[idx,'id'])\n        feature_path = os.path.join(self.path, f'{file_name}.h5')\n        with h5py.File(feature_path, 'r') as h5f:\n            frames = h5f['frames'][:]\n            depth_channel = h5f['depth_channel'][:]\n            video_id = h5f.attrs['video_id']\n            if self.mode == 'Train':\n                target = h5f.attrs['target']\n                return frames,depth_channel,video_id,target\n            else:\n                return frames,depth_channel,video_id","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:07:52.597857Z","iopub.execute_input":"2025-05-03T22:07:52.598124Z","iopub.status.idle":"2025-05-03T22:07:52.622173Z","shell.execute_reply.started":"2025-05-03T22:07:52.598106Z","shell.execute_reply":"2025-05-03T22:07:52.621688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TRAIN_FILES = r'/kaggle/input/nexar-train-slow-fast/train_video_features/'\nTEST_FILES = r'/kaggle/input/nexar-test-slow-fast/test_video_features/'\n\nTRAIN_DF = pd.read_csv('/kaggle/input/nexar-collision-prediction/train.csv')\nTEST_DF = pd.read_csv('/kaggle/input/nexar-collision-prediction/test.csv')\n\nDEVICE = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n\noutput_dir = \"output_weights\"\nos.makedirs(output_dir, exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:07:52.622778Z","iopub.execute_input":"2025-05-03T22:07:52.622962Z","iopub.status.idle":"2025-05-03T22:07:52.771679Z","shell.execute_reply.started":"2025-05-03T22:07:52.622947Z","shell.execute_reply":"2025-05-03T22:07:52.770947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = SlowFastRGBD(pretrained=True)\n\ncheckpoint_path = \"/kaggle/input/slow_fast_model_2_epoch/pytorch/default/1/model_epoch_2.pth\"\nmodel.load_state_dict(torch.load(checkpoint_path))\n\nmodel = nn.DataParallel(model)\nmodel.to(DEVICE)\nprint('Done')\n# loss_func = torch.nn.BCEWithLogitsLoss()\n# optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-4, weight_decay=1e-5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:07.803874Z","iopub.execute_input":"2025-05-03T22:08:07.804140Z","iopub.status.idle":"2025-05-03T22:08:12.647124Z","shell.execute_reply.started":"2025-05-03T22:08:07.804120Z","shell.execute_reply":"2025-05-03T22:08:12.646545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:15.300217Z","iopub.execute_input":"2025-05-03T22:08:15.300975Z","iopub.status.idle":"2025-05-03T22:08:15.303975Z","shell.execute_reply.started":"2025-05-03T22:08:15.300942Z","shell.execute_reply":"2025-05-03T22:08:15.303401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# X_train, X_valid, y_train, y_valid = train_test_split(TRAIN_DF.drop('target',axis=1) ,TRAIN_DF['target'],test_size = 0.20,stratify=TRAIN_DF['target'] )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:15.488487Z","iopub.execute_input":"2025-05-03T22:08:15.488718Z","iopub.status.idle":"2025-05-03T22:08:15.491989Z","shell.execute_reply.started":"2025-05-03T22:08:15.488702Z","shell.execute_reply":"2025-05-03T22:08:15.491276Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# X_train.reset_index(drop=True , inplace=True)\n# X_valid.reset_index(drop=True , inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:15.666881Z","iopub.execute_input":"2025-05-03T22:08:15.667058Z","iopub.status.idle":"2025-05-03T22:08:15.670110Z","shell.execute_reply.started":"2025-05-03T22:08:15.667045Z","shell.execute_reply":"2025-05-03T22:08:15.669574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# print(X_train.shape)\n# X_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:15.826876Z","iopub.execute_input":"2025-05-03T22:08:15.827048Z","iopub.status.idle":"2025-05-03T22:08:15.830104Z","shell.execute_reply.started":"2025-05-03T22:08:15.827036Z","shell.execute_reply":"2025-05-03T22:08:15.829581Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# print(X_valid.shape)\n# X_valid.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:16.837779Z","iopub.execute_input":"2025-05-03T22:08:16.838451Z","iopub.status.idle":"2025-05-03T22:08:16.841382Z","shell.execute_reply.started":"2025-05-03T22:08:16.838415Z","shell.execute_reply":"2025-05-03T22:08:16.840783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TRAIN_DATASET = H5DataLoader(path=TRAIN_FILES, mode='Train', df=TRAIN_DF)\n# VALID_DATASET = H5DataLoader(path=TRAIN_FILES, mode='Train', df=X_valid)\nTEST_DATASET = H5DataLoader(path=TEST_FILES, mode='Test', df=TEST_DF)\n\nTRAIN_LOADER = DataLoader(TRAIN_DATASET, batch_size=16,num_workers = 3,pin_memory=True, shuffle=True)\n# VALID_LOADER = DataLoader(VALID_DATASET, batch_size=16,num_workers = 2)\nTEST_LOADER = DataLoader(TEST_DATASET, batch_size=16,num_workers = 3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:17.082554Z","iopub.execute_input":"2025-05-03T22:08:17.083174Z","iopub.status.idle":"2025-05-03T22:08:17.087687Z","shell.execute_reply.started":"2025-05-03T22:08:17.083152Z","shell.execute_reply":"2025-05-03T22:08:17.086907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model.train()\n# EPOCHS = 10\n\n# epoch_bar = tqdm(range(EPOCHS), desc=\"Training\")\n\n# for epc in epoch_bar:\n#     total_loss = 0\n#     train_batch_bar = tqdm(TRAIN_LOADER, leave=False, desc=f\"Epoch {epc+1}\")\n#     for i , (frames,depth_channel,video_id,target) in enumerate(train_batch_bar):\n#         frames = frames.permute(0, 4, 1, 2, 3)\n#         depth_channel = depth_channel.unsqueeze(1)\n        \n#         inputs = torch.cat([frames, depth_channel], dim=1).float().to(DEVICE, non_blocking=True)\n#         target = target.float().to(DEVICE,non_blocking=True)\n        \n#         optimizer.zero_grad()\n#         output = model(inputs)\n#         loss = loss_func(output.view(-1), target)\n#         loss.backward()\n#         optimizer.step()\n#         total_loss += loss.item()\n#         train_batch_bar.set_postfix(loss=loss.item(), avg_loss=total_loss / (i + 1))\n    \n#     avg_loss = total_loss / len(TRAIN_LOADER)\n#     print(f'AVG LOSS :- {avg_loss}')\n#     epoch_bar.set_postfix(avg_loss=avg_loss)\n\n#     if (epc + 1) % 2 == 0:\n#         checkpoint_path = os.path.join(output_dir, f\"model_epoch_{epc+1}.pth\")\n#         torch.save(model.module.state_dict(), checkpoint_path)\n#         print(f\"Checkpoint saved: {checkpoint_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:18.754001Z","iopub.execute_input":"2025-05-03T22:08:18.754310Z","iopub.status.idle":"2025-05-03T22:08:18.758367Z","shell.execute_reply.started":"2025-05-03T22:08:18.754286Z","shell.execute_reply":"2025-05-03T22:08:18.757539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# checkpoint_path = os.path.join(output_dir, f\"model_epoch_{epc+1}.pth\")\n# torch.save(model.module.state_dict(), checkpoint_path)\n# print(f\"Checkpoint saved: {checkpoint_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:20.493717Z","iopub.execute_input":"2025-05-03T22:08:20.494267Z","iopub.status.idle":"2025-05-03T22:08:20.497258Z","shell.execute_reply.started":"2025-05-03T22:08:20.494238Z","shell.execute_reply":"2025-05-03T22:08:20.496538Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/nexar-collision-prediction/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:20.817414Z","iopub.execute_input":"2025-05-03T22:08:20.817638Z","iopub.status.idle":"2025-05-03T22:08:20.833723Z","shell.execute_reply.started":"2025-05-03T22:08:20.817622Z","shell.execute_reply":"2025-05-03T22:08:20.832942Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\ntest_preds = []\nvideo_ids = []\n# test_targets = []\nwith torch.no_grad():\n    valid_batch_bar = tqdm(TEST_LOADER, leave=False)\n    for i, (frames,depth_channel,video_id) in enumerate(valid_batch_bar):\n        if i % 5 == 0:\n            print(f'{i} Done !!!')\n        frames = frames.permute(0, 4, 1, 2, 3).to(DEVICE)\n        depth_channel = depth_channel.unsqueeze(1).to(DEVICE)\n        inputs = torch.cat([frames, depth_channel], dim=1).float()\n\n        output = model(inputs)\n        probs = torch.sigmoid(output).squeeze().cpu().numpy()\n        test_preds.extend(probs)\n        video_ids.extend(video_id.cpu().numpy())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:08:41.171558Z","iopub.execute_input":"2025-05-03T22:08:41.172291Z","iopub.status.idle":"2025-05-03T22:12:46.330447Z","shell.execute_reply.started":"2025-05-03T22:08:41.172266Z","shell.execute_reply":"2025-05-03T22:12:46.329185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub.target = test_preds\nsub.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-03T22:12:58.485073Z","iopub.execute_input":"2025-05-03T22:12:58.485376Z","iopub.status.idle":"2025-05-03T22:12:58.497805Z","shell.execute_reply.started":"2025-05-03T22:12:58.485352Z","shell.execute_reply":"2025-05-03T22:12:58.497262Z"}},"outputs":[],"execution_count":null}]}