{"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":"code","source":"!pip install -r ../input/github-rsna2022/requirements.txt --no-index --find-links=file:///kaggle/input/rsna2022-pip-wheels/ -q","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:06:25.775299Z","iopub.execute_input":"2022-10-26T14:06:25.776384Z","iopub.status.idle":"2022-10-26T14:06:53.994401Z","shell.execute_reply.started":"2022-10-26T14:06:25.776211Z","shell.execute_reply":"2022-10-26T14:06:53.993222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/github-rsna2022/* ./","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:06:53.999041Z","iopub.execute_input":"2022-10-26T14:06:53.999347Z","iopub.status.idle":"2022-10-26T14:06:55.782419Z","shell.execute_reply.started":"2022-10-26T14:06:53.999316Z","shell.execute_reply":"2022-10-26T14:06:55.780988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Your notebook tried to allocate more memory than is available. It has restarted","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:06:55.784261Z","iopub.execute_input":"2022-10-26T14:06:55.784674Z","iopub.status.idle":"2022-10-26T14:06:55.790462Z","shell.execute_reply.started":"2022-10-26T14:06:55.78463Z","shell.execute_reply":"2022-10-26T14:06:55.789471Z"},"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":"2022-10-26T14:06:55.79206Z","iopub.execute_input":"2022-10-26T14:06:55.792842Z","iopub.status.idle":"2022-10-26T14:06:59.235853Z","shell.execute_reply.started":"2022-10-26T14:06:55.792786Z","shell.execute_reply":"2022-10-26T14:06:59.234857Z"},"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":"2022-10-26T14:06:59.239514Z","iopub.execute_input":"2022-10-26T14:06:59.240052Z","iopub.status.idle":"2022-10-26T14:06:59.245874Z","shell.execute_reply.started":"2022-10-26T14:06:59.240018Z","shell.execute_reply":"2022-10-26T14:06:59.244866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dcmdf_fn(DATA_FOLDER):\n    keycols = 'StudyInstanceUID slice_number'.split()\n    uid_map = dict((d.split('/')[-1], t) for t,d in enumerate(glob.glob(f'{DATA_FOLDER}/*')))\n    dfls = []\n    for dirnm in uid_map.keys():\n        k = uid_map[dirnm]\n        for fnm in glob.glob(f'{DATA_FOLDER}/{dirnm}/*.dcm'):\n            fkey = int(fnm.replace('.dcm', '').split('/')[-1])\n            dfls.append([k, fkey])\n    df = pd.DataFrame(dfls, columns = keycols)\n    return df, uid_map\n        ","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:06:59.247154Z","iopub.execute_input":"2022-10-26T14:06:59.247737Z","iopub.status.idle":"2022-10-26T14:06:59.309727Z","shell.execute_reply.started":"2022-10-26T14:06:59.247701Z","shell.execute_reply":"2022-10-26T14:06:59.308779Z"},"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\ncatargs = {'StudyInstanceUID':'category'}\ntrain_df = pd.read_csv(COMP_FOLDER + 'train.csv', dtype = catargs)\ntest_df = pd.read_csv(COMP_FOLDER + 'test.csv', dtype = catargs)\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)\n#df, uid_map = dcmdf_fn(DATA_FOLDER)\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\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 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, uid_map = dcmdf_fn(DATA_FOLDER)\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', dtype = catargs).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, uid_map = dcmdf_fn(DATA_FOLDER)\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['StudyInstanceUID'] = df.StudyInstanceUID.astype('category')\ndf['fold'] = -1\nprint(train_df.shape)\nprint(test_df.shape)\nprint(df.shape)\ngc.collect()\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:06:59.31301Z","iopub.execute_input":"2022-10-26T14:06:59.313319Z","iopub.status.idle":"2022-10-26T14:06:59.768374Z","shell.execute_reply.started":"2022-10-26T14:06:59.31329Z","shell.execute_reply":"2022-10-26T14:06:59.767339Z"},"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":"2022-10-26T14:06:59.769874Z","iopub.execute_input":"2022-10-26T14:06:59.770259Z","iopub.status.idle":"2022-10-26T14:07:02.795222Z","shell.execute_reply.started":"2022-10-26T14:06:59.770223Z","shell.execute_reply":"2022-10-26T14:07:02.794226Z"},"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":"2022-10-26T14:07:02.798859Z","iopub.execute_input":"2022-10-26T14:07:02.799169Z","iopub.status.idle":"2022-10-26T14:07:02.816861Z","shell.execute_reply.started":"2022-10-26T14:07:02.799141Z","shell.execute_reply":"2022-10-26T14:07:02.815848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bbdf = df\n# bbdf['count'] = bbdf.groupby('StudyInstanceUID')['fold'].transform('count').values\n# bbdf['cumcount'] = 1+ bbdf.groupby('StudyInstanceUID')['fold'].transform('cumcount').values\n# bbdf['key'] = (bbdf['cumcount']  / (bbdf['count'] / NUM_SLICES_PER_UID_BBOX)).round().astype(int)\n# bbdf = bbdf.drop_duplicates('StudyInstanceUID key'.split()).drop('count cumcount key'.split(), 1)","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:07:02.818373Z","iopub.execute_input":"2022-10-26T14:07:02.818881Z","iopub.status.idle":"2022-10-26T14:07:02.831325Z","shell.execute_reply.started":"2022-10-26T14:07:02.818844Z","shell.execute_reply":"2022-10-26T14:07:02.830267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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)\nstate_dict_fps = sorted(glob.glob('../input/weights-cfg-loc-dh-01b-all-fold-1/fold*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":"2022-10-26T14:07:02.832727Z","iopub.execute_input":"2022-10-26T14:07:02.833166Z","iopub.status.idle":"2022-10-26T14:07:13.81807Z","shell.execute_reply.started":"2022-10-26T14:07:02.83313Z","shell.execute_reply":"2022-10-26T14:07:13.817043Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:07:13.819588Z","iopub.execute_input":"2022-10-26T14:07:13.820051Z","iopub.status.idle":"2022-10-26T14:07:41.710598Z","shell.execute_reply.started":"2022-10-26T14:07:13.820007Z","shell.execute_reply":"2022-10-26T14:07:41.70945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predbbdf = 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['slice_numbers_all'] = slices.numpy().tolist()\npredbbdf['StudyInstanceUID'] = test_dl.dataset.df.loc[test_dl.dataset.ids]['StudyInstanceUID'].values\npredbbdf['StudyInstanceUID'] = predbbdf['StudyInstanceUID'].astype('category')","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:07:41.712336Z","iopub.execute_input":"2022-10-26T14:07:41.712715Z","iopub.status.idle":"2022-10-26T14:07:41.726925Z","shell.execute_reply.started":"2022-10-26T14:07:41.712677Z","shell.execute_reply":"2022-10-26T14:07:41.726086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"allpreddf = predbbdf\nallpreddf = allpreddf.explode('slice_numbers_all')\nallpreddf['slice_numbers'] = allpreddf['slice_numbers_all']\nallpreddf = allpreddf.set_index('StudyInstanceUID').drop(['slice_numbers_all', 'uid'], 1)\n\nallpreddf.to_csv('train_bbox_all_pred_v02.csv')\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":"2022-10-26T14:07:41.732595Z","iopub.execute_input":"2022-10-26T14:07:41.732901Z","iopub.status.idle":"2022-10-26T14:07:42.102719Z","shell.execute_reply.started":"2022-10-26T14:07:41.732846Z","shell.execute_reply":"2022-10-26T14:07:42.101778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_dl, nets, preds, uids, slices, predbbdf, bbpreddf, allpreddf\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:07:42.103896Z","iopub.execute_input":"2022-10-26T14:07:42.104272Z","iopub.status.idle":"2022-10-26T14:07:42.407193Z","shell.execute_reply.started":"2022-10-26T14:07:42.104244Z","shell.execute_reply":"2022-10-26T14:07:42.406173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head train_bbox_pred_v01.csv","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:07:42.411429Z","iopub.execute_input":"2022-10-26T14:07:42.416087Z","iopub.status.idle":"2022-10-26T14:07:43.66039Z","shell.execute_reply.started":"2022-10-26T14:07:42.416038Z","shell.execute_reply":"2022-10-26T14:07:43.659097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head train_bbox_all_pred_v02.csv","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:07:43.662682Z","iopub.execute_input":"2022-10-26T14:07:43.66318Z","iopub.status.idle":"2022-10-26T14:07:44.720139Z","shell.execute_reply.started":"2022-10-26T14:07:43.66314Z","shell.execute_reply":"2022-10-26T14:07:44.718964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name = 'cfg_dh_fracseq_04F_crop3_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'  #. f'{cfg.data_dir}/train_bbox_pred_v02.csv.gz'\ncfg.bboxall_df = 'train_bbox_all_pred_v02.csv' # f'{cfg.data_dir}/train_bbox_all_pred_v02.csv.gz'\n# cfg.bbox_df = 'train_bbox_pred_v01.csv'\n# cfg.bbox_df = 'train_bbox_all_pred_v02.csv'\n\ncfg.batch_size=1\n#cfg.dataset = \"ds_dh_fracseg_3F_crop_infer\"\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()\n#test_dl.dataset.df[cfg.target_seg] = 0.\n#test_dl.dataset.df[cfg.target_frac] = 0.\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#test_dl.dataset.metadf\n\nstate_dict_fps = sorted(glob.glob('../input/weights-cfg-dh-fracseq-04f-crop3-gx1-4f3ff/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":"2022-10-26T14:07:44.722062Z","iopub.execute_input":"2022-10-26T14:07:44.722799Z","iopub.status.idle":"2022-10-26T14:07:58.088164Z","shell.execute_reply.started":"2022-10-26T14:07:44.722755Z","shell.execute_reply":"2022-10-26T14:07:58.087167Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:07:58.089906Z","iopub.execute_input":"2022-10-26T14:07:58.090337Z","iopub.status.idle":"2022-10-26T14:08:17.836829Z","shell.execute_reply.started":"2022-10-26T14:07:58.090297Z","shell.execute_reply":"2022-10-26T14:08:17.835804Z"},"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":"2022-10-26T14:08:17.838626Z","iopub.execute_input":"2022-10-26T14:08:17.839013Z","iopub.status.idle":"2022-10-26T14:08:17.858695Z","shell.execute_reply.started":"2022-10-26T14:08:17.838979Z","shell.execute_reply":"2022-10-26T14:08:17.857457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preddfls = [preddf]","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:08:17.860269Z","iopub.execute_input":"2022-10-26T14:08:17.86064Z","iopub.status.idle":"2022-10-26T14:08:17.867258Z","shell.execute_reply.started":"2022-10-26T14:08:17.860606Z","shell.execute_reply":"2022-10-26T14:08:17.865239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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/weights-cfg-dh-fracseq-04f-crop-gx1-6ff/fold-1*check*'))[:-3]\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":"2022-10-26T14:08:17.868523Z","iopub.execute_input":"2022-10-26T14:08:17.86938Z","iopub.status.idle":"2022-10-26T14:08:30.397706Z","shell.execute_reply.started":"2022-10-26T14:08:17.869342Z","shell.execute_reply":"2022-10-26T14:08:30.396748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name = 'cfg_dh_fracseq_04F_crop_gx2'\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\n#cfg.dataset = \"ds_dh_fracseg_3F_crop_infer\"\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()\n#test_dl.dataset.df[cfg.target_seg] = 0.\n#test_dl.dataset.df[cfg.target_frac] = 0.\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#test_dl.dataset.metadf\n\nstate_dict_fps = sorted(glob.glob('../input/weights-cfg-dh-fracseq-04f-crop-gx2-4f2ff/fold-1*check*'))[:-3]\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":"2022-10-26T14:08:30.399219Z","iopub.execute_input":"2022-10-26T14:08:30.399851Z","iopub.status.idle":"2022-10-26T14:08:42.843956Z","shell.execute_reply.started":"2022-10-26T14:08:30.399786Z","shell.execute_reply":"2022-10-26T14:08:42.842958Z"},"trusted":true},"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.\nstate_dict_fps = sorted(glob.glob('../input/weights-cfg-dh-fracseq-04g-crop-gx1-5f2ff/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":"2022-10-26T14:08:42.845475Z","iopub.execute_input":"2022-10-26T14:08:42.845858Z","iopub.status.idle":"2022-10-26T14:09:02.253015Z","shell.execute_reply.started":"2022-10-26T14:08:42.845803Z","shell.execute_reply":"2022-10-26T14:09:02.252024Z"},"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":"2022-10-26T14:09:02.254418Z","iopub.execute_input":"2022-10-26T14:09:02.254787Z","iopub.status.idle":"2022-10-26T14:09:02.262452Z","shell.execute_reply.started":"2022-10-26T14:09:02.254746Z","shell.execute_reply":"2022-10-26T14:09:02.261482Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:09:02.263857Z","iopub.execute_input":"2022-10-26T14:09:02.264653Z","iopub.status.idle":"2022-10-26T14:09:46.660031Z","shell.execute_reply.started":"2022-10-26T14:09:02.264617Z","shell.execute_reply":"2022-10-26T14:09:46.658983Z"},"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":"2022-10-26T14:09:46.661572Z","iopub.execute_input":"2022-10-26T14:09:46.661964Z","iopub.status.idle":"2022-10-26T14:09:46.677474Z","shell.execute_reply.started":"2022-10-26T14:09:46.661927Z","shell.execute_reply":"2022-10-26T14:09:46.676547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preddfls.append(preddf)","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:09:46.679008Z","iopub.execute_input":"2022-10-26T14:09:46.679615Z","iopub.status.idle":"2022-10-26T14:09:46.688626Z","shell.execute_reply.started":"2022-10-26T14:09:46.679577Z","shell.execute_reply":"2022-10-26T14:09:46.68752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(preddfls))","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:09:46.690093Z","iopub.execute_input":"2022-10-26T14:09:46.690434Z","iopub.status.idle":"2022-10-26T14:09:46.7048Z","shell.execute_reply.started":"2022-10-26T14:09:46.6904Z","shell.execute_reply":"2022-10-26T14:09:46.703926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preddfls[0].head()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:09:46.707368Z","iopub.execute_input":"2022-10-26T14:09:46.707713Z","iopub.status.idle":"2022-10-26T14:09:46.721596Z","shell.execute_reply.started":"2022-10-26T14:09:46.707688Z","shell.execute_reply":"2022-10-26T14:09:46.719935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preddfls[1].head()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:09:46.722928Z","iopub.execute_input":"2022-10-26T14:09:46.723207Z","iopub.status.idle":"2022-10-26T14:09:46.73978Z","shell.execute_reply.started":"2022-10-26T14:09:46.723181Z","shell.execute_reply":"2022-10-26T14:09:46.738759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preddfall = ((preddfls[0] * 1)+ (preddfls[1] * 4)) / 5\npreddfall.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:09:46.743105Z","iopub.execute_input":"2022-10-26T14:09:46.743392Z","iopub.status.idle":"2022-10-26T14:09:46.757718Z","shell.execute_reply.started":"2022-10-26T14:09:46.743367Z","shell.execute_reply":"2022-10-26T14:09:46.75676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subdf = preddfall.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":"2022-10-26T14:09:46.759622Z","iopub.execute_input":"2022-10-26T14:09:46.760744Z","iopub.status.idle":"2022-10-26T14:09:46.779395Z","shell.execute_reply.started":"2022-10-26T14:09:46.760708Z","shell.execute_reply":"2022-10-26T14:09:46.778569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"subdf.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:09:46.781571Z","iopub.execute_input":"2022-10-26T14:09:46.782067Z","iopub.status.idle":"2022-10-26T14:09:46.787971Z","shell.execute_reply.started":"2022-10-26T14:09:46.782032Z","shell.execute_reply":"2022-10-26T14:09:46.786888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if OOF_CHECK is True:\n    actdf = pd.read_csv('../input/rsna2022-train-dataset/train_folded_v01.csv').query(f'fold == {FOLD}').reset_index(drop = True)\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":"2022-10-26T14:09:46.789688Z","iopub.execute_input":"2022-10-26T14:09:46.790047Z","iopub.status.idle":"2022-10-26T14:09:46.799516Z","shell.execute_reply.started":"2022-10-26T14:09:46.790013Z","shell.execute_reply":"2022-10-26T14:09:46.798215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}