{"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":"## 3rd Place Solution Inference","metadata":{}},{"cell_type":"markdown","source":"![](https://github.com/darraghdog/RSNA22/raw/main/figs/study.gif)","metadata":{"execution":{"iopub.status.busy":"2022-10-31T08:31:04.742634Z","iopub.execute_input":"2022-10-31T08:31:04.743701Z","iopub.status.idle":"2022-10-31T08:31:05.909636Z","shell.execute_reply.started":"2022-10-31T08:31:04.743652Z","shell.execute_reply":"2022-10-31T08:31:05.908284Z"}}},{"cell_type":"code","source":"!pip install -r ../input/github-rsna22/requirements.txt --no-index --find-links=file:///kaggle/input/rsna2022-pip-wheels/ -q","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-26T17:18:11.566276Z","iopub.execute_input":"2023-02-26T17:18:11.566613Z","iopub.status.idle":"2023-02-26T17:18:38.554159Z","shell.execute_reply.started":"2023-02-26T17:18:11.56654Z","shell.execute_reply":"2023-02-26T17:18:38.553027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/rsna-models-utoronto/Spine22-main/Spine22-main/* ./","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:18:38.556827Z","iopub.execute_input":"2023-02-26T17:18:38.557572Z","iopub.status.idle":"2023-02-26T17:18:39.671703Z","shell.execute_reply.started":"2023-02-26T17:18:38.557527Z","shell.execute_reply":"2023-02-26T17:18:39.670009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport scipy as sp\nimport os\nimport json\nimport sys\nimport importlib\nimport multiprocessing as mp\nimport gc\nfrom tqdm.auto import tqdm\nimport glob\nimport torch\nfrom copy import copy\nfrom torch.cuda.amp import GradScaler, autocast\nfrom torch.utils.data import DataLoader\nimport timm\nimport pylibjpeg\nimport pydicom\nfrom PIL import Image\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:18:39.673849Z","iopub.execute_input":"2023-02-26T17:18:39.674228Z","iopub.status.idle":"2023-02-26T17:18:42.748477Z","shell.execute_reply.started":"2023-02-26T17:18:39.674188Z","shell.execute_reply":"2023-02-26T17:18:42.747353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.backends.cudnn.benchmark = True\n\nsys.path.append('./configs')\nsys.path.append('./data')\nsys.path.append('./models')\nsys.path.append('./postprocess')","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:18:42.751609Z","iopub.execute_input":"2023-02-26T17:18:42.752137Z","iopub.status.idle":"2023-02-26T17:18:42.758218Z","shell.execute_reply.started":"2023-02-26T17:18:42.752108Z","shell.execute_reply":"2023-02-26T17:18:42.7572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:21:46.069084Z","iopub.execute_input":"2023-02-26T17:21:46.069448Z","iopub.status.idle":"2023-02-26T17:21:46.085636Z","shell.execute_reply.started":"2023-02-26T17:21:46.069419Z","shell.execute_reply":"2023-02-26T17:21:46.084559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"COMP_FOLDER = '../input/rsna-2022-cervical-spine-fracture-detection/'\nDATA_FOLDER = COMP_FOLDER + 'test_images/'\nMETA_DF = COMP_FOLDER + 'test.csv'\n\ntrain_df = pd.read_csv(COMP_FOLDER + 'train.csv')\ntest_df = pd.read_csv(COMP_FOLDER + 'test.csv')\nsample_submission = pd.read_csv(COMP_FOLDER + 'sample_submission.csv')\n\nkeycols = 'StudyInstanceUID slice_number'.split()\ndf = pd.DataFrame([i.replace('.dcm', '').split('/')[-2:] \n                       for i in glob.glob(DATA_FOLDER+'/*/*.dcm')], columns = keycols)\ndf['slice_number'] = df['slice_number'].astype(int)\ndf = df.sort_values(keycols).reset_index(drop = True)\n\nPUBLIC_RUN = len(test_df) == 3\nN_CORES = mp.cpu_count()\nMIXED_PRECISION = False\nPIN_MEMORY = True\nDL_PREFETCH_FACTOR = 1\nNUM_SLICES_PER_UID_BBOX = 600\n\nRAM_CHECK = False\nOOF_CHECK = False\nTORONOTO_CHECK = True\nFOLD = 3\nMAX_FOLDS = 99\n\nassert (RAM_CHECK + OOF_CHECK) <= 1\n\nDEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'\n\nif PUBLIC_RUN is False:\n    RAM_CHECK = False\n    OOF_CHECK = False\n    \nif TORONOTO_CHECK is True:\n    test_df = pd.read_csv('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train.csv')\n    utdf = pd.read_csv('/kaggle/input/rsna-models-utoronto/RSNA_Kaggle_Experiment.csv')\n    test_df = test_df.loc[~test_df['StudyInstanceUID'].isin(utdf.StudyInstanceUID.unique())].reset_index(drop = True)\n    \n    # test_df = test_df[   test_df.StudyInstanceUID.isin(test_df.StudyInstanceUID.unique()[:20])   ]\n                                                            \n    print(f'Number of studies : {len(test_df)}')\n    # Filter UT samples\n    \n    DATA_FOLDER = DATA_FOLDER.replace('test','train')\n    META_DF = META_DF.replace('test','train')\n    print(test_df.head())\n    \n    keycols = 'StudyInstanceUID slice_number'.split()\n    #dicom_names = glob.glob(DATA_FOLDER+'/*/*.dcm')\n    dicom_names = os.popen(f'find {DATA_FOLDER} -wholename \"*/*.dcm\"').read().split()\n    df = pd.DataFrame([i.replace('.dcm', '').split('/')[-2:]  for i in tqdm(dicom_names, total = len(dicom_names)) ], columns = keycols)\n    df['slice_number'] = df['slice_number'].astype(int)\n    df = df.sort_values(keycols).reset_index(drop = True)\n    \n    df = df[df.StudyInstanceUID.isin(test_df.StudyInstanceUID)].reset_index(drop = True)\n    \nif RAM_CHECK is True:\n    test_df = train_df[:250].reset_index(drop=True)  # Number to be checked\n    DATA_FOLDER = DATA_FOLDER.replace('test','train')\n    META_DF = META_DF.replace('test','train')\n    print(test_df.head())\n    \n    keycols = 'StudyInstanceUID slice_number'.split()\n    df = pd.DataFrame([i.replace('.dcm', '').split('/')[-2:] \n                       for i in glob.glob(DATA_FOLDER+'/*/*.dcm')], columns = keycols)\n    df['slice_number'] = df['slice_number'].astype(int)\n    df = df.sort_values(keycols).reset_index(drop = True)\n    \n    df = df[df.StudyInstanceUID.isin(test_df.StudyInstanceUID)].reset_index(drop = True)\n    \nif OOF_CHECK is True:\n    test_df = pd.read_csv('../input/rsna2022-train-dataset/train_folded_v01.csv').query(f'fold == {FOLD}').reset_index(drop = True)\n    DATA_FOLDER = DATA_FOLDER.replace('test','train')\n    META_DF = META_DF.replace('test','train')\n    print(test_df.head())\n    \n    keycols = 'StudyInstanceUID slice_number'.split()\n    df = pd.DataFrame([i.replace('.dcm', '').split('/')[-2:] \n                       for i in glob.glob(DATA_FOLDER+'/*/*.dcm')], columns = keycols)\n    df['slice_number'] = df['slice_number'].astype(int)\n    df = df.sort_values(keycols).reset_index(drop = True)\n    \n    df = df[df.StudyInstanceUID.isin(test_df.StudyInstanceUID)].reset_index(drop = True)\n\ndf['fold'] = -1\nprint(f'Shape of test set : {df.shape}')\ngc.collect()\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:24:15.951661Z","iopub.execute_input":"2023-02-26T17:24:15.952155Z","iopub.status.idle":"2023-02-26T17:24:22.864773Z","shell.execute_reply.started":"2023-02-26T17:24:15.952116Z","shell.execute_reply":"2023-02-26T17:24:22.863725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n# change it to nn.BCELoss(reduction='none') if you have sigmoid activation in last layer\nloss_fn = torch.nn.BCELoss(reduction=\"none\") \ncompetition_weights = {\n    '-' : torch.tensor([7, 1, 1, 1, 1, 1, 1, 1], dtype=torch.float, device=device),\n    '+' : torch.tensor([14, 2, 2, 2, 2, 2, 2, 2], dtype=torch.float, device=device),\n}","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:24:26.415982Z","iopub.execute_input":"2023-02-26T17:24:26.416557Z","iopub.status.idle":"2023-02-26T17:24:29.009025Z","shell.execute_reply.started":"2023-02-26T17:24:26.416515Z","shell.execute_reply":"2023-02-26T17:24:29.008011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_cfg(CFG):\n    cfg = importlib.import_module('default_config')\n    importlib.reload(cfg)\n    cfg = importlib.import_module(CFG)\n    importlib.reload(cfg)\n    cfg = copy(cfg.cfg)\n    cfg.post_process_pipeline = importlib.import_module(cfg.post_process_pipeline).post_process_pipeline\n\n    cfg.data_dir = COMP_FOLDER\n    cfg.test_data_folder = DATA_FOLDER\n    cfg.mixed_precision = MIXED_PRECISION\n    cfg.pretrained = False\n    cfg.pretrained_weights = False\n    cfg.batch_size = cfg.batch_size\n    cfg.offline_inference = True\n\n    print(CFG, cfg.model, cfg.dataset, cfg.backbone, cfg.pretrained_weights, cfg.post_process_pipeline)\n    \n    return cfg\n\ndef get_dl(cfg):\n    ds = importlib.import_module(cfg.dataset)\n    importlib.reload(ds)\n\n    CustomDataset = ds.CustomDataset\n    batch_to_device = ds.batch_to_device\n\n    test_ds = CustomDataset(df, cfg, cfg.val_aug, mode=\"test\")\n    test_dl = DataLoader(test_ds, shuffle=False, batch_size=cfg.batch_size, collate_fn=ds.val_collate_fn, num_workers=N_CORES, pin_memory=PIN_MEMORY, prefetch_factor = DL_PREFETCH_FACTOR)\n\n    return test_dl, batch_to_device\n\ndef get_bb_dl(cfg):\n    ds = importlib.import_module(cfg.dataset)\n    importlib.reload(ds)\n\n    CustomDataset = ds.CustomDataset\n    batch_to_device = ds.batch_to_device\n\n    test_ds = CustomDataset(bbdf, cfg, cfg.val_aug, mode=\"test\")\n    test_dl = DataLoader(test_ds, shuffle=False, batch_size=cfg.batch_size, collate_fn=ds.val_collate_fn, num_workers=N_CORES, pin_memory=PIN_MEMORY, prefetch_factor = DL_PREFETCH_FACTOR)\n\n    return test_dl, batch_to_device\n\ndef get_state_dict(sd_fp):\n    sd = torch.load(sd_fp, map_location=\"cpu\")\n    sd = {k.replace(\"module.\", \"\"):v for k,v in sd.items()}\n    return sd\n\ndef get_nets(cfg,state_dicts,test_ds):\n    model = importlib.import_module(cfg.model)\n    importlib.reload(model)\n    Net = model.Net\n\n    nets = []\n\n    for i,state_dict in enumerate(state_dicts):\n        net = Net(cfg).eval().to(DEVICE)\n        print(\"loading dict\")\n        sd = get_state_dict(state_dict)\n        if \"model\" in sd.keys():\n            sd = sd[\"model\"]\n        net.load_state_dict(sd, strict=True)\n        nets += [net.half()]\n        del sd\n        gc.collect()\n    return nets\n\ndef competiton_loss_row_norm(y_hat, y):\n    loss = loss_fn(y_hat, y)\n    weights = y * competition_weights['+'] + (1 - y) * competition_weights['-']\n    loss = (loss * weights).sum(axis=1)\n    w_sum = weights.sum(axis=1)\n    loss = torch.div(loss, w_sum)\n    return loss.mean().item()\n\ndef window_range(g, thresh = 0.1, window = 5, min_periods=3, center=True):\n    bbseqprobas = g.has_bbox.rolling(window, min_periods=min_periods, center=center).mean()\n    if bbseqprobas.max() <= thresh:\n        sn_from, sn_to = g.slice_numbers.iloc[[0,-1]]\n        return [sn_from, sn_to]\n    bb_range = np.where(bbseqprobas > thresh)[0]\n    sn_from = g.slice_numbers.iloc[bb_range[0]]\n    sn_to = g.slice_numbers.iloc[bb_range[-1]]\n    return [sn_from, sn_to]","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:24:30.60214Z","iopub.execute_input":"2023-02-26T17:24:30.602502Z","iopub.status.idle":"2023-02-26T17:24:30.622362Z","shell.execute_reply.started":"2023-02-26T17:24:30.602462Z","shell.execute_reply":"2023-02-26T17:24:30.62074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bbdf = df\nbbdf['count'] = bbdf.groupby('StudyInstanceUID')['fold'].transform('count').values\nbbdf['cumcount'] = 1+ bbdf.groupby('StudyInstanceUID')['fold'].transform('cumcount').values\nbbdf['key'] = (bbdf['cumcount']  / (bbdf['count'] / NUM_SLICES_PER_UID_BBOX)).round().astype(int)\nbbdf = bbdf.drop_duplicates('StudyInstanceUID key'.split()).drop('count cumcount key'.split(), 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:24:33.74918Z","iopub.execute_input":"2023-02-26T17:24:33.749539Z","iopub.status.idle":"2023-02-26T17:24:33.766074Z","shell.execute_reply.started":"2023-02-26T17:24:33.74951Z","shell.execute_reply":"2023-02-26T17:24:33.764984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name = 'cfg_loc_dh_01B'\n\ncfg = get_cfg(name)\ncfg.meta_df = META_DF\ncfg.data_folder = DATA_FOLDER\ncfg.verify_sample = False\ncfg.load_jpg = False\ncfg.batch_size = 8\ncfg.drop_scans = []\ntest_dl, batch_to_device = get_bb_dl(cfg)\n\n\nstate_dict_fps = sorted(glob.glob('../input/rsna-models-utoronto/weights_loc/weights1/cfg_loc_dh_01B/fold-1/check*'))[:MAX_FOLDS]\nprint('\\n'.join(state_dict_fps))\nif OOF_CHECK:\n    nets = get_nets(cfg,state_dict_fps[:1], test_dl.dataset)\nelse:\n    nets = get_nets(cfg,state_dict_fps, test_dl.dataset)","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:25:09.751385Z","iopub.execute_input":"2023-02-26T17:25:09.752037Z","iopub.status.idle":"2023-02-26T17:25:19.820782Z","shell.execute_reply.started":"2023-02-26T17:25:09.751983Z","shell.execute_reply":"2023-02-26T17:25:19.819733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data = {}\npreds = []\nuids = []\nslices = []\n#ids = []\nwith torch.inference_mode():\n    for tt, batch in tqdm(enumerate(test_dl), total = len(test_dl)):\n        batch = batch_to_device(batch,DEVICE)\n        batch['image'] = batch['image'].half()\n        outs = [net(batch) for net in nets]\n        preds += [torch.stack([out['preds'] for out in outs], dim=0).cpu().mean(0)]\n        uids +=  [outs[0]['StudyUID'].cpu()]\n        slices += [outs[0]['slice_numbers'].cpu()]\n        if tt % 5 == 0:\n            gc.collect()\n            torch.cuda.empty_cache()\n        #ids += batch['StudyUID'].cpu()\npreds = torch.cat(preds, dim=0)\nuids = torch.cat(uids, dim=0)\nslices = torch.cat(slices, dim=0)\npredbbdf = pd.DataFrame(preds.float().clip(0, 1).numpy(), columns = 'x0 y0 x1 y1 has_bbox'.split())\npredbbdf['uid'] = uids.numpy()\npredbbdf['slice_numbers'] = slices.numpy()[:,1]\npredbbdf['StudyInstanceUID'] = test_dl.dataset.df.loc[test_dl.dataset.ids]['StudyInstanceUID'].values\n\n# Hack for studies with no bounding box > 0.5.... - just put in a dummy box and let it go thru the pipeline\npredbbdf['max_has_bbox'] = predbbdf.groupby('StudyInstanceUID')['has_bbox'].transform(max)\npredbbdf.loc[predbbdf.max_has_bbox<0.5, 'x0 y0'.split()] = 0.2\npredbbdf.loc[predbbdf.max_has_bbox<0.5, 'x1 y1'.split()] = 0.8\npredbbdf.loc[predbbdf.max_has_bbox<0.5, 'has_bbox'.split()] = 0.51\npredbbdf = predbbdf.drop('max_has_bbox', 1)\n\nbbpreddf = pd.concat([ \\\n    predbbdf.query('has_bbox > 0.5').groupby('StudyInstanceUID')['x0 y0'.split()].apply(min),\n    predbbdf.query('has_bbox > 0.5').groupby('StudyInstanceUID')['x1 y1'.split()].apply(max)], 1)\n\nwrls = predbbdf.groupby('StudyInstanceUID').apply(window_range)\nwrdf = pd.DataFrame(wrls.tolist(), index = wrls.index, columns = 'slnum_from slnum_to'.split())\nbbpreddf['slnum_from slnum_to'.split()] = wrdf.loc[bbpreddf.index]\nbbpreddf['slnum_max'] = predbbdf.groupby('StudyInstanceUID')['slice_numbers'].max().loc[bbpreddf.index]\n\nbbpreddf.iloc[:,:2] = np.floor((bbpreddf.iloc[:,:2] * 512)).astype(int).clip(0, 512)\nbbpreddf.iloc[:,2:4] = np.ceil((bbpreddf.iloc[:,2:4] * 512)).astype(int).clip(0, 512)\nbbpreddf.to_csv('train_bbox_pred_v01.csv')\n\n((bbpreddf.slnum_to - bbpreddf.slnum_from) / bbpreddf.slnum_max).hist(bins = 50 )","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:25:25.134791Z","iopub.execute_input":"2023-02-26T17:25:25.135819Z","iopub.status.idle":"2023-02-26T17:25:51.563787Z","shell.execute_reply.started":"2023-02-26T17:25:25.13577Z","shell.execute_reply":"2023-02-26T17:25:51.562746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_dl, nets, preds, uids, slices, predbbdf, bbpreddf\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:25:53.859054Z","iopub.execute_input":"2023-02-26T17:25:53.859429Z","iopub.status.idle":"2023-02-26T17:25:54.222336Z","shell.execute_reply.started":"2023-02-26T17:25:53.859398Z","shell.execute_reply":"2023-02-26T17:25:54.22136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head train_bbox_pred_v01.csv","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:25:55.836766Z","iopub.execute_input":"2023-02-26T17:25:55.837132Z","iopub.status.idle":"2023-02-26T17:25:56.877461Z","shell.execute_reply.started":"2023-02-26T17:25:55.8371Z","shell.execute_reply":"2023-02-26T17:25:56.876242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:26:51.005174Z","iopub.execute_input":"2023-02-26T17:26:51.005617Z","iopub.status.idle":"2023-02-26T17:26:51.021344Z","shell.execute_reply.started":"2023-02-26T17:26:51.005568Z","shell.execute_reply":"2023-02-26T17:26:51.020401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name = 'cfg_dh_fracseq_04F_crop_gx1'\n\ncfg = get_cfg(name)\ncfg.meta_df = META_DF\ncfg.data_folder = DATA_FOLDER\ncfg.verify_sample = False\ncfg.load_jpg = False\ncfg.cnn_chunk_size = 32\ncfg.bbox_df = 'train_bbox_pred_v01.csv'\ncfg.batch_size=1\ncfg.norm_on_cuda=True\ntest_dl, batch_to_device = get_dl(cfg)\ntest_dl.dataset.norm_mean = test_dl.dataset.norm_mean.to(DEVICE)\ntest_dl.dataset.norm_std = test_dl.dataset.norm_std.to(DEVICE)\ntest_dl.dataset.metadf = pd.DataFrame({'StudyInstanceUID': \\\n                                       test_dl.dataset.df.index.unique()}).set_index('StudyInstanceUID')\ntest_dl.dataset.metadf[['patient_overall'] + cfg.target] = 0.\n\nstate_dict_fps = sorted(glob.glob('../input/rsna-models-utoronto/weights/weights1/cfg_dh_fracseq_04F_crop_gx1/fold-1/*check*'))[:5]\nif OOF_CHECK:\n    state_dict_fps = [i for i in state_dict_fps if f'fold{FOLD}_' in i]\nstate_dict_fps = state_dict_fps[:MAX_FOLDS]\n\nprint('\\n'.join(state_dict_fps))\nif OOF_CHECK:\n    nets = get_nets(cfg,state_dict_fps[:1], test_dl.dataset)\nelse:\n    nets = get_nets(cfg,state_dict_fps, test_dl.dataset)","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:27:00.661285Z","iopub.execute_input":"2023-02-26T17:27:00.661704Z","iopub.status.idle":"2023-02-26T17:27:21.447107Z","shell.execute_reply.started":"2023-02-26T17:27:00.661656Z","shell.execute_reply":"2023-02-26T17:27:21.446079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name = 'cfg_dh_fracseq_04G_crop_gx1'\n\ncfg = get_cfg(name)\ncfg.meta_df = META_DF\ncfg.data_folder = DATA_FOLDER\ncfg.verify_sample = False\ncfg.load_jpg = False\ncfg.cnn_chunk_size = 16\ncfg.bbox_df = 'train_bbox_pred_v01.csv'\ncfg.batch_size=1\ncfg.norm_on_cuda=True\ntest_dl, batch_to_device = get_dl(cfg)\ntest_dl.dataset.norm_mean = test_dl.dataset.norm_mean.to(DEVICE)#.half()\ntest_dl.dataset.norm_std = test_dl.dataset.norm_std.to(DEVICE)#.half()\ntest_dl.dataset.metadf = pd.DataFrame({'StudyInstanceUID': \\\n                                       test_dl.dataset.df.index.unique()}).set_index('StudyInstanceUID')\ntest_dl.dataset.metadf[['patient_overall'] + cfg.target] = 0.\n\n\nstate_dict_fps = sorted(glob.glob('../input/rsna-models-utoronto/weights/weights1/cfg_dh_fracseq_04G_crop_gx1/fold-1/*check*'))\nif OOF_CHECK:\n    state_dict_fps = [i for i in state_dict_fps if f'fold{FOLD}_' in i]\nstate_dict_fps = state_dict_fps[:MAX_FOLDS]\n\nprint('\\n'.join(state_dict_fps))\nif OOF_CHECK:\n    nets += get_nets(cfg,state_dict_fps[:1], test_dl.dataset)\nelse:\n    nets += get_nets(cfg,state_dict_fps, test_dl.dataset)","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:27:31.098527Z","iopub.execute_input":"2023-02-26T17:27:31.099547Z","iopub.status.idle":"2023-02-26T17:27:43.272635Z","shell.execute_reply.started":"2023-02-26T17:27:31.099507Z","shell.execute_reply":"2023-02-26T17:27:43.271485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(nets))\nprint('Cnn chunk sizes : '+' '.join(map(str, [i.cfg.cnn_chunk_size for i in nets])))","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:27:44.122718Z","iopub.execute_input":"2023-02-26T17:27:44.123749Z","iopub.status.idle":"2023-02-26T17:27:44.13906Z","shell.execute_reply.started":"2023-02-26T17:27:44.123662Z","shell.execute_reply":"2023-02-26T17:27:44.136103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vaacoshl_data = {}\npreds = []\n#ids = []\nwith torch.inference_mode():\n    for tt, batch in tqdm(enumerate(test_dl), total = len(test_dl)):\n        batch = batch_to_device(batch,DEVICE)\n        batch['image'] = test_dl.dataset.norm4d( batch['image'] )\n        #batch['image'] = batch['image'].half()\n        outs = [net(batch) for net in nets]\n        preds += [torch.sigmoid(torch.stack([out['logits'] for out in outs], dim=0)).cpu().mean(0)]\n        if tt % 10 == 0:\n            gc.collect()\n            torch.cuda.empty_cache()\nids = test_dl.dataset.ids\npreds = torch.cat(preds, dim=0).float()\ndel test_dl, nets\ngc.collect()\ntorch.cuda.empty_cache()\n","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:27:46.724628Z","iopub.execute_input":"2023-02-26T17:27:46.725047Z","iopub.status.idle":"2023-02-26T17:28:34.771401Z","shell.execute_reply.started":"2023-02-26T17:27:46.725012Z","shell.execute_reply":"2023-02-26T17:28:34.770356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preddf = pd.DataFrame(preds.numpy(), columns = ['patient_overall'] + cfg.target)\npreddf['StudyInstanceUID'] = ids\npreddf = preddf.set_index('StudyInstanceUID')\npreddf.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:28:38.047503Z","iopub.execute_input":"2023-02-26T17:28:38.048652Z","iopub.status.idle":"2023-02-26T17:28:38.065717Z","shell.execute_reply.started":"2023-02-26T17:28:38.048605Z","shell.execute_reply":"2023-02-26T17:28:38.064518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subdf = preddf.reset_index().melt(id_vars=[\"StudyInstanceUID\"], var_name=\"row_id\", value_name=\"fractured\")\nsubdf['row_id'] = subdf[['StudyInstanceUID', 'row_id']] .agg('_'.join, axis=1)\nsubdf = subdf.drop('StudyInstanceUID', 1)\nsubdf.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:28:41.800189Z","iopub.execute_input":"2023-02-26T17:28:41.800568Z","iopub.status.idle":"2023-02-26T17:28:41.821259Z","shell.execute_reply.started":"2023-02-26T17:28:41.800537Z","shell.execute_reply":"2023-02-26T17:28:41.820344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subdf.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:28:47.45977Z","iopub.execute_input":"2023-02-26T17:28:47.460215Z","iopub.status.idle":"2023-02-26T17:28:47.468801Z","shell.execute_reply.started":"2023-02-26T17:28:47.460179Z","shell.execute_reply":"2023-02-26T17:28:47.467739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if (OOF_CHECK is True) or (TORONOTO_CHECK is True):\n    \n    actdf = pd.read_csv('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train.csv')\n    actdf = actdf[actdf.StudyInstanceUID.isin(test_df.StudyInstanceUID)].reset_index(drop = True)\n    actdf = actdf[actdf.StudyInstanceUID.isin(test_df.StudyInstanceUID)]\n    subdf1 = subdf.copy()\n    subdf1['StudyInstanceUID colid'.split()] = subdf1['row_id'].str.split('_', n = 1, expand = True)\n    subdf1 = subdf1.pivot_table('fractured', ['StudyInstanceUID'], 'colid').loc[actdf.StudyInstanceUID.tolist()].reset_index()\n    cols = ['StudyInstanceUID', 'patient_overall', 'C1', 'C2', 'C3', 'C4', 'C5','C6', 'C7']\n    actdf = actdf[cols].set_index('StudyInstanceUID')\n    subdf1 = subdf1[cols].set_index('StudyInstanceUID')\n\n    y_hat = torch.from_numpy(subdf1.values).float().to(device)\n    y     = torch.from_numpy(actdf.values).float().to(device)\n    print(f\"The overall loss is {competiton_loss_row_norm(y_hat, y):0.4f}\")","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:36:58.682354Z","iopub.execute_input":"2023-02-26T17:36:58.682809Z","iopub.status.idle":"2023-02-26T17:36:58.713615Z","shell.execute_reply.started":"2023-02-26T17:36:58.682771Z","shell.execute_reply":"2023-02-26T17:36:58.712457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-02-26T17:35:22.373568Z","iopub.execute_input":"2023-02-26T17:35:22.374763Z","iopub.status.idle":"2023-02-26T17:35:22.388734Z","shell.execute_reply.started":"2023-02-26T17:35:22.374701Z","shell.execute_reply":"2023-02-26T17:35:22.387489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Clean up local directory\n!rm -rf ./*.txt\n!rm -rf ./*.py*","metadata":{},"execution_count":null,"outputs":[]}]}