{"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 monai","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:35:37.135521Z","iopub.execute_input":"2023-08-20T23:35:37.135984Z","iopub.status.idle":"2023-08-20T23:35:51.39785Z","shell.execute_reply.started":"2023-08-20T23:35:37.135943Z","shell.execute_reply":"2023-08-20T23:35:51.396714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport tempfile\nimport matplotlib.pyplot as plt\nimport PIL\nimport torch\nimport numpy as np\nimport random\nimport pandas as pd\nfrom tqdm import tqdm\n\nimport torch.nn as nn\nimport torchvision.models as models\nimport torch.nn.functional as F\nfrom monai.apps import download_and_extract\nfrom monai.config import print_config\nfrom monai.data import decollate_batch, DataLoader,CacheDataset,ImageDataset,SmartCacheDataset,ThreadDataLoader\nfrom monai.metrics import ROCAUCMetric,MSEMetric\nfrom monai.networks.nets import DenseNet121,EfficientNetBN\nfrom monai.transforms import (\n    Activations,\n    EnsureChannelFirstd,\n    AsDiscrete,\n    Compose,\n    LoadImaged,\n    RandFlipd,\n    RandRotated,\n    RandZoomd,\n    ScaleIntensityd,\n    SpatialPadd,\n    AsChannelFirstd,\n    EnsureTyped,\n    Lambdad,\n    AddChanneld,\n    ScaleIntensityRanged,\n    Resized,\n    ToTensord\n)\nfrom monai.transforms.transform import MapTransform, RandomizableTransform\nfrom monai.config import DtypeLike, KeysCollection, SequenceStr\n\nfrom monai.utils import set_determinism\n","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:35:51.401344Z","iopub.execute_input":"2023-08-20T23:35:51.401703Z","iopub.status.idle":"2023-08-20T23:36:07.277161Z","shell.execute_reply.started":"2023-08-20T23:35:51.401649Z","shell.execute_reply":"2023-08-20T23:36:07.276198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set_determinism(seed=0)\nDATA_DIR = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images\"\nOUT_DIR = \"/kaggle/working/\"\ntrain_ct_labels = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')\ntrain_series_meta = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_series_meta.csv')\ntrain_image_label = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/image_level_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:36:07.278709Z","iopub.execute_input":"2023-08-20T23:36:07.279069Z","iopub.status.idle":"2023-08-20T23:36:07.332225Z","shell.execute_reply.started":"2023-08-20T23:36:07.279032Z","shell.execute_reply":"2023-08-20T23:36:07.331341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_label['subject_scans_slice'] = (train_image_label['patient_id'].astype(str) + '_'+\n                                            train_image_label['series_id'].astype(str) + '_'+\n                                            train_image_label['instance_number'].astype(str))","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:36:07.334522Z","iopub.execute_input":"2023-08-20T23:36:07.334949Z","iopub.status.idle":"2023-08-20T23:36:07.375Z","shell.execute_reply.started":"2023-08-20T23:36:07.334914Z","shell.execute_reply":"2023-08-20T23:36:07.374146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INJURY = \"bowel\"","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:36:07.376219Z","iopub.execute_input":"2023-08-20T23:36:07.376621Z","iopub.status.idle":"2023-08-20T23:36:07.381177Z","shell.execute_reply.started":"2023-08-20T23:36:07.376587Z","shell.execute_reply":"2023-08-20T23:36:07.380158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subjects = []\nfor x in os.listdir(DATA_DIR):\n    subjects.append(x)\nrandom.shuffle(subjects)\n\ndef get_subject_scans(subject_list):\n    subject_scans = []\n    for x in subject_list:\n        for y in os.listdir(os.path.join(DATA_DIR,str(x))):\n            subject_scans.append(str(x)+\"_\"+y)\n    random.shuffle(subject_scans)\n    return subject_scans\n\ndef get_subject_scan_slice(subject_scans_list):\n    subject_scans_slice = []\n    for x in tqdm(subject_scans_list):\n        for y in os.listdir(os.path.join(DATA_DIR,x.split(\"_\")[0],x.split(\"_\")[1])):\n            subject_scans_slice.append(x+\"_\"+y)\n    random.shuffle(subject_scans_slice)\n    return subject_scans_slice","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:36:07.382609Z","iopub.execute_input":"2023-08-20T23:36:07.383615Z","iopub.status.idle":"2023-08-20T23:36:07.649067Z","shell.execute_reply.started":"2023-08-20T23:36:07.38358Z","shell.execute_reply":"2023-08-20T23:36:07.648198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subject_scans = get_subject_scans(subjects)\nsubject_scans_slice = get_subject_scan_slice(subject_scans)\nlen(subject_scans_slice)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:36:12.47934Z","iopub.execute_input":"2023-08-20T23:36:12.479718Z","iopub.status.idle":"2023-08-20T23:44:21.178534Z","shell.execute_reply.started":"2023-08-20T23:36:12.479683Z","shell.execute_reply":"2023-08-20T23:44:21.177606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_val_test_split=[.8,.1,.1]\ninjuried_subjects = list(set(train_image_label[train_image_label['injury_name']=='Bowel']['patient_id']))\ntrain_injuried_subjects = injuried_subjects[:int(len(injuried_subjects)*train_val_test_split[0])]\nval_injuried_subjects = injuried_subjects[int(len(injuried_subjects)*train_val_test_split[0]):int(len(injuried_subjects)*train_val_test_split[0])+int(len(injuried_subjects)*train_val_test_split[1])]\ntest_injuried_subjects = injuried_subjects[int(len(injuried_subjects)*train_val_test_split[0])+int(len(injuried_subjects)*train_val_test_split[1]):]","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:44:21.180633Z","iopub.execute_input":"2023-08-20T23:44:21.181092Z","iopub.status.idle":"2023-08-20T23:44:21.194499Z","shell.execute_reply.started":"2023-08-20T23:44:21.181055Z","shell.execute_reply":"2023-08-20T23:44:21.193585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_injuried_subjects_scan = get_subject_scans(train_injuried_subjects)\nval_injuried_subjects_scans = get_subject_scans(val_injuried_subjects)\ntest_injuried_subjects_scans = get_subject_scans(test_injuried_subjects)\n\ntrain_injuried_subjects_subject_scans_slice = get_subject_scan_slice(train_injuried_subjects_scan)\nval_injuried_subjects_subject_scans_slice = get_subject_scan_slice(val_injuried_subjects_scans)\ntest_injuried_subjects_subject_scans_slice = get_subject_scan_slice(test_injuried_subjects_scans)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:44:21.19619Z","iopub.execute_input":"2023-08-20T23:44:21.19656Z","iopub.status.idle":"2023-08-20T23:44:21.361665Z","shell.execute_reply.started":"2023-08-20T23:44:21.196527Z","shell.execute_reply":"2023-08-20T23:44:21.35983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_image_label_dic(subject_scan_slice_list):\n    image_label_dic = []\n    subject_scans_slice_injuried = set(train_image_label['subject_scans_slice'].values)\n    total = 0\n    for s in subject_scan_slice_list:\n        image_path = os.path.join(DATA_DIR,s.split(\"_\")[0],s.split(\"_\")[1],s.split(\"_\")[2])\n        total +=int(s.split('.')[0] in subject_scans_slice_injuried)\n        image_label_dic.append(\n            {\n                \"image\":image_path,\n                \"label\": int(s.split('.')[0] in subject_scans_slice_injuried)\n            }\n        )\n    print('Total: ' + str(len(subject_scan_slice_list))+ \", Signal: \"+str(total))\n    return image_label_dic","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:44:21.363971Z","iopub.execute_input":"2023-08-20T23:44:21.364528Z","iopub.status.idle":"2023-08-20T23:44:21.372249Z","shell.execute_reply.started":"2023-08-20T23:44:21.364494Z","shell.execute_reply":"2023-08-20T23:44:21.371288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_list = subject_scans_slice[:5000]+train_injuried_subjects_subject_scans_slice\nval_list = subject_scans_slice[5000:5500]+val_injuried_subjects_subject_scans_slice\ntest_list = subject_scans_slice[5500:6500]+test_injuried_subjects_subject_scans_slice","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:44:21.375054Z","iopub.execute_input":"2023-08-20T23:44:21.375735Z","iopub.status.idle":"2023-08-20T23:44:21.387449Z","shell.execute_reply.started":"2023-08-20T23:44:21.375701Z","shell.execute_reply":"2023-08-20T23:44:21.386535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dic = get_image_label_dic(train_list)\nval_dic = get_image_label_dic(val_list)\ntest_dic = get_image_label_dic(test_list)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:44:21.388738Z","iopub.execute_input":"2023-08-20T23:44:21.389222Z","iopub.status.idle":"2023-08-20T23:44:21.708771Z","shell.execute_reply.started":"2023-08-20T23:44:21.389187Z","shell.execute_reply":"2023-08-20T23:44:21.707706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nclass SliceSelectord(RandomizableTransform,MapTransform):\n    def __init__(\n        self,\n        keys: KeysCollection,\n        prob= 1,\n        allow_missing_keys: bool = False,\n    ) -> None:\n        MapTransform.__init__(self, keys, allow_missing_keys)\n        RandomizableTransform.__init__(self, prob)\n\n    def __call__(self, data,randomize = True) :\n        self.randomize(None)\n        d = dict(data)\n        n_slice = random.randint(0,d['image'].shape[-1])\n        d['image'] = d['image'][:,:,:,n_slice]\n        filepath = d['image_meta_dict']['filename_or_obj']\n        subject_scan_slice = filepath.split(\"/\")[-2] + \"_\"+filepath.split(\"/\")[-1]+ \"_\"+str(n_slice)\n        d['label'] = int(subject_scan_slice in subject_scans_slice_injuried)\n        return d\n        \n'''","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:46:32.868778Z","iopub.execute_input":"2023-08-20T21:46:32.869083Z","iopub.status.idle":"2023-08-20T21:46:32.879687Z","shell.execute_reply.started":"2023-08-20T21:46:32.869059Z","shell.execute_reply":"2023-08-20T21:46:32.878641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transforms = Compose(\n    [\n        LoadImaged(keys=[\"image\"]),\n        EnsureChannelFirstd(keys=[\"image\"]),\n        ScaleIntensityRanged(\n            keys=[\"image\"],\n            a_min=-175,\n            a_max=250,\n            b_min=0.0,\n            b_max=1.0,\n            clip=True,\n        ),\n        Resized(\n            keys=[\"image\"],\n            spatial_size =(256,256)\n        ),\n        #Resized(\n        #    keys=[\"image\"],\n        #    spatial_size =(256,256,-1)\n        #),\n        ScaleIntensityd(keys = ['image']),\n        RandRotated(keys = ['image'],range_x=np.pi / 12, prob=0.5, keep_size=True),\n        RandFlipd(keys = ['image'],spatial_axis=0, prob=0.5),\n        #SliceSelectord(keys=[\"image\"]),\n        #ToTensord(keys=[\"image\"]),\n    ]\n)\nval_transforms = Compose(\n    [ LoadImaged(keys=[\"image\"]),\n        \n        EnsureChannelFirstd(keys=[\"image\"]),\n\n        ScaleIntensityRanged(\n            keys=[\"image\"],\n            a_min=-175,\n            a_max=250,\n            b_min=0.0,\n            b_max=1.0,\n            clip=True,\n        ),\n        #Resized(\n        #    keys=[\"image\"],\n        #    spatial_size =(256,256,-1)\n        #),\n         Resized(\n            keys=[\"image\"],\n            spatial_size =(256,256)\n        ),\n        #SliceSelectord(keys=[\"image\"]),\n        #ToTensord(keys=[\"image\"])\n     \n     ]\n)\ny_pred_trans = Compose([Activations(softmax=True)])\ny_trans = Compose([AsDiscrete(to_onehot=2)])","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:44:21.710092Z","iopub.execute_input":"2023-08-20T23:44:21.710635Z","iopub.status.idle":"2023-08-20T23:44:21.732539Z","shell.execute_reply.started":"2023-08-20T23:44:21.710598Z","shell.execute_reply":"2023-08-20T23:44:21.730464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = CacheDataset(data=train_dic, transform=train_transforms, cache_rate=1.0,cache_num=5000, num_workers=2)\ntrain_loader = DataLoader(\n    train_ds, batch_size=48, shuffle=True, )#num_workers=10","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:44:21.734025Z","iopub.execute_input":"2023-08-20T23:44:21.734381Z","iopub.status.idle":"2023-08-20T23:46:31.651975Z","shell.execute_reply.started":"2023-08-20T23:44:21.734346Z","shell.execute_reply":"2023-08-20T23:46:31.650228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = CacheDataset(data=val_dic, transform=val_transforms, cache_rate=1.0,num_workers=2)\nval_loader = DataLoader(\n    val_ds, batch_size=48, shuffle=True)# num_workers=10","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:46:31.654553Z","iopub.execute_input":"2023-08-20T23:46:31.654937Z","iopub.status.idle":"2023-08-20T23:47:49.570149Z","shell.execute_reply.started":"2023-08-20T23:46:31.654897Z","shell.execute_reply":"2023-08-20T23:47:49.569183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ[\"CUDA_DEVICE_ORDER\"] = \"PCI_BUS_ID\"\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = DenseNet121(spatial_dims=2, in_channels=1, out_channels=2).to(device)\nloss_function = torch.nn.CrossEntropyLoss()\n#loss_function = torch.nn.BCELoss()\noptimizer = torch.optim.Adam(model.parameters(), 1e-5)\nmax_epochs = 10\nval_interval = 1\nauc_metric = ROCAUCMetric()\nout_model= 'best_metric_model_bowel.pth'","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:48:44.587261Z","iopub.execute_input":"2023-08-20T23:48:44.587668Z","iopub.status.idle":"2023-08-20T23:48:50.381733Z","shell.execute_reply.started":"2023-08-20T23:48:44.58762Z","shell.execute_reply":"2023-08-20T23:48:50.380713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_metric = -1\nbest_metric_epoch = -1\nepoch_loss_values = []\nmetric_values = []\n\nfor epoch in tqdm(range(max_epochs)):\n    print(\"-\" * 10)\n    print(f\"epoch {epoch + 1}/{max_epochs}\")\n    model.train()\n    epoch_loss = 0\n    step = 0\n    for batch_data in train_loader:\n        step += 1\n        inputs, labels = batch_data['image'].to(device), batch_data['label'].to(device)\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        loss = loss_function(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        epoch_loss += loss.item()\n        print(f\"{step}/{len(train_ds) // train_loader.batch_size}, \" f\"train_loss: {loss.item():.4f}\")\n        epoch_len = len(train_ds) // train_loader.batch_size\n    epoch_loss /= step\n    epoch_loss_values.append(epoch_loss)\n    print(f\"epoch {epoch + 1} average loss: {epoch_loss:.4f}\")\n\n    if (epoch + 1) % val_interval == 0:\n        model.eval()\n        with torch.no_grad():\n            y_pred = torch.tensor([], dtype=torch.float32, device=device)\n            y = torch.tensor([], dtype=torch.long, device=device)\n            for val_data in val_loader:\n                val_images, val_labels = (\n                    val_data['image'].to(device),\n                    val_data['label'].to(device),\n                )\n                y_pred = torch.cat([y_pred, model(val_images)], dim=0)\n            \n                y = torch.cat([y, val_labels], dim=0)\n     \n            y_onehot = [y_trans(i) for i in decollate_batch(y, detach=False)]\n            y_pred_act = [y_pred_trans(i) for i in decollate_batch(y_pred)]\n            auc_metric(y_pred_act, y_onehot)\n            result = auc_metric.aggregate()\n            \n            auc_metric.reset()\n            del y_pred_act, y_onehot\n            metric_values.append(result)\n            acc_value = torch.eq(y_pred.argmax(dim=1), y)\n            acc_metric = acc_value.sum().item() / len(acc_value)\n            if result > best_metric:\n                best_metric = result\n                best_metric_epoch = epoch + 1\n                torch.save(model.state_dict(), os.path.join(OUT_DIR, out_model))\n                print(\"saved new best metric model\")\n            print(\n                f\"current epoch: {epoch + 1} current AUC: {result:.4f}\"\n                f\" current accuracy: {acc_metric:.4f}\"\n                f\" best AUC: {best_metric:.4f}\"\n                f\" at epoch: {best_metric_epoch}\"\n            )\n\nprint(f\"train completed, best_metric: {best_metric:.4f} \" f\"at epoch: {best_metric_epoch}\")","metadata":{"execution":{"iopub.status.busy":"2023-08-20T23:48:50.385618Z","iopub.execute_input":"2023-08-20T23:48:50.387717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bowel_injuried_patient_test","metadata":{"execution":{"iopub.status.busy":"2023-08-20T19:38:40.035756Z","iopub.execute_input":"2023-08-20T19:38:40.03618Z","iopub.status.idle":"2023-08-20T19:38:40.049238Z","shell.execute_reply.started":"2023-08-20T19:38:40.036148Z","shell.execute_reply":"2023-08-20T19:38:40.04825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_subject_scans_slice = []\nfor x in tqdm(test_subject_scans[:30]):\n    for y in os.listdir(os.path.join(DATA_DIR,x.split(\"_\")[0],x.split(\"_\")[1])):\n        test_subject_scans_slice.append(x+\"_\"+y)\nrandom.shuffle(test_subject_scans_slice)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:46:38.338646Z","iopub.execute_input":"2023-08-20T21:46:38.338989Z","iopub.status.idle":"2023-08-20T21:46:38.402739Z","shell.execute_reply.started":"2023-08-20T21:46:38.338957Z","shell.execute_reply":"2023-08-20T21:46:38.401758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_subject_scans_bowel = get_subject_scans(bowel_injuried_patient_test)\n#Training with individual slices\nsubject_scans_slice_bowel_test = []\nfor x in test_subject_scans_bowel:\n    for y in os.listdir(os.path.join(DATA_DIR,x.split(\"_\")[0],x.split(\"_\")[1])):\n        subject_scans_slice_bowel_test.append(x+\"_\"+y)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:46:38.405313Z","iopub.execute_input":"2023-08-20T21:46:38.405654Z","iopub.status.idle":"2023-08-20T21:46:39.595474Z","shell.execute_reply.started":"2023-08-20T21:46:38.405629Z","shell.execute_reply":"2023-08-20T21:46:39.594294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_subject_scans_slice = subject_scans_slice_bowel_test+test_subject_scans_slice\ntest_subject_scans_slice = set(test_subject_scans_slice)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:46:39.597047Z","iopub.execute_input":"2023-08-20T21:46:39.597446Z","iopub.status.idle":"2023-08-20T21:46:39.604567Z","shell.execute_reply.started":"2023-08-20T21:46:39.59741Z","shell.execute_reply":"2023-08-20T21:46:39.603329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_subject_scans_slice)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:46:39.606087Z","iopub.execute_input":"2023-08-20T21:46:39.606629Z","iopub.status.idle":"2023-08-20T21:46:39.618891Z","shell.execute_reply.started":"2023-08-20T21:46:39.606597Z","shell.execute_reply":"2023-08-20T21:46:39.617766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#generate image label dictionary\ntest_image_label_dic = []\nsubject_scans_slice_injuried = set(train_image_label['subject_scans_slice'].values)\nfor s in test_subject_scans_slice:\n    image_path = os.path.join(DATA_DIR,s.split(\"_\")[0],s.split(\"_\")[1],s.split(\"_\")[2])\n    test_image_label_dic.append(\n        {\n            \"image\":image_path,\n            \"label\": int(s.split('.')[0] in subject_scans_slice_injuried)\n        }\n    )\nrandom.shuffle(test_image_label_dic)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:46:39.620645Z","iopub.execute_input":"2023-08-20T21:46:39.621338Z","iopub.status.idle":"2023-08-20T21:46:40.076659Z","shell.execute_reply.started":"2023-08-20T21:46:39.621302Z","shell.execute_reply":"2023-08-20T21:46:40.075624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = CacheDataset(data=test_image_label_dic, transform=val_transforms, cache_rate=1.0,cache_num=7000,\n                         num_workers=2\n                        )\ntest_loader = DataLoader(test_ds, batch_size=48, shuffle=True, \n                          num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:46:40.078002Z","iopub.execute_input":"2023-08-20T21:46:40.07846Z","iopub.status.idle":"2023-08-20T21:49:30.674605Z","shell.execute_reply.started":"2023-08-20T21:46:40.078427Z","shell.execute_reply":"2023-08-20T21:49:30.673562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model_path = OUT_DIR+out_model\nmodel.load_state_dict(torch.load(\"/kaggle/input/best-metric-model-bowel/best_metric_model_bowel.pth\"))","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:49:30.677968Z","iopub.execute_input":"2023-08-20T21:49:30.678262Z","iopub.status.idle":"2023-08-20T21:49:31.265698Z","shell.execute_reply.started":"2023-08-20T21:49:30.678238Z","shell.execute_reply":"2023-08-20T21:49:31.264799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\nwith torch.no_grad():\n    y_pred = torch.tensor([], dtype=torch.float32, device=device)\n    y = torch.tensor([], dtype=torch.long, device=device)\n    name = []\n    for test_data in tqdm(test_loader):\n        test_images, test_labels = (\n            test_data['image'].to(device),\n            test_data['label'].to(device),\n        )\n        name = name+test_data['image_meta_dict']['filename_or_obj']\n        y_pred = torch.cat([y_pred, model(test_images)], dim=0)\n        y = torch.cat([y, test_labels], dim=0)\n\n    y_onehot = [y_trans(i) for i in decollate_batch(y, detach=False)]\n    y_pred_act = [y_pred_trans(i) for i in decollate_batch(y_pred)]\n    auc_metric(y_pred_act, y_onehot)\n    result = auc_metric.aggregate()\n\n    auc_metric.reset()\n    del y_pred_act, y_onehot\n    acc_value = torch.eq(y_pred.argmax(dim=1), y)\n    acc_metric = acc_value.sum().item() / len(acc_value)\n   \n    print(\n        f\"AUC: {result:.4f}\",\n        f\"accuracy: {acc_metric:.4f}\"\n    )","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:49:31.266931Z","iopub.execute_input":"2023-08-20T21:49:31.267538Z","iopub.status.idle":"2023-08-20T21:53:12.035266Z","shell.execute_reply.started":"2023-08-20T21:49:31.267504Z","shell.execute_reply":"2023-08-20T21:53:12.033774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_ds\nimport gc \ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-08-20T21:57:22.779924Z","iopub.execute_input":"2023-08-20T21:57:22.780335Z","iopub.status.idle":"2023-08-20T21:57:23.243892Z","shell.execute_reply.started":"2023-08-20T21:57:22.780306Z","shell.execute_reply":"2023-08-20T21:57:23.242912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_act = [y_pred_trans(i).cpu().numpy() for i in decollate_batch(y_pred)]\nsignal = [x[1] for x in y_pred_act]\nzip_results = zip(name,y.cpu().numpy(),signal)\nresults = pd.DataFrame(list(zip_results), columns=['path', 'y', 'signal'])","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:08:46.208449Z","iopub.execute_input":"2023-08-20T22:08:46.208828Z","iopub.status.idle":"2023-08-20T22:08:51.281172Z","shell.execute_reply.started":"2023-08-20T22:08:46.2088Z","shell.execute_reply":"2023-08-20T22:08:51.280089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.to_csv(\"/kaggle/working/results.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:08:51.283094Z","iopub.execute_input":"2023-08-20T22:08:51.283466Z","iopub.status.idle":"2023-08-20T22:08:51.424229Z","shell.execute_reply.started":"2023-08-20T22:08:51.283432Z","shell.execute_reply":"2023-08-20T22:08:51.423064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.read_csv('/kaggle/input/results/results (1).csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:13:53.281068Z","iopub.execute_input":"2023-08-20T22:13:53.281461Z","iopub.status.idle":"2023-08-20T22:13:53.34308Z","shell.execute_reply.started":"2023-08-20T22:13:53.281414Z","shell.execute_reply":"2023-08-20T22:13:53.341943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results['path'][1]","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:14:19.991299Z","iopub.execute_input":"2023-08-20T22:14:19.991799Z","iopub.status.idle":"2023-08-20T22:14:20.001778Z","shell.execute_reply.started":"2023-08-20T22:14:19.991759Z","shell.execute_reply":"2023-08-20T22:14:20.000595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results['path'].str.split(pat = \"/\", expand=True)[5]","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:20:00.968329Z","iopub.execute_input":"2023-08-20T22:20:00.968743Z","iopub.status.idle":"2023-08-20T22:20:01.023937Z","shell.execute_reply.started":"2023-08-20T22:20:00.96871Z","shell.execute_reply":"2023-08-20T22:20:01.022762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bowel_injuried_patient_test","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:26:03.483763Z","iopub.execute_input":"2023-08-20T22:26:03.484149Z","iopub.status.idle":"2023-08-20T22:26:03.491216Z","shell.execute_reply.started":"2023-08-20T22:26:03.484121Z","shell.execute_reply":"2023-08-20T22:26:03.490114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results['subject'] = results['path'].str.split(pat=\"/\", expand=True)[5]\nresults['scan'] = results['path'].str.split(pat=\"/\", expand=True)[6]\nresults['slice'] = results['path'].str.split(pat=\"/\", expand=True)[7]","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:20:48.000531Z","iopub.execute_input":"2023-08-20T22:20:48.000924Z","iopub.status.idle":"2023-08-20T22:20:48.170612Z","shell.execute_reply.started":"2023-08-20T22:20:48.000894Z","shell.execute_reply":"2023-08-20T22:20:48.169472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results['slice_int'] = results['slice'].str.split('.', expand=True)[0].astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:30:19.236985Z","iopub.execute_input":"2023-08-20T22:30:19.237393Z","iopub.status.idle":"2023-08-20T22:30:19.282284Z","shell.execute_reply.started":"2023-08-20T22:30:19.23736Z","shell.execute_reply":"2023-08-20T22:30:19.281109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:30:22.775701Z","iopub.execute_input":"2023-08-20T22:30:22.776102Z","iopub.status.idle":"2023-08-20T22:30:22.794028Z","shell.execute_reply.started":"2023-08-20T22:30:22.776072Z","shell.execute_reply":"2023-08-20T22:30:22.79295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import display, HTML\ndisplay(HTML(results[results['subject'] == '15876'].sort_values(by=\"slice_int\").to_html()))\n","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:32:28.72308Z","iopub.execute_input":"2023-08-20T22:32:28.723481Z","iopub.status.idle":"2023-08-20T22:32:28.819364Z","shell.execute_reply.started":"2023-08-20T22:32:28.723451Z","shell.execute_reply":"2023-08-20T22:32:28.818198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results[results['subject'] == '15876'].sort_values(by=\"slice_int\")","metadata":{"execution":{"iopub.status.busy":"2023-08-20T22:31:22.802417Z","iopub.execute_input":"2023-08-20T22:31:22.803476Z","iopub.status.idle":"2023-08-20T22:31:22.836027Z","shell.execute_reply.started":"2023-08-20T22:31:22.803394Z","shell.execute_reply":"2023-08-20T22:31:22.834784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2023-08-20T20:16:14.753104Z","iopub.execute_input":"2023-08-20T20:16:14.754008Z","iopub.status.idle":"2023-08-20T20:16:14.832068Z","shell.execute_reply.started":"2023-08-20T20:16:14.753968Z","shell.execute_reply":"2023-08-20T20:16:14.8311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2023-08-20T20:16:20.357Z","iopub.execute_input":"2023-08-20T20:16:20.357404Z","iopub.status.idle":"2023-08-20T20:16:20.367838Z","shell.execute_reply.started":"2023-08-20T20:16:20.357372Z","shell.execute_reply":"2023-08-20T20:16:20.366715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name","metadata":{"execution":{"iopub.status.busy":"2023-08-20T20:16:24.902738Z","iopub.execute_input":"2023-08-20T20:16:24.90313Z","iopub.status.idle":"2023-08-20T20:16:24.955327Z","shell.execute_reply.started":"2023-08-20T20:16:24.903095Z","shell.execute_reply":"2023-08-20T20:16:24.95442Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_act = [y_pred_trans(i) for i in decollate_batch(y_pred)]","metadata":{"execution":{"iopub.status.busy":"2023-08-20T20:17:38.209622Z","iopub.execute_input":"2023-08-20T20:17:38.209997Z","iopub.status.idle":"2023-08-20T20:17:41.540302Z","shell.execute_reply.started":"2023-08-20T20:17:38.209968Z","shell.execute_reply":"2023-08-20T20:17:41.53923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}