{"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":"In this notebook, we will see how we can make use nnUnet to make masks for this competition. \n\nWhat is [nnUnet](https://github.com/MIC-DKFZ/nnUNet)?\n>nnU-Net is the first segmentation method that is designed to deal with the dataset diversity found in the domain (medical). It condenses and automates the key decisions for designing a successful segmentation pipeline for any given dataset. \nnnUnet was published in [Nature](https://www.nature.com/articles/s41592-020-01008-z)\n\nAbstract from nature:\n>Biomedical imaging is a driver of scientific discovery and a core component of medical care and is being stimulated by the field of deep learning. While semantic segmentation algorithms enable image analysis and quantification in many applications, the design of respective specialized solutions is non-trivial and highly dependent on dataset properties and hardware conditions. We developed nnU-Net, a deep learning-based segmentation method that automatically configures itself, including preprocessing, network architecture, training and post-processing for any new task. The key design choices in this process are modeled as a set of fixed parameters, interdependent rules and empirical decisions. Without manual intervention, nnU-Net surpasses most existing approaches, including highly specialized solutions on 23 public datasets used in international biomedical segmentation competitions. We make nnU-Net publicly available as an out-of-the-box tool, rendering state-of-the-art segmentation accessible to a broad audience by requiring neither expert knowledge nor computing resources beyond standard network training.\n\nnnUnet lead the [Medical Decathlon competition](http://medicaldecathlon.com/results/).  \n\nWe will make use of nnUnet's Task01-Brain Tumouur Segmentation pretrained model to generate masks. \n\nWe will follow the following steps\n1. Select 5 random patients\n2. Resample the images using SimpleITK and SIR24 dataset - I have a [notebook](https://www.kaggle.com/marshath/btrg-dicom-to-nifti-using-sri24-to-resample) completely for this\n3. Git clone and set up nnUnet - This [notebook](https://github.com/prateekgupta891/nnUNet/blob/master/nnunetmec2020.ipynb) here was my light\n4. Prepare the data as per nnUnet's requirement\n5. Make masks!","metadata":{}},{"cell_type":"markdown","source":"# 0. Imports and paths","metadata":{}},{"cell_type":"code","source":"import os\nfrom fastai.vision.all import *\nimport SimpleITK as sitk\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:21:18.94426Z","iopub.execute_input":"2021-09-03T12:21:18.944692Z","iopub.status.idle":"2021-09-03T12:21:22.632521Z","shell.execute_reply.started":"2021-09-03T12:21:18.944602Z","shell.execute_reply":"2021-09-03T12:21:22.631453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train')","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:21:22.634407Z","iopub.execute_input":"2021-09-03T12:21:22.634853Z","iopub.status.idle":"2021-09-03T12:21:22.640218Z","shell.execute_reply.started":"2021-09-03T12:21:22.634809Z","shell.execute_reply":"2021-09-03T12:21:22.638914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir t1w_nifti\n!mkdir t1wce_nifti\n!mkdir t2w_nifti\n!mkdir flair_nifti","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:21:22.642997Z","iopub.execute_input":"2021-09-03T12:21:22.643645Z","iopub.status.idle":"2021-09-03T12:21:25.880562Z","shell.execute_reply.started":"2021-09-03T12:21:22.643599Z","shell.execute_reply":"2021-09-03T12:21:25.879164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Select 5 random patients ","metadata":{}},{"cell_type":"code","source":"samples = [Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00002'),\n           Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00194'),\n           Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00094'),\n           Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00734'),\n           Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00526')]","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:21:25.883159Z","iopub.execute_input":"2021-09-03T12:21:25.883635Z","iopub.status.idle":"2021-09-03T12:21:25.89357Z","shell.execute_reply.started":"2021-09-03T12:21:25.883586Z","shell.execute_reply":"2021-09-03T12:21:25.892431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_train_t2w, path_train_t1wce,path_train_t1w,path_train_flair = [],[],[],[]\nfor each in samples:\n    path_train_t2w.append(each.ls()[0])\n    path_train_t1wce.append(each.ls()[1])\n    path_train_t1w.append(each.ls()[2])\n    path_train_flair.append(each.ls()[3])","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:21:25.895549Z","iopub.execute_input":"2021-09-03T12:21:25.896126Z","iopub.status.idle":"2021-09-03T12:21:25.938193Z","shell.execute_reply.started":"2021-09-03T12:21:25.896049Z","shell.execute_reply":"2021-09-03T12:21:25.937074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dicom2nifti(image_dir, out_dir, save=True):\n    \"given a dicom directory, loads them into single file and can save it as .nii file\"\n    reader = sitk.ImageSeriesReader()\n    reader.LoadPrivateTagsOn()\n    filenamesDICOM = reader.GetGDCMSeriesFileNames(str(image_dir))\n    reader.SetFileNames(filenamesDICOM)\n    img = reader.Execute()\n    img = sitk.Cast(img, sitk.sitkFloat32)\n    \n    if save:\n        sitk.WriteImage(img, f'{out_dir}/{image_dir.parent.name}.nii')\n    else:\n        return img","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:21:25.939905Z","iopub.execute_input":"2021-09-03T12:21:25.940373Z","iopub.status.idle":"2021-09-03T12:21:25.948716Z","shell.execute_reply.started":"2021-09-03T12:21:25.940322Z","shell.execute_reply":"2021-09-03T12:21:25.946922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's convert the images into nifti","metadata":{}},{"cell_type":"code","source":"for fn in path_train_t1w: dicom2nifti(fn, \"/kaggle/working/t1w_nifti/\")\nfor fn in path_train_t1wce: dicom2nifti(fn, \"/kaggle/working/t1wce_nifti/\")\nfor fn in path_train_t2w: dicom2nifti(fn, \"/kaggle/working/t2w_nifti/\")\nfor fn in path_train_flair: dicom2nifti(fn, \"/kaggle/working/flair_nifti/\")","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:21:25.950817Z","iopub.execute_input":"2021-09-03T12:21:25.951382Z","iopub.status.idle":"2021-09-03T12:22:10.475179Z","shell.execute_reply.started":"2021-09-03T12:21:25.951335Z","shell.execute_reply":"2021-09-03T12:22:10.473954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's take a look at the images","metadata":{}},{"cell_type":"code","source":"def 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 plot_slice(imgd, sli):\n    \"given an image of shape slices x height x width, plots a slice\"\n    plt.imshow(imgd[sli], cmap='gray')\n    plt.axis('off')\n    \ndef get_array_plot(fn, sli):\n    imgd = get_array(fn)\n    plot_slice(imgd, sli)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:10.479263Z","iopub.execute_input":"2021-09-03T12:22:10.479693Z","iopub.status.idle":"2021-09-03T12:22:10.488392Z","shell.execute_reply.started":"2021-09-03T12:22:10.479656Z","shell.execute_reply":"2021-09-03T12:22:10.487111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_pat = '00094'","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:10.491739Z","iopub.execute_input":"2021-09-03T12:22:10.492281Z","iopub.status.idle":"2021-09-03T12:22:10.504329Z","shell.execute_reply.started":"2021-09-03T12:22:10.492231Z","shell.execute_reply":"2021-09-03T12:22:10.503082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(Path(f'./t1w_nifti/{samp_pat}.nii'), 20)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:10.506284Z","iopub.execute_input":"2021-09-03T12:22:10.506754Z","iopub.status.idle":"2021-09-03T12:22:10.745257Z","shell.execute_reply.started":"2021-09-03T12:22:10.506707Z","shell.execute_reply":"2021-09-03T12:22:10.744096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(Path(f'./t1wce_nifti/{samp_pat}.nii'), 45)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:10.746944Z","iopub.execute_input":"2021-09-03T12:22:10.74739Z","iopub.status.idle":"2021-09-03T12:22:11.214352Z","shell.execute_reply.started":"2021-09-03T12:22:10.747343Z","shell.execute_reply":"2021-09-03T12:22:11.212965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(Path(f'./t2w_nifti/{samp_pat}'), 120)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:11.216337Z","iopub.execute_input":"2021-09-03T12:22:11.216811Z","iopub.status.idle":"2021-09-03T12:22:12.412961Z","shell.execute_reply.started":"2021-09-03T12:22:11.216764Z","shell.execute_reply":"2021-09-03T12:22:12.411889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(Path(f'./flair_nifti/{samp_pat}'), 80)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:12.414659Z","iopub.execute_input":"2021-09-03T12:22:12.415125Z","iopub.status.idle":"2021-09-03T12:22:12.903613Z","shell.execute_reply.started":"2021-09-03T12:22:12.415076Z","shell.execute_reply":"2021-09-03T12:22:12.902545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. Resample the images using SimpleITK and SIR24 dataset","metadata":{}},{"cell_type":"markdown","source":"As we can see, for orientations are different across the different modalities. Let's resample using SRI24 as the template. ","metadata":{}},{"cell_type":"code","source":"def resample_nifti(image_dir, ref_image, fn, save=True):\n    \"resample using a reference image\"\n\n    image = sitk.ReadImage(str(image_dir), sitk.sitkFloat32)\n    \n    initial_transform = sitk.CenteredTransformInitializer(ref_image, \n                                                          image, \n                                                          sitk.Euler3DTransform(), \n                                                          sitk.CenteredTransformInitializerFilter.GEOMETRY)\n\n    resampler = sitk.ResampleImageFilter()\n    resampler.SetReferenceImage(ref_image)\n    resampler.SetInterpolator(sitk.sitkLinear)\n    resampler.SetTransform(initial_transform)\n    resampler.SetOutputSpacing(ref_image.GetSpacing())\n    resampler.SetSize((ref_image.GetSize()))\n    resampler.SetOutputDirection(ref_image.GetDirection())\n    resampler.SetOutputOrigin(ref_image.GetOrigin())\n    resampler.SetDefaultPixelValue(image.GetPixelIDValue())\n    resamped_image = resampler.Execute(image)\n    \n    if save:\n        sitk.WriteImage(resamped_image, fn)\n\n    return resamped_image","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:12.905145Z","iopub.execute_input":"2021-09-03T12:22:12.905518Z","iopub.status.idle":"2021-09-03T12:22:12.915354Z","shell.execute_reply.started":"2021-09-03T12:22:12.905486Z","shell.execute_reply":"2021-09-03T12:22:12.91324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ref_image = sitk.ReadImage('../input/sri24-dataset/sri24/spgr.nii', sitk.sitkFloat32)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:12.917408Z","iopub.execute_input":"2021-09-03T12:22:12.917859Z","iopub.status.idle":"2021-09-03T12:22:13.1907Z","shell.execute_reply.started":"2021-09-03T12:22:12.917813Z","shell.execute_reply":"2021-09-03T12:22:13.189556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir t1w_resample\n!mkdir t1wce_resample\n!mkdir t2w_resample\n!mkdir flair_resample","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:13.194397Z","iopub.execute_input":"2021-09-03T12:22:13.19474Z","iopub.status.idle":"2021-09-03T12:22:16.492515Z","shell.execute_reply.started":"2021-09-03T12:22:13.194707Z","shell.execute_reply":"2021-09-03T12:22:16.491106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for fn in Path(\"./t1w_nifti\").ls():\n    pat_id = str(fn).split('/')[-1].split('.')[0]\n    final_fn = f\"/kaggle/working/t1w_resample/{pat_id}_0001.nii.gz\"\n    resample_nifti(fn, ref_image, final_fn, True)\n    os.remove(str(fn))","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:16.49666Z","iopub.execute_input":"2021-09-03T12:22:16.497022Z","iopub.status.idle":"2021-09-03T12:22:21.418302Z","shell.execute_reply.started":"2021-09-03T12:22:16.496984Z","shell.execute_reply":"2021-09-03T12:22:21.417154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for fn in Path(\"./t1wce_nifti\").ls():\n    pat_id = str(fn).split('/')[-1].split('.')[0]\n    final_fn = f\"/kaggle/working/t1wce_resample/{pat_id}_0002.nii.gz\"\n    resample_nifti(fn, ref_image, final_fn, True)\n    os.remove(str(fn))","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:21.41986Z","iopub.execute_input":"2021-09-03T12:22:21.420301Z","iopub.status.idle":"2021-09-03T12:22:26.936432Z","shell.execute_reply.started":"2021-09-03T12:22:21.420255Z","shell.execute_reply":"2021-09-03T12:22:26.935299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for fn in Path(\"./t2w_nifti\").ls():\n    pat_id = str(fn).split('/')[-1].split('.')[0]\n    final_fn = f\"/kaggle/working/t2w_resample/{pat_id}_0003.nii.gz\"\n    resample_nifti(fn, ref_image, final_fn, True)\n    os.remove(str(fn))","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:26.937996Z","iopub.execute_input":"2021-09-03T12:22:26.93847Z","iopub.status.idle":"2021-09-03T12:22:34.992728Z","shell.execute_reply.started":"2021-09-03T12:22:26.938421Z","shell.execute_reply":"2021-09-03T12:22:34.991557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for fn in Path(\"./flair_nifti\").ls():\n    pat_id = str(fn).split('/')[-1].split('.')[0]\n    final_fn = f\"/kaggle/working/flair_resample/{pat_id}_0000.nii.gz\"\n    resample_nifti(fn, ref_image, final_fn, True)\n    os.remove(str(fn))","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:34.994484Z","iopub.execute_input":"2021-09-03T12:22:34.994947Z","iopub.status.idle":"2021-09-03T12:22:40.551619Z","shell.execute_reply.started":"2021-09-03T12:22:34.994898Z","shell.execute_reply":"2021-09-03T12:22:40.550469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(Path(f'/kaggle/working/t1w_resample/{samp_pat}_0001.nii.gz'), 85)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:40.5532Z","iopub.execute_input":"2021-09-03T12:22:40.553621Z","iopub.status.idle":"2021-09-03T12:22:40.829684Z","shell.execute_reply.started":"2021-09-03T12:22:40.553576Z","shell.execute_reply":"2021-09-03T12:22:40.828294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(Path(f'/kaggle/working/t1wce_resample/{samp_pat}_0002.nii.gz'), 85)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:40.831503Z","iopub.execute_input":"2021-09-03T12:22:40.831974Z","iopub.status.idle":"2021-09-03T12:22:41.114678Z","shell.execute_reply.started":"2021-09-03T12:22:40.831926Z","shell.execute_reply":"2021-09-03T12:22:41.113335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(Path(f'/kaggle/working/t2w_resample/{samp_pat}_0003.nii.gz'), 85)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:41.12046Z","iopub.execute_input":"2021-09-03T12:22:41.120788Z","iopub.status.idle":"2021-09-03T12:22:41.390885Z","shell.execute_reply.started":"2021-09-03T12:22:41.120756Z","shell.execute_reply":"2021-09-03T12:22:41.389349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(Path(f'/kaggle/working/flair_resample/{samp_pat}_0000.nii.gz'), 85)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:41.393187Z","iopub.execute_input":"2021-09-03T12:22:41.393653Z","iopub.status.idle":"2021-09-03T12:22:41.677984Z","shell.execute_reply.started":"2021-09-03T12:22:41.393598Z","shell.execute_reply":"2021-09-03T12:22:41.676594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf t1w_nifti\n!rm -rf t1wce_nifti\n!rm -rf t2w_nifti\n!rm -rf flair_nifti","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:41.679942Z","iopub.execute_input":"2021-09-03T12:22:41.680437Z","iopub.status.idle":"2021-09-03T12:22:45.251039Z","shell.execute_reply.started":"2021-09-03T12:22:41.680389Z","shell.execute_reply":"2021-09-03T12:22:45.249569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Looks like we have resampled them. If you noticed, we added four digits to the file names. This is because nnUnet (Task01 - Brain Tumour) requires all four modalities \n\n`0000 - flair, 0001 - T1w, 0002 - T1wce, 0003 - T2w`","metadata":{}},{"cell_type":"markdown","source":"# 3. Git clone and set up nnUnet","metadata":{}},{"cell_type":"markdown","source":"`labels: {'0': 'background',  '1': 'edema', '2': 'non-enhancing tumor', '3': 'enhancing tumour'}` - following is the labels ","metadata":{}},{"cell_type":"code","source":"import os\nbase_dir = '/kaggle/working/'\nos.chdir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:45.253036Z","iopub.execute_input":"2021-09-03T12:22:45.253541Z","iopub.status.idle":"2021-09-03T12:22:45.262209Z","shell.execute_reply.started":"2021-09-03T12:22:45.253487Z","shell.execute_reply":"2021-09-03T12:22:45.259532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/MIC-DKFZ/nnUNet.git \n!git clone https://github.com/NVIDIA/apex","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:45.26429Z","iopub.execute_input":"2021-09-03T12:22:45.265108Z","iopub.status.idle":"2021-09-03T12:22:51.165575Z","shell.execute_reply.started":"2021-09-03T12:22:45.265012Z","shell.execute_reply":"2021-09-03T12:22:51.164305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"respository_dir = os.path.join(base_dir,'nnUNet')\nos.chdir(respository_dir)\n\n!pip install -e .\n#(optional installation)\n!pip install --upgrade git+https://github.com/nanohanno/hiddenlayer.git@bugfix/get_trace_graph#egg=hiddenlayer\n\nos.chdir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:22:51.167614Z","iopub.execute_input":"2021-09-03T12:22:51.168113Z","iopub.status.idle":"2021-09-03T12:23:34.829361Z","shell.execute_reply.started":"2021-09-03T12:22:51.16804Z","shell.execute_reply":"2021-09-03T12:23:34.828081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Note** Restart the notebook","metadata":{}},{"cell_type":"code","source":"#import IPython\n#IPython.Application.instance().kernel.do_shutdown(True)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:23:34.833611Z","iopub.execute_input":"2021-09-03T12:23:34.833962Z","iopub.status.idle":"2021-09-03T12:23:34.843759Z","shell.execute_reply.started":"2021-09-03T12:23:34.833926Z","shell.execute_reply":"2021-09-03T12:23:34.842543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom fastai.vision.all import *\nimport SimpleITK as sitk\nimport shutil\n\nbase_dir = '/kaggle/working/'\nos.chdir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:23:34.845276Z","iopub.execute_input":"2021-09-03T12:23:34.845722Z","iopub.status.idle":"2021-09-03T12:23:34.856781Z","shell.execute_reply.started":"2021-09-03T12:23:34.845675Z","shell.execute_reply":"2021-09-03T12:23:34.855495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset Folder Structure","metadata":{}},{"cell_type":"code","source":"def 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":"2021-09-03T12:23:34.858389Z","iopub.execute_input":"2021-09-03T12:23:34.858751Z","iopub.status.idle":"2021-09-03T12:23:34.871319Z","shell.execute_reply.started":"2021-09-03T12:23:34.858686Z","shell.execute_reply":"2021-09-03T12:23:34.868586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"task_name = 'Task001_BrainTumour' #change here for different task name\nnnunet_dir = \"nnUNet/nnunet/nnUNet_raw_data_base/nnUNet_raw_data\"\ntask_folder_name = os.path.join(nnunet_dir,task_name)\ntrain_image_dir = os.path.join(task_folder_name,'imagesTr')\ntrain_label_dir = os.path.join(task_folder_name,'labelsTr')\ntest_dir = os.path.join(task_folder_name,'imagesTs')\nmain_dir = os.path.join(base_dir,'nnUNet/nnunet')","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:23:34.873881Z","iopub.execute_input":"2021-09-03T12:23:34.874462Z","iopub.status.idle":"2021-09-03T12:23:34.886295Z","shell.execute_reply.started":"2021-09-03T12:23:34.874408Z","shell.execute_reply":"2021-09-03T12:23:34.885048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_if_dont_exist(task_folder_name,overwrite = False)\nmake_if_dont_exist(train_image_dir)\nmake_if_dont_exist(train_label_dir)\nmake_if_dont_exist(test_dir,overwrite= False)\nmake_if_dont_exist(os.path.join(main_dir,'nnunet_trained_models'))","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:23:34.88835Z","iopub.execute_input":"2021-09-03T12:23:34.888901Z","iopub.status.idle":"2021-09-03T12:23:34.902902Z","shell.execute_reply.started":"2021-09-03T12:23:34.888837Z","shell.execute_reply":"2021-09-03T12:23:34.901476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Environment Veriables","metadata":{}},{"cell_type":"code","source":"os.environ['nnUNet_raw_data_base'] = os.path.join(main_dir,'nnUNet_raw_data_base')\nos.environ['nnUNet_preprocessed'] = os.path.join(main_dir,'preprocessed')\nos.environ['RESULTS_FOLDER'] = os.path.join(main_dir,'nnUNet_trained_models')","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:23:34.906523Z","iopub.execute_input":"2021-09-03T12:23:34.908671Z","iopub.status.idle":"2021-09-03T12:23:34.916386Z","shell.execute_reply.started":"2021-09-03T12:23:34.908635Z","shell.execute_reply":"2021-09-03T12:23:34.915163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Download the pretrained model","metadata":{}},{"cell_type":"code","source":"#!nnUNet_print_available_pretrained_models\n!nnUNet_download_pretrained_model Task001_BrainTumour","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:23:34.920072Z","iopub.execute_input":"2021-09-03T12:23:34.920465Z","iopub.status.idle":"2021-09-03T12:25:05.952608Z","shell.execute_reply.started":"2021-09-03T12:23:34.920432Z","shell.execute_reply":"2021-09-03T12:25:05.951325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Prepare the data as per nnUnet's requirement","metadata":{}},{"cell_type":"code","source":"path_ts = '/kaggle/working/nnUNet/nnunet/nnUNet_raw_data_base/nnUNet_raw_data/Task001_BrainTumour/imagesTs'","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:25:05.95686Z","iopub.execute_input":"2021-09-03T12:25:05.957245Z","iopub.status.idle":"2021-09-03T12:25:05.965475Z","shell.execute_reply.started":"2021-09-03T12:25:05.957207Z","shell.execute_reply":"2021-09-03T12:25:05.964365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for fn in Path(\"./t1w_resample\").ls(): \n    pat_id = fn.name.split('.')[0].split('_')[0]\n    shutil.copy(f'/kaggle/working/t1w_resample/{pat_id}_0001.nii.gz', f'{path_ts}')\n    shutil.copy(f'/kaggle/working/t1wce_resample/{pat_id}_0002.nii.gz', f'{path_ts}/{pat_id}_0002.nii.gz')\n    shutil.copy(f'/kaggle/working/t2w_resample/{pat_id}_0003.nii.gz', f'{path_ts}/{pat_id}_0003.nii.gz')\n    shutil.copy(f'/kaggle/working/flair_resample/{pat_id}_0000.nii.gz', f'{path_ts}/{pat_id}_0000.nii.gz')\n    \n    os.remove(f'/kaggle/working/t1w_resample/{pat_id}_0001.nii.gz')\n    os.remove(f'/kaggle/working/t1wce_resample/{pat_id}_0002.nii.gz')\n    os.remove(f'/kaggle/working/t2w_resample/{pat_id}_0003.nii.gz')\n    os.remove(f'/kaggle/working/flair_resample/{pat_id}_0000.nii.gz')","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:25:05.968978Z","iopub.execute_input":"2021-09-03T12:25:05.969423Z","iopub.status.idle":"2021-09-03T12:25:07.766288Z","shell.execute_reply.started":"2021-09-03T12:25:05.969376Z","shell.execute_reply":"2021-09-03T12:25:07.765172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Inference - Make Masks","metadata":{}},{"cell_type":"code","source":"result_dir = os.path.join(main_dir,'nnUNet_Prediction_Results',task_name)\nmake_if_dont_exist(result_dir)\n\nteam_name = 'arshath'\n\n#location where you want save your results, will be created if dont exist\nos.chdir(main_dir)\n!nnUNet_predict -i nnUNet_raw_data_base/nnUNet_raw_data/Task001_BrainTumour/imagesTs -o nnUNet_Prediction_Results/Task001_BrainTumour -t 001 -tr nnUNetTrainerV2 -m 3d_fullres --disable_tta \nos.chdir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:25:07.767955Z","iopub.execute_input":"2021-09-03T12:25:07.768498Z","iopub.status.idle":"2021-09-03T12:29:00.969592Z","shell.execute_reply.started":"2021-09-03T12:25:07.768449Z","shell.execute_reply":"2021-09-03T12:29:00.968291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finally let's check the masks","metadata":{}},{"cell_type":"code","source":"import os\nfrom fastai.vision.all import *\nimport SimpleITK as sitk\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:32:17.772551Z","iopub.execute_input":"2021-09-03T12:32:17.77294Z","iopub.status.idle":"2021-09-03T12:32:19.428623Z","shell.execute_reply.started":"2021-09-03T12:32:17.772906Z","shell.execute_reply":"2021-09-03T12:32:19.427438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_ts = '/kaggle/working/nnUNet/nnunet/nnUNet_raw_data_base/nnUNet_raw_data/Task001_BrainTumour/imagesTs'\npath_lbl = '/kaggle/working/nnUNet/nnunet/nnUNet_Prediction_Results/Task001_BrainTumour'\nsamp_pat = '00094'","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:35:17.203472Z","iopub.execute_input":"2021-09-03T12:35:17.203871Z","iopub.status.idle":"2021-09-03T12:35:17.210152Z","shell.execute_reply.started":"2021-09-03T12:35:17.203823Z","shell.execute_reply":"2021-09-03T12:35:17.208917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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 plot_slice(imgd, sli):\n    \"given an image of shape slices x height x width, plots a slice\"\n    plt.imshow(imgd[sli], cmap='gray')\n    plt.axis('off')\n    \ndef get_array_plot(fn, sli):\n    imgd = get_array(fn)\n    plot_slice(imgd, sli)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:33:46.2891Z","iopub.execute_input":"2021-09-03T12:33:46.28949Z","iopub.status.idle":"2021-09-03T12:33:46.298395Z","shell.execute_reply.started":"2021-09-03T12:33:46.289456Z","shell.execute_reply":"2021-09-03T12:33:46.297259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(f'{path_ts}/{samp_pat}_0003.nii.gz', 85)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:35:42.017339Z","iopub.execute_input":"2021-09-03T12:35:42.017758Z","iopub.status.idle":"2021-09-03T12:35:42.374077Z","shell.execute_reply.started":"2021-09-03T12:35:42.017725Z","shell.execute_reply":"2021-09-03T12:35:42.372563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_array_plot(f'{path_lbl}/{samp_pat}.nii.gz', 85)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T12:35:46.391946Z","iopub.execute_input":"2021-09-03T12:35:46.392382Z","iopub.status.idle":"2021-09-03T12:35:46.517425Z","shell.execute_reply.started":"2021-09-03T12:35:46.392339Z","shell.execute_reply":"2021-09-03T12:35:46.515907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}