{"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":"# **Import 3rdparty**","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport nibabel as nib\nfrom tqdm import tqdm\nimport glob\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:47:59.879867Z","iopub.execute_input":"2022-07-13T01:47:59.880435Z","iopub.status.idle":"2022-07-13T01:48:00.152291Z","shell.execute_reply.started":"2022-07-13T01:47:59.880316Z","shell.execute_reply":"2022-07-13T01:48:00.151313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### infer https://www.kaggle.com/code/walterok/batch-inference-making-masks-using-nnunet/notebook\ndef make_if_dont_exist(folder_path,overwrite=False):\n    \"\"\"\n    creates a folder if it does not exists\n    input: \n    folder_path : relative path of the folder which needs to be created\n    over_write :(default: False) if True overwrite the existing folder \n    \"\"\"\n    if os.path.exists(folder_path):\n        \n        if not overwrite:\n            print(f'{folder_path} exists.')\n        else:\n            print(f\"{folder_path} overwritten\")\n            shutil.rmtree(folder_path)\n            os.makedirs(folder_path)\n\n    else:\n        os.makedirs(folder_path)\n        print(f\"{folder_path} created!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:48:00.154315Z","iopub.execute_input":"2022-07-13T01:48:00.154685Z","iopub.status.idle":"2022-07-13T01:48:00.161563Z","shell.execute_reply.started":"2022-07-13T01:48:00.154648Z","shell.execute_reply":"2022-07-13T01:48:00.160546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_if_dont_exist('/tmp/nnUNet')\nmake_if_dont_exist('/tmp/nnUNet_dep_libs/')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:48:00.163104Z","iopub.execute_input":"2022-07-13T01:48:00.163731Z","iopub.status.idle":"2022-07-13T01:48:00.177136Z","shell.execute_reply.started":"2022-07-13T01:48:00.163694Z","shell.execute_reply":"2022-07-13T01:48:00.176270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Install nnUNet libs**","metadata":{}},{"cell_type":"code","source":"### install MedPy-0.4.0\n!cp -rf /kaggle/input/nnunet-packages/packages/MedPy-0.4.0/MedPy-0.4.0  /tmp/nnUNet_dep_libs/\n!cp -rf /kaggle/input/nnunet-packages/packages/batchgenerators-0.23 /tmp/nnUNet_dep_libs/\n!cp -rf /kaggle/input/nnunet-packages/packages/dicom2nifti-2.3.3 /tmp/nnUNet_dep_libs/\nrespository_dir = '/tmp/nnUNet_dep_libs/'\nos.chdir(respository_dir)\n\n### install MedPy\nmedpy_dir = f'/tmp/nnUNet_dep_libs/MedPy-0.4.0/'\nos.chdir(medpy_dir)\n!pip install -e .\n\n### install batchgenerators\nbatchgenerators_dir = f'/tmp/nnUNet_dep_libs/batchgenerators-0.23/'\nos.chdir(batchgenerators_dir)\n!pip install -e .\n\n### install dicom2nifti\ndicom2nifti_dir = f'/tmp/nnUNet_dep_libs/dicom2nifti-2.3.3/dicom2nifti-2.3.3'\nos.chdir(dicom2nifti_dir)\n!pip install -e .\n\n### install nnUNet\n!cp -rf /kaggle/input/uwmgit-nnunet/nnUNet-master /tmp/nnUNet\nnnunet_dir = '/tmp/nnUNet/nnUNet-master'\nos.chdir(nnunet_dir)\n!pip install -e .","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:48:00.180212Z","iopub.execute_input":"2022-07-13T01:48:00.180544Z","iopub.status.idle":"2022-07-13T01:50:22.582475Z","shell.execute_reply.started":"2022-07-13T01:48:00.180512Z","shell.execute_reply":"2022-07-13T01:50:22.581306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Create dirs**","metadata":{}},{"cell_type":"code","source":"base_nnunet_dir = \"/tmp/input/\"\nraw_data_base_dir = \"/tmp/input/nnUNet_raw_data_base\"\npreprocessed_dir = \"/tmp/input/nnUNet_preprocessed\"\ntrained_models_dir = \"/tmp/input/nnUNet_trained_models\"\n\ntask_name = 'Task522_UWMGITImageSegmentation' #change here for different task name\ntask_folder_name = os.path.join(raw_data_base_dir, 'nnUNet_raw_data', task_name)\nimagestr = os.path.join(task_folder_name,'imagesTr')\nimagests = os.path.join(task_folder_name,'imagesTs')\nlabelstr = os.path.join(task_folder_name,'labelsTr')\noutput_dir = \"/tmp/output/segmented\"\n\nall_output_dir = \"/tmp/output/segmented/all_0\"\nall_output_dir1 = \"/tmp/output/segmented/all_1\"\nall_output_dir2 = \"/tmp/output/segmented/all_2\"\n\nfold0_output_dir = \"/tmp/output/segmented/fold_0\"\nfold1_output_dir = \"/tmp/output/segmented/fold_1\"\nfold2_output_dir = \"/tmp/output/segmented/fold_2\"\nfold3_output_dir = \"/tmp/output/segmented/fold_3\"\nfold4_output_dir = \"/tmp/output/segmented/fold_4\"\n\nensemble_output_dir = \"/tmp/output/segmented/ensemble\"\nmodels_save_dir = '/tmp/input/nnUNet_trained_models/nnUNet/3d_fullres/Task522_UWMGITImageSegmentation/'\n\nmake_if_dont_exist(base_nnunet_dir, overwrite = False)\nmake_if_dont_exist(raw_data_base_dir, overwrite = False)\nmake_if_dont_exist(preprocessed_dir, overwrite = False)\nmake_if_dont_exist(trained_models_dir, overwrite = False)\nmake_if_dont_exist(imagestr, overwrite = False)\nmake_if_dont_exist(imagests, overwrite = False)\nmake_if_dont_exist(labelstr, overwrite = False)\nmake_if_dont_exist(output_dir, overwrite = False)\nmake_if_dont_exist(all_output_dir1, overwrite = False)\nmake_if_dont_exist(all_output_dir2, overwrite = False)\n\nmake_if_dont_exist(fold0_output_dir, overwrite = False)\nmake_if_dont_exist(fold1_output_dir, overwrite = False)\nmake_if_dont_exist(fold2_output_dir, overwrite = False)\nmake_if_dont_exist(fold3_output_dir, overwrite = False)\nmake_if_dont_exist(fold4_output_dir, overwrite = False)\nmake_if_dont_exist(ensemble_output_dir, overwrite = False)\nmake_if_dont_exist(models_save_dir, overwrite = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:50:22.584703Z","iopub.execute_input":"2022-07-13T01:50:22.585101Z","iopub.status.idle":"2022-07-13T01:50:22.619180Z","shell.execute_reply.started":"2022-07-13T01:50:22.585063Z","shell.execute_reply":"2022-07-13T01:50:22.618199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder = os.path.join(base_nnunet_dir, \"train_images/\")\ntest_folder = os.path.join(base_nnunet_dir, \"test_images/\")\nlabel_folder = os.path.join(base_nnunet_dir, \"masks/\")\n\nmake_if_dont_exist(train_folder, overwrite = False)\nmake_if_dont_exist(test_folder, overwrite = False)\nmake_if_dont_exist(label_folder, overwrite = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:50:22.620584Z","iopub.execute_input":"2022-07-13T01:50:22.620933Z","iopub.status.idle":"2022-07-13T01:50:22.636814Z","shell.execute_reply.started":"2022-07-13T01:50:22.620899Z","shell.execute_reply":"2022-07-13T01:50:22.635601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('/tmp/')\n!ls","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:50:22.640418Z","iopub.execute_input":"2022-07-13T01:50:22.641146Z","iopub.status.idle":"2022-07-13T01:50:23.451501Z","shell.execute_reply.started":"2022-07-13T01:50:22.640988Z","shell.execute_reply":"2022-07-13T01:50:23.450452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Helpers**","metadata":{}},{"cell_type":"code","source":"#### inference by https://github.com/Borda/kaggle_image-segm/blob/main/kaggle_imsegm/mask.py\nfrom typing import Dict\ndef rle_decode(mask_rle: str, img: np.ndarray = None, img_shape: tuple = None, label: int = 1) -> np.ndarray:\n    \"\"\"Create a single label mask for Run-length encoding.\n    >>> mask = rle_decode(\"3 2 11 5 23 3 35 1\", img_shape=(8, 10))\n    >>> mask = rle_decode(\"55 3 66 2 77 1\", img=mask, label=2)\n    >>> mask = rle_decode(\"26 3 36 2\", img=mask, label=3)\n    >>> mask\n    array([[0, 0, 0, 1, 1, 0, 0, 0, 0, 0],\n           [0, 1, 1, 1, 1, 1, 0, 0, 0, 0],\n           [0, 0, 0, 1, 1, 1, 3, 3, 3, 0],\n           [0, 0, 0, 0, 0, 1, 3, 3, 0, 0],\n           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n           [0, 0, 0, 0, 0, 2, 2, 2, 0, 0],\n           [0, 0, 0, 0, 0, 0, 2, 2, 0, 0],\n           [0, 0, 0, 0, 0, 0, 0, 2, 0, 0]], dtype=uint16)\n    >>> from pprint import pprint\n    >>> pprint(rle_encode(mask))\n    {1: '3 2 11 5 23 3 35 1', 2: '55 3 66 2 77 1', 3: '26 3 36 2'}\n    \"\"\"\n    seq = mask_rle.split()\n    starts = np.array(list(map(int, seq[0::2])))\n    lengths = np.array(list(map(int, seq[1::2])))\n    assert len(starts) == len(lengths)\n    ends = starts + lengths\n\n    if img is None:\n        img = np.zeros((np.product(img_shape),), dtype=np.uint16)\n    else:\n        img_shape = img.shape\n        img = img.flatten()\n    for begin, end in zip(starts, ends):\n        img[begin:end] = label\n    return img.reshape(img_shape)\n\n\ndef rle_encode(mask: np.ndarray, label_bg: int = 0) -> Dict[int, str]:\n    \"\"\"Encode mask to Run-length encoding.\n    Inspiration took from: https://gist.github.com/nvictus/66627b580c13068589957d6ab0919e66\n    >>> from pprint import pprint\n    >>> mask = np.array([[0, 1, 1, 1, 1, 1, 0, 0, 0, 0],\n    ...                  [0, 0, 0, 1, 1, 1, 2, 2, 2, 0],\n    ...                  [0, 0, 0, 0, 0, 1, 3, 3, 0, 0],])\n    >>> pprint(rle_encode(mask))\n    {1: '1 5 13 3 25 1', 2: '16 3', 3: '26 2'}\n    \"\"\"\n    vec = mask.flatten()\n    nb = len(vec)\n    where = np.flatnonzero\n    starts = np.r_[0, where(~np.isclose(vec[1:], vec[:-1], equal_nan=True)) + 1]\n    lengths = np.diff(np.r_[starts, nb])\n    values = vec[starts]\n    assert len(starts) == len(lengths) == len(values)\n    rle = {}\n    for start, length, val in zip(starts, lengths, values):\n        if val == label_bg:\n            continue\n        rle[val] = rle.get(val, []) + [str(start), length]\n    # post-processing\n    rle = {lb: \" \".join(map(str, id_lens)) for lb, id_lens in rle.items()}\n    return rle\n\ndef load_image_volume(img_dir, quant=0.01):\n    imgs = sorted(glob.glob(os.path.join(img_dir, f\"*.png\")))\n    imgs = [np.array(Image.open(p)).tolist() for p in imgs]\n    # print([np.max(im) for im in imgs])\n    vol = np.array(imgs)\n    if quant:\n        q_low, q_high = np.percentile(vol, [quant * 100, (1 - quant) * 100])\n        vol = np.clip(vol, q_low, q_high)\n    v_min, v_max = np.min(vol), np.max(vol)\n    vol = (vol - v_min) / (v_max - v_min)\n    vol = (vol * 255).astype(np.uint8)\n    return vol\n\ndef create_organs_segm(df_vol, vol_shape):\n    df_vol = df_vol.replace(np.nan, '')\n    segm = np.zeros(vol_shape, dtype=np.uint8)\n    lbs = sorted(df_vol[\"class\"].unique())\n#     print(f'lbs is {lbs}')\n    for idx_, dfg in df_vol.groupby(\"Slice\"):\n        idx = int(idx_) - 1\n        mask = segm[idx, :, :]\n        for _, (lb, rle) in dfg[[\"class\", \"segmentation\"]].iterrows():\n            lb = lbs.index(lb) + 1\n            if not rle:\n                continue\n            mask = rle_decode(rle, img=mask, label=lb)\n        segm[idx, :, :] = mask\n        # plt.figure(); plt.imshow(mask)\n    return segm","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:50:23.453345Z","iopub.execute_input":"2022-07-13T01:50:23.453906Z","iopub.status.idle":"2022-07-13T01:50:23.478442Z","shell.execute_reply.started":"2022-07-13T01:50:23.453847Z","shell.execute_reply":"2022-07-13T01:50:23.477398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Load data**","metadata":{}},{"cell_type":"code","source":"root_dir = \"/kaggle/input/uw-madison-gi-tract-image-segmentation\"\ntrain_dir = f\"{root_dir}/train\"\ntest_dir = f\"{root_dir}/test\"\ntrain_csv_path = f\"{root_dir}/train.csv\"\nsample_csv_path = f\"{root_dir}/sample_submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:50:23.480837Z","iopub.execute_input":"2022-07-13T01:50:23.481490Z","iopub.status.idle":"2022-07-13T01:50:23.488725Z","shell.execute_reply.started":"2022-07-13T01:50:23.481453Z","shell.execute_reply":"2022-07-13T01:50:23.487812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(train_csv_path)\ndf_train","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:50:23.489746Z","iopub.execute_input":"2022-07-13T01:50:23.492142Z","iopub.status.idle":"2022-07-13T01:50:23.988718Z","shell.execute_reply.started":"2022-07-13T01:50:23.492106Z","shell.execute_reply":"2022-07-13T01:50:23.987806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv(sample_csv_path)\ndf_test","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:50:23.990063Z","iopub.execute_input":"2022-07-13T01:50:23.990942Z","iopub.status.idle":"2022-07-13T01:50:24.005913Z","shell.execute_reply.started":"2022-07-13T01:50:23.990903Z","shell.execute_reply":"2022-07-13T01:50:24.005073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Generate train data**","metadata":{}},{"cell_type":"code","source":"def extract_details(id_):\n    id_fields = id_.split(\"_\")\n    case = id_fields[0].replace(\"case\", \"\")\n    day = id_fields[1].replace(\"day\", \"\")\n    slice_id = id_fields[3]\n    img_dir = os.path.join(root_dir, \"train\",\n                           f\"case{case}\", f\"case{case}_day{day}\", \"scans\")\n    imgs = glob.glob(os.path.join(img_dir, f\"slice_{slice_id}_*.png\"))\n    assert len(imgs) == 1\n    img_path = imgs[0].replace(root_dir + \"/\", \"\")\n    img = os.path.basename(img_path)\n    # slice_0001_266_266_1.50_1.50.png\n    im_fields = img.split(\"_\")\n    return {\n        \"Case\": int(case),\n        \"Day\": int(day),\n        \"Slice\": slice_id,\n        \"image\": img,\n        \"image_path\": img_path, \n        \"height\": int(im_fields[3]),\n        \"width\": int(im_fields[2]),\n    }","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:50:24.008935Z","iopub.execute_input":"2022-07-13T01:50:24.009180Z","iopub.status.idle":"2022-07-13T01:50:24.017444Z","shell.execute_reply.started":"2022-07-13T01:50:24.009157Z","shell.execute_reply":"2022-07-13T01:50:24.016175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tqdm.pandas(desc='pandas bar')\ndf_train[['Case','Day','Slice', 'image', 'image_path', 'height', 'width']] = \\\n    df_train['id'].progress_apply(lambda x: pd.Series(extract_details(x)))\n\ndisplay(df_train.head())","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:50:24.018814Z","iopub.execute_input":"2022-07-13T01:50:24.019834Z","iopub.status.idle":"2022-07-13T01:52:31.887708Z","shell.execute_reply.started":"2022-07-13T01:50:24.019770Z","shell.execute_reply":"2022-07-13T01:52:31.886662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_scans_dir = []\nfor dirname, _, filenames in os.walk(f'{root_dir}/train'):\n    if dirname.endswith('scans'):\n        train_scans_dir.append(dirname)\n        \nprint(f'Found {len(train_scans_dir)} scans directories')\n\ndebug = True ###just select 5 samples for training, please set debug=False for using all train data\nfor index, s_dir in tqdm(enumerate(train_scans_dir),total=len(train_scans_dir)):\n    if debug and index == 2:\n        break\n    image_id = s_dir.split('/')[-2]\n    case_str, day_str= image_id.split('_')\n    case = int(case_str.split('case')[-1])\n    day = int(day_str.split('day')[-1])\n\n    IMAGE_FOLDER = os.path.join(root_dir, \"train\", f\"case{case}\", f\"case{case}_day{day}\", \"scans\")\n    vol = load_image_volume(img_dir=IMAGE_FOLDER)\n    print(f'case{case}_day{day} shape is {vol.shape}')        \n    ### convert np to nibabel reference https://gist.github.com/tonyreina/64ac5703251b87118cf5d2886169fd5a\n    img = nib.Nifti1Image(vol, np.eye(4))  # Save axis for data (just identity)\n    img.header.get_xyzt_units()\n    img.to_filename(f'{train_folder}/{image_id}.nii.gz')  # Save as NiBabel file\n    \n    df_ = df_train[(df_train[\"Case\"] == case) & (df_train[\"Day\"] == day)]\n    segm = create_organs_segm(df_vol=df_, vol_shape=vol.shape)\n    \n    mask = nib.Nifti1Image(segm, np.eye(4))  # Save axis for data (just identity)\n    mask.header.get_xyzt_units()\n    mask.to_filename(f'{label_folder}/{image_id}.nii.gz')  # Save as NiBabel file","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:31.889286Z","iopub.execute_input":"2022-07-13T01:52:31.889801Z","iopub.status.idle":"2022-07-13T01:52:39.368713Z","shell.execute_reply.started":"2022-07-13T01:52:31.889741Z","shell.execute_reply":"2022-07-13T01:52:39.367676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Generate test data**","metadata":{}},{"cell_type":"code","source":"#### https://www.kaggle.com/code/outwrest/create-gifs-of-medical-3d-images\nscans_dir = []\n\nif df_test.shape[0] == 0: ### select data from train\n    for dirname, _, filenames in os.walk(f'{root_dir}/train'):\n        if dirname.endswith('scans'):\n            scans_dir.append(dirname)\nelse:\n    for dirname, _, filenames in os.walk(f'{root_dir}/test'):\n        if dirname.endswith('scans'):\n            scans_dir.append(dirname)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:39.374590Z","iopub.execute_input":"2022-07-13T01:52:39.375058Z","iopub.status.idle":"2022-07-13T01:52:39.724985Z","shell.execute_reply.started":"2022-07-13T01:52:39.375018Z","shell.execute_reply":"2022-07-13T01:52:39.724098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if df_test.shape[0] == 0:\n    for index, s_dir in tqdm(enumerate(scans_dir),total=len(scans_dir)):\n        image_id = s_dir.split('/')[-2]\n        case_str, day_str= image_id.split('_')\n        case = int(case_str.split('case')[-1])\n        day = int(day_str.split('day')[-1])\n        if index < 2:\n            IMAGE_FOLDER = os.path.join(root_dir, \"train\", f\"case{case}\", f\"case{case}_day{day}\", \"scans\")\n            vol = load_image_volume(img_dir=IMAGE_FOLDER)\n            print(f'case{case}_day{day} shape is {vol.shape}')\n            \n            ### convert np to nibabel reference https://gist.github.com/tonyreina/64ac5703251b87118cf5d2886169fd5a\n            img = nib.Nifti1Image(vol, np.eye(4))  # Save axis for data (just identity)\n            img.header.get_xyzt_units()\n            img.to_filename(f'{test_folder}/{image_id}.nii.gz')  # Save as NiBabel file\n            \nelse:\n    for index, s_dir in tqdm(enumerate(scans_dir),total=len(scans_dir)):\n        image_id = s_dir.split('/')[-2]\n        case_str, day_str= image_id.split('_')\n        case = int(case_str.split('case')[-1])\n        day = int(day_str.split('day')[-1])\n        \n        IMAGE_FOLDER = os.path.join(root_dir, \"test\", f\"case{case}\", f\"case{case}_day{day}\", \"scans\")\n        vol = load_image_volume(img_dir=IMAGE_FOLDER)\n        \n        ### convert np to nibabel reference https://gist.github.com/tonyreina/64ac5703251b87118cf5d2886169fd5a\n        img = nib.Nifti1Image(vol, np.eye(4))  # Save axis for data (just identity)\n        img.header.get_xyzt_units()\n        img.to_filename(f'{test_folder}/{image_id}.nii.gz')  # Save as NiBabel file","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:39.726405Z","iopub.execute_input":"2022-07-13T01:52:39.727080Z","iopub.status.idle":"2022-07-13T01:52:44.819470Z","shell.execute_reply.started":"2022-07-13T01:52:39.727043Z","shell.execute_reply":"2022-07-13T01:52:44.818460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('/tmp/nnUNet_dep_libs/batchgenerators-0.23')\nsys.path.append('/tmp/nnUNet/nnUNet-master/')\nfrom collections import OrderedDict\nfrom nnunet.paths import nnUNet_raw_data\nfrom batchgenerators.utilities.file_and_folder_operations import *\nfrom nnunet.dataset_conversion.utils import generate_dataset_json\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:44.821272Z","iopub.execute_input":"2022-07-13T01:52:44.822000Z","iopub.status.idle":"2022-07-13T01:52:44.841900Z","shell.execute_reply.started":"2022-07-13T01:52:44.821963Z","shell.execute_reply":"2022-07-13T01:52:44.841014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_patient_names = []\ntest_patient_names = []\ntrain_patients = subfiles(train_folder, join=False, suffix = 'nii.gz')\ntest_patients = subfiles(test_folder, join=False, suffix = 'nii.gz')\n\nprint(train_patients[0])\nprint(f'train samples is {len(train_patients)}')\nprint(test_patients[0])\nprint(f'test samples is {len(test_patients)}')\n\n\nfor index,patient_name in tqdm(enumerate(train_patients),total=len(train_patients)):\n    pex_name = patient_name.split('.')[0]\n    image_file = join(train_folder,patient_name)\n    label_file = join(label_folder,patient_name)\n    \n    shutil.copy(image_file, join(imagestr, f'{pex_name}_0000.nii.gz'))\n    shutil.copy(label_file, join(labelstr, patient_name))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:44.843202Z","iopub.execute_input":"2022-07-13T01:52:44.843557Z","iopub.status.idle":"2022-07-13T01:52:44.876475Z","shell.execute_reply.started":"2022-07-13T01:52:44.843524Z","shell.execute_reply":"2022-07-13T01:52:44.875415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('/tmp/nnUNet/nnUNet-master')\n!ls","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:44.877851Z","iopub.execute_input":"2022-07-13T01:52:44.878436Z","iopub.status.idle":"2022-07-13T01:52:45.566406Z","shell.execute_reply.started":"2022-07-13T01:52:44.878400Z","shell.execute_reply":"2022-07-13T01:52:45.565333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### set environment veriables\nos.environ['nnUNet_raw_data_base'] = raw_data_base_dir\nos.environ['nnUNet_preprocessed'] = preprocessed_dir\nos.environ['RESULTS_FOLDER'] = trained_models_dir","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:45.568421Z","iopub.execute_input":"2022-07-13T01:52:45.569012Z","iopub.status.idle":"2022-07-13T01:52:45.574455Z","shell.execute_reply.started":"2022-07-13T01:52:45.568973Z","shell.execute_reply":"2022-07-13T01:52:45.573265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls  /tmp/input/nnUNet_trained_models/nnUNet/3d_fullres/Task522_UWMGITImageSegmentation/","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:45.576245Z","iopub.execute_input":"2022-07-13T01:52:45.576787Z","iopub.status.idle":"2022-07-13T01:52:46.422279Z","shell.execute_reply.started":"2022-07-13T01:52:45.576749Z","shell.execute_reply":"2022-07-13T01:52:46.421039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('/tmp/nnUNet/nnUNet-master')\n!ls","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:46.425335Z","iopub.execute_input":"2022-07-13T01:52:46.425694Z","iopub.status.idle":"2022-07-13T01:52:47.142603Z","shell.execute_reply.started":"2022-07-13T01:52:46.425665Z","shell.execute_reply":"2022-07-13T01:52:47.141463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile /tmp/nnUNet/nnUNet-master/nnunet/training/network_training/nnUNetTrainerV2_UWMGIT.py\n#    Copyright 2020 Division of Medical Image Computing, German Cancer Research Center (DKFZ), Heidelberg, Germany\n#\n#    Licensed under the Apache License, Version 2.0 (the \"License\");\n#    you may not use this file except in compliance with the License.\n#    You may obtain a copy of the License at\n#\n#        http://www.apache.org/licenses/LICENSE-2.0\n#\n#    Unless required by applicable law or agreed to in writing, software\n#    distributed under the License is distributed on an \"AS IS\" BASIS,\n#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n#    See the License for the specific language governing permissions and\n#    limitations under the License.\n\n\nfrom time import sleep\n\nimport numpy as np\nimport torch\nfrom batchgenerators.utilities.file_and_folder_operations import *\nfrom nnunet.training.data_augmentation.data_augmentation_moreDA import get_moreDA_augmentation\nfrom nnunet.training.data_augmentation.default_data_augmentation import default_3D_augmentation_params, \\\n    default_2D_augmentation_params, get_patch_size\nfrom torch import nn\nfrom torch.nn.parallel import DistributedDataParallel as DDP\nfrom torch.nn.utils import clip_grad_norm_\n\nfrom nnunet.evaluation.region_based_evaluation import evaluate_regions, get_brats_regions\nfrom nnunet.network_architecture.generic_UNet import Generic_UNet\nfrom nnunet.network_architecture.initialization import InitWeights_He\nfrom nnunet.network_architecture.neural_network import SegmentationNetwork\nfrom nnunet.training.dataloading.dataset_loading import unpack_dataset\nfrom nnunet.training.loss_functions.deep_supervision import MultipleOutputLoss2\nfrom nnunet.training.loss_functions.dice_loss import DC_and_BCE_loss, get_tp_fp_fn_tn, SoftDiceLoss\nfrom nnunet.training.network_training.nnUNetTrainerV2 import nnUNetTrainerV2\nfrom nnunet.training.network_training.nnUNetTrainerV2_DDP import nnUNetTrainerV2_DDP\nfrom nnunet.utilities.distributed import awesome_allgather_function\nfrom nnunet.utilities.to_torch import maybe_to_torch, to_cuda\nfrom nnunet.training.learning_rate.poly_lr import poly_lr\nfrom nnunet.utilities.nd_softmax import softmax_helper\n \nclass nnUNetTrainerV2_largeunet(nnUNetTrainerV2):\n    def __init__(self, plans_file, fold, output_folder=None, dataset_directory=None, batch_dice=True, stage=None,\n                 unpack_data=True, deterministic=True, fp16=False):\n        super().__init__(plans_file, fold, output_folder, dataset_directory, batch_dice, stage, unpack_data,\n                         deterministic, fp16)\n\n    def initialize_network(self):\n        \n        \"\"\"inference_apply_nonlin to sigmoid + larger unet\"\"\"\n        if self.threeD:\n            conv_op = nn.Conv3d\n            dropout_op = nn.Dropout3d\n            norm_op = nn.BatchNorm3d\n\n        else:\n            conv_op = nn.Conv2d\n            dropout_op = nn.Dropout2d\n            norm_op = nn.BatchNorm2d\n        \n        norm_op_kwargs = {'eps': 1e-5, 'affine': True}\n        dropout_op_kwargs = {'p': 0, 'inplace': True}\n        net_nonlin = nn.LeakyReLU\n        net_nonlin_kwargs = {'negative_slope': 1e-2, 'inplace': True}\n        \n        ### large UNet\n        self.network = Generic_UNet(self.num_input_channels, self.base_num_features, self.num_classes,\n                                    len(self.net_num_pool_op_kernel_sizes),\n                                    self.conv_per_stage, 2, conv_op, norm_op, norm_op_kwargs, dropout_op,\n                                    dropout_op_kwargs,\n                                    net_nonlin, net_nonlin_kwargs, True, False, lambda x: x, InitWeights_He(1e-2),\n                                    self.net_num_pool_op_kernel_sizes, self.net_conv_kernel_sizes, False, True, True, 512)\n        if torch.cuda.is_available():\n            self.network.cuda()\n        self.network.inference_apply_nonlin = softmax_helper\n        ","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:47.146542Z","iopub.execute_input":"2022-07-13T01:52:47.146870Z","iopub.status.idle":"2022-07-13T01:52:47.158991Z","shell.execute_reply.started":"2022-07-13T01:52:47.146835Z","shell.execute_reply":"2022-07-13T01:52:47.157822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def batch_inference(i, batch_size, total_size):\n    print(f\"Batch size: {batch_size}\")\n    print(f\"Processing batch: {i}\")\n    start = i*batch_size\n    end = (i+1)*batch_size\n    if end > total_size:\n        end = total_size\n    for index,patient_name in tqdm(enumerate(test_patients[start:end]),total=(end-start)):\n        pex_name = patient_name.split('.')[0]\n        image_file = join(test_folder,patient_name)\n\n        shutil.copy(image_file, join(imagests, f'{pex_name}_0000.nii.gz'))\n        \n        generate_dataset_json(join(task_folder_name, 'dataset.json'),\n                          imagestr,\n                          imagests,\n                          ('CT',),\n                          {\n                              0: 'background',\n                              1: \"large_bowel\",\n                              2: \"small_bowel\",\n                              3: \"stomach\",\n                          },\n                          task_name,\n                          license='see challenge website',\n                          dataset_description='kaggle-uw-madison-gi-tract-image-segmentation',\n                          dataset_reference='https://www.kaggle.com/competitions/uw-madison-gi-tract-image-segmentation',\n                          dataset_release='0')\n        \n    !ls /tmp/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/imagesTs\n    \n    ###### start inference all fold ###########################\n    ### all\n#     !nnUNet_predict -i /kaggle/working/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/imagesTs -o /kaggle/working/output/segmented/all -t 522 -f all -chk model_latest -tr nnUNetTrainerV2_largeunet -p nnUNetPlansv2.1_bs2_ps256x256x144_290e  -m 3d_fullres --save_npz\n#     ### delete \n#     !rm -rf /kaggle/working/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/imagesTs/*\n#     !rm -rf /kaggle/working/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/dataset.json\n    ###### end inference all fold ###########################\n\n    ###### start ensemble all folds ###########################\n    ### all\n    !nnUNet_predict -i /tmp/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/imagesTs -o /tmp/output/segmented/all_0 -t 522 -f all -tr nnUNetTrainerV2_largeunet -p nnUNetPlansv2.1_bs2_ps192x192x144  -m 3d_fullres --save_npz\n    !nnUNet_predict -i /tmp/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/imagesTs -o /tmp/output/segmented/all_1 -t 522 -f all -chk model_latest -tr nnUNetTrainerV2_largeunet -p nnUNetPlansv2.1_bs2_ps256x256x144_804e  -m 3d_fullres --save_npz\n    !nnUNet_predict -i /tmp/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/imagesTs -o /tmp/output/segmented/all_2 -t 522 -f all -tr nnUNetTrainerV2_largeunet -p nnUNetPlansv2.1_bs2_ps160x160x80 --disable_tta  -m 3d_fullres --save_npz\n    !nnUNet_predict -i /tmp/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/imagesTs -o /tmp/output/segmented/fold_0 -t 522 -f 0 -chk model_best -tr nnUNetTrainerV2_largeunet -p nnUNetPlansv2.1_bs2_ps192x192x144 --disable_tta  -m 3d_fullres --save_npz\n    !nnUNet_predict -i /tmp/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/imagesTs -o /tmp/output/segmented/fold_1 -t 522 -f 0 -tr nnUNetTrainerV2 -p nnUNetPlansv2.1_bs4_ps320x320x96 --disable_tta  -m 3d_fullres --save_npz\n\n    \n\n#     !nnUNet_ensemble -f \\\n# /tmp/output/segmented/all \\\n# /tmp/output/segmented/all_1 \\\n# /tmp/output/segmented/all_2 \\\n# -o /tmp/output/segmented/ensemble --npz \\\n# -pp /kaggle/input/nnunet-3d-fullres-192x192x144-alldata/nnUNetTrainerV2_largeunet__nnUNetPlansv2.1_bs2_ps192x192x144/all/postprocessing.json\n    \n    ### delete \n    !rm -rf /tmp/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/imagesTs/*\n    !rm -rf /tmp/input/nnUNet_raw_data_base/nnUNet_raw_data/Task522_UWMGITImageSegmentation/dataset.json\n#     !rm -rf /tmp/output/segmented/all/*\n#     !rm -rf /tmp/output/segmented/all_1/*\n#     !rm -rf /tmp/output/segmented/all_2/*\n    ###### end ensemble all folds ###########################\n\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:47.161292Z","iopub.execute_input":"2022-07-13T01:52:47.162318Z","iopub.status.idle":"2022-07-13T01:52:47.222934Z","shell.execute_reply.started":"2022-07-13T01:52:47.162276Z","shell.execute_reply":"2022-07-13T01:52:47.221946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### copy models\n### 192x192x144 alldata 886\n!cp -rf /kaggle/input/nnunet-3d-fullres-192x192x144-alldata/nnUNetTrainerV2_largeunet__nnUNetPlansv2.1_bs2_ps192x192x144/ /tmp/input/nnUNet_trained_models/nnUNet/3d_fullres/Task522_UWMGITImageSegmentation/\n### 256x256x144 alldata finetune 804e lb=xx\n!cp -rf /kaggle/input/nnunet-3d-fullres-256x256x144-ft-804e/nnUNetTrainerV2_largeunet__nnUNetPlansv2.1_bs2_ps256x256x144_804e/ /tmp/input/nnUNet_trained_models/nnUNet/3d_fullres/Task522_UWMGITImageSegmentation/\n### 160x160x80 alldata  lb=xx\n!cp -rf /kaggle/input/nnunet-3d-fullres-160x160x80-alldata/nnUNetTrainerV2_largeunet__nnUNetPlansv2.1_bs2_ps160x160x80/ /tmp/input/nnUNet_trained_models/nnUNet/3d_fullres/Task522_UWMGITImageSegmentation/\n### 192x912x144 fold0 lb=880\n!cp -rf /kaggle/input/nnunet-3d-fullres-192x192x144-f0/nnUNetTrainerV2_largeunet__nnUNetPlansv2.1_bs2_ps192x192x144/ /tmp/input/nnUNet_trained_models/nnUNet/3d_fullres/Task522_UWMGITImageSegmentation/\n### 320x320x96 fold0 lb=876\n!cp -rf /kaggle/input/nnunet-3d-fullres-320x320x96-f0/nnUNetTrainerV2__nnUNetPlansv2.1_bs4_ps320x320x96/ /tmp/input/nnUNet_trained_models/nnUNet/3d_fullres/Task522_UWMGITImageSegmentation/\n","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:52:47.224132Z","iopub.execute_input":"2022-07-13T01:52:47.227106Z","iopub.status.idle":"2022-07-13T01:53:17.040306Z","shell.execute_reply.started":"2022-07-13T01:52:47.227070Z","shell.execute_reply":"2022-07-13T01:53:17.039087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Batch Inference**","metadata":{}},{"cell_type":"code","source":"total_size = len(test_patients)\nbs = 64\n\nif total_size % bs == 0:\n    l = total_size // bs\nelse:\n    l = total_size // bs + 1\nprint(f'bs = {bs}, l = {l}, total_size = {total_size}')\nfor idx in range(l):\n    batch_inference(idx, bs, total_size)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:53:17.043006Z","iopub.execute_input":"2022-07-13T01:53:17.043659Z","iopub.status.idle":"2022-07-13T01:57:38.860254Z","shell.execute_reply.started":"2022-07-13T01:53:17.043621Z","shell.execute_reply":"2022-07-13T01:57:38.858981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Ensemble**","metadata":{}},{"cell_type":"code","source":"### pred dir\npred_dir0 = f'/tmp/output/segmented/all_0/' ### 192x192x144 lb=886\npred_dir1 = f'/tmp/output/segmented/all_1/' ### 256x256x144 \npred_dir2 = f'/tmp/output/segmented/all_2/' ### 160x160x80 \npred_dir3 = f'/tmp/output/segmented/fold_0/' ### 192x192x144 lb=880\npred_dir4 = f'/tmp/output/segmented/fold_1/' ### 320x320x96 lb=876\n\n\n\npred_paths_list = sorted(glob.glob(f'{pred_dir0}*.nii.gz'))\nprint(len(pred_paths_list))","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:38.862119Z","iopub.execute_input":"2022-07-13T01:57:38.862505Z","iopub.status.idle":"2022-07-13T01:57:38.871840Z","shell.execute_reply.started":"2022-07-13T01:57:38.862468Z","shell.execute_reply":"2022-07-13T01:57:38.870632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /tmp/output/segmented/all_0/","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:38.873589Z","iopub.execute_input":"2022-07-13T01:57:38.874175Z","iopub.status.idle":"2022-07-13T01:57:39.555424Z","shell.execute_reply.started":"2022-07-13T01:57:38.874140Z","shell.execute_reply":"2022-07-13T01:57:39.554368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /tmp/output/segmented/all_1/","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:39.558782Z","iopub.execute_input":"2022-07-13T01:57:39.559080Z","iopub.status.idle":"2022-07-13T01:57:40.230851Z","shell.execute_reply.started":"2022-07-13T01:57:39.559053Z","shell.execute_reply":"2022-07-13T01:57:40.229727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Check results**","metadata":{}},{"cell_type":"code","source":"from nnunet.preprocessing.preprocessing import get_lowres_axis, get_do_separate_z, resample_data_or_seg\nfrom batchgenerators.augmentations.utils import resize_segmentation\nimport skimage.morphology as sm\n\ndef load_nnunet_npz(pred_dir,image_name):\n    logit_path = f'{pred_dir}/{image_name}.npz'\n    props_path = f'{pred_dir}/{image_name}.pkl'\n    logits = np.load(logit_path)['softmax'] # num_classes + 1,w,h,d\n    props = [load_pickle(props_path)]\n    \n    reg_class_orders = [p['regions_class_order'] if 'regions_class_order' in p.keys() else None for p in props]\n    if not all([i is None for i in reg_class_orders]):\n        # if reg_class_orders are not None then they must be the same in all pkls\n        tmp = reg_class_orders[0]\n        for r in reg_class_orders[1:]:\n            assert tmp == r, 'If merging files with regions_class_order, the regions_class_orders of all ' \\\n                                 'files must be the same. regions_class_order: %s, \\n files: %s' % \\\n                                 (str(reg_class_orders), str(files))\n        regions_class_order = tmp\n    else:\n        regions_class_order = None\n    \n    segmentation_softmax = logits\n    properties_dict = props[0]\n    \n    # first resample, then put result into bbox of cropping, then save\n    current_shape = segmentation_softmax.shape\n    shape_original_after_cropping = properties_dict.get('size_after_cropping')\n    shape_original_before_cropping = properties_dict.get('original_size_of_raw_data')\n    if np.any([i != j for i, j in zip(np.array(current_shape[1:]), np.array(shape_original_after_cropping))]):\n        if force_separate_z is None:\n            if get_do_separate_z(properties_dict.get('original_spacing')):\n                do_separate_z = True\n                lowres_axis = get_lowres_axis(properties_dict.get('original_spacing'))\n            elif get_do_separate_z(properties_dict.get('spacing_after_resampling')):\n                do_separate_z = True\n                lowres_axis = get_lowres_axis(properties_dict.get('spacing_after_resampling'))\n            else:\n                do_separate_z = False\n                lowres_axis = None\n        else:\n            do_separate_z = force_separate_z\n            if do_separate_z:\n                lowres_axis = get_lowres_axis(properties_dict.get('original_spacing'))\n            else:\n                lowres_axis = None\n\n        if lowres_axis is not None and len(lowres_axis) != 1:\n            # this happens for spacings like (0.24, 1.25, 1.25) for example. In that case we do not want to resample\n            # separately in the out of plane axis\n            do_separate_z = False\n\n        print(\"separate z:\", do_separate_z, \"lowres axis\", lowres_axis)\n        seg_old_spacing = resample_data_or_seg(segmentation_softmax, shape_original_after_cropping, is_seg=False,\n                                               axis=lowres_axis, order=order, do_separate_z=do_separate_z,\n                                               order_z=interpolation_order_z)\n        # seg_old_spacing = resize_softmax_output(segmentation_softmax, shape_original_after_cropping, order=order)\n    else:\n        print(\"no resampling necessary\")\n        seg_old_spacing = segmentation_softmax\n    \n    resize_shape_original_before_cropping = (4,shape_original_before_cropping[0],shape_original_before_cropping[1], shape_original_before_cropping[2])\n    \n    resize_seg_old_spacing = resize_segmentation(seg_old_spacing, resize_shape_original_before_cropping, 0)\n    \n    return resize_seg_old_spacing","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:40.232953Z","iopub.execute_input":"2022-07-13T01:57:40.233323Z","iopub.status.idle":"2022-07-13T01:57:40.841719Z","shell.execute_reply.started":"2022-07-13T01:57:40.233288Z","shell.execute_reply":"2022-07-13T01:57:40.840804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### check segmented output\nimport cv2\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\nimport SimpleITK as sitk\n\nclasses = ['large_bowel', 'small_bowel', 'stomach']\n\ndef show_img(img, mask=None):\n    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))\n    img = clahe.apply(img)\n#     plt.figure(figsize=(10,10))\n    plt.imshow(img, cmap='bone')\n    \n    if mask is not None:\n        mask_lb = np.where(mask==1,1,0)\n        mask_sb = np.where(mask==2,1,0)\n        mask_st = np.where(mask==3,1,0)\n        \n        mask = np.stack((mask_lb,mask_sb,mask_st),-1)*255.0\n#         mask = np.transpose(mask,(1,2,0))*255.0\n        plt.imshow(mask, alpha=0.5)\n        handles = [Rectangle((0,0),1,1, color=_c) for _c in [(0.667,0.0,0.0), (0.0,0.667,0.0), (0.0,0.0,0.667)]]\n        labels = classes\n        plt.legend(handles,labels)\n    plt.axis('off')\n\ndef get_array(fn):\n    \"opens .nii file and return the array\"\n    img = sitk.ReadImage(str(fn))\n    imgd = sitk.GetArrayFromImage(img)\n    return imgd\n\ndef dice(im1, im2, empty_score=1.0):\n    im1 = np.asarray(im1).astype(np.bool)\n    im2 = np.asarray(im2).astype(np.bool)\n\n    if im1.shape != im2.shape:\n        raise ValueError(\"Shape mismatch: im1 and im2 must have the same shape.\")\n\n    im_sum = im1.sum() + im2.sum()\n    if im_sum == 0:\n        return empty_score\n\n    # Compute Dice coefficient\n    intersection = np.logical_and(im1, im2)\n\n    return 2. * intersection.sum() / im_sum\n\n# image_ids = ['case36_day14','case36_day16','case36_day0','case36_day10','case36_day6','case36_day8']   \n# image_ids = ['case36_day14','case36_day16']   \n# th_list = [0.5,0.4,0.4,0.4]\n# weights = [0.4,0.3,0.15,0.1,0.05]\n# if df_test.shape[0] == 0:\n#     slice_id = 88\n#     dice_scores = []\n#     ensemble_dice_scores = []\n#     for image_id in image_ids:\n#         image_name = f'{image_id}.nii.gz'\n#         image_path = f'{imagestr}/{image_id}_0000.nii.gz'\n#         mask_path = f'{labelstr}/{image_id}.nii.gz'\n#         pred_path = f'{pred_dir0}/{image_id}.nii.gz'\n        \n#         images = get_array(image_path) ## w h d\n#         masks = get_array(mask_path) ## w h d\n#         preds = get_array(pred_path) ## w h d\n        \n#         logits0 = load_nnunet_npz(pred_dir0,image_id)\n#         logits1 = load_nnunet_npz(pred_dir1,image_id)\n#         logits2 = load_nnunet_npz(pred_dir2,image_id)\n#         logits3 = load_nnunet_npz(pred_dir3,image_id)\n#         logits4 = load_nnunet_npz(pred_dir4,image_id)\n\n\n#         images = np.transpose(images,(1,0,2)) ## w h d -> h w d\n#         masks = np.transpose(masks,(1,0,2)) ## w h d -> h w d\n#         preds = np.transpose(preds,(1,0,2)) ## w h d -> h w d\n        \n#         logits0 = np.transpose(logits0,(0,2,1,3)) ## 4, w h d -> 4, h w d\n#         logits1 = np.transpose(logits1,(0,2,1,3)) ## 4, w h d -> 4, h w d\n#         logits2 = np.transpose(logits2,(0,2,1,3)) ## 4, w h d -> 4, h w d\n#         logits3 = np.transpose(logits3,(0,2,1,3)) ## 4, w h d -> 4, h w d\n#         logits4 = np.transpose(logits4,(0,2,1,3)) ## 4, w h d -> 4, h w d\n\n\n#         image = images[:,:,slice_id]\n#         mask = masks[:,:,slice_id]\n#         pred = preds[:,:,slice_id]\n        \n#         ensemble_logits = weights[0]*logits0 + weights[1]*logits1 + weights[2]*logits2 + weights[3]*logits3 + weights[4]*logits4\n# #         ensemble_logits = logits2\n# #         ensemble_preds = ensemble_logits.argmax(0)\n#         ensemble_preds_final = np.zeros(ensemble_logits.shape[1:])\n#         for i, c in enumerate([0,1,2,3]):\n#             ensemble_preds_final[ensemble_logits[i] > th_list[i]] = c\n#         ensemble_preds = ensemble_preds_final\n#         ensemble_pred = ensemble_preds[:,:,slice_id]\n        \n#         ### compute dice score\n#         for i in range(masks.shape[2]):\n#             temp_mask = masks[:,:,i]\n#             temp_pred = preds[:,:,i]\n#             temp_ensemble_pred = ensemble_preds[:,:,i]\n\n#             dice_score = dice(temp_pred, temp_mask)\n#             ensemble_dice_score = dice(temp_ensemble_pred, temp_mask)\n\n#             sum_mask = np.sum(temp_mask) + np.sum(temp_pred)\n#             sum_ensemble_mask = np.sum(temp_mask) + np.sum(temp_ensemble_pred)\n# #             print(f'slice_{i+1:04d} dice_score={dice_score:03f},ensemble_dice_score={ensemble_dice_score:03f}')\n#             if sum_mask != 0:\n#                 dice_scores.append(dice_score)\n#             if sum_ensemble_mask != 0:\n#                 ensemble_dice_scores.append(ensemble_dice_score)\n                \n#     print(f'{image_id} dice_score is {np.mean(dice_scores)}')\n#     print(f'{image_id} ensemble_dice_score is {np.mean(ensemble_dice_scores)}')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:40.843410Z","iopub.execute_input":"2022-07-13T01:57:40.843756Z","iopub.status.idle":"2022-07-13T01:57:41.055798Z","shell.execute_reply.started":"2022-07-13T01:57:40.843721Z","shell.execute_reply":"2022-07-13T01:57:41.054930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if df_test.shape[0] == 0:    \n#     show_img(image,mask)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:41.057382Z","iopub.execute_input":"2022-07-13T01:57:41.057733Z","iopub.status.idle":"2022-07-13T01:57:41.062224Z","shell.execute_reply.started":"2022-07-13T01:57:41.057698Z","shell.execute_reply":"2022-07-13T01:57:41.061294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if df_test.shape[0] == 0:\n#     show_img(image,pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:41.063679Z","iopub.execute_input":"2022-07-13T01:57:41.064311Z","iopub.status.idle":"2022-07-13T01:57:41.073226Z","shell.execute_reply.started":"2022-07-13T01:57:41.064278Z","shell.execute_reply":"2022-07-13T01:57:41.072319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# if df_test.shape[0] == 0:\n#     show_img(image,ensemble_pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:41.074527Z","iopub.execute_input":"2022-07-13T01:57:41.075028Z","iopub.status.idle":"2022-07-13T01:57:41.082991Z","shell.execute_reply.started":"2022-07-13T01:57:41.074994Z","shell.execute_reply":"2022-07-13T01:57:41.082025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submit**","metadata":{}},{"cell_type":"code","source":"def rle_decode(mask_rle, shape):\n    s = np.array(mask_rle.split(), dtype=int)\n    starts, lengths = s[0::2] - 1, s[1::2]\n    ends = starts + lengths\n    h, w = shape\n    img = np.zeros((h * w,), dtype = np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo : hi] = 1\n    return img.reshape(shape)\n\ndef rle_encode(img):\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:41.084517Z","iopub.execute_input":"2022-07-13T01:57:41.084915Z","iopub.status.idle":"2022-07-13T01:57:41.095809Z","shell.execute_reply.started":"2022-07-13T01:57:41.084862Z","shell.execute_reply":"2022-07-13T01:57:41.094807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nclass_strs = []\npredicteds = []\n\nth_list = [0.5,0.4,0.4,0.4]\nweights = [0.4,0.3,0.15,0.1,0.05]\nprint(sum(weights))\nfor index, pred_path in tqdm(enumerate(pred_paths_list),total=len(pred_paths_list)):\n    image_id = pred_path.split('/')[-1].split('.')[0]\n    \n    logits0 = load_nnunet_npz(pred_dir0,image_id)\n    logits1 = load_nnunet_npz(pred_dir1,image_id)\n    logits2 = load_nnunet_npz(pred_dir2,image_id)\n    logits3 = load_nnunet_npz(pred_dir3,image_id)\n    logits4 = load_nnunet_npz(pred_dir4,image_id)\n\n    logits0 = np.transpose(logits0,(0,2,1,3)) ## 4, w h d -> 4, h w d\n    logits1 = np.transpose(logits1,(0,2,1,3)) ## 4, w h d -> 4, h w d\n    logits2 = np.transpose(logits2,(0,2,1,3)) ## 4, w h d -> 4, h w d\n    logits3 = np.transpose(logits3,(0,2,1,3)) ## 4, w h d -> 4, h w d\n    logits4 = np.transpose(logits4,(0,2,1,3)) ## 4, w h d -> 4, h w d\n    \n    ensemble_logits = weights[0]*logits0 + weights[1]*logits1 + weights[2]*logits2 + weights[3]*logits3 + weights[4]*logits4\n    ensemble_preds_final = np.zeros(ensemble_logits.shape[1:])\n    for i, c in enumerate([0,1,2,3]):\n        ensemble_preds_final[ensemble_logits[i] > th_list[i]] = c\n    pred_masks = ensemble_preds_final\n    for i in range(pred_masks.shape[2]):\n        slice_id = f'slice_{i+1:04d}'\n        idstr = f'{image_id}_{slice_id}'\n        pred_mask = pred_masks[:,:,i] ## w,h\n        for j in range(3):\n            # lb ,sb, st\n            ids.append(idstr)\n            p_mask = np.where(pred_mask==(j+1),1,0)\n            rle = rle_encode(p_mask)\n            class_strs.append(classes[j])\n            predicteds.append(rle)\n    del pred_masks","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:57:41.097292Z","iopub.execute_input":"2022-07-13T01:57:41.097715Z","iopub.status.idle":"2022-07-13T01:59:51.209248Z","shell.execute_reply.started":"2022-07-13T01:57:41.097682Z","shell.execute_reply":"2022-07-13T01:59:51.208193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ids = []\n# class_strs = []\n# predicteds = []\n# for index, pred_path in tqdm(enumerate(pred_paths_list),total=len(pred_paths_list)):\n#     image_id = pred_path.split('/')[-1].split('.')[0] \n#     pred_masks = get_array(pred_path) ## w h d\n#     pred_masks = np.transpose(pred_masks,(1,0,2)) ## w h d -> h w d\n#     for i in range(pred_masks.shape[2]):\n#         slice_id = f'slice_{i+1:04d}'\n#         idstr = f'{image_id}_{slice_id}'\n#         pred_mask = pred_masks[:,:,i] ## w,h\n#         for j in range(3):\n#             # lb ,sb, st\n#             ids.append(idstr)\n#             p_mask = np.where(pred_mask==(j+1),1,0)\n#             rle = rle_encode(p_mask)\n#             class_strs.append(classes[j])\n#             predicteds.append(rle)\n#     del pred_masks","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:59:51.210817Z","iopub.execute_input":"2022-07-13T01:59:51.212158Z","iopub.status.idle":"2022-07-13T01:59:51.218119Z","shell.execute_reply.started":"2022-07-13T01:59:51.212116Z","shell.execute_reply":"2022-07-13T01:59:51.216813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df = pd.DataFrame({\n    \"id\":ids,\n    \"class\":class_strs,\n    \"predicted\":predicteds\n})\npred_df","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:59:51.219466Z","iopub.execute_input":"2022-07-13T01:59:51.219920Z","iopub.status.idle":"2022-07-13T01:59:51.244801Z","shell.execute_reply.started":"2022-07-13T01:59:51.219857Z","shell.execute_reply":"2022-07-13T01:59:51.243896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /tmp/input/\n!rm -rf /tmp/nnUNet/\n!rm -rf /tmp/output/\n!rm -rf /tmp/nnUNet_dep_libs/","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:59:51.251292Z","iopub.execute_input":"2022-07-13T01:59:51.251674Z","iopub.status.idle":"2022-07-13T01:59:54.504218Z","shell.execute_reply.started":"2022-07-13T01:59:51.251649Z","shell.execute_reply":"2022-07-13T01:59:54.503064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if df_test.shape[0] != 0:\n    sub_df = pd.read_csv('/kaggle/input/uw-madison-gi-tract-image-segmentation/sample_submission.csv')\n    del sub_df['predicted']\n    sub_df = sub_df.merge(pred_df, on=['id','class'])\nelse:\n    sub_df = pred_df\n\nsub_df.to_csv('/kaggle/working/submission.csv',index=False)\ndisplay(sub_df.head())","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:59:54.505951Z","iopub.execute_input":"2022-07-13T01:59:54.506672Z","iopub.status.idle":"2022-07-13T01:59:54.536734Z","shell.execute_reply.started":"2022-07-13T01:59:54.506628Z","shell.execute_reply":"2022-07-13T01:59:54.535892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df[sub_df.id == 'case36_day14_slice_0065']","metadata":{"execution":{"iopub.status.busy":"2022-07-13T01:59:54.538140Z","iopub.execute_input":"2022-07-13T01:59:54.538472Z","iopub.status.idle":"2022-07-13T01:59:54.554031Z","shell.execute_reply.started":"2022-07-13T01:59:54.538439Z","shell.execute_reply":"2022-07-13T01:59:54.553164Z"},"trusted":true},"execution_count":null,"outputs":[]}]}