# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="MMBM34S1NZKE"
# #Data Transforms and Data Augmentation for medical imaging
# In this notebook, i'll implement data augmentation and data trasnforms to 3d medical imaging. Dataset is placed in a dictinoary, so i will be using dictionary transforms. I will first load an image, following that i will Add a channel to 3d image, because they have (hieght, width, slices). I will add a channal then it will become (channles,height, width, slices). Following that we scale the pixels intensity value, after that image will be resized to (128, 128, 128).
#
# Now after that i will apply some data augmentation to 3d medical images to make dataset more diverse.
#
#

# + id="g_SHWeS1NHvy"
#first install monai
# !pip install -qU "monai[ignite, nibabel, torchvision, tqdm]==0.6.0"

# + id="1ANnd59JPUIF"
#lets import some necessary packakges
import torch
from monai.transforms import (AddChanneld,
                              Compose,
                              LoadImaged,
                              RandRotate90d,
                              Resized,
                              ScaleIntensityd,
                              RandAffined,
                              RandRotated,
                              Zoomd,
                              EnsureTyped,
                              ToTensord)

# + colab={"base_uri": "https://localhost:8080/"} id="z9a8tMSGSpU1" outputId="b5835bbd-36b1-4f59-f386-a1da5d6b1efd"
from google.colab import drive
drive.mount('/gdrive')


# + id="Gm29W1qUQBk2"
def transfoms_augmentation():
  ''' transform and augment images
  return:
         transformed and augmentated images'''
  data_transforms = {
        'train':    Compose([
                     LoadImaged(keys = ["image"]),
                     AddChanneld(keys = ["image"]),
                     ScaleIntensityd(keys = ["image"]),
                     Resized(keys = ["image"], spatial_size = (128, 128, 128)),
                     RandAffined(keys = ["image"], prob=0.5, translate_range = 10),
                     RandRotate90d(keys=["image"], prob=0.8, spatial_axes=[0, 2]),
                     EnsureTyped(keys=["image"]),
                    #  ToTensord(keys = ["image"])
                     ]),
        'val':      Compose([
                     LoadImaged(keys=["image"]),
                     AddChanneld(keys=["image"]),
                     ScaleIntensityd(keys=["image"]),
                     Resized(keys=["image"], spatial_size=(128, 128, 128)),
                     EnsureTyped(keys=["image"]),
                    #  ToTensord(keys = ["image"])
                      ])
                     }
  return data_transforms

# + id="5_FzQkGzTINx"


StarcoderdataJupyterScriptsDedupFiltered