{"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":"<img src=\"https://raw.githubusercontent.com/Project-MONAI/MONAI/dev/docs/images/MONAI-logo-color.png\" style=\"width: 300px\"/><br/>\nThis notebook refferred to the Colab Notebook, \"Getting Started with MONAI\".<br/>\nhttps://colab.research.google.com/github/Project-MONAI/MONAIBootcamp2021/blob/master/day1/1.%20Getting%20Started%20with%20MONAI.ipynb","metadata":{"id":"gwHMquBHfbFa","papermill":{"duration":0.029678,"end_time":"2021-11-07T09:07:09.513318","exception":false,"start_time":"2021-11-07T09:07:09.48364","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"https://github.com/Project-MONAI/MONAI<br/>","metadata":{}},{"cell_type":"code","source":"!pip install -qU \"monai[ignite, nibabel, torchvision, tqdm]==0.6.0\"","metadata":{"id":"AttVzO1RfbFg","outputId":"70c77ac8-8f1e-4cd4-ddd1-6955bfec4c66","papermill":{"duration":14.929833,"end_time":"2021-11-07T09:07:24.470621","exception":false,"start_time":"2021-11-07T09:07:09.540788","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-09T15:09:21.464405Z","iopub.execute_input":"2021-11-09T15:09:21.465008Z","iopub.status.idle":"2021-11-09T15:09:29.941261Z","shell.execute_reply.started":"2021-11-09T15:09:21.464965Z","shell.execute_reply":"2021-11-09T15:09:29.940177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"id":"7JvgRNq3fbFi","outputId":"c453720a-7a20-4866-e70b-87c20240d715","papermill":{"duration":0.783442,"end_time":"2021-11-07T09:07:25.280025","exception":false,"start_time":"2021-11-07T09:07:24.496583","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-09T15:09:29.94397Z","iopub.execute_input":"2021-11-09T15:09:29.945641Z","iopub.status.idle":"2021-11-09T15:09:30.695711Z","shell.execute_reply.started":"2021-11-09T15:09:29.9456Z","shell.execute_reply":"2021-11-09T15:09:30.69475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport random\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport tempfile\nimport nibabel as nib\nfrom typing import Optional, Any, Mapping, Hashable\n\nimport monai\nfrom monai.config import print_config\nfrom monai.utils import first\nfrom monai.config import KeysCollection\nfrom monai.data import Dataset, ArrayDataset, create_test_image_3d, DataLoader\nfrom monai.transforms import *\n\n#print_config()","metadata":{"id":"aveRNu7YfbFo","outputId":"e16f01a1-8b56-4da1-a104-c68f9035fd46","papermill":{"duration":3.590821,"end_time":"2021-11-07T09:07:28.945718","exception":false,"start_time":"2021-11-07T09:07:25.354897","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-09T15:09:30.6972Z","iopub.execute_input":"2021-11-09T15:09:30.697483Z","iopub.status.idle":"2021-11-09T15:09:30.705569Z","shell.execute_reply.started":"2021-11-09T15:09:30.697441Z","shell.execute_reply":"2021-11-09T15:09:30.704467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir='../input/rsna-miccai-png/train/00000/T2w'\nfn_keys = (\"img\", \"seg\")        # filename keys for image and seg files\nnames0=os.listdir(data_dir)\nnames=sorted(names0)\nfilenames=[]\nimagedata=[]\nfor im in os.listdir(data_dir):\n    im_filename=os.path.join(data_dir,im)\n    image=cv2.imread(im_filename)\n    seg_filename=im\n    filenames.append({\"img\": im_filename, \"seg\": seg_filename})\n    imagedata.append(image)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:30.706982Z","iopub.execute_input":"2021-11-09T15:09:30.707213Z","iopub.status.idle":"2021-11-09T15:09:32.226075Z","shell.execute_reply.started":"2021-11-09T15:09:30.707187Z","shell.execute_reply":"2021-11-09T15:09:32.225227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transform Single Image","metadata":{}},{"cell_type":"code","source":"print(filenames[0]['img'])\nprint(np.array(imagedata[0]).shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:32.228204Z","iopub.execute_input":"2021-11-09T15:09:32.228424Z","iopub.status.idle":"2021-11-09T15:09:32.234176Z","shell.execute_reply.started":"2021-11-09T15:09:32.228398Z","shell.execute_reply":"2021-11-09T15:09:32.233405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img0=cv2.imread(filenames[0][\"img\"])\nprint(img0.shape)\nplt.imshow(img0)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:32.23524Z","iopub.execute_input":"2021-11-09T15:09:32.23544Z","iopub.status.idle":"2021-11-09T15:09:32.511547Z","shell.execute_reply.started":"2021-11-09T15:09:32.235417Z","shell.execute_reply":"2021-11-09T15:09:32.510707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans0 = Compose([LoadImage(image_only=True)])\nimg0=trans0(filenames[0][\"img\"])\nprint(img0.shape)\nplt.imshow(img0)\n# LoadImage deprives of channel data","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:32.512737Z","iopub.execute_input":"2021-11-09T15:09:32.512958Z","iopub.status.idle":"2021-11-09T15:09:33.190457Z","shell.execute_reply.started":"2021-11-09T15:09:32.512929Z","shell.execute_reply":"2021-11-09T15:09:33.189529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans0 = Compose([LoadImage(image_only=True),Resize((-1))])\nimg0=trans0(filenames[0][\"img\"])\nprint(img0.shape)\nplt.imshow(img0)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans0 = Compose([LoadImage(image_only=True),Resize((-1,1))])\nimg0=trans0(filenames[0][\"img\"])\nprint(img0.shape)\nplt.imshow(img0)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:33.191672Z","iopub.execute_input":"2021-11-09T15:09:33.191946Z","iopub.status.idle":"2021-11-09T15:09:33.46843Z","shell.execute_reply.started":"2021-11-09T15:09:33.191914Z","shell.execute_reply":"2021-11-09T15:09:33.466841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans0 = Compose([LoadImage(image_only=True),Resize((-1,3))])\nimg0=trans0(filenames[0][\"img\"])\nprint(img0.shape)\nplt.imshow(img0)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:33.470167Z","iopub.execute_input":"2021-11-09T15:09:33.470477Z","iopub.status.idle":"2021-11-09T15:09:33.757913Z","shell.execute_reply.started":"2021-11-09T15:09:33.470424Z","shell.execute_reply":"2021-11-09T15:09:33.757253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans0 = Compose([LoadImage(image_only=True),Resize((-1,4))])\nimg0=trans0(filenames[0][\"img\"])\nprint(img0.shape)\nplt.imshow(img0)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:33.759292Z","iopub.execute_input":"2021-11-09T15:09:33.759591Z","iopub.status.idle":"2021-11-09T15:09:34.032Z","shell.execute_reply.started":"2021-11-09T15:09:33.759551Z","shell.execute_reply":"2021-11-09T15:09:34.030911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SumDimension(Transform):\n    def __init__(self, dim=1):\n        self.dim = dim\n    def __call__(self, inputs):\n        return inputs.sum(self.dim)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:34.033118Z","iopub.execute_input":"2021-11-09T15:09:34.033347Z","iopub.status.idle":"2021-11-09T15:09:34.038322Z","shell.execute_reply.started":"2021-11-09T15:09:34.033318Z","shell.execute_reply":"2021-11-09T15:09:34.037517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans0 = Compose([LoadImage(image_only=True),Resize((-1,4)),SumDimension(2)])\nimg0=trans0(filenames[0][\"img\"])\nprint(img0.shape)\nplt.imshow(img0)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:34.039701Z","iopub.execute_input":"2021-11-09T15:09:34.039958Z","iopub.status.idle":"2021-11-09T15:09:34.301363Z","shell.execute_reply.started":"2021-11-09T15:09:34.03993Z","shell.execute_reply":"2021-11-09T15:09:34.300657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = Compose([LoadImage(image_only=True), AddChannel(), ToTensor()])\nimg0 = trans(filenames[0][\"img\"])\nprint(img0.shape)\nplt.imshow(img0[0])","metadata":{"id":"X4K1JpewfbFq","outputId":"91797e72-9d7a-4330-8b23-fc314b8bda12","papermill":{"duration":0.48409,"end_time":"2021-11-07T09:07:46.223995","exception":false,"start_time":"2021-11-07T09:07:45.739905","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-09T15:09:34.30255Z","iopub.execute_input":"2021-11-09T15:09:34.302751Z","iopub.status.idle":"2021-11-09T15:09:34.550346Z","shell.execute_reply.started":"2021-11-09T15:09:34.302725Z","shell.execute_reply":"2021-11-09T15:09:34.549594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = Compose([LoadImage(image_only=True), Resize((32,32,3))])\nimg0 = trans(filenames[0][\"img\"])\nprint(img0.shape)\nprint(img0[0,:,:,:].shape)\nplt.imshow(img0[0,:,:,:])","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:34.554773Z","iopub.execute_input":"2021-11-09T15:09:34.555016Z","iopub.status.idle":"2021-11-09T15:09:34.811238Z","shell.execute_reply.started":"2021-11-09T15:09:34.554986Z","shell.execute_reply":"2021-11-09T15:09:34.810354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# from monai.data import create_test_image_3d","metadata":{}},{"cell_type":"code","source":"root_dir = tempfile.mkdtemp()\nprint(root_dir)\nfilenames = []\nfor i in range(5):\n    im, _ = create_test_image_3d(64,64,64)\n    filename = f\"{root_dir}/im{i}.nii.gz\"\n    filenames.append(filename)\n    n = nib.Nifti1Image(im,np.eye(4))\n    nib.save(n, filename)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:34.812335Z","iopub.execute_input":"2021-11-09T15:09:34.812567Z","iopub.status.idle":"2021-11-09T15:09:34.93127Z","shell.execute_reply.started":"2021-11-09T15:09:34.812541Z","shell.execute_reply":"2021-11-09T15:09:34.93031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:34.932406Z","iopub.execute_input":"2021-11-09T15:09:34.932652Z","iopub.status.idle":"2021-11-09T15:09:34.93989Z","shell.execute_reply.started":"2021-11-09T15:09:34.932623Z","shell.execute_reply":"2021-11-09T15:09:34.938835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:34.94151Z","iopub.execute_input":"2021-11-09T15:09:34.942106Z","iopub.status.idle":"2021-11-09T15:09:34.949213Z","shell.execute_reply.started":"2021-11-09T15:09:34.94206Z","shell.execute_reply":"2021-11-09T15:09:34.948487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = Compose([LoadImage(image_only=True)])\nimg = trans(filenames[0])\nprint(img.shape)\nN=list(range(32))\nrandom.seed(2021)\nrandom.shuffle(N)\n\nfig, axs = plt.subplots(3,3,figsize=(10,10))\nfor i in range(9):\n    r=i//3\n    c=i%3\n    ax=axs[r][c].imshow(img[N[i]])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:34.950613Z","iopub.execute_input":"2021-11-09T15:09:34.950815Z","iopub.status.idle":"2021-11-09T15:09:35.984992Z","shell.execute_reply.started":"2021-11-09T15:09:34.95079Z","shell.execute_reply":"2021-11-09T15:09:35.984154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SumDimension(Transform):\n    def __init__(self, dim=1):\n        self.dim = dim\n    def __call__(self, inputs):\n        return inputs.sum(self.dim)","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:35.986114Z","iopub.execute_input":"2021-11-09T15:09:35.986335Z","iopub.status.idle":"2021-11-09T15:09:35.990879Z","shell.execute_reply.started":"2021-11-09T15:09:35.986306Z","shell.execute_reply":"2021-11-09T15:09:35.990096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = Compose([LoadImage(image_only=True), AddChannel(), SumDimension(2)])\nimg = trans(filenames[0])\nprint(img.shape)\nplt.imshow(img[0])","metadata":{"execution":{"iopub.status.busy":"2021-11-09T15:09:35.991819Z","iopub.execute_input":"2021-11-09T15:09:35.99263Z","iopub.status.idle":"2021-11-09T15:09:36.221653Z","shell.execute_reply.started":"2021-11-09T15:09:35.992572Z","shell.execute_reply":"2021-11-09T15:09:36.22081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.051246,"end_time":"2021-11-07T09:07:59.268899","exception":false,"start_time":"2021-11-07T09:07:59.217653","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![Upvote!](https://img.shields.io/badge/Upvote-If%20you%20like%20my%20work-07b3c8?style=for-the-badge&logo=kaggle)","metadata":{"papermill":{"duration":0.051548,"end_time":"2021-11-07T09:07:59.372095","exception":false,"start_time":"2021-11-07T09:07:59.320547","status":"completed"},"tags":[]}},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.052352,"end_time":"2021-11-07T09:07:59.476756","exception":false,"start_time":"2021-11-07T09:07:59.424404","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}