{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"pip install pynrrd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-18T20:07:31.869136Z","iopub.execute_input":"2023-04-18T20:07:31.869801Z","iopub.status.idle":"2023-04-18T20:07:49.057195Z","shell.execute_reply.started":"2023-04-18T20:07:31.869755Z","shell.execute_reply":"2023-04-18T20:07:49.055640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install opencv-contrib-python","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:02.628356Z","iopub.execute_input":"2023-04-18T20:08:02.628840Z","iopub.status.idle":"2023-04-18T20:08:04.525613Z","shell.execute_reply.started":"2023-04-18T20:08:02.628795Z","shell.execute_reply":"2023-04-18T20:08:04.524300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nimport os \nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras \nfrom tensorflow.keras.models import  Model\nfrom tensorflow.keras.layers import Conv2D ,MaxPooling2D ,UpSampling2D ,concatenate,Dropout ,Activation ,Input\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler ,ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras import backend as K\nfrom matplotlib import cm\nimport matplotlib.animation as anim\nfrom tqdm import tqdm\nfrom random import randint\nimport nibabel as nib\nimport pydicom as pdm\nimport nilearn as nl\nimport nilearn.plotting as nlplt\nimport nrrd\nimport h5py\nfrom IPython.display import Image as show_gif","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:07.360564Z","iopub.execute_input":"2023-04-18T20:08:07.361658Z","iopub.status.idle":"2023-04-18T20:08:17.139542Z","shell.execute_reply.started":"2023-04-18T20:08:07.361613Z","shell.execute_reply":"2023-04-18T20:08:17.138472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/chest-ct-segmentation/train.csv')\ndata.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:21.238131Z","iopub.execute_input":"2023-04-18T20:08:21.239700Z","iopub.status.idle":"2023-04-18T20:08:21.322021Z","shell.execute_reply.started":"2023-04-18T20:08:21.239649Z","shell.execute_reply":"2023-04-18T20:08:21.321093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageToGIF:\n    \"\"\"Create GIF without saving image files.\"\"\"\n    def __init__(self,\n                 size=(600, 400), \n                 xy_text=(80, 10),\n                 dpi=100, \n                 cmap='CMRmap'):\n\n        self.fig = plt.figure()\n        self.fig.set_size_inches(size[0] / dpi, size[1] / dpi)\n        self.xy_text = xy_text\n        self.cmap = cmap\n        \n        self.ax = self.fig.add_axes([0, 0, 1, 1])\n        self.ax.set_xticks([])\n        self.ax.set_yticks([])\n        self.images = []\n \n    def add(self, *args, label, with_mask=True):\n        \n        image = args[0]\n        mask = args[-1]\n        plt.set_cmap(self.cmap)\n        plt_img = self.ax.imshow(image, animated=True)\n        if with_mask:\n            plt_mask = self.ax.imshow(np.ma.masked_where(mask == False, mask),\n                                      alpha=0.4, animated=True)\n\n        plt_text = self.ax.text(*self.xy_text, label, color='red')\n        to_plot = [plt_img, plt_mask, plt_text] if with_mask else [plt_img, plt_text]\n        self.images.append(to_plot)\n        plt.close()\n \n    def save(self, filename, fps):\n        animation = anim.ArtistAnimation(self.fig, self.images)\n        animation.save(filename, writer='imagemagick', fps=fps)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:22.081735Z","iopub.execute_input":"2023-04-18T20:08:22.082859Z","iopub.status.idle":"2023-04-18T20:08:22.095148Z","shell.execute_reply.started":"2023-04-18T20:08:22.082812Z","shell.execute_reply":"2023-04-18T20:08:22.093970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_nrrd_file(path: str, tensor_shape: tuple ) -> np.ndarray:\n    if os.path.exists(path):\n        tensor = nrrd.read(path)[0] \n        tensor = np.flip(tensor, -1)  \n    else: \n        tensor = np.zeros(tensor_shape, dtype=np.float32)\n    return tensor\n\ndef nrrd_to_numpy(id_: str, tensor_shape: tuple):\n    '''\n    Returns:  all id masks in single numpy tensor.\n    '''\n    lung_file_path = '../input/ct-lung-heart-trachea-segmentation/nrrd_lung/nrrd_lung/' + id_ + '_lung.nrrd'\n    heart_file_path  = '../input/ct-lung-heart-trachea-segmentation/nrrd_heart/nrrd_heart/' + id_ + '_heart.nrrd'      \n    trachea_file_path = '../input/ct-lung-heart-trachea-segmentation/nrrd_trachea/nrrd_trachea/' + id_ + '_trachea.nrrd'\n\n    lung_tensor = read_nrrd_file(lung_file_path, tensor_shape)\n    heart_tensor = read_nrrd_file(heart_file_path, tensor_shape)\n    trachea_tensor = read_nrrd_file(trachea_file_path, tensor_shape)\n    \n    # chek if all tensors  have the same shape.\n    if not (lung_tensor.shape == heart_tensor.shape == trachea_tensor.shape):\n        #print(lung_tensor.shape, heart_tensor.shape, trachea_tensor.shape)\n        print(\"problem with id:\", id_)\n        return \n        # now each tensor channel is a mask with a unique label\n    full_mask = np.stack([lung_tensor, heart_tensor, trachea_tensor])\n    print(\"shape : \", full_mask.shape)\n    # reorient the axes from CHWB to BWHC\n    full_mask = np.moveaxis(full_mask,\n                            [0, 1, 2, 3],\n                            [3, 2, 1, 0]).astype(np.float32)\n\n    return full_mask","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:22.720701Z","iopub.execute_input":"2023-04-18T20:08:22.721759Z","iopub.status.idle":"2023-04-18T20:08:22.732729Z","shell.execute_reply.started":"2023-04-18T20:08:22.721709Z","shell.execute_reply":"2023-04-18T20:08:22.731607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_path =  '../input/osic-pulmonary-fibrosis-progression/train/ID00019637202178323708467'\nsample_path_files = sorted(os.listdir(sample_path), key=lambda x: int(x[:-4]))","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:23.071334Z","iopub.execute_input":"2023-04-18T20:08:23.071690Z","iopub.status.idle":"2023-04-18T20:08:23.102263Z","shell.execute_reply.started":"2023-04-18T20:08:23.071659Z","shell.execute_reply":"2023-04-18T20:08:23.101250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data_gif = ImageToGIF(size=(730,730 ),\n                             xy_text=(250, 15))\n\nlabel = sample_path.split('/')[-1]\nfor i in range(len(sample_path_files)):\n    path = os.path.join(sample_path, sample_path_files[i])\n    image = pdm.dcmread(path).pixel_array\n    sample_data_gif.add(image, label=f'{label}_{str(i)}', with_mask=False)\n\nsample_data_gif.save(f'{label}.gif', fps=8)\nshow_gif(f'{label}.gif', format='png')","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:23.358685Z","iopub.execute_input":"2023-04-18T20:08:23.359519Z","iopub.status.idle":"2023-04-18T20:08:37.276750Z","shell.execute_reply.started":"2023-04-18T20:08:23.359462Z","shell.execute_reply":"2023-04-18T20:08:37.275180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_ = 'ID00019637202178323708467'\nsample_masks = nrrd_to_numpy(id_, (768, 768))\nsample_masks.shape","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-18T20:08:37.280079Z","iopub.execute_input":"2023-04-18T20:08:37.280631Z","iopub.status.idle":"2023-04-18T20:08:37.727378Z","shell.execute_reply.started":"2023-04-18T20:08:37.280569Z","shell.execute_reply":"2023-04-18T20:08:37.726270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data_gif = ImageToGIF(size=(768, 768),\n                             xy_text=(250, 15))\n\nlabel = id_ + '_mask'\nfor i in range(sample_masks.shape[0]):\n    sample_data_gif.add(sample_masks[i],label=f'{label}_{str(i)}', with_mask=False)\n \nsample_data_gif.save(f'{label}.gif', fps=8)\nshow_gif(f'{label}.gif', format='png')","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:37.729174Z","iopub.execute_input":"2023-04-18T20:08:37.729960Z","iopub.status.idle":"2023-04-18T20:08:49.343716Z","shell.execute_reply.started":"2023-04-18T20:08:37.729919Z","shell.execute_reply":"2023-04-18T20:08:49.342412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageToGIF:\n    def __init__(self,\n                 size=(768, 768), \n                 xy_text=(250, 25),\n                 dpi=100):\n        \n        self.fig = plt.figure()\n        self.fig.set_size_inches(size[0] / dpi, size[1] / dpi)\n        self.xy_text = xy_text\n        \n        self.ax = self.fig.add_axes([0, 0, 1, 1])\n        self.ax.set_xticks([])\n        self.ax.set_yticks([])\n        self.images = []\n \n    def add(self, image, mask, label, with_mask=True):\n        \n        lung, heart, trachea = [mask[:, :, i] for i in range(3)]\n        plt_img = self.ax.imshow(image, cmap=\"bone\", animated=True)\n\n        # Overlaying segmentation masks\n        plt_mask1 = self.ax.imshow(np.ma.masked_where(lung == False, lung),\n                               cmap='cool', alpha=0.3, animated=True)\n        plt_mask2 = self.ax.imshow(np.ma.masked_where(heart == False, heart),\n                               cmap='autumn', alpha=0.3, animated=True)\n        plt_mask3 = self.ax.imshow(np.ma.masked_where(trachea == False, trachea),\n                               cmap='autumn_r', alpha=0.3, animated=True) #cool_r - blue\n        plt_text = self.ax.text(*self.xy_text, label, color='red')\n        to_plot = [plt_img, plt_mask1, plt_mask2, plt_mask3, plt_text] \n        self.images.append(to_plot)\n        plt.close()\n \n    def save(self, filename, fps):\n        animation = anim.ArtistAnimation(self.fig, self.images)\n        animation.save(filename, writer='imagemagick', fps=fps)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:49.347909Z","iopub.execute_input":"2023-04-18T20:08:49.348725Z","iopub.status.idle":"2023-04-18T20:08:49.364060Z","shell.execute_reply.started":"2023-04-18T20:08:49.348674Z","shell.execute_reply":"2023-04-18T20:08:49.363011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_data_gif = ImageToGIF()\n\nlabel = id_ + '_with_masks'\nfor i in range(sample_masks.shape[0]):\n    path = os.path.join(sample_path, sample_path_files[i])\n    image = pdm.dcmread(path).pixel_array\n    mask = sample_masks[i]\n    sample_data_gif.add(image, mask, label=f'{label}_{str(i)}',)\n    \nsample_data_gif.save(f'{label}.gif', fps=8)\nshow_gif(f'{label}.gif', format='png')","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:08:49.366789Z","iopub.execute_input":"2023-04-18T20:08:49.367294Z","iopub.status.idle":"2023-04-18T20:09:11.187702Z","shell.execute_reply.started":"2023-04-18T20:08:49.367232Z","shell.execute_reply":"2023-04-18T20:09:11.185866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img  = plt.imread('../input/chest-ct-segmentation/images/images/ID00007637202177411956430_20.jpg')\nmask = plt.imread('../input/chest-ct-segmentation/masks/masks/ID00007637202177411956430_mask_20.jpg')\n\nprint(\"Image Shape : \" , img.shape)\nprint(\"Mask Shape : \" , mask.shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:09:11.190639Z","iopub.execute_input":"2023-04-18T20:09:11.191539Z","iopub.status.idle":"2023-04-18T20:09:11.263537Z","shell.execute_reply.started":"2023-04-18T20:09:11.191482Z","shell.execute_reply":"2023-04-18T20:09:11.262640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a figure with two subplots\nfig, axs = plt.subplots(1, 2, figsize=(16, 8), constrained_layout=True)\n\n# Plot the first image on the left subplot\naxs[0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) \naxs[0].axis('off')\naxs[0].set_title('Image')\n# Plot the second image on the right subplot\naxs[1].imshow(mask)\naxs[1].axis('off')\naxs[1].set_title('Mask')\n\n# Show the figure\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:09:11.264963Z","iopub.execute_input":"2023-04-18T20:09:11.265601Z","iopub.status.idle":"2023-04-18T20:09:12.002032Z","shell.execute_reply.started":"2023-04-18T20:09:11.265566Z","shell.execute_reply":"2023-04-18T20:09:11.997457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n# we create two instances with the same arguments\n\n# VI Note: use the same seed for image_datagen and mask_datagen to ensure the transformation for image and mask is the same\ndata_gen_args = dict(rotation_range=0.2,\n                    width_shift_range=0.05,\n                    height_shift_range=0.05,\n                    shear_range=0.05,\n                    zoom_range=0.05,\n                    horizontal_flip=True,\n                    fill_mode='nearest',\n                    rescale=1./255,\n                    validation_split = 0.2)\n\nmask_gen_args = dict(rotation_range=0.2,\n                    width_shift_range=0.05,\n                    height_shift_range=0.05,\n                    shear_range=0.05,\n                    zoom_range=0.05,\n                    horizontal_flip=True,\n                    fill_mode='nearest',\n                    rescale=1./255,\n                    validation_split = 0.2)\n                    \n\nimage_datagen = ImageDataGenerator(**data_gen_args)\nmask_datagen  = ImageDataGenerator(**mask_gen_args)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T21:02:04.726474Z","iopub.execute_input":"2023-04-18T21:02:04.727628Z","iopub.status.idle":"2023-04-18T21:02:04.736049Z","shell.execute_reply.started":"2023-04-18T21:02:04.727579Z","shell.execute_reply":"2023-04-18T21:02:04.734837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = (256, 256) # resolution\nBATCH_SIZE = 16\ndirectory='../input/chest-ct-segmentation/'\n\nimage_generator = image_datagen.flow_from_dataframe(\n    dataframe=data,\n    directory=directory + '/images/images',\n    class_mode=None,\n    #classes=['images'],\n    x_col='ImageId',\n    seed=50,\n    color_mode='grayscale',\n    batch_size=BATCH_SIZE,\n    target_size=IMG_SIZE,\n    subset='training'\n)\n\nimage_generator_val = image_datagen.flow_from_dataframe(\n    dataframe=data,\n    directory=directory + '/images/images',\n    class_mode=None,\n    #classes=['images'],\n    x_col='ImageId',\n    seed=50,\n    color_mode='grayscale',\n    batch_size=BATCH_SIZE,\n    target_size=IMG_SIZE,\n    subset='validation'\n)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T21:02:04.738362Z","iopub.execute_input":"2023-04-18T21:02:04.738744Z","iopub.status.idle":"2023-04-18T21:02:21.010396Z","shell.execute_reply.started":"2023-04-18T21:02:04.738707Z","shell.execute_reply":"2023-04-18T21:02:21.009197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_generator = mask_datagen.flow_from_dataframe(\n    dataframe=data,\n    directory= directory + '/masks/masks',\n    #classes=['masks'],\n    class_mode=None,\n    x_col='MaskId',\n    seed=50,\n    batch_size=BATCH_SIZE,\n    color_mode='rgb',\n    target_size=IMG_SIZE,\n    subset='training')\n\nmask_generator_val = mask_datagen.flow_from_dataframe(\n    dataframe=data,\n    directory=directory + '/masks/masks',\n    #classes=['masks'],\n    class_mode=None,\n    x_col='MaskId',\n    seed=50,\n    batch_size=BATCH_SIZE,\n    color_mode='rgb',\n    target_size=IMG_SIZE,\n    subset='validation')","metadata":{"execution":{"iopub.status.busy":"2023-04-18T21:02:21.012792Z","iopub.execute_input":"2023-04-18T21:02:21.013545Z","iopub.status.idle":"2023-04-18T21:02:37.253957Z","shell.execute_reply.started":"2023-04-18T21:02:21.013505Z","shell.execute_reply":"2023-04-18T21:02:37.252884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = zip(image_generator, mask_generator)\ndef train_generator_fn():\n    for (img,mask) in train_generator:\n        yield (img,mask) \nvalid_generator = zip(image_generator_val, mask_generator_val)\ndef valid_generator_fn():\n    for (img,mask) in valid_generator:\n        yield (img,mask)  ","metadata":{"execution":{"iopub.status.busy":"2023-04-18T21:02:37.255901Z","iopub.execute_input":"2023-04-18T21:02:37.256305Z","iopub.status.idle":"2023-04-18T21:02:37.264420Z","shell.execute_reply.started":"2023-04-18T21:02:37.256265Z","shell.execute_reply":"2023-04-18T21:02:37.262158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nfor X_batch, y_batch in train_generator:\n    # create a grid of 3x3 images\n    for i in range(0,16):\n        plt.subplot(4,4,i+1)\n        plt.imshow(X_batch[i],cmap='gray')\n    # show the plot\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:11:19.337461Z","iopub.execute_input":"2023-04-18T20:11:19.338357Z","iopub.status.idle":"2023-04-18T20:11:22.733312Z","shell.execute_reply.started":"2023-04-18T20:11:19.338321Z","shell.execute_reply":"2023-04-18T20:11:22.731957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nfor X_batch, y_batch in train_generator:\n    # create a grid of 3x3 images\n    for i in range(0,16):\n        plt.subplot(4,4,i+1)\n        plt.imshow(y_batch[i])\n    # show the plot\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:11:22.735065Z","iopub.execute_input":"2023-04-18T20:11:22.735414Z","iopub.status.idle":"2023-04-18T20:11:26.248853Z","shell.execute_reply.started":"2023-04-18T20:11:22.735380Z","shell.execute_reply":"2023-04-18T20:11:26.247363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"smooth=1.\n\ndef dice_coef(y_true, y_pred):\n    y_true = K.flatten(y_true)\n    y_pred = K.flatten(y_pred)\n    intersection = K.sum(y_true * y_pred)\n    union = K.sum(y_true) + K.sum(y_pred)\n    return (2.0 * intersection + smooth) / (union + smooth)\n\ndef dice_coef_loss(y_true, y_pred):\n    return 1 - dice_coef(y_true, y_pred)\n\ndef bce_dice_loss(y_true, y_pred):\n    bce = tf.keras.losses.BinaryCrossentropy(from_logits=True)\n    return dice_coef_loss(y_true, y_pred) + bce(y_true, y_pred)\n\ndef iou(y_true, y_pred):\n    intersection = K.sum(y_true * y_pred)\n    sum_ = K.sum(y_true + y_pred)\n    jac = (intersection + smooth) / (sum_ - intersection + smooth)\n    return jac","metadata":{"execution":{"iopub.status.busy":"2023-04-18T20:11:26.250792Z","iopub.execute_input":"2023-04-18T20:11:26.251594Z","iopub.status.idle":"2023-04-18T20:11:26.261834Z","shell.execute_reply.started":"2023-04-18T20:11:26.251531Z","shell.execute_reply":"2023-04-18T20:11:26.260297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unet(n_classes,input_size = (256,256,1)):\n    inputs = Input(input_size)\n    conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)\n    conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)\n    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)\n    conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)\n    conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2)\n    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)\n    conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool2)\n    conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)\n    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)\n    conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool3)\n    conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv4)\n    drop4 = Dropout(0.5)(conv4)\n    pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)\n\n    conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool4)\n    conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)\n    drop5 = Dropout(0.5)(conv5)\n\n    x = UpSampling2D(size = (2,2))(drop5)\n    up6 = Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(x)\n    merge6 = concatenate([drop4,up6], axis = 3)\n    conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge6)\n    conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)\n\n    up7 = Conv2D(256, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv6))\n    merge7 = concatenate([conv3,up7], axis = 3)\n    conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge7)\n    conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)\n\n    up8 = Conv2D(128, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv7))\n    merge8 = concatenate([conv2,up8], axis = 3)\n    conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge8)\n    conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)\n\n    up9 = Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(conv8))\n    merge9 = concatenate([conv1,up9], axis = 3)\n    conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge9)\n    conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)\n    \n    conv10 = Conv2D(n_classes, (1,1), padding='same')(conv9)\n    \n    output_layer = Activation('softmax')(conv10)\n     \n\n    model = Model(inputs = inputs, outputs = output_layer)\n\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2023-04-18T21:12:00.015092Z","iopub.execute_input":"2023-04-18T21:12:00.015540Z","iopub.status.idle":"2023-04-18T21:12:00.034659Z","shell.execute_reply.started":"2023-04-18T21:12:00.015495Z","shell.execute_reply":"2023-04-18T21:12:00.033418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = unet(n_classes = 3)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-18T21:12:03.648818Z","iopub.execute_input":"2023-04-18T21:12:03.649486Z","iopub.status.idle":"2023-04-18T21:12:04.001923Z","shell.execute_reply.started":"2023-04-18T21:12:03.649447Z","shell.execute_reply":"2023-04-18T21:12:04.001082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EarlyStop=EarlyStopping(patience=10,restore_best_weights=True)\nReduce_LR=ReduceLROnPlateau(monitor='val_loss',verbose=2,factor=0.5,min_lr=0.00001)\nmodel_checkpoint = ModelCheckpoint('unet_camvid.hdf5', monitor='val_loss',verbose=1, save_best_only=True)\ncallback=[EarlyStop , Reduce_LR,model_checkpoint]","metadata":{"execution":{"iopub.status.busy":"2023-04-18T21:13:45.459162Z","iopub.execute_input":"2023-04-18T21:13:45.460165Z","iopub.status.idle":"2023-04-18T21:13:45.466717Z","shell.execute_reply.started":"2023-04-18T21:13:45.460128Z","shell.execute_reply":"2023-04-18T21:13:45.465500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate = 1e-2), loss=bce_dice_loss, metrics=[iou, dice_coef])","metadata":{"execution":{"iopub.status.busy":"2023-04-18T21:13:49.620028Z","iopub.execute_input":"2023-04-18T21:13:49.621050Z","iopub.status.idle":"2023-04-18T21:13:49.634743Z","shell.execute_reply.started":"2023-04-18T21:13:49.620993Z","shell.execute_reply":"2023-04-18T21:13:49.633604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_generator_fn(),\n                    validation_data=valid_generator_fn(),\n                    steps_per_epoch=500,\n                    validation_steps=10,\n                    epochs=20,\n                    callbacks=callback)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T21:18:23.375938Z","iopub.execute_input":"2023-04-18T21:18:23.376363Z","iopub.status.idle":"2023-04-18T23:05:31.324502Z","shell.execute_reply.started":"2023-04-18T21:18:23.376326Z","shell.execute_reply":"2023-04-18T23:05:31.323486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,15))\nplt.subplot(3,1,1)\nplt.plot(model.history.history['loss'], 'b-', label='train_loss')\nplt.plot(model.history.history['val_loss'], 'r-', label='val_loss')\nplt.legend(loc='best')\nplt.title('Loss')\n\nplt.subplot(3,1,2)\nplt.plot(model.history.history['iou'], 'b-', label='train_iou')\nplt.plot(model.history.history['val_iou'], 'r-', label='val_iou')\nplt.legend(loc='best')\nplt.title('IoU')\n\nplt.subplot(3,1,3)\nplt.plot(model.history.history['dice_coef'], 'b-', label='train_dice_coef')\nplt.plot(model.history.history['val_dice_coef'], 'r-', label='val_dice_coef')\nplt.legend(loc='best')\nplt.title('Dice Coef')","metadata":{"execution":{"iopub.status.busy":"2023-04-18T23:08:37.312395Z","iopub.execute_input":"2023-04-18T23:08:37.312761Z","iopub.status.idle":"2023-04-18T23:08:37.846202Z","shell.execute_reply.started":"2023-04-18T23:08:37.312729Z","shell.execute_reply":"2023-04-18T23:08:37.845186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.history.history","metadata":{"execution":{"iopub.status.busy":"2023-04-18T23:39:02.783445Z","iopub.execute_input":"2023-04-18T23:39:02.784446Z","iopub.status.idle":"2023-04-18T23:39:02.792458Z","shell.execute_reply.started":"2023-04-18T23:39:02.784391Z","shell.execute_reply":"2023-04-18T23:39:02.791096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\ncolors = ['#C042FF', '#03C576FF', '#FF355A', '#03C5BF', '#96C503', '#C5035B']\npalettes = [sns.color_palette(colors, 2),\n            sns.color_palette(colors, 4), \n            sns.color_palette(colors[:2]+colors[-2:] + colors[2:-2], 6)]\n            \nfig, ax = plt.subplots(1, 3, figsize=(22, 4))\n\nsns.lineplot(data=model.history.history['val_loss'], palette=palettes[0], markers=True, ax=ax[0], linewidth=2.5,)\nax[0].set_title(\"Loss Function during Model Training\", fontsize=14)\nax[0].set_xlabel(\"Epoch\", fontsize=14)\n\nsns.lineplot(data= model.history.history['val_iou'], palette=palettes[1], markers=True, ax=ax[1], linewidth=2.5, legend=\"full\")\nax[1].set_title(\"Iou during Model Training\", fontsize=14)\nax[1].set_xlabel(\"Epoch\", fontsize=14)\n\nsns.boxplot(data=model.history.history['dice_coef'], palette=palettes[2], ax=ax[2])\nax[2].set_title(\"Dice Coefficient for each Label from Validation\", fontsize=14)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T23:19:04.025163Z","iopub.execute_input":"2023-04-18T23:19:04.025889Z","iopub.status.idle":"2023-04-18T23:19:04.535171Z","shell.execute_reply.started":"2023-04-18T23:19:04.025845Z","shell.execute_reply":"2023-04-18T23:19:04.534230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}