{"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":"#### ","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-23T05:57:23.568847Z","iopub.execute_input":"2023-10-23T05:57:23.569092Z","iopub.status.idle":"2023-10-23T05:57:23.900079Z","shell.execute_reply.started":"2023-10-23T05:57:23.569069Z","shell.execute_reply":"2023-10-23T05:57:23.899354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 安装\n!pip install /kaggle/input/mm-whls/addict-2.4.0-py3-none-any.whl\n!pip install /kaggle/input/mm-whls/yapf-0.33.0-py2.py3-none-any.whl\n!pip install /kaggle/input/mm-whls/DCNv3-1.0-cp310-cp310-linux_x86_64.whl\n!pip install /kaggle/input/mm-whls/terminaltables-3.1.5-py2.py3-none-any.whl\n!pip install /kaggle/input/mm-whls/pycocotools-2.0.6-cp310-cp310-linux_x86_64.whl\n\n!pip install /kaggle/input/mm-whls/mmengine-0.7.3-py3-none-any.whl\n!pip install /kaggle/input/mm-whls/mmcv-2.0.0-cp310-cp310-linux_x86_64.whl\n!pip install /kaggle/input/mm-whls/mmsegmentation-1.0.0-py3-none-any.whl\n!pip install /kaggle/input/mm-whls/mmdet-3.1.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-10-23T05:57:23.901849Z","iopub.execute_input":"2023-10-23T05:57:23.902522Z","iopub.status.idle":"2023-10-23T06:02:12.202033Z","shell.execute_reply.started":"2023-10-23T05:57:23.902489Z","shell.execute_reply":"2023-10-23T06:02:12.200951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!rm -r /kaggle/working/mm_custom\n!cp -r /kaggle/input/ops-dcnv3 /kaggle/working/ops_dcnv3\n!cp -r /kaggle/input/mm-custom /kaggle/working/mm_custom\nimport mm_custom\nfrom mm_custom import *","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:02:12.203513Z","iopub.execute_input":"2023-10-23T06:02:12.20383Z","iopub.status.idle":"2023-10-23T06:02:14.299123Z","shell.execute_reply.started":"2023-10-23T06:02:12.203796Z","shell.execute_reply":"2023-10-23T06:02:14.298106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport numpy as np\n\nimport torch\nimport torch.nn.functional as F\n\n\nfrom multiprocessing import Pool\nfrom multiprocessing import cpu_count\nfrom tqdm import tqdm\nimport random\n\nfrom typing import Dict, List, Optional, Tuple, Union\n\nfrom mmengine.config import Config, DictAction\nfrom mmengine.runner import Runner\nfrom mmengine.dataset import BaseDataset","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:11:16.604856Z","iopub.execute_input":"2023-10-23T06:11:16.605274Z","iopub.status.idle":"2023-10-23T06:11:16.611184Z","shell.execute_reply.started":"2023-10-23T06:11:16.605223Z","shell.execute_reply":"2023-10-23T06:11:16.610291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root = '/kaggle/input/rsna-2023-abdominal-trauma-detection'\n\nbatch_size = 1\nnum_workers = 1","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:02:18.651058Z","iopub.execute_input":"2023-10-23T06:02:18.651493Z","iopub.status.idle":"2023-10-23T06:02:18.656084Z","shell.execute_reply.started":"2023-10-23T06:02:18.651466Z","shell.execute_reply":"2023-10-23T06:02:18.655267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv')\nsub_df","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:02:18.657746Z","iopub.execute_input":"2023-10-23T06:02:18.658014Z","iopub.status.idle":"2023-10-23T06:02:18.708773Z","shell.execute_reply.started":"2023-10-23T06:02:18.65799Z","shell.execute_reply":"2023-10-23T06:02:18.707903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_cfg(cfg_file, load_from):\n    \n    cfg = Config.fromfile(cfg_file)\n    cfg.launcher = 'pytorch'\n    cfg.load_from = load_from\n    \n    cfg.test_dataloader.dataset.root = root\n    cfg.test_dataloader.batch_size = batch_size\n    cfg.test_dataloader.num_workers = num_workers\n\n\n#     cfg.test_pipeline = [\n#         dict(type='mm_custom.LoadImageSub', in_channel=3, scale=cfg.scale),\n#         cfg.CropValidV2,\n#         dict(type='mm_custom.PackInputs')\n#     ]\n\n\n    cfg.test_cfg = dict(type='TestLoop')\n\n#     cfg.test_dataloader = dict(\n#         batch_size=batch_size,\n#         num_workers=num_workers,\n#         dataset=dict(\n#             type='mm_custom.DatasetSub',\n#             scale=cfg.scale,\n#             clip_num=cfg.clip_num,\n#             root=root,\n#             metainfo=cfg.metainfo,\n#             pipeline=cfg.test_pipeline,\n#             test_mode=True,\n#         ),\n#         collate_fn=dict(type='default_collate'),\n#         sampler=dict(type='DefaultSampler', shuffle=False, round_up=False),\n#     )\n\n    cfg.test_evaluator = dict(\n        type='mm_custom.Metric',\n        metainfo=cfg.metainfo,\n    )\n    \n    \n    return cfg    \n\n\ndef load_runner(cfg):\n    runner = Runner(\n        model=cfg.model,\n        work_dir='submit',\n        load_from=cfg.load_from,\n        test_dataloader=cfg.test_dataloader,\n        test_cfg=cfg.test_cfg,\n        test_evaluator=dict(\n            type='mmseg.IoUMetric',\n            iou_metrics=['mDice'],\n        )\n    )\n\n    \n    runner.load_or_resume()\n\n    runner.model = runner.model.cuda()\n    runner.model.eval()\n\n\n    return runner\ndef load_cfgs(cfg_files, load_froms):\n    cfgs = [load_cfg(cfg_file, load_from) for cfg_file, load_from in zip(cfg_files, load_froms)]\n    return cfgs\n\ndef load_runners(cfgs):\n    runners = [load_runner(cfg) for cfg in cfgs]\n    return runners","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:02:18.710136Z","iopub.execute_input":"2023-10-23T06:02:18.710518Z","iopub.status.idle":"2023-10-23T06:02:18.721318Z","shell.execute_reply.started":"2023-10-23T06:02:18.710485Z","shell.execute_reply":"2023-10-23T06:02:18.72048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg_files_list = [\n    \n    [\n        '/kaggle/input/checkpoints/atd_cfg_b_fold0_data_v2-0.0001-24x384x256-data_v2-fold_0/atd_cfg_b_fold0_data_v2.py',\n    ],\n    \n    [\n        '/kaggle/input/checkpoints/atd_cfg_b_fold1_data_v2-0.0001-32x384x256-data_v2-fold_1/atd_cfg_b_fold1_data_v2.py',\n    ],\n\n    [\n        '/kaggle/input/checkpoints/atd_cfg_b_fold2_data_v2-0.0001-48x384x256-data_v2-fold_2/atd_cfg_b_fold2_data_v2.py',\n    ],\n    \n]\n    \nload_froms_list = [\n    [\n        '/kaggle/input/checkpoints/atd_cfg_b_fold0_data_v2-0.0001-24x384x256-data_v2-fold_0/iter_1400.pth',\n    ],\n    \n    [\n        '/kaggle/input/checkpoints/atd_cfg_b_fold1_data_v2-0.0001-32x384x256-data_v2-fold_1/iter_1400.pth',\n    ],\n    \n    [\n        '/kaggle/input/checkpoints/atd_cfg_b_fold2_data_v2-0.0001-48x384x256-data_v2-fold_2/iter_1400.pth',\n    ],\n    \n]\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:02:18.72233Z","iopub.execute_input":"2023-10-23T06:02:18.722603Z","iopub.status.idle":"2023-10-23T06:02:18.737173Z","shell.execute_reply.started":"2023-10-23T06:02:18.72258Z","shell.execute_reply":"2023-10-23T06:02:18.736468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert len(cfg_files_list) == len(load_froms_list)\n\nall_model_num = 0\nfolds_num = len(cfg_files_list)\nall_pred_probs = []\n\nclass_names = ['bowel', 'extravasation', 'kidney', 'liver', 'spleen']\n\nfor fold in range(folds_num):\n    cfg_files = cfg_files_list[fold]\n    load_froms = load_froms_list[fold]\n\n    model_num = len(load_froms)\n    \n    all_model_num += model_num\n\n    assert len(cfg_files) == len(load_froms)\n\n    # load cfgs\n    cfgs = load_cfgs(cfg_files, load_froms)\n\n    # load runners\n    runners = load_runners(cfgs)\n\n    test_dataloader = runners[0].test_dataloader\n    N = len(test_dataloader.dataset)\n    batch_size = test_dataloader.batch_size\n    pred_probs = np.zeros(shape=(N, 13))\n    patient_ids, series_ids = [], []\n\n    for batch, data in enumerate(test_dataloader):\n        with torch.no_grad():\n            # runners\n            for i in range(model_num):\n\n                if i == 0:\n                    patient_ids.append(data['data_batch']['patient_id'].numpy())\n                    series_ids.append(data['data_batch']['series_id'].numpy())\n\n                results_batch = runners[i].model.test_step(data)\n\n                for ii, result in enumerate(results_batch):\n\n                    idx = batch * batch_size + ii\n\n                    pred_prob = []\n                    for name in class_names:\n                        pred_prob.append(result.pred_prob_dic[name])\n                    \n                    pred_prob = torch.cat(pred_prob)\n\n                    pred_probs[idx] += pred_prob.cpu().detach().numpy()\n    \n    patient_ids = np.concatenate(patient_ids).reshape(-1, 1).astype(int)\n    series_ids = np.concatenate(series_ids).reshape(-1, 1).astype(int)\n    \n    \n    all_pred_probs.append(pred_probs)\n    \n    # 释放显存\n    del runners\n    torch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:02:18.738202Z","iopub.execute_input":"2023-10-23T06:02:18.738487Z","iopub.status.idle":"2023-10-23T06:03:02.34787Z","shell.execute_reply.started":"2023-10-23T06:02:18.738456Z","shell.execute_reply":"2023-10-23T06:03:02.346637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nnew_pred_probs = None\n\n\nfor fold in range(folds_num):\n    if fold == 0:\n        new_pred_probs = all_pred_probs[fold].copy()\n    else:\n        new_pred_probs += all_pred_probs[fold].copy()\n\npred_probs = new_pred_probs / float(all_model_num)\n\ndata = np.concatenate([patient_ids, series_ids, pred_probs], axis=1) # [N, col_nums]\n\ncolumn_names = ['patient_id', 'series_id']\n\nbinary_class_names = ['bowel', 'extravasation']\ntriple_class_names = ['kidney', 'liver', 'spleen']\nany_injury_class_names = ['any_injury']\n\nbinary_injury_types = ['healthy', 'injury']\ntriple_injury_types = ['healthy', 'low', 'high']\n\nfor name in class_names:\n    if name in binary_class_names:\n        injury_types = binary_injury_types\n    elif name in triple_class_names:\n        injury_types = triple_injury_types\n    else:\n        raise NotImplementedError\n    \n    for injury_type in injury_types:\n        column_names.append(f'{name}_{injury_type}')\n\n\n\npred_df = pd.DataFrame(data=data, columns=column_names)\npred_df = pred_df.groupby(['patient_id'], as_index=False).mean()\ndel pred_df['series_id']","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:03:02.352645Z","iopub.execute_input":"2023-10-23T06:03:02.3534Z","iopub.status.idle":"2023-10-23T06:03:02.380656Z","shell.execute_reply.started":"2023-10-23T06:03:02.353368Z","shell.execute_reply":"2023-10-23T06:03:02.379955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for patient_id in pred_df.patient_id.values:\n    if patient_id in sub_df.patient_id.values:\n        sub_df.loc[sub_df.patient_id == patient_id] = pred_df[pred_df.patient_id == patient_id].values\n\nsub_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:03:02.381744Z","iopub.execute_input":"2023-10-23T06:03:02.382025Z","iopub.status.idle":"2023-10-23T06:03:02.409603Z","shell.execute_reply.started":"2023-10-23T06:03:02.382002Z","shell.execute_reply":"2023-10-23T06:03:02.408782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:03:02.410945Z","iopub.execute_input":"2023-10-23T06:03:02.411321Z","iopub.status.idle":"2023-10-23T06:03:02.429496Z","shell.execute_reply.started":"2023-10-23T06:03:02.411279Z","shell.execute_reply":"2023-10-23T06:03:02.428639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(all_model_num)\nprint(folds_num)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T06:03:02.430853Z","iopub.execute_input":"2023-10-23T06:03:02.431207Z","iopub.status.idle":"2023-10-23T06:03:02.441241Z","shell.execute_reply.started":"2023-10-23T06:03:02.431176Z","shell.execute_reply":"2023-10-23T06:03:02.440411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}