{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2023-10-13T09:49:13.159664Z","iopub.execute_input":"2023-10-13T09:49:13.160295Z","iopub.status.idle":"2023-10-13T09:50:22.433172Z","shell.execute_reply.started":"2023-10-13T09:49:13.160268Z","shell.execute_reply":"2023-10-13T09:50:22.431721Z"},"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":"2023-10-13T09:50:22.435651Z","iopub.execute_input":"2023-10-13T09:50:22.436041Z","iopub.status.idle":"2023-10-13T09:51:15.399883Z","shell.execute_reply.started":"2023-10-13T09:50:22.436003Z","shell.execute_reply":"2023-10-13T09:51:15.398684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\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":"2023-10-13T09:51:15.40153Z","iopub.execute_input":"2023-10-13T09:51:15.401905Z","iopub.status.idle":"2023-10-13T09:51:18.751579Z","shell.execute_reply.started":"2023-10-13T09:51:15.401863Z","shell.execute_reply":"2023-10-13T09:51:18.750525Z"},"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\nfrom 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":"2023-10-13T09:51:18.75424Z","iopub.execute_input":"2023-10-13T09:51:18.755279Z","iopub.status.idle":"2023-10-13T09:51:32.030498Z","shell.execute_reply.started":"2023-10-13T09:51:18.755244Z","shell.execute_reply":"2023-10-13T09:51:32.029309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Params","metadata":{}},{"cell_type":"code","source":"EVAL = False","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:51:32.032132Z","iopub.execute_input":"2023-10-13T09:51:32.347662Z","iopub.status.idle":"2023-10-13T09:51:32.35335Z","shell.execute_reply.started":"2023-10-13T09:51:32.347607Z","shell.execute_reply":"2023-10-13T09:51:32.351722Z"},"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\n# os.makedirs(SAVE_FOLDER, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:51:32.355004Z","iopub.execute_input":"2023-10-13T09:51:32.355448Z","iopub.status.idle":"2023-10-13T09:51:32.367276Z","shell.execute_reply.started":"2023-10-13T09:51:32.355408Z","shell.execute_reply":"2023-10-13T09:51:32.366366Z"},"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 [\n        10082,\n        10004, 10005, 10007,\n#         2898,\n    ]:\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-abdomen-weights-1/2023-10-02_60/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]","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:51:32.368498Z","iopub.execute_input":"2023-10-13T09:51:32.369414Z","iopub.status.idle":"2023-10-13T09:51:52.126881Z","shell.execute_reply.started":"2023-10-13T09:51:32.369383Z","shell.execute_reply":"2023-10-13T09:51:52.12576Z"},"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 = True\nHALF = True","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:51:52.128145Z","iopub.execute_input":"2023-10-13T09:51:52.128461Z","iopub.status.idle":"2023-10-13T09:51:52.154419Z","shell.execute_reply.started":"2023-10-13T09:51:52.128434Z","shell.execute_reply":"2023-10-13T09:51:52.149409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # FOLD = 2\n# FOLD = \"fullfit_0\"\n# HALF = True\n\n# EXP_FOLDERS = [\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-09-20_14/\", \"seg\", [FOLD]),\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-09-20_36_r/\", \"probas_2d\", [FOLD]),  # 0.357 - convnextv2_tiny   <-\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-10_25/\", \"probas_2d\", [FOLD]),    # 0.352 - maxvit_small_tf_384   <-\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-10_27/\", \"probas_2d\", [FOLD]),    # 0.346  - maxvit_tiny_tf_512   <-\n# ]\n\n# CROP_EXP_FOLDERS = [\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-02_11/\", \"crop\", [FOLD]),   # coat_lite_medium  -> 0.326   +0.002\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-02_36/\", \"crop\", [FOLD]),   # coat_lite_medium_384 -> 0.326  +0.001\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-02_41/\", \"crop\", [FOLD]),   # coatnet_1_rw_224 -> 0.325  +0.001\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-05_6/\", \"crop\", [FOLD]),    # coatnet_1_rw_224   -> 0.322\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-05_20/\", \"crop\", [FOLD]),   # coatnet_rmlp_1_rw2_224 -> 0.325  +0.001\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-05_21/\", \"crop\", [FOLD]),   # coat_lite_medium_384 -> 0.325  +0.001\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-05_31/\", \"crop\", [FOLD]),   # coatnet_1_rw_224   -> 0.320\n#     (\"/kaggle/input/rsna-abdomen-weights-2/2023-10-06_31/\", \"crop\", [FOLD]),   # coatnet_rmlp_1_rw2_224 -> 0.325\n# ]\n\n# EXP_FOLDERS_2 = [\n#     \"/kaggle/input/rsna-abdomen-weights-1/2023-10-10_45/\",  # 0.3111\n#     \"/kaggle/input/rsna-abdomen-weights-1/2023-10-10_42/\",\n#     \"/kaggle/input/rsna-abdomen-weights-1/2023-10-10_46/\",\n# ]\n# EXP_FOLDER_3D = \"/kaggle/input/rsna-abdomen-weights-2/2023-09-24_20/\"\n\n# FOLDS_2 = [0, 1, 2, 3]\n# if \"fullfit\" not in str(FOLD):\n#     FOLDS_2 = [FOLD]\n\n# for f in EXP_FOLDERS_2:\n#     folders = Config(json.load(open(f + \"config.json\", 'r'))).exp_folders\n#     print(f)\n#     print([f[0] for f in folders if \"probas\" in f[1]])\n#     print([f[0] for f in folders if \"crop\" in f[1]])\n#     print()","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:51:52.157032Z","iopub.execute_input":"2023-10-13T09:51:52.157399Z","iopub.status.idle":"2023-10-13T09:51:53.689976Z","shell.execute_reply.started":"2023-10-13T09:51:52.157367Z","shell.execute_reply":"2023-10-13T09:51:53.688823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# FOLD = 2\nFOLD = \"fullfit_0\"\nHALF = True\n\nEXP_FOLDERS = [\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-09-20_14/\", \"seg\", [FOLD]),\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-10-05_13/\", \"probas_2d\", [FOLD]),  # 0.353  - maxvit_tiny_tf_384   <-\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-10-06_38/\", \"probas_2d\", [FOLD]),  # 0.361  - convnextv2_tiny\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-10-10_27/\", \"probas_2d\", [FOLD]),  # 0.346  - maxvit_tiny_tf_512   <-\n]\n\nCROP_EXP_FOLDERS = [\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-10-05_6/\", \"crop\", [FOLD]),   # coatnet_1_rw_224 -1 7        -> 0.322 +0.0008\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-10-05_31/\", \"crop\", [FOLD]),  # coatnet_1_rw_224 -1 11       -> 0.320  +0.0016\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-10-06_31/\", \"crop\", [FOLD]),  # coatnet_rmlp_1_rw2_224 -1 11 -> 0.325  +0.0021\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-10-12_1/\", \"crop\", [FOLD]),   # coatnet_1_rw_224 -1 11 transfo -> -0.0001  - SWAP FOR  05/21 - 0.3109\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-10-12_8/\", \"crop\", [FOLD]),   # coat_lite_medium transfo -1 11 lstm att -> -0.0001    - SWAP FOR  05/21 0.3110\n    (\"/kaggle/input/rsna-abdomen-weights-3/2023-10-12_5/\", \"crop\", [FOLD]),   # coat_lite_medium_384\n]\n\nEXP_FOLDERS_2 = [\n    \"/kaggle/input/rsna-abdomen-weights-3/2023-10-13_0/\",  # 0.3154\n    \"/kaggle/input/rsna-abdomen-weights-3/2023-10-13_1/\",  # 0.3130\n]\nEXP_FOLDER_3D = \"/kaggle/input/rsna-abdomen-weights-2/2023-09-24_20/\"\n\nFOLDS_2 = [0, 1, 2, 3]\nif \"fullfit\" not in str(FOLD):\n    FOLDS_2 = [FOLD]\n\nfor f in EXP_FOLDERS_2:\n    folders = Config(json.load(open(f + \"config.json\", 'r'))).exp_folders\n    print(f)\n    print([f[0] for f in folders if \"probas\" in f[1]])\n    print([f[0] for f in folders if \"crop\" in f[1]])\n    print()","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:51:53.693532Z","iopub.execute_input":"2023-10-13T09:51:53.694007Z","iopub.status.idle":"2023-10-13T09:51:53.740562Z","shell.execute_reply.started":"2023-10-13T09:51:53.69398Z","shell.execute_reply":"2023-10-13T09:51:53.739501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Level 1","metadata":{}},{"cell_type":"code","source":"class Config3d:\n    size = 256\n    plot = (FOLD == 0) and (not EVAL)\n    margin = 5\n\nconfig = Config(json.load(open(EXP_FOLDER_3D + \"config.json\", \"r\")))\n\nmodel_seg = define_model_seg(\n    config.decoder_name,\n    config.name,\n    num_classes=config.num_classes,\n    num_classes_aux=config.num_classes_aux,\n    n_channels=config.n_channels,\n    increase_stride=config.increase_stride,\n    pretrained=False,\n)\n\nmodel_seg = convert_3d(model_seg)\nmodel_seg = load_model_weights(model_seg, EXP_FOLDER_3D + f\"{config.name}_{FOLD}.pt\")\nmodel_seg = model_seg.eval().cuda()","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:51:53.742145Z","iopub.execute_input":"2023-10-13T09:51:53.742831Z","iopub.status.idle":"2023-10-13T09:52:01.721154Z","shell.execute_reply.started":"2023-10-13T09:51:53.742796Z","shell.execute_reply":"2023-10-13T09:52:01.719763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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    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,\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    for fold in folds:\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":"2023-10-13T09:52:01.722831Z","iopub.execute_input":"2023-10-13T09:52:01.723412Z","iopub.status.idle":"2023-10-13T09:52:07.431853Z","shell.execute_reply.started":"2023-10-13T09:52:01.723379Z","shell.execute_reply":"2023-10-13T09:52:07.430892Z"},"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=RESTRICT\n        )\n\n        # Seg & Crop\n        with torch.cuda.amp.autocast(enabled=True):\n            x = F.interpolate(imgs.unsqueeze(0).unsqueeze(0), size=(Config3d.size, Config3d.size, Config3d.size), mode=\"nearest\")\n            pred = model_seg(x)[0].argmax(1, keepdims=True).float()\n            pred = F.interpolate(pred, size=(len(imgs), 384, 384), mode=\"nearest\")\n            \n        seg = pred[0][0]\n        coords = get_crops(seg)\n\n        for (x0, x1, y0, y1, z0, z1), name in zip(coords, ['liver', 'spleen', 'kidney']):\n            x0, x1 = max(0, x0 - Config3d.margin), min(imgs.shape[0], x1 + Config3d.margin)\n            y0, y1 = max(0, y0 - Config3d.margin), min(imgs.shape[1], y1 + Config3d.margin)\n            z0, z1 = max(0, z0 - Config3d.margin), min(imgs.shape[2], z1 + Config3d.margin)\n\n            img_crop = (imgs[x0: x1, y0:y1, z0:z1].cpu().numpy() * 255).astype(np.uint8)\n            np.save(SAVE_FOLDER + f'{patient}_{series}_{name}.npy', img_crop.copy())\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 RESTRICT:\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\n#             break\n#     break\n\ndf = pd.concat(dfs, ignore_index=True)\ndf = df.groupby(['patient', 'series']).max().reset_index()\n\ndel model_seg, models, imgs, x, pred, seg, dataset\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:52:07.433462Z","iopub.execute_input":"2023-10-13T09:52:07.433882Z","iopub.status.idle":"2023-10-13T09:53:03.14612Z","shell.execute_reply.started":"2023-10-13T09:52:07.433851Z","shell.execute_reply":"2023-10-13T09:53:03.145227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Crop Model","metadata":{}},{"cell_type":"code","source":"df_series = pd.DataFrame({\"img_path\": sorted(glob.glob(SAVE_FOLDER + f'*.npy'))})\ndf_series['patient_id'] = df_series['img_path'].apply(lambda x: x.split('/')[-1].split('_')[0])\ndf_series['series'] = df_series['img_path'].apply(lambda x: x.split('_')[-2])\ndf_series['organ'] = df_series['img_path'].apply(lambda x: x.split('_')[-1][:-4])\n\ndf_series['target'] = 0\ndf_series = df_series[df_series['organ'].isin(['kidney', 'liver', 'spleen'])].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:53:03.147648Z","iopub.execute_input":"2023-10-13T09:53:03.148288Z","iopub.status.idle":"2023-10-13T09:53:03.160528Z","shell.execute_reply.started":"2023-10-13T09:53:03.148254Z","shell.execute_reply":"2023-10-13T09:53:03.159517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crop_fts = []\nfor exp_folder, mode, folds in tqdm(CROP_EXP_FOLDERS):\n    \n    config = Config(json.load(open(exp_folder + \"config.json\", \"r\")))\n\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,\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    preds = []\n    for fold in folds:\n        weights = exp_folder + f\"{config.name}_{fold}.pt\"\n        model = load_model_weights(model, weights, verbose=config.local_rank == 0)\n        \n        transfos = get_transfos(\n            augment=False, resize=config.resize, crop=config.crop\n        )\n\n        dataset = AbdominalCropDataset(\n            None,\n            None,\n            transforms=transfos,\n            frames_chanel=config.frames_chanel,\n            n_frames=config.n_frames,\n            stride=config.stride,\n            use_mask=config.use_mask,\n            train=False,\n            df_series=df_series\n        )\n\n        pred = predict(\n            model,\n            dataset,\n            config.loss_config,\n            batch_size=BATCH_SIZE,\n            use_fp16=USE_FP16,\n            num_workers=NUM_WORKERS,\n        )\n        preds.append(pred)\n\n    preds = np.mean(preds, 0)\n    crop_fts.append(preds)\n    \n    del model, dataset\n    torch.cuda.empty_cache()\n    gc.collect()\n    \ncrop_fts = np.array(crop_fts) # n_models x 3*n_studies x n_classes\nnp.save(SAVE_FOLDER + \"crop_fts.npy\", crop_fts)","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:53:03.161999Z","iopub.execute_input":"2023-10-13T09:53:03.162684Z","iopub.status.idle":"2023-10-13T09:53:29.573248Z","shell.execute_reply.started":"2023-10-13T09:53:03.162651Z","shell.execute_reply":"2023-10-13T09:53:29.572188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crop_fts = np.load(SAVE_FOLDER + \"crop_fts.npy\")\n\ncrop_fts = crop_fts.reshape(crop_fts.shape[0], crop_fts.shape[1] // 3, 3, crop_fts.shape[2])  # n_models x n_studies x n_organs x 3\ncrop_fts = crop_fts.transpose(1, 2, 0, 3)  # n_studies x n_organs x n_models x 3\ncrop_fts = crop_fts.reshape(crop_fts.shape[0], crop_fts.shape[1], crop_fts.shape[2] * crop_fts.shape[3])  # n_studies x n_organs x 3 * n_models","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:53:29.574954Z","iopub.execute_input":"2023-10-13T09:53:29.575536Z","iopub.status.idle":"2023-10-13T09:53:29.584066Z","shell.execute_reply.started":"2023-10-13T09:53:29.5755Z","shell.execute_reply":"2023-10-13T09:53:29.583095Z"},"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\n    dataset = PatientFeatureInfDataset(\n        df['series'],\n        config_2.exp_folders,\n        crop_fts=crop_fts,\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,\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":"2023-10-13T09:53:29.585477Z","iopub.execute_input":"2023-10-13T09:53:29.585841Z","iopub.status.idle":"2023-10-13T09:53:31.247209Z","shell.execute_reply.started":"2023-10-13T09:53:29.58581Z","shell.execute_reply":"2023-10-13T09:53:31.246145Z"},"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.to_csv(\"submission.csv\", index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-13T09:53:31.248897Z","iopub.execute_input":"2023-10-13T09:53:31.249262Z","iopub.status.idle":"2023-10-13T09:53:31.29278Z","shell.execute_reply.started":"2023-10-13T09:53:31.249223Z","shell.execute_reply":"2023-10-13T09:53:31.291876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Done ! ","metadata":{}}]}