{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":52254,"databundleVersionId":9674523,"sourceType":"competition"},{"sourceId":6089786,"sourceType":"datasetVersion","datasetId":3487378},{"sourceId":6267500,"sourceType":"datasetVersion","datasetId":3581068},{"sourceId":6682670,"sourceType":"datasetVersion","datasetId":3745884},{"sourceId":6713246,"sourceType":"datasetVersion","datasetId":3867724},{"sourceId":9539442,"sourceType":"datasetVersion","datasetId":5060753},{"sourceId":135925962,"sourceType":"kernelVersion"},{"sourceId":143511308,"sourceType":"kernelVersion"}],"dockerImageVersionId":30559,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Initialization","metadata":{}},{"cell_type":"code","source":"!pip install -qqq /kaggle/input/rsna-abdomen-packages/{pydicom-2.4.3-py3-none-any.whl,pylibjpeg-1.4.0-py3-none-any.whl,python_gdcm-3.0.22-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl}\n!pip install -qqq /kaggle/input/rsna-abdomen-packages/dicomsdl-0.109.2-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-10-03T13:04:27.111458Z","iopub.execute_input":"2024-10-03T13:04:27.111766Z","iopub.status.idle":"2024-10-03T13:05:32.204524Z","shell.execute_reply.started":"2024-10-03T13:04:27.111738Z","shell.execute_reply":"2024-10-03T13:05:32.203425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -qqq ../input/contrails-model-def1/einops-0.6.1-py3-none-any.whl\n!pip install -qqq --no-index --find-links /kaggle/input/contrails-wheels/ pretrainedmodels==0.7.4\n!pip install -qqq --no-index --find-links /kaggle/input/contrails-wheels/ efficientnet_pytorch==0.7.1","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:05:32.206396Z","iopub.execute_input":"2024-10-03T13:05:32.206703Z","iopub.status.idle":"2024-10-03T13:06:30.930757Z","shell.execute_reply.started":"2024-10-03T13:05:32.206677Z","shell.execute_reply":"2024-10-03T13:06:30.92971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport re\nimport sys\nimport glob\nimport json\nimport torch\nimport shutil\nimport warnings\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport torch.nn.functional as F\n\nfrom tqdm.notebook import tqdm","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-10-03T13:06:30.932034Z","iopub.execute_input":"2024-10-03T13:06:30.932322Z","iopub.status.idle":"2024-10-03T13:06:34.360729Z","shell.execute_reply.started":"2024-10-03T13:06:30.932286Z","shell.execute_reply":"2024-10-03T13:06:34.359922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append('/kaggle/input/rsna-abdomen-code/src')\n\nfrom inference.extract_features import Config\nfrom inference.lvl2 import predict as predict_2\nfrom inference.lvl2 import PatientFeatureInfDataset, to_sub_format\nfrom inference.crop import get_crops\n\nfrom util.torch import load_model_weights\nfrom util.plots import plot_mask\n\nfrom data.transforms import get_transfos\nfrom data.dataset import AbdominalCropDataset\n\nfrom inference.processing import process, restrict_imgs\nfrom inference.lvl1 import predict, AbdominalInfDataset\n\nsys.path.append('/kaggle/input/timm-smp/pytorch-image-models-main/pytorch-image-models-main')\nsys.path.append(\n    \"/kaggle/input/timm-smp/segmentation_models.pytorch-master/segmentation_models.pytorch-master\"\n)\n\nfrom model_zoo.models import define_model\n# from model_zoo.models_lvl2 import define_model as define_model_2\nfrom model_zoo.models_seg import define_model as define_model_seg\nfrom model_zoo.models_seg import convert_3d","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-10-03T13:06:34.362809Z","iopub.execute_input":"2024-10-03T13:06:34.363194Z","iopub.status.idle":"2024-10-03T13:06:48.643168Z","shell.execute_reply.started":"2024-10-03T13:06:34.363168Z","shell.execute_reply":"2024-10-03T13:06:48.64232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\n\n\ndef define_model_2(\n    name=\"rnn_att\",\n    ft_dim=2048,\n    layer_dim=64,\n    n_layers=1,\n    dense_dim=256,\n    p=0.1,\n    num_classes=2,\n    num_classes_aux=0,\n    n_fts=0,\n):\n    \"\"\"\n    Define the level 2 model.\n\n    Args:\n        name (str): The name of the model to define. Default is \"rnn_att\".\n        ft_dim (int): Dimension of input features. Default is 2048.\n        layer_dim (int): Dimension of LSTM layers. Default is 64.\n        n_layers (int): Number of LSTM layers. Default is 1.\n        dense_dim (int): Dimension of the dense layer. Default is 256.\n        p (float): Dropout probability. Default is 0.1.\n        num_classes (int): Number of main classes. Default is 2.\n        num_classes_aux (int): Number of auxiliary classes. Default is 0.\n        n_fts (int): Number of features to use. Default is 0.\n\n    Returns:\n        nn.Module: The defined model.\n    \"\"\"\n    if name == \"rnn_att\":\n        pass\n    elif name == \"rnn_spleen\":\n        pass\n    elif name == \"rnn_bowel\":\n        return RNNBowelModel(\n            ft_dim=ft_dim,\n            lstm_dim=layer_dim,\n            n_lstm=n_layers,\n            dense_dim=dense_dim,\n            p=p,\n            num_classes=num_classes,\n            num_classes_aux=num_classes_aux,\n            n_fts=n_fts,\n        )\n    else:\n        raise NotImplementedError\n\n\nclass RNNBowelModel(nn.Module):\n    \"\"\"\n    Recurrent Neural Network with attention.\n\n    Attributes:\n        ft_dim (int): The dimension of input features.\n        lstm_dim (int): The dimension of the LSTM layer.\n        n_lstm (int): The number of LSTM layers.\n        dense_dim (int): The dimension of the dense layer.\n        p (float): Dropout probability.\n        num_classes (int): The number of primary target classes.\n        num_classes_aux (int): The number of auxiliary target classes.\n        n_fts (int): The number of additional features.\n    \"\"\"\n    def __init__(\n        self,\n        ft_dim=64,\n        lstm_dim=64,\n        n_lstm=1,\n        dense_dim=64,\n        p=0.1,\n        num_classes=8,\n        num_classes_aux=0,\n        n_fts=0,\n    ):\n        \"\"\"\n        Constructor.\n\n        Args:\n            ft_dim (int): The dimension of input features. Defaults to 64.\n            lstm_dim (int): The dimension of the LSTM layer. Defaults to 64.\n            n_lstm (int): The number of LSTM layers. Defaults to 1.\n            dense_dim (int): The dimension of the dense layer. Defaults to 64.\n            p (float): Dropout probability. Defaults to 0.1.\n            num_classes (int): The number of primary target classes. Defaults to 8.\n            num_classes_aux (int): The number of auxiliary target classes. Defaults to 0.\n            n_fts (int): The number of additional features. Defaults to 0.\n\n        \"\"\"\n        super().__init__()\n        self.n_fts = n_fts\n        self.n_lstm = n_lstm\n        self.num_classes = num_classes\n        self.num_classes_aux = num_classes_aux\n\n        self.mlp = nn.Sequential(\n            nn.Linear(ft_dim, dense_dim),\n            nn.Mish(),\n        )\n\n        if n_fts > 0:\n            self.mlp_fts = nn.Sequential(\n                nn.Linear(n_fts, dense_dim),\n                nn.Dropout(p=p),\n                nn.Mish(),\n            )\n            n_fts = n_fts + dense_dim\n\n        self.lstm = nn.LSTM(dense_dim, lstm_dim, batch_first=True, bidirectional=True)\n\n        self.logits_bowel = nn.Sequential(\n            nn.Dropout(p=0),\n            nn.Linear(2 * (lstm_dim * 2 + dense_dim) + 4, dense_dim),\n            nn.Mish(),\n            nn.Linear(dense_dim, 1),\n        )\n        self.logits_extrav = nn.Sequential(\n            nn.Dropout(p=0),\n            nn.Linear(2 * (lstm_dim * 2 + dense_dim) + 4, dense_dim),\n            nn.Mish(),\n            nn.Linear(dense_dim, 1),\n        )\n\n        if num_classes_aux:\n            raise NotImplementedError\n\n    def attention_pooling(self, x, w):\n        \"\"\"\n        Apply attention pooling to input features.\n\n        Args:\n            x (torch.Tensor): Input feature tensor.\n            w (torch.Tensor): Attention weights.\n\n        Returns:\n            torch.Tensor: The pooled result.\n        \"\"\"\n        return (x * w).sum(1) / (w.sum(1) + 1e-6), (x * w).amax(1)\n\n    def forward(self, x, ft=None):\n        \"\"\"\n        Forward pass of the RNN with attention model.\n\n        Args:\n            x (torch.Tensor): Input tensor of shape (batch_size, sequence_length, input_features).\n            ft (torch.Tensor, optional): Additional features tensor. Default is None.\n\n        Returns:\n            torch.Tensor: Model outputs as logits for different classes.\n            torch.Tensor: Placeholder for auxiliary outputs.\n        \"\"\"\n        ft = None  # ft[:, 2:]\n        seg = x[:, :, :5]\n        x = x[:, :, 5:]\n    \n        x = torch.cat([\n            seg[:, :, -1:],  # bowel\n            seg.amax(-1, keepdims=True),  # extrav\n            x[:, :, :2],  # bowel & extrav\n            x[:, :, 11: 13],  # bowel & extrav * seg\n        ], -1)\n\n        features = self.mlp(x)\n        features_lstm, _ = self.lstm(features)\n\n        features = torch.cat([features, features_lstm], -1)\n\n        bowel = seg[:, :, :1]\n        att_bowel, max_bowel = self.attention_pooling(features, bowel)\n\n        mean = features.mean(1)\n        max_ = features.amax(1)\n        \n#         print(x.amax(1))\n#         print(x.size())\n\n        scores = x[:, :, 2:]\n        scores = scores.view(x.size(0), x.size(1), -1, 2, 2)\n        pooled_scores = scores.mean(2)  # avg across models\n        pooled_scores = torch.cat([pooled_scores.amax(1), pooled_scores.mean(1)], 1)  # temporal pool\n#         print(pooled_scores)\n\n        pooled_scores_bowel = pooled_scores[:, :, 0]\n        pooled_scores_extrav = pooled_scores[:, :, 1]\n        \n#         print(pooled_scores_bowel.size())\n#         print(pooled_scores_extrav.size())\n\n        logits_bowel = self.logits_bowel(\n            torch.cat([att_bowel, max_bowel, pooled_scores_bowel], -1)\n        )\n        logits_extrav = self.logits_extrav(\n            torch.cat([mean, max_, pooled_scores_extrav], -1)\n        )\n    \n        logits_spleen = torch.zeros((x.size(0), 3), dtype=logits_bowel.dtype, device=logits_bowel.device)\n        logits_kidney = torch.zeros((x.size(0), 3), dtype=logits_bowel.dtype, device=logits_bowel.device)\n        logits_liver = torch.zeros((x.size(0), 3), dtype=logits_bowel.dtype, device=logits_bowel.device)\n\n        logits = torch.cat(\n            [logits_bowel, logits_extrav, logits_kidney, logits_liver, logits_spleen],\n            -1,\n        )\n\n        return logits, torch.zeros((x.size(0)))\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-10-03T13:06:48.644462Z","iopub.execute_input":"2024-10-03T13:06:48.934998Z","iopub.status.idle":"2024-10-03T13:06:48.959124Z","shell.execute_reply.started":"2024-10-03T13:06:48.93495Z","shell.execute_reply":"2024-10-03T13:06:48.958144Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Params","metadata":{}},{"cell_type":"code","source":"EVAL = False","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:17:14.47118Z","iopub.execute_input":"2024-10-03T13:17:14.471584Z","iopub.status.idle":"2024-10-03T13:17:14.476094Z","shell.execute_reply.started":"2024-10-03T13:17:14.471557Z","shell.execute_reply":"2024-10-03T13:17:14.47509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_PATH = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images/\"\nSAVE_FOLDER = \"/tmp/\"\n\nos.makedirs(SAVE_FOLDER, exist_ok=True)\n!rm -r $SAVE_FOLDER\nos.makedirs(SAVE_FOLDER, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:17:14.806754Z","iopub.execute_input":"2024-10-03T13:17:14.807113Z","iopub.status.idle":"2024-10-03T13:17:16.037183Z","shell.execute_reply.started":"2024-10-03T13:17:14.807085Z","shell.execute_reply":"2024-10-03T13:17:16.035874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if len(os.listdir(DATA_PATH)) <= 3:\n    DATA_PATH = \"/tmp/data/\"\n    os.makedirs(DATA_PATH, exist_ok=True)\n    \n    for pid in [10082,10004, 10005, 10007]:\n        try:\n            shutil.copytree(f\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/{pid}\", DATA_PATH + f\"{pid}/\")\n        except FileExistsError:\n            pass\n#         break\n\nif EVAL:\n    df = pd.read_csv(\"/kaggle/input/rsna-weights-spleen-fix/2024-09-20_1/df_val_0.csv\")\n    DATA_PATH = f\"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/\"\n    patients = sorted(df['patient_id'].unique())[:100]\n    print(patients)","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:17:16.039381Z","iopub.execute_input":"2024-10-03T13:17:16.039721Z","iopub.status.idle":"2024-10-03T13:17:20.19782Z","shell.execute_reply.started":"2024-10-03T13:17:16.039692Z","shell.execute_reply":"2024-10-03T13:17:20.19684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 16\nBATCH_SIZE_2 = 512\nUSE_FP16 = True\nNUM_WORKERS = 2\n\nRESTRICT = False\nHALF = True","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:17:20.199466Z","iopub.execute_input":"2024-10-03T13:17:20.199761Z","iopub.status.idle":"2024-10-03T13:17:20.204154Z","shell.execute_reply.started":"2024-10-03T13:17:20.199736Z","shell.execute_reply":"2024-10-03T13:17:20.203038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# FOLD = 0\nFOLD = 0 if EVAL else \"fullfit_0\"\n\nEXP_FOLDERS = [\n    (\"/kaggle/input/rsna-weights-spleen-fix/2023-09-20_14/\", \"seg\", [FOLD]),\n    (\"/kaggle/input/rsna-weights-spleen-fix/2024-05-21_5/\", \"probas_2d\", [FOLD]),  # maxvit_tiny_tf_512 NO SMH\n]\n\nCROP_EXP_FOLDERS = [\n#     (\"/kaggle/input/rsna-weights-spleen-fix/2024-05-21_7/\", \"crop\", [FOLD]),   # coatnet_1_rw_224 NO SMH FIX\n]\n\nEXP_FOLDERS_2 = [\n#     \"/kaggle/input/rsna-weights-spleen-fix/2024-05-22_0/\",  # Spleen only, no SMH FIX\n#     \"/kaggle/input/rsna-weights-spleen-fix/2024-09-20_1/\",  # Bowel & Extrav only\n    \"/kaggle/input/rsna-weights-spleen-fix/2024-10-03_0/\",  # Bowel & Extrav only no restrict\n]\n\nEXP_FOLDER_3D = \"/kaggle/input/rsna-weights-spleen-fix/2023-09-24_20/\"\n\nFOLDS_2 = [0, 1, 2, 3]\nif \"fullfit\" not in str(FOLD):\n    FOLDS_2 = [FOLD]\n    \nConfig(json.load(open(EXP_FOLDERS_2[0] + \"config.json\", 'r'))).exp_folders","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:17:20.206773Z","iopub.execute_input":"2024-10-03T13:17:20.207132Z","iopub.status.idle":"2024-10-03T13:17:20.39666Z","shell.execute_reply.started":"2024-10-03T13:17:20.207097Z","shell.execute_reply":"2024-10-03T13:17:20.395597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Level 1","metadata":{}},{"cell_type":"code","source":"models = []\nfor exp_folder, mode, folds in EXP_FOLDERS:\n    models_ = []\n    config = Config(json.load(open(exp_folder + \"config.json\", \"r\")))\n    \n    for fold in folds:\n        model = define_model(\n            config.name,\n            drop_rate=config.drop_rate,\n            drop_path_rate=config.drop_path_rate,\n            use_gem=config.use_gem,\n            head_3d=config.head_3d if hasattr(config, \"head_3d\") else \"\",\n            n_frames=config.n_frames if hasattr(config, \"n_frames\") else \"\",\n            replace_pad_conv=config.replace_pad_conv if hasattr(config, \"replace_pad_conv\") else False,\n            num_classes=config.num_classes,\n            num_classes_aux=config.num_classes_aux,\n            n_channels=config.n_channels,\n            reduce_stride=config.reduce_stride,\n            increase_stride=config.increase_stride if hasattr(config, \"increase_stride\") else False,\n            pretrained=False\n        )\n        model = model.cuda().eval()\n    \n        weights = exp_folder + f\"{config.name}_{fold}.pt\"\n        model = load_model_weights(model, weights, verbose=config.local_rank == 0)\n        models_.append(model)\n        \n    models.append(models_)","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:17:20.39808Z","iopub.execute_input":"2024-10-03T13:17:20.398436Z","iopub.status.idle":"2024-10-03T13:17:23.517875Z","shell.execute_reply.started":"2024-10-03T13:17:20.398409Z","shell.execute_reply":"2024-10-03T13:17:23.516915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n\ndfs = []\nfor patient in tqdm(sorted(os.listdir(DATA_PATH))):\n#     if FOLD == 0:\n    if EVAL:\n        if int(patient) not in patients:\n            continue\n#     else:\n#     if patient != \"10082\":\n#         continue\n\n    for series in sorted(os.listdir(DATA_PATH + patient)):\n        print(\"-> Patient\", patient, '- Series', series)        \n#         continue\n\n#         imgs, paths, n_imgs = process(\n#             patient,\n#             series,\n#             data_path=DATA_PATH,\n#             on_gpu=True,\n#             crop_size=384,\n#             restrict=False\n#         )\n    \n#         plt.figure(figsize=(15, 8))\n#         plt.subplot(1, 2, 1)\n#         plt.imshow(imgs[:, :, 200].cpu().numpy(), cmap=\"gray\")\n\n        imgs, paths, n_imgs = process(\n            patient,\n            series,\n            data_path=DATA_PATH,\n            on_gpu=True,\n            crop_size=384,\n            restrict=RESTRICT\n        )\n        \n#         plt.subplot(1, 2, 2)\n#         plt.imshow(imgs[:, :, 200].cpu().numpy(), cmap=\"gray\")\n#         plt.show()\n    \n#         break\n\n        # Cls\n        df_series = pd.DataFrame({\"path\": paths})\n        df_series['patient_id'] = df_series['path'].apply(lambda x: x.split('_')[0])\n        df_series['patient'] = df_series['path'].apply(lambda x: x.split('_')[0])\n        df_series['series'] = df_series['path'].apply(lambda x: x.split('_')[1])\n        df_series['frame'] = df_series['path'].apply(lambda x: int(x.split('_')[2][:-4]))\n        dfs.append(df_series)\n\n        for models_list, (exp_folder, _, _) in zip(models, EXP_FOLDERS):\n            exp_name = \"_\".join(exp_folder.split('/')[-2:-1])\n#             if \"2023\" not in exp_name:  # locally\n#                 exp_name = \"_\".join(exp_folder.split('/')[-3:-1])\n            \n            config = Config(json.load(open(exp_folder + \"config.json\", \"r\")))\n            dataset = AbdominalInfDataset(\n                df_series,\n                frames_chanel=config.frames_chanel if hasattr(config, \"frames_chanel\") else 0,\n                n_frames=config.n_frames if hasattr(config, \"n_frames\") else 1,\n                stride=config.stride if hasattr(config, \"stride\") else 1,\n                imgs=imgs,\n                paths=paths,\n            )\n            if HALF:\n                dataset.info = dataset.info[::2]\n\n            preds = []\n            for model in models_list:\n                pred = predict(\n                    model,\n                    dataset,\n                    config.loss_config,\n                    batch_size=BATCH_SIZE,\n                    use_fp16=USE_FP16,\n                    num_workers=0,\n                    resize=config.resize if config.resize[0] != 384 else None\n                )\n                if HALF:\n                    pred = np.repeat(pred, 2, axis=0)[:len(df_series)]\n                preds.append(pred)\n\n                    \n            if RESTRICT:\n                pred_padded = np.zeros((n_imgs, pred.shape[-1]))\n                pred_padded[-len(pred):] = np.mean(preds, 0)\n            else:\n                pred_padded = np.mean(preds, 0)\n\n            np.save(SAVE_FOLDER + f\"{series}_{exp_name}.npy\", pred_padded)\n            \n#             if FOLD == 0 and not EVAL:\n#                 ref = np.load(exp_folder + \"pred_val_0.npy\")\n#                 ref = ref[:len(pred)]\n                \n#                 plt.plot(ref - np.mean(preds, 0))\n#                 plt.title(np.abs(ref - np.mean(preds, 0)).max())\n#                 plt.show()\n#             break\n#     break\n\ndf = pd.concat(dfs, ignore_index=True)\ndf = df.groupby(['patient', 'series']).max().reset_index()\n\ndel models, imgs, pred, dataset\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:17:23.519207Z","iopub.execute_input":"2024-10-03T13:17:23.519522Z","iopub.status.idle":"2024-10-03T13:41:31.764201Z","shell.execute_reply.started":"2024-10-03T13:17:23.519495Z","shell.execute_reply":"2024-10-03T13:41:31.763251Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Level 2","metadata":{}},{"cell_type":"code","source":"all_preds = []\n\nfor exp_folder in EXP_FOLDERS_2:\n    config_2 = Config(json.load(open(exp_folder + \"config.json\", \"r\")))\n#     config_2.exp_folders = [[f[:-3] + \"/\" if \"_r/\" in f else f, m] for f, m in config_2.exp_folders]\n    config_2.exp_folders = [[re.sub('_', '/', f[0]), f[1]] for f in config_2.exp_folders]\n\n    dataset = PatientFeatureInfDataset(\n        df['series'],\n        config_2.exp_folders,\n        crop_fts=None,\n        max_len=config_2.max_len,\n        restrict=config_2.restrict,\n        resize=config_2.resize,\n        save_folder=SAVE_FOLDER,\n        half=HALF,\n    )\n\n    model = define_model_2(\n        config_2.name,\n        ft_dim=config_2.ft_dim,\n        layer_dim=config_2.layer_dim,\n        n_layers=config_2.n_layers,\n        dense_dim=config_2.dense_dim,\n        p=config_2.p,\n#         use_msd=config_2.use_msd if hasattr(config, \"use_msd\") else False,\n        num_classes=config_2.num_classes,\n        num_classes_aux=config_2.num_classes_aux,\n        n_fts=config_2.n_fts,\n    )\n    model = model.eval().cuda()\n\n    for fold in FOLDS_2:\n        weights = exp_folder + f\"{config_2.name}_{fold}.pt\"\n        model = load_model_weights(model, weights, verbose=config.local_rank == 0)\n    \n        preds = predict_2(\n            model,\n            dataset,\n            config_2.loss_config,\n            batch_size=BATCH_SIZE_2,\n            use_fp16=USE_FP16,\n            num_workers=NUM_WORKERS,\n        )\n        all_preds.append(preds)\n        \n    del model  # , dataset\n    torch.cuda.empty_cache()\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:41:31.765517Z","iopub.execute_input":"2024-10-03T13:41:31.765783Z","iopub.status.idle":"2024-10-03T13:41:32.497395Z","shell.execute_reply.started":"2024-10-03T13:41:31.765761Z","shell.execute_reply":"2024-10-03T13:41:32.496467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = np.mean(all_preds, 0).astype(np.float64)\nfor i in range(preds.shape[1]):\n    df[f'pred_{i}'] = preds[:, i]\n\ndfg = df.drop(['series', 'path', 'frame', 'patient_id'], axis=1).groupby('patient').mean().reset_index()\nsub = to_sub_format(dfg)\n\nsub = sub[[\"patient_id\", \"bowel_healthy\", \"bowel_injury\", \"extravasation_healthy\", \"extravasation_injury\"]]\n\nsub.to_csv(\"submission.csv\", index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:41:32.498816Z","iopub.execute_input":"2024-10-03T13:41:32.499118Z","iopub.status.idle":"2024-10-03T13:41:32.534443Z","shell.execute_reply.started":"2024-10-03T13:41:32.499093Z","shell.execute_reply":"2024-10-03T13:41:32.533346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Debug","metadata":{}},{"cell_type":"code","source":"from training.main_lvl2 import retrieve_preds\nfrom data.preparation import prepare_data\nfrom sklearn.metrics import roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:41:32.53557Z","iopub.execute_input":"2024-10-03T13:41:32.535851Z","iopub.status.idle":"2024-10-03T13:41:32.540371Z","shell.execute_reply.started":"2024-10-03T13:41:32.535828Z","shell.execute_reply":"2024-10-03T13:41:32.539392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if EVAL:\n    df_, df_img_ = prepare_data(\"/kaggle/input/rsna-abdominal-prepro-data/\", with_seg=False)\n    folds_ = pd.read_csv(\"/kaggle/input/rsna-abdominal-prepro-data/folds_4.csv\")\n    df_ = df_.merge(folds_)\n    df_img_ = df_img_.merge(folds_)\n\n    df_oof, pred_oof = retrieve_preds(\n        df_, df_img_, config_2, \"/kaggle/input/rsna-weights-spleen-fix/2024-10-03_0/\", \n    )","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:41:32.543691Z","iopub.execute_input":"2024-10-03T13:41:32.543948Z","iopub.status.idle":"2024-10-03T13:41:35.693358Z","shell.execute_reply.started":"2024-10-03T13:41:32.543926Z","shell.execute_reply":"2024-10-03T13:41:35.692379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if EVAL:\n    sub[\"patient_id\"] = sub[\"patient_id\"].astype(int)\n    s = sub[[\"patient_id\", \"bowel_injury\"]].merge(df_oof[[\"patient_id\", \"bowel_injury\"]], how=\"left\", on=\"patient_id\", suffixes=('', '_gt'))\n\n    print(roc_auc_score(s[\"bowel_injury_gt\"], s[\"bowel_injury\"]))","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:41:35.694924Z","iopub.execute_input":"2024-10-03T13:41:35.695272Z","iopub.status.idle":"2024-10-03T13:41:35.71336Z","shell.execute_reply.started":"2024-10-03T13:41:35.695238Z","shell.execute_reply":"2024-10-03T13:41:35.71238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if EVAL:\n    df_ref = df_oof[df_oof['patient_id'].isin(patients)][[\"patient_id\", \"bowel_healthy\", \"bowel_injury\", \"pred_0\"]]\n    # roc_auc_score(df_oof[\"bowel_injury\"], df_oof[\"pred_0\"])\n    print(roc_auc_score(df_ref[\"bowel_injury\"], df_ref[\"pred_0\"]))","metadata":{"execution":{"iopub.status.busy":"2024-10-03T13:41:35.714664Z","iopub.execute_input":"2024-10-03T13:41:35.715027Z","iopub.status.idle":"2024-10-03T13:41:35.7266Z","shell.execute_reply.started":"2024-10-03T13:41:35.714994Z","shell.execute_reply":"2024-10-03T13:41:35.72557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Done ! ","metadata":{}}]}