{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":86142,"databundleVersionId":9786425,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Here I tried to train a relatively simpler model , for the reasons of computational constraints , but couldnt achieve it ofcourse**","metadata":{}},{"cell_type":"code","source":"train_dir = '/kaggle/input/iitg-ai-overnight-hackathon-2024/dataset/dataset/train'\nlabel_dir = '/kaggle/input/iitg-ai-overnight-hackathon-2024/dataset/dataset/labels'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-11T03:06:16.529992Z","iopub.execute_input":"2024-10-11T03:06:16.530942Z","iopub.status.idle":"2024-10-11T03:06:16.534834Z","shell.execute_reply.started":"2024-10-11T03:06:16.530898Z","shell.execute_reply":"2024-10-11T03:06:16.533889Z"},"trusted":true},"execution_count":61,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:16.536248Z","iopub.execute_input":"2024-10-11T03:06:16.536569Z","iopub.status.idle":"2024-10-11T03:06:16.544395Z","shell.execute_reply.started":"2024-10-11T03:06:16.536536Z","shell.execute_reply":"2024-10-11T03:06:16.543532Z"},"trusted":true},"execution_count":62,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.layers import Conv2D, UpSampling2D, concatenate, Input\nfrom tensorflow.keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:16.622149Z","iopub.execute_input":"2024-10-11T03:06:16.622449Z","iopub.status.idle":"2024-10-11T03:06:16.627295Z","shell.execute_reply.started":"2024-10-11T03:06:16.622417Z","shell.execute_reply":"2024-10-11T03:06:16.626362Z"},"trusted":true},"execution_count":63,"outputs":[]},{"cell_type":"markdown","source":"# Defining Label Class","metadata":{}},{"cell_type":"code","source":"# Define the Label class\nclass Label:\n    def __init__(self, name, id, csId, csTrainId, level4id, level3Id, category, level2Id, level1Id, hasInstances, ignoreInEval, color):\n        self.name = name\n        self.id = id\n        self.csId = csId\n        self.csTrainId = csTrainId\n        self.level4id = level4id\n        self.level3Id = level3Id\n        self.category = category\n        self.level2Id = level2Id\n        self.level1Id = level1Id\n        self.hasInstances = hasInstances\n        self.ignoreInEval = ignoreInEval\n        self.color = color\n\n# Define your label definitions here\n# Define your label definitions here\nlabels = [\n    Label('road', 0, 7, 0, 0, 0, 'drivable', 0, 0, False, False, (128, 64, 128)),\n    Label('parking', 1, 9, 255, 1, 1, 'drivable', 1, 0, False, False, (250, 170, 160)),\n    Label('drivable fallback', 2, 255, 255, 2, 1, 'drivable', 1, 0, False, False, (81, 0, 81)),\n    Label('sidewalk', 3, 8, 1, 3, 2, 'non-drivable', 2, 1, False, False, (244, 35, 232)),\n    Label('rail track', 4, 10, 255, 3, 3, 'non-drivable', 3, 1, False, False, (230, 150, 140)),\n    Label('non-drivable fallback', 5, 255, 9, 4, 3, 'non-drivable', 3, 1, False, False, (152, 251, 152)),\n    Label('person', 6, 24, 11, 5, 4, 'living-thing', 4, 2, True, False, (220, 20, 60)),\n    Label('animal', 7, 255, 255, 6, 4, 'living-thing', 4, 2, True, True, (246, 198, 145)),\n    Label('rider', 8, 25, 12, 7, 5, 'living-thing', 5, 2, True, False, (255, 0, 0)),\n    Label('motorcycle', 9, 32, 17, 8, 6, '2-wheeler', 6, 3, True, False, (0, 0, 230)),\n    Label('bicycle', 10, 33, 18, 9, 7, '2-wheeler', 6, 3, True, False, (119, 11, 32)),\n    Label('autorickshaw', 11, 255, 255, 10, 8, 'autorickshaw', 7, 3, True, False, (255, 204, 54)),\n    Label('car', 12, 26, 13, 11, 9, 'car', 7, 3, True, False, (0, 0, 142)),\n    Label('truck', 13, 27, 14, 12, 10, 'large-vehicle', 8, 3, True, False, (0, 0, 70)),\n    Label('bus', 14, 28, 15, 13, 11, 'large-vehicle', 8, 3, True, False, (0, 60, 100)),\n    Label('caravan', 15, 29, 255, 14, 12, 'large-vehicle', 8, 3, True, True, (0, 0, 90)),\n    Label('trailer', 16, 30, 255, 15, 12, 'large-vehicle', 8, 3, True, True, (0, 0, 110)),\n    Label('train', 17, 31, 16, 15, 12, 'large-vehicle', 8, 3, True, True, (0, 80, 100)),\n    Label('vehicle fallback', 18, 355, 255, 15, 12, 'large-vehicle', 8, 3, True, False, (136, 143, 153)),\n    Label('curb', 19, 255, 255, 16, 13, 'barrier', 9, 4, False, False, (220, 190, 40)),\n    Label('wall', 20, 12, 3, 17, 14, 'barrier', 9, 4, False, False, (102, 102, 156)),\n    Label('fence', 21, 13, 4, 18, 15, 'barrier', 10, 4, False, False, (190, 153, 153)),\n    Label('guard rail', 22, 14, 255, 19, 16, 'barrier', 10, 4, False, False, (180, 165, 180)),\n    Label('billboard', 23, 255, 255, 20, 17, 'structures', 11, 4, False, False, (174, 64, 67)),\n    Label('traffic sign', 24, 20, 7, 21, 18, 'structures', 11, 4, False, False, (220, 220, 0)),\n    Label('traffic light', 25, 19, 6, 22, 19, 'structures', 11, 4, False, False, (250, 170, 30)),\n    Label('pole', 26, 17, 5, 23, 20, 'structures', 12, 4, False, False, (153, 153, 153)),\n    Label('polegroup', 27, 18, 255, 23, 20, 'structures', 12, 4, False, False, (153, 153, 153)),\n    Label('obs-str-bar-fallback', 28, 255, 255, 24, 21, 'structures', 12, 4, False, False, (169, 187, 214)),\n    Label('building', 29, 11, 2, 25, 22, 'construction', 13, 5, False, False, (70, 70, 70)),\n    Label('bridge', 30, 15, 255, 26, 23, 'construction', 13, 5, False, False, (150, 100, 100)),\n    Label('tunnel', 31, 16, 255, 26, 23, 'construction', 13, 5, False, False, (150, 120, 90)),\n    Label('vegetation', 32, 21, 8, 27, 24, 'vegetation', 14, 5, False, False, (107, 142, 35)),\n    Label('sky', 33, 23, 10, 28, 25, 'sky', 15, 6, False, False, (70, 130, 180)),\n    Label('fallback background', 34, 255, 255, 29, 25, 'object fallback', 15, 6, False, False, (169, 187, 214)),\n    Label('unlabeled', 35, 0, 255, 255, 255, 'void', 255, 255, False, True, (0, 0, 0)),\n    Label('ego vehicle', 36, 1, 255, 255, 255, 'void', 255, 255, False, True, (0, 0, 0)),\n    Label('rectification border', 37, 2, 255, 255, 255, 'void', 255, 255, False, True, (0, 0, 0)),\n    Label('out of roi', 38, 3, 255, 255, 255, 'void', 255, 255, False, True, (0, 0, 0)),\n    Label('license plate', 39, 255, 255, 255, 255, 'vehicle', 255, 255, False, True, (0, 0, 142)),\n]\n","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:16.839657Z","iopub.execute_input":"2024-10-11T03:06:16.839986Z","iopub.status.idle":"2024-10-11T03:06:16.87867Z","shell.execute_reply.started":"2024-10-11T03:06:16.839954Z","shell.execute_reply":"2024-10-11T03:06:16.877703Z"},"trusted":true},"execution_count":64,"outputs":[]},{"cell_type":"markdown","source":"# Colour to class mapping","metadata":{}},{"cell_type":"code","source":"def get_color_to_class_mapping():\n    \"\"\"\n    Map the RGB color values from the segmented images to their corresponding class IDs.\n    \"\"\"\n    color_to_class_map = {tuple(label.color): label.id for label in labels}\n    return color_to_class_map\n\n# Create the mapping\ncolor_to_class_map = get_color_to_class_mapping()\n\n# Print the mapping for verification\nfor color, class_id in color_to_class_map.items():\n#     print(f\"Color {color} corresponds to Class ID {class_id}\")\n    print(f\"{color}: {class_id},\")","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:16.880294Z","iopub.execute_input":"2024-10-11T03:06:16.881074Z","iopub.status.idle":"2024-10-11T03:06:16.891399Z","shell.execute_reply.started":"2024-10-11T03:06:16.881029Z","shell.execute_reply":"2024-10-11T03:06:16.890368Z"},"trusted":true},"execution_count":65,"outputs":[{"name":"stdout","text":"(128, 64, 128): 0,\n(250, 170, 160): 1,\n(81, 0, 81): 2,\n(244, 35, 232): 3,\n(230, 150, 140): 4,\n(152, 251, 152): 5,\n(220, 20, 60): 6,\n(246, 198, 145): 7,\n(255, 0, 0): 8,\n(0, 0, 230): 9,\n(119, 11, 32): 10,\n(255, 204, 54): 11,\n(0, 0, 142): 39,\n(0, 0, 70): 13,\n(0, 60, 100): 14,\n(0, 0, 90): 15,\n(0, 0, 110): 16,\n(0, 80, 100): 17,\n(136, 143, 153): 18,\n(220, 190, 40): 19,\n(102, 102, 156): 20,\n(190, 153, 153): 21,\n(180, 165, 180): 22,\n(174, 64, 67): 23,\n(220, 220, 0): 24,\n(250, 170, 30): 25,\n(153, 153, 153): 27,\n(169, 187, 214): 34,\n(70, 70, 70): 29,\n(150, 100, 100): 30,\n(150, 120, 90): 31,\n(107, 142, 35): 32,\n(70, 130, 180): 33,\n(0, 0, 0): 38,\n","output_type":"stream"}]},{"cell_type":"markdown","source":"clss id 35, 36, 37 , 38 here has samee color value wich we are classifying as \n\ncls id 12 and 39 have same colour code so we better classify it as 12\n\nfor id 24 and 38 ,m we choose the class 24\n\nout of 26 and 27 we choose 26","metadata":{}},{"cell_type":"code","source":"COLOR_TO_CLASS = {\n    \n    (128, 64, 128): 0,\n    (250, 170, 160): 1,\n    (81, 0, 81): 2,\n    (244, 35, 232): 3,\n    (230, 150, 140): 4,\n    (152, 251, 152): 5,\n    (220, 20, 60): 6,\n    (246, 198, 145): 7,\n(255, 0, 0): 8,\n(0, 0, 230): 9,\n(119, 11, 32): 10,\n(255, 204, 54): 11,\n(0, 0, 142): 12,  # same as 39\n(0, 0, 70): 13,\n(0, 60, 100): 14,\n(0, 0, 90): 15,\n(0, 0, 110): 16,\n(0, 80, 100): 17,\n(136, 143, 153): 18,\n(220, 190, 40): 19,\n(102, 102, 156): 20,\n(190, 153, 153): 21,\n(180, 165, 180): 22,\n(174, 64, 67): 23,\n(220, 220, 0): 24,\n(250, 170, 30): 25,\n(153, 153, 153): 26,  # same as 27\n(169, 187, 214): 34,\n(70, 70, 70): 29,\n(150, 100, 100): 30,\n(150, 120, 90): 31,\n(107, 142, 35): 32,\n(70, 130, 180): 33,  \n(0, 0, 0): 38, # for 34-38\n}","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:16.923108Z","iopub.execute_input":"2024-10-11T03:06:16.92343Z","iopub.status.idle":"2024-10-11T03:06:16.933494Z","shell.execute_reply.started":"2024-10-11T03:06:16.923396Z","shell.execute_reply":"2024-10-11T03:06:16.932562Z"},"trusted":true},"execution_count":66,"outputs":[]},{"cell_type":"markdown","source":"# Pairing Train and Label Images","metadata":{}},{"cell_type":"code","source":"def get_image_pairs(train_dir, label_dir):\n    \"\"\"\n    Pair the train images with their corresponding label images \n    (segmented images) in the label directory.\n    \n    train_dir: path to the directory containing the RGB images.\n    label_dir: path to the directory containing the segmented images.\n    \n    Returns:\n    - train_files: list of file paths to RGB images.\n    - label_segmented_files: list of file paths to segmented images.\n    \"\"\"\n    train_files = []\n    label_segmented_files = []\n    \n    for root, _, files in os.walk(train_dir):\n        for file in files:\n            if file.endswith('.jpg'):  # Assuming RGB images are in .jpg format\n                common_part = file.split('_')[0]  # Extract common part from filename\n                \n                # Train image path\n                train_image_path = os.path.join(root, file)\n                \n                # Corresponding segmented image in label directory\n                segmented_image_path = os.path.join(label_dir, root.split('/')[-1], f\"{common_part}_gtFine_labelColors.png\")\n                \n                # Append only if the corresponding files exist\n                if os.path.exists(segmented_image_path):\n                    train_files.append(train_image_path)\n                    label_segmented_files.append(segmented_image_path)\n                else:\n                    print(f\"Warning: Missing segmented image for {common_part}\")\n    \n    return train_files, label_segmented_files\n\n\ntrain_files, label_segmented_files = get_image_pairs(train_dir, label_dir)\nprint(f\"Found {len(train_files)} training pairs.\")\nprint(f\"Found {len(label_segmented_files)} training pairs.\")","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:17.022043Z","iopub.execute_input":"2024-10-11T03:06:17.022788Z","iopub.status.idle":"2024-10-11T03:06:30.452136Z","shell.execute_reply.started":"2024-10-11T03:06:17.022742Z","shell.execute_reply":"2024-10-11T03:06:30.451134Z"},"trusted":true},"execution_count":67,"outputs":[{"name":"stdout","text":"Found 7034 training pairs.\nFound 7034 training pairs.\n","output_type":"stream"}]},{"cell_type":"markdown","source":"# Splitting the Dataset into Training and Validation","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Split the dataset into training and validation sets (80% train, 20% validation)\ntrain_files, val_files, label_train_files, label_val_files = train_test_split(\n    train_files, label_segmented_files, test_size=0.2, random_state=42\n)\n\nprint(f\"Training set size: {len(train_files)}\")\nprint(f\"Validation set size: {len(val_files)}\")","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:30.453691Z","iopub.execute_input":"2024-10-11T03:06:30.454011Z","iopub.status.idle":"2024-10-11T03:06:30.46624Z","shell.execute_reply.started":"2024-10-11T03:06:30.453978Z","shell.execute_reply":"2024-10-11T03:06:30.465458Z"},"trusted":true},"execution_count":68,"outputs":[{"name":"stdout","text":"Training set size: 5627\nValidation set size: 1407\n","output_type":"stream"}]},{"cell_type":"markdown","source":"#  Preprocessing the RGB and Segmented Images","metadata":{}},{"cell_type":"code","source":"# from tensorflow.keras.utils import normalize\n\n# def preprocess_rgb_image(image_path):\n#     # Read the RGB image\n#     image = cv2.imread(image_path, cv2.IMREAD_COLOR)  # (H, W, 3)\n    \n#     # Resize the image to (540, 960, 3)\n#     resized_image = cv2.resize(image, (960, 540))  # Resize to (width, height)\n    \n#     # Normalize pixel values to range [0, 1]\n#     normalized_image = resized_image / 255.0\n    \n#     return normalized_image  # Shape: (540, 960, 3)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:30.467514Z","iopub.execute_input":"2024-10-11T03:06:30.468194Z","iopub.status.idle":"2024-10-11T03:06:30.4767Z","shell.execute_reply.started":"2024-10-11T03:06:30.468147Z","shell.execute_reply":"2024-10-11T03:06:30.475827Z"},"trusted":true},"execution_count":69,"outputs":[]},{"cell_type":"code","source":"def preprocess_image(img_path, target_size=(540, 960)):\n    \"\"\"\n    Function to preprocess input RGB images.\n    \"\"\"\n    image = cv2.imread(img_path, cv2.IMREAD_COLOR)\n    image = cv2.resize(image, target_size)\n    image = image / 255.0  # Normalize to [0, 1]\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:30.479387Z","iopub.execute_input":"2024-10-11T03:06:30.48003Z","iopub.status.idle":"2024-10-11T03:06:30.484987Z","shell.execute_reply.started":"2024-10-11T03:06:30.479996Z","shell.execute_reply":"2024-10-11T03:06:30.484142Z"},"trusted":true},"execution_count":70,"outputs":[]},{"cell_type":"code","source":"# def preprocess_segmented_image(segmented_image, num_classes=40):\n#     \"\"\"\n#     Preprocess the segmented image to derive class labels and one-hot encode them.\n\n#     segmented_image: numpy array of shape (1080, 1920, 4)\n#     num_classes: total number of classes for one-hot encoding\n\n#     Returns:\n#     one_hot_encoded_image: numpy array of shape (1080, 1920, num_classes)\n#     \"\"\"\n#     # Create a class values array\n#     class_values = np.zeros((segmented_image.shape[0], segmented_image.shape[1]), dtype=np.int32)\n\n#     # Iterate over each pixel and assign class values based on RGB mapping\n#     for y in range(segmented_image.shape[0]):\n#         for x in range(segmented_image.shape[1]):\n#             rgb_tuple = tuple(segmented_image[y, x, :3])  # Get RGB values\n#             class_values[y, x] = COLOR_TO_CLASS.get(rgb_tuple, -1)  # Assign class or -1 if not found\n\n#     # One-hot encode the class values\n#     one_hot_encoded = to_categorical(class_values, num_classes=num_classes)\n\n#     return one_hot_encoded  # Shape: (1080, 1920, num_classes)\n\n\n\n# num_classes=40","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:30.486141Z","iopub.execute_input":"2024-10-11T03:06:30.486477Z","iopub.status.idle":"2024-10-11T03:06:30.493051Z","shell.execute_reply.started":"2024-10-11T03:06:30.486445Z","shell.execute_reply":"2024-10-11T03:06:30.4921Z"},"trusted":true},"execution_count":71,"outputs":[]},{"cell_type":"code","source":"# Function to convert color-coded segmentation to class IDs using the COLOR_TO_CLASS dictionary\ndef color_to_class(segmented_image, color_to_class_mapping):\n    \"\"\"\n    Convert a color-coded segmented image to a class ID image.\n    \"\"\"\n    class_id_image = np.zeros((segmented_image.shape[0], segmented_image.shape[1]), dtype=np.uint8)\n    \n    for color, class_id in color_to_class_mapping.items():\n        mask = np.all(segmented_image == np.array(color), axis=-1)\n        class_id_image[mask] = class_id\n        \n    return class_id_image\n\n# One-hot encoding function\ndef one_hot_encode(class_id_image, num_classes):\n    \"\"\"\n    One-hot encode the class ID image.\n    \"\"\"\n    one_hot = np.eye(num_classes)[class_id_image]\n    # Ensure the shape is (height, width, num_classes)\n    one_hot = np.transpose(one_hot, (0, 1, 2))  # This ensures the correct order\n    return one_hot\n\n# Preprocess segmented image: resize, convert to class IDs, and one-hot encode\ndef preprocess_segmented_image(seg_path, target_size=(1080, 1920), num_classes=40):\n    \"\"\"\n    Preprocess the segmented image: resize, convert colors to class IDs, and one-hot encode.\n    \"\"\"\n    segmented_image = cv2.imread(seg_path, cv2.IMREAD_COLOR)  # Read as color image\n    segmented_image = cv2.resize(segmented_image, target_size)  # Resize to target size\n    \n    class_id_image = color_to_class(segmented_image, COLOR_TO_CLASS)  # Convert color to class ID\n    one_hot_label = one_hot_encode(class_id_image, num_classes)  # One-hot encode\n\n    # Convert the one-hot encoded label to a TensorFlow tensor\n    one_hot_label_tensor = tf.convert_to_tensor(one_hot_label, dtype=tf.float32)\n    \n    # This will help identify if the shape is correct\n    one_hot_label_tensor.set_shape([target_size[0], target_size[1], num_classes])\n    \n    # Set the shape explicitly (this should match the actual shape of the tensor)\n    one_hot_label_tensor.set_shape([target_size[0], target_size[1], num_classes])  # Shape: (1080, 1920, num_classes)\n\n    return one_hot_label_tensor  # Now returns a tensor\n","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:13:10.683805Z","iopub.execute_input":"2024-10-11T03:13:10.684223Z","iopub.status.idle":"2024-10-11T03:13:10.695179Z","shell.execute_reply.started":"2024-10-11T03:13:10.684184Z","shell.execute_reply":"2024-10-11T03:13:10.69415Z"},"trusted":true},"execution_count":94,"outputs":[]},{"cell_type":"markdown","source":"# Data Generator for Efficient Data Loading","metadata":{}},{"cell_type":"code","source":"# import os\n# from tensorflow.keras.utils import Sequence\n\n# class DataGenerator(Sequence):\n#     def __init__(self, train_files, label_segmented_files, batch_size=32, img_size=(540, 960), label_size=(1080, 1920), num_classes=40, shuffle=True,  **kwargs):\n#         self.train_files = train_files\n#         self.label_segmented_files = label_segmented_files\n#         self.batch_size = batch_size\n#         self.img_size = img_size\n#         self.label_size = label_size\n#         self.num_classes = num_classes\n#         self.shuffle = shuffle\n#         self.indices = np.arange(len(self.train_files))\n        \n#         # Call the base class constructor to ensure it initializes correctly\n#         super().__init__(**kwargs)\n        \n#         self.on_epoch_end()\n\n#     def __len__(self):\n#         return int(np.floor(len(self.train_files) / self.batch_size))\n\n#     def __getitem__(self, index):\n#         # Generate one batch of data\n#         batch_indices = self.indices[index * self.batch_size:(index + 1) * self.batch_size]\n#         return self.__data_generation(batch_indices)\n\n#     def on_epoch_end(self):\n#         if self.shuffle:\n#             np.random.shuffle(self.indices)\n\n#     def __data_generation(self, batch_indices):\n#         # Initialize arrays to hold the batch data\n#         X_batch = np.empty((self.batch_size, *self.img_size, 3))  # RGB images\n#         y_batch = np.empty((self.batch_size, *self.label_size, self.num_classes))  # One-hot encoded labels\n\n#         for i, idx in enumerate(batch_indices):\n#             # Load and preprocess RGB image\n#             rgb_image = preprocess_rgb_image(self.train_files[idx])\n#             X_batch[i,] = rgb_image\n\n#             # Load and preprocess segmented image\n#             segmented_image = cv2.imread(self.label_segmented_files[idx], cv2.IMREAD_COLOR)  # Read segmented image\n#             one_hot_label = preprocess_segmented_image(segmented_image, num_classes=self.num_classes)\n#             y_batch[i,] = one_hot_label\n\n#         return X_batch, y_batch","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:30.505373Z","iopub.execute_input":"2024-10-11T03:06:30.505678Z","iopub.status.idle":"2024-10-11T03:06:30.515107Z","shell.execute_reply.started":"2024-10-11T03:06:30.505646Z","shell.execute_reply":"2024-10-11T03:06:30.51429Z"},"trusted":true},"execution_count":73,"outputs":[]},{"cell_type":"code","source":"# Function to load image and its corresponding label (segmented image)\n# Function to load image and its corresponding label (segmented image)\ndef load_data(image_path, label_path):\n    image_path = image_path.numpy().decode('utf-8')  # Convert to string\n    label_path = label_path.numpy().decode('utf-8')  # Convert to string\n\n    image = tf.io.read_file(image_path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, [540, 960])  # Resize input image\n    image.set_shape([540, 960, 3])  # Set explicit shape for the image\n\n    label = preprocess_segmented_image(label_path)  # Preprocess segmented label\n    # No need to set shape here since it's done in preprocess_segmented_image\n\n    return image, label\n\n# Create TensorFlow Dataset\ndef create_dataset(image_paths, label_paths, batch_size=8, buffer_size=1000, num_classes=40):\n    dataset = tf.data.Dataset.from_tensor_slices((image_paths, label_paths))\n    \n    # Map the preprocessing function to the dataset\n    dataset = dataset.map(lambda x, y: tf.py_function(\n        func=load_data, inp=[x, y], Tout=(tf.float32, tf.float32)), \n        num_parallel_calls=tf.data.AUTOTUNE)\n    \n    # Explicitly set shapes after the py_function call\n    dataset = dataset.map(lambda x, y: (tf.ensure_shape(x, [540, 960, 3]),\n                                        tf.ensure_shape(y, [1080, 1920, 40])),\n                                        num_parallel_calls=tf.data.AUTOTUNE)\n    \n    # Shuffle, batch, prefetch\n    dataset = dataset.shuffle(buffer_size).batch(batch_size).prefetch(buffer_size=tf.data.AUTOTUNE)\n    \n    return dataset","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:09:35.126898Z","iopub.execute_input":"2024-10-11T03:09:35.127617Z","iopub.status.idle":"2024-10-11T03:09:35.138599Z","shell.execute_reply.started":"2024-10-11T03:09:35.127573Z","shell.execute_reply":"2024-10-11T03:09:35.137711Z"},"trusted":true},"execution_count":90,"outputs":[]},{"cell_type":"code","source":"# Assuming train_image_paths and train_label_paths are lists of paths\ntrain_dataset = create_dataset(train_files, label_train_files, batch_size=8)\nval_dataset = create_dataset(val_files , label_val_files , batch_size=8)\n\n# train_generator = DataGenerator(train_files, label_train_files, batch_size=8)\n# val_generator = DataGenerator(val_files , label_val_files , batch_size=8)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:09:38.387133Z","iopub.execute_input":"2024-10-11T03:09:38.387531Z","iopub.status.idle":"2024-10-11T03:09:38.494023Z","shell.execute_reply.started":"2024-10-11T03:09:38.387493Z","shell.execute_reply":"2024-10-11T03:09:38.493056Z"},"trusted":true},"execution_count":91,"outputs":[]},{"cell_type":"markdown","source":"#  Building the U-Net Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, Concatenate, ZeroPadding2D\nfrom tensorflow.keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:30.678027Z","iopub.execute_input":"2024-10-11T03:06:30.678338Z","iopub.status.idle":"2024-10-11T03:06:30.682804Z","shell.execute_reply.started":"2024-10-11T03:06:30.678292Z","shell.execute_reply":"2024-10-11T03:06:30.681793Z"},"trusted":true},"execution_count":76,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, Concatenate, Dropout, BatchNormalization\nfrom tensorflow.keras.models import Model\n\ndef unet_model(input_shape=(540, 960, 3), output_channels=40):\n    inputs = Input(input_shape)\n    print(f\"Input shape: {inputs.shape}\")\n\n    # Contracting Path (Encoder)\n    c1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)\n    c1 = BatchNormalization()(c1)\n    c1 = Conv2D(32, (3, 3), activation='relu', padding='same')(c1)\n    c1 = BatchNormalization()(c1)\n    p1 = MaxPooling2D((2, 2))(c1)\n    print(f\"c1 shape: {c1.shape}, p1 shape: {p1.shape}\")\n\n    c2 = Conv2D(64, (3, 3), activation='relu', padding='same')(p1)\n    c2 = BatchNormalization()(c2)\n    c2 = Conv2D(64, (3, 3), activation='relu', padding='same')(c2)\n    c2 = BatchNormalization()(c2)\n    p2 = MaxPooling2D((2, 2))(c2)\n    print(f\"c2 shape: {c2.shape}, p2 shape: {p2.shape}\")\n\n    c3 = Conv2D(128, (3, 3), activation='relu', padding='same')(p2)\n    c3 = BatchNormalization()(c3)\n    c3 = Conv2D(128, (3, 3), activation='relu', padding='same')(c3)\n    c3 = BatchNormalization()(c3)\n    p3 = MaxPooling2D((2, 2))(c3)\n    print(f\"c3 shape: {c3.shape}, p3 shape: {p3.shape}\")\n\n    c4 = Conv2D(256, (3, 3), activation='relu', padding='same')(p3)\n    c4 = BatchNormalization()(c4)\n    c4 = Conv2D(256, (3, 3), activation='relu', padding='same')(c4)\n    c4 = BatchNormalization()(c4)\n    print(f\"c4 shape: {c4.shape}\")\n\n    # Expansive Path (Decoder)\n    u5 = UpSampling2D((2, 2))(c4)\n    print(f\"u5 shape (after upsampling): {u5.shape}\")\n    \n    u5 = ZeroPadding2D(((1, 0), (0, 0)))(u5)  # Adjust size if needed\n    u5 = Concatenate()([u5, c3])\n    print(f\"u5 shape (after concatenate with c3): {u5.shape}\")\n\n    c5 = Conv2D(128, (3, 3), activation='relu', padding='same')(u5)\n    c5 = BatchNormalization()(c5)\n    c5 = Conv2D(128, (3, 3), activation='relu', padding='same')(c5)\n    c5 = BatchNormalization()(c5)\n    print(f\"c5 shape: {c5.shape}\")\n\n    u6 = UpSampling2D((2, 2))(c5)\n    print(f\"u6 shape (after upsampling): {u6.shape}\")\n    \n    u6 = Concatenate()([u6, c2])\n    print(f\"u6 shape (after concatenate with c2): {u6.shape}\")\n\n    c6 = Conv2D(64, (3, 3), activation='relu', padding='same')(u6)\n    c6 = BatchNormalization()(c6)\n    c6 = Conv2D(64, (3, 3), activation='relu', padding='same')(c6)\n    c6 = BatchNormalization()(c6)\n    print(f\"c6 shape: {c6.shape}\")\n\n    u7 = UpSampling2D((2, 2))(c6)\n    print(f\"u7 shape (after upsampling): {u7.shape}\")\n    \n    u7 = Concatenate()([u7, c1])\n    print(f\"u7 shape (after concatenate with c1): {u7.shape}\")\n\n    c7 = Conv2D(32, (3, 3), activation='relu', padding='same')(u7)\n    c7 = BatchNormalization()(c7)\n    c7 = Conv2D(32, (3, 3), activation='relu', padding='same')(c7)\n    c7 = BatchNormalization()(c7)\n    print(f\"c7 shape: {c7.shape}\")\n\n    # Final Convolution Layer - Output Layer\n    outputs = UpSampling2D((2, 2))(c7)  # UpSample to (1080, 1920)\n    print(f\"Output shape before final convolution: {outputs.shape}\")\n\n    outputs = Conv2D(output_channels, (1, 1), activation='softmax')(outputs)  # Use softmax for multi-class\n    print(f\"Final output shape: {outputs.shape}\")\n\n    model = Model(inputs, outputs)\n    return model\n\n# Instantiate the model\nmodel = unet_model()","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:30.684159Z","iopub.execute_input":"2024-10-11T03:06:30.684487Z","iopub.status.idle":"2024-10-11T03:06:30.928774Z","shell.execute_reply.started":"2024-10-11T03:06:30.684455Z","shell.execute_reply":"2024-10-11T03:06:30.927864Z"},"trusted":true},"execution_count":77,"outputs":[{"name":"stdout","text":"Input shape: (None, 540, 960, 3)\nc1 shape: (None, 540, 960, 32), p1 shape: (None, 270, 480, 32)\nc2 shape: (None, 270, 480, 64), p2 shape: (None, 135, 240, 64)\nc3 shape: (None, 135, 240, 128), p3 shape: (None, 67, 120, 128)\nc4 shape: (None, 67, 120, 256)\nu5 shape (after upsampling): (None, 134, 240, 256)\nu5 shape (after concatenate with c3): (None, 135, 240, 384)\nc5 shape: (None, 135, 240, 128)\nu6 shape (after upsampling): (None, 270, 480, 128)\nu6 shape (after concatenate with c2): (None, 270, 480, 192)\nc6 shape: (None, 270, 480, 64)\nu7 shape (after upsampling): (None, 540, 960, 64)\nu7 shape (after concatenate with c1): (None, 540, 960, 96)\nc7 shape: (None, 540, 960, 32)\nOutput shape before final convolution: (None, 1080, 1920, 32)\nFinal output shape: (None, 1080, 1920, 40)\n","output_type":"stream"}]},{"cell_type":"code","source":"# def unet_model(input_shape=(540, 960, 3), output_channels=40):  # 36 classes\n#     inputs = Input(input_shape)\n#     print(f\"Input shape: {inputs.shape}\")\n\n#     # Contracting Path (Encoder)\n#     c1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)\n#     c1 = Conv2D(32, (3, 3), activation='relu', padding='same')(c1)\n#     p1 = MaxPooling2D((2, 2))(c1)\n#     print(f\"c1 shape: {c1.shape}\")\n#     print(f\"p1 shape: {p1.shape}\")\n\n#     c2 = Conv2D(64, (3, 3), activation='relu', padding='same')(p1)\n#     c2 = Conv2D(64, (3, 3), activation='relu', padding='same')(c2)\n#     p2 = MaxPooling2D((2, 2))(c2)\n#     print(f\"c2 shape: {c2.shape}\")\n#     print(f\"p2 shape: {p2.shape}\")\n\n#     c3 = Conv2D(128, (3, 3), activation='relu', padding='same')(p2)\n#     c3 = Conv2D(128, (3, 3), activation='relu', padding='same')(c3)\n#     p3 = MaxPooling2D((2, 2))(c3)\n#     print(f\"c3 shape: {c3.shape}\")\n#     print(f\"p3 shape: {p3.shape}\")\n\n#     c4 = Conv2D(256, (3, 3), activation='relu', padding='same')(p3)\n#     c4 = Conv2D(256, (3, 3), activation='relu', padding='same')(c4)\n#     print(f\"c4 shape: {c4.shape}\")\n\n#     # Expansive Path (Decoder)\n#     u5 = UpSampling2D((2, 2))(c4)\n#     print(f\"u5 shape (after upsampling): {u5.shape}\")\n    \n#     u5 = ZeroPadding2D(((1, 0), (0, 0)))(u5)  # Zero padding if needed\n#     print(f\"u5 shape (after zero padding): {u5.shape}\")\n    \n#     u5 = Concatenate()([u5, c3])\n#     print(f\"u5 shape (after concatenate with c3): {u5.shape}\")\n\n#     c5 = Conv2D(128, (3, 3), activation='relu', padding='same')(u5)\n#     c5 = Conv2D(128, (3, 3), activation='relu', padding='same')(c5)\n#     print(f\"c5 shape: {c5.shape}\")\n\n#     u6 = UpSampling2D((2, 2))(c5)\n#     print(f\"u6 shape (after upsampling): {u6.shape}\")\n    \n#     u6 = Concatenate()([u6, c2])\n#     print(f\"u6 shape (after concatenate with c2): {u6.shape}\")\n\n#     c6 = Conv2D(64, (3, 3), activation='relu', padding='same')(u6)\n#     c6 = Conv2D(64, (3, 3), activation='relu', padding='same')(c6)\n#     print(f\"c6 shape: {c6.shape}\")\n\n#     u7 = UpSampling2D((2, 2))(c6)\n#     print(f\"u7 shape (after upsampling): {u7.shape}\")\n    \n#     u7 = Concatenate()([u7, c1])\n#     print(f\"u7 shape (after concatenate with c1): {u7.shape}\")\n\n#     c7 = Conv2D(32, (3, 3), activation='relu', padding='same')(u7)\n#     c7 = Conv2D(32, (3, 3), activation='relu', padding='same')(c7)\n#     print(f\"c7 shape: {c7.shape}\")\n\n#     # Final Convolution Layer - Output Layer\n#     outputs = UpSampling2D((2, 2))(c7)  # UpSample to (1080, 1920)\n#     print(f\"Output shape before final convolution: {outputs.shape}\")\n\n#     outputs = Conv2D(output_channels, (1, 1), activation='softmax')(outputs)  # Use softmax for multi-class\n#     print(f\"Final output shape: {outputs.shape}\")\n\n#     model = Model(inputs, outputs)\n#     return model\n\n# # Instantiate the model with 36 classes (0 to 35)\n# model = unet_model()","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:30.929856Z","iopub.execute_input":"2024-10-11T03:06:30.930121Z","iopub.status.idle":"2024-10-11T03:06:30.936567Z","shell.execute_reply.started":"2024-10-11T03:06:30.930092Z","shell.execute_reply":"2024-10-11T03:06:30.935492Z"},"trusted":true},"execution_count":78,"outputs":[]},{"cell_type":"code","source":"# Summary of the model\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:30.937729Z","iopub.execute_input":"2024-10-11T03:06:30.938032Z","iopub.status.idle":"2024-10-11T03:06:31.010554Z","shell.execute_reply.started":"2024-10-11T03:06:30.938001Z","shell.execute_reply":"2024-10-11T03:06:31.009748Z"},"trusted":true},"execution_count":79,"outputs":[{"output_type":"display_data","data":{"text/plain":"\u001b[1mModel: \"functional_3\"\u001b[0m\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">Model: \"functional_3\"</span>\n</pre>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓\n┃\u001b[1m \u001b[0m\u001b[1mLayer (type)       \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape     \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m   Param #\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mConnected to     \u001b[0m\u001b[1m \u001b[0m┃\n┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩\n│ input_layer_1       │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ -                 │\n│ (\u001b[38;5;33mInputLayer\u001b[0m)        │ \u001b[38;5;34m3\u001b[0m)                │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_15 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │        \u001b[38;5;34m896\u001b[0m │ input_layer_1[\u001b[38;5;34m0\u001b[0m]… │\n│                     │ \u001b[38;5;34m32\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │        \u001b[38;5;34m128\u001b[0m │ conv2d_15[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m32\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_16 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │      \u001b[38;5;34m9,248\u001b[0m │ batch_normalizat… │\n│                     │ \u001b[38;5;34m32\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │        \u001b[38;5;34m128\u001b[0m │ conv2d_16[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m32\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ max_pooling2d_3     │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ batch_normalizat… │\n│ (\u001b[38;5;33mMaxPooling2D\u001b[0m)      │ \u001b[38;5;34m32\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_17 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │     \u001b[38;5;34m18,496\u001b[0m │ max_pooling2d_3[\u001b[38;5;34m…\u001b[0m │\n│                     │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │        \u001b[38;5;34m256\u001b[0m │ conv2d_17[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_18 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │     \u001b[38;5;34m36,928\u001b[0m │ batch_normalizat… │\n│                     │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │        \u001b[38;5;34m256\u001b[0m │ conv2d_18[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ max_pooling2d_4     │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ batch_normalizat… │\n│ (\u001b[38;5;33mMaxPooling2D\u001b[0m)      │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_19 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │     \u001b[38;5;34m73,856\u001b[0m │ max_pooling2d_4[\u001b[38;5;34m…\u001b[0m │\n│                     │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │        \u001b[38;5;34m512\u001b[0m │ conv2d_19[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_20 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │    \u001b[38;5;34m147,584\u001b[0m │ batch_normalizat… │\n│                     │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │        \u001b[38;5;34m512\u001b[0m │ conv2d_20[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ max_pooling2d_5     │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m67\u001b[0m, \u001b[38;5;34m120\u001b[0m,   │          \u001b[38;5;34m0\u001b[0m │ batch_normalizat… │\n│ (\u001b[38;5;33mMaxPooling2D\u001b[0m)      │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_21 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m67\u001b[0m, \u001b[38;5;34m120\u001b[0m,   │    \u001b[38;5;34m295,168\u001b[0m │ max_pooling2d_5[\u001b[38;5;34m…\u001b[0m │\n│                     │ \u001b[38;5;34m256\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m67\u001b[0m, \u001b[38;5;34m120\u001b[0m,   │      \u001b[38;5;34m1,024\u001b[0m │ conv2d_21[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m256\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_22 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m67\u001b[0m, \u001b[38;5;34m120\u001b[0m,   │    \u001b[38;5;34m590,080\u001b[0m │ batch_normalizat… │\n│                     │ \u001b[38;5;34m256\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m67\u001b[0m, \u001b[38;5;34m120\u001b[0m,   │      \u001b[38;5;34m1,024\u001b[0m │ conv2d_22[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m256\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ up_sampling2d_4     │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m134\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ batch_normalizat… │\n│ (\u001b[38;5;33mUpSampling2D\u001b[0m)      │ \u001b[38;5;34m256\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ zero_padding2d_1    │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ up_sampling2d_4[\u001b[38;5;34m…\u001b[0m │\n│ (\u001b[38;5;33mZeroPadding2D\u001b[0m)     │ \u001b[38;5;34m256\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ concatenate_3       │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ zero_padding2d_1… │\n│ (\u001b[38;5;33mConcatenate\u001b[0m)       │ \u001b[38;5;34m384\u001b[0m)              │            │ batch_normalizat… │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_23 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │    \u001b[38;5;34m442,496\u001b[0m │ concatenate_3[\u001b[38;5;34m0\u001b[0m]… │\n│                     │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │        \u001b[38;5;34m512\u001b[0m │ conv2d_23[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_24 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │    \u001b[38;5;34m147,584\u001b[0m │ batch_normalizat… │\n│                     │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m135\u001b[0m, \u001b[38;5;34m240\u001b[0m,  │        \u001b[38;5;34m512\u001b[0m │ conv2d_24[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ up_sampling2d_5     │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ batch_normalizat… │\n│ (\u001b[38;5;33mUpSampling2D\u001b[0m)      │ \u001b[38;5;34m128\u001b[0m)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ concatenate_4       │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ up_sampling2d_5[\u001b[38;5;34m…\u001b[0m │\n│ (\u001b[38;5;33mConcatenate\u001b[0m)       │ \u001b[38;5;34m192\u001b[0m)              │            │ batch_normalizat… │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_25 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │    \u001b[38;5;34m110,656\u001b[0m │ concatenate_4[\u001b[38;5;34m0\u001b[0m]… │\n│                     │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │        \u001b[38;5;34m256\u001b[0m │ conv2d_25[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_26 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │     \u001b[38;5;34m36,928\u001b[0m │ batch_normalizat… │\n│                     │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m270\u001b[0m, \u001b[38;5;34m480\u001b[0m,  │        \u001b[38;5;34m256\u001b[0m │ conv2d_26[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ up_sampling2d_6     │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ batch_normalizat… │\n│ (\u001b[38;5;33mUpSampling2D\u001b[0m)      │ \u001b[38;5;34m64\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ concatenate_5       │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │          \u001b[38;5;34m0\u001b[0m │ up_sampling2d_6[\u001b[38;5;34m…\u001b[0m │\n│ (\u001b[38;5;33mConcatenate\u001b[0m)       │ \u001b[38;5;34m96\u001b[0m)               │            │ batch_normalizat… │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_27 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │     \u001b[38;5;34m27,680\u001b[0m │ concatenate_5[\u001b[38;5;34m0\u001b[0m]… │\n│                     │ \u001b[38;5;34m32\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │        \u001b[38;5;34m128\u001b[0m │ conv2d_27[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m32\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_28 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │      \u001b[38;5;34m9,248\u001b[0m │ batch_normalizat… │\n│                     │ \u001b[38;5;34m32\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m540\u001b[0m, \u001b[38;5;34m960\u001b[0m,  │        \u001b[38;5;34m128\u001b[0m │ conv2d_28[\u001b[38;5;34m0\u001b[0m][\u001b[38;5;34m0\u001b[0m]   │\n│ (\u001b[38;5;33mBatchNormalizatio…\u001b[0m │ \u001b[38;5;34m32\u001b[0m)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ up_sampling2d_7     │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1080\u001b[0m,      │          \u001b[38;5;34m0\u001b[0m │ batch_normalizat… │\n│ (\u001b[38;5;33mUpSampling2D\u001b[0m)      │ \u001b[38;5;34m1920\u001b[0m, \u001b[38;5;34m32\u001b[0m)         │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_29 (\u001b[38;5;33mConv2D\u001b[0m)  │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m1080\u001b[0m,      │      \u001b[38;5;34m1,320\u001b[0m │ up_sampling2d_7[\u001b[38;5;34m…\u001b[0m │\n│                     │ \u001b[38;5;34m1920\u001b[0m, \u001b[38;5;34m40\u001b[0m)         │            │                   │\n└─────────────────────┴───────────────────┴────────────┴───────────────────┘\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">┏━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓\n┃<span style=\"font-weight: bold\"> Layer (type)        </span>┃<span style=\"font-weight: bold\"> Output Shape      </span>┃<span style=\"font-weight: bold\">    Param # </span>┃<span style=\"font-weight: bold\"> Connected to      </span>┃\n┡━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩\n│ input_layer_1       │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ -                 │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">InputLayer</span>)        │ <span style=\"color: #00af00; text-decoration-color: #00af00\">3</span>)                │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_15 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">896</span> │ input_layer_1[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span> │ conv2d_15[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_16 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │      <span style=\"color: #00af00; text-decoration-color: #00af00\">9,248</span> │ batch_normalizat… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span> │ conv2d_16[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ max_pooling2d_3     │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ batch_normalizat… │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)      │ <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_17 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │     <span style=\"color: #00af00; text-decoration-color: #00af00\">18,496</span> │ max_pooling2d_3[<span style=\"color: #00af00; text-decoration-color: #00af00\">…</span> │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span> │ conv2d_17[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_18 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │     <span style=\"color: #00af00; text-decoration-color: #00af00\">36,928</span> │ batch_normalizat… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span> │ conv2d_18[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ max_pooling2d_4     │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ batch_normalizat… │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)      │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_19 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │     <span style=\"color: #00af00; text-decoration-color: #00af00\">73,856</span> │ max_pooling2d_4[<span style=\"color: #00af00; text-decoration-color: #00af00\">…</span> │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span> │ conv2d_19[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_20 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │    <span style=\"color: #00af00; text-decoration-color: #00af00\">147,584</span> │ batch_normalizat… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span> │ conv2d_20[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ max_pooling2d_5     │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">67</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">120</span>,   │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ batch_normalizat… │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">MaxPooling2D</span>)      │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_21 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">67</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">120</span>,   │    <span style=\"color: #00af00; text-decoration-color: #00af00\">295,168</span> │ max_pooling2d_5[<span style=\"color: #00af00; text-decoration-color: #00af00\">…</span> │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">67</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">120</span>,   │      <span style=\"color: #00af00; text-decoration-color: #00af00\">1,024</span> │ conv2d_21[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_22 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">67</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">120</span>,   │    <span style=\"color: #00af00; text-decoration-color: #00af00\">590,080</span> │ batch_normalizat… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">67</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">120</span>,   │      <span style=\"color: #00af00; text-decoration-color: #00af00\">1,024</span> │ conv2d_22[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ up_sampling2d_4     │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">134</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ batch_normalizat… │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">UpSampling2D</span>)      │ <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ zero_padding2d_1    │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ up_sampling2d_4[<span style=\"color: #00af00; text-decoration-color: #00af00\">…</span> │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">ZeroPadding2D</span>)     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ concatenate_3       │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ zero_padding2d_1… │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Concatenate</span>)       │ <span style=\"color: #00af00; text-decoration-color: #00af00\">384</span>)              │            │ batch_normalizat… │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_23 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │    <span style=\"color: #00af00; text-decoration-color: #00af00\">442,496</span> │ concatenate_3[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span> │ conv2d_23[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_24 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │    <span style=\"color: #00af00; text-decoration-color: #00af00\">147,584</span> │ batch_normalizat… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">135</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">240</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">512</span> │ conv2d_24[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ up_sampling2d_5     │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ batch_normalizat… │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">UpSampling2D</span>)      │ <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)              │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ concatenate_4       │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ up_sampling2d_5[<span style=\"color: #00af00; text-decoration-color: #00af00\">…</span> │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Concatenate</span>)       │ <span style=\"color: #00af00; text-decoration-color: #00af00\">192</span>)              │            │ batch_normalizat… │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_25 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │    <span style=\"color: #00af00; text-decoration-color: #00af00\">110,656</span> │ concatenate_4[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span> │ conv2d_25[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_26 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │     <span style=\"color: #00af00; text-decoration-color: #00af00\">36,928</span> │ batch_normalizat… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">270</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">480</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">256</span> │ conv2d_26[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ up_sampling2d_6     │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ batch_normalizat… │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">UpSampling2D</span>)      │ <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ concatenate_5       │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ up_sampling2d_6[<span style=\"color: #00af00; text-decoration-color: #00af00\">…</span> │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Concatenate</span>)       │ <span style=\"color: #00af00; text-decoration-color: #00af00\">96</span>)               │            │ batch_normalizat… │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_27 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │     <span style=\"color: #00af00; text-decoration-color: #00af00\">27,680</span> │ concatenate_5[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span> │ conv2d_27[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_28 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │      <span style=\"color: #00af00; text-decoration-color: #00af00\">9,248</span> │ batch_normalizat… │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ batch_normalizatio… │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">540</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">960</span>,  │        <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span> │ conv2d_28[<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>][<span style=\"color: #00af00; text-decoration-color: #00af00\">0</span>]   │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">BatchNormalizatio…</span> │ <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)               │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ up_sampling2d_7     │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1080</span>,      │          <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> │ batch_normalizat… │\n│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">UpSampling2D</span>)      │ <span style=\"color: #00af00; text-decoration-color: #00af00\">1920</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">32</span>)         │            │                   │\n├─────────────────────┼───────────────────┼────────────┼───────────────────┤\n│ conv2d_29 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Conv2D</span>)  │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">1080</span>,      │      <span style=\"color: #00af00; text-decoration-color: #00af00\">1,320</span> │ up_sampling2d_7[<span style=\"color: #00af00; text-decoration-color: #00af00\">…</span> │\n│                     │ <span style=\"color: #00af00; text-decoration-color: #00af00\">1920</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">40</span>)         │            │                   │\n└─────────────────────┴───────────────────┴────────────┴───────────────────┘\n</pre>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"\u001b[1m Total params: \u001b[0m\u001b[38;5;34m1,953,800\u001b[0m (7.45 MB)\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Total params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">1,953,800</span> (7.45 MB)\n</pre>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m1,950,984\u001b[0m (7.44 MB)\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">1,950,984</span> (7.44 MB)\n</pre>\n"},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m2,816\u001b[0m (11.00 KB)\n","text/html":"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Non-trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">2,816</span> (11.00 KB)\n</pre>\n"},"metadata":{}}]},{"cell_type":"markdown","source":"# Dice Loss and Focal Loss","metadata":{}},{"cell_type":"code","source":"def dice_loss(y_true, y_pred, smooth=1e-6):\n    y_true_f = tf.cast(tf.reshape(y_true, [-1]), tf.float32)\n    y_pred_f = tf.cast(tf.reshape(y_pred, [-1]), tf.float32)\n    intersection = tf.reduce_sum(y_true_f * y_pred_f)\n    return 1 - (2. * intersection + smooth) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + smooth)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:31.011547Z","iopub.execute_input":"2024-10-11T03:06:31.011821Z","iopub.status.idle":"2024-10-11T03:06:31.017271Z","shell.execute_reply.started":"2024-10-11T03:06:31.01179Z","shell.execute_reply":"2024-10-11T03:06:31.016384Z"},"trusted":true},"execution_count":80,"outputs":[]},{"cell_type":"code","source":"def focal_loss(y_true, y_pred, alpha=0.25, gamma=2.0):\n    y_true = tf.cast(y_true, tf.float32)\n    y_pred = tf.cast(y_pred, tf.float32)\n    y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon())\n    cross_entropy = -y_true * tf.math.log(y_pred)\n    weight = alpha * tf.math.pow(1 - y_pred, gamma)\n    loss = weight * cross_entropy\n    return tf.reduce_sum(loss, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:31.018659Z","iopub.execute_input":"2024-10-11T03:06:31.019037Z","iopub.status.idle":"2024-10-11T03:06:31.026063Z","shell.execute_reply.started":"2024-10-11T03:06:31.018993Z","shell.execute_reply":"2024-10-11T03:06:31.025178Z"},"trusted":true},"execution_count":81,"outputs":[]},{"cell_type":"code","source":"def combined_dice_focal_loss(y_true, y_pred, dice_weight=0.5, focal_weight=0.5):\n    \"\"\"\n    Combine Dice loss and Focal loss into a single loss function.\n\n    y_true: Ground truth labels.\n    y_pred: Model predictions.\n    dice_weight: Weight for Dice loss.\n    focal_weight: Weight for Focal loss.\n\n    Returns:\n    - Combined loss.\n    \"\"\"\n    dice = dice_loss(y_true, y_pred)\n    focal = focal_loss(y_true, y_pred)\n    combined_loss = dice_weight * dice + focal_weight * focal\n    return tf.cast(combined_loss, tf.float32)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:31.027228Z","iopub.execute_input":"2024-10-11T03:06:31.027598Z","iopub.status.idle":"2024-10-11T03:06:31.034129Z","shell.execute_reply.started":"2024-10-11T03:06:31.027558Z","shell.execute_reply":"2024-10-11T03:06:31.033339Z"},"trusted":true},"execution_count":82,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation Metrics","metadata":{}},{"cell_type":"markdown","source":"**Dice Coefficients**","metadata":{}},{"cell_type":"code","source":"def dice_coefficient(y_true, y_pred, smooth=1e-6):\n    # Cast both y_true and y_pred to float32 to ensure they have the same data type\n    y_true_f = tf.cast(tf.reshape(y_true, [-1]), tf.float32)\n    y_pred_f = tf.cast(tf.reshape(y_pred, [-1]), tf.float32)\n    intersection = tf.reduce_sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + smooth)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:31.035628Z","iopub.execute_input":"2024-10-11T03:06:31.036363Z","iopub.status.idle":"2024-10-11T03:06:31.045287Z","shell.execute_reply.started":"2024-10-11T03:06:31.036307Z","shell.execute_reply":"2024-10-11T03:06:31.044598Z"},"trusted":true},"execution_count":83,"outputs":[]},{"cell_type":"markdown","source":"**F score with beta parameter**","metadata":{}},{"cell_type":"code","source":"def precision(y_true, y_pred):\n    y_true = tf.cast(y_true, tf.float32)  # Ensure y_true is of type float32\n    y_pred = tf.cast(y_pred, tf.float32)  # Ensure y_pred is of type float32\n    true_positives = tf.reduce_sum(tf.cast(y_true * y_pred, tf.float32))\n    predicted_positives = tf.reduce_sum(tf.cast(y_pred, tf.float32))\n    precision = true_positives / (predicted_positives + tf.keras.backend.epsilon())\n    return precision\n\ndef recall(y_true, y_pred):\n    y_true = tf.cast(y_true, tf.float32)  # Ensure y_true is of type float32\n    y_pred = tf.cast(y_pred, tf.float32)  # Ensure y_pred is of type float32\n    true_positives = tf.reduce_sum(tf.cast(y_true * y_pred, tf.float32))\n    possible_positives = tf.reduce_sum(tf.cast(y_true, tf.float32))\n    recall = true_positives / (possible_positives + tf.keras.backend.epsilon())\n    return recall\n\ndef f_score(y_true, y_pred, beta=1):\n    y_true = tf.cast(y_true, tf.float32)  # Ensure y_true is of type float32\n    y_pred = tf.cast(y_pred, tf.float32)  # Ensure y_pred is of type float32\n    prec = precision(y_true, y_pred)\n    rec = recall(y_true, y_pred)\n    beta_squared = beta ** 2\n    return (1 + beta_squared) * (prec * rec) / (beta_squared * prec + rec + tf.keras.backend.epsilon())","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:31.046421Z","iopub.execute_input":"2024-10-11T03:06:31.046721Z","iopub.status.idle":"2024-10-11T03:06:31.056071Z","shell.execute_reply.started":"2024-10-11T03:06:31.046677Z","shell.execute_reply":"2024-10-11T03:06:31.055145Z"},"trusted":true},"execution_count":84,"outputs":[]},{"cell_type":"markdown","source":"**Mean Dice Coefficient with F-Score**\n","metadata":{}},{"cell_type":"code","source":"def mean_dice_fscore(y_true, y_pred, beta=1):\n    dice = dice_coefficient(y_true, y_pred)\n    fscore = f_score(y_true, y_pred, beta=beta)\n    mean_metric = (dice + fscore) / 2.0  # Averaging Dice Coefficient and F-Score\n    return tf.cast(mean_metric, tf.float32) ","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:31.057256Z","iopub.execute_input":"2024-10-11T03:06:31.057739Z","iopub.status.idle":"2024-10-11T03:06:31.066191Z","shell.execute_reply.started":"2024-10-11T03:06:31.057696Z","shell.execute_reply":"2024-10-11T03:06:31.065371Z"},"trusted":true},"execution_count":85,"outputs":[]},{"cell_type":"markdown","source":"# Compiling and Training the Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\n# Define callbacks\nearly_stopping = EarlyStopping(\n    monitor='val_loss',  # You can monitor 'val_mean_dice_fscore' if desired\n    patience=10,         # Number of epochs with no improvement to wait before stopping\n    restore_best_weights=True\n)\n\nmodel_checkpoint = ModelCheckpoint(\n    'best_model.keras',  # Path to save the model\n    monitor='val_loss',  # Again, can monitor 'val_mean_dice_fscore'\n    save_best_only=True,\n    mode='min'  # Saves the model with the lowest validation loss\n)\n\nreduce_lr = ReduceLROnPlateau(\n    monitor='val_loss',  # Can also monitor 'val_mean_dice_fscore'\n    factor=0.5,         # Factor by which the learning rate will be reduced\n    patience=5,         # Number of epochs with no improvement\n    min_lr=1e-6         # Minimum learning rate\n) ","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:31.067223Z","iopub.execute_input":"2024-10-11T03:06:31.067591Z","iopub.status.idle":"2024-10-11T03:06:31.074239Z","shell.execute_reply.started":"2024-10-11T03:06:31.067558Z","shell.execute_reply":"2024-10-11T03:06:31.073537Z"},"trusted":true},"execution_count":86,"outputs":[]},{"cell_type":"code","source":"# Compile the model with the combined loss\nmodel.compile(optimizer='adam', loss=[combined_dice_focal_loss], metrics=[mean_dice_fscore])","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:09:46.546167Z","iopub.execute_input":"2024-10-11T03:09:46.54689Z","iopub.status.idle":"2024-10-11T03:09:46.557395Z","shell.execute_reply.started":"2024-10-11T03:09:46.546846Z","shell.execute_reply":"2024-10-11T03:09:46.556351Z"},"trusted":true},"execution_count":92,"outputs":[]},{"cell_type":"code","source":"# # Train the model with the callbacks\n# history = model.fit(\n#     train_generator,\n#     validation_data=val_generator,\n#     epochs=20,  # Set a suitable number of epochs\n#     callbacks=[early_stopping, model_checkpoint, reduce_lr]\n# )\n\n\n# Use this dataset with model training\nhistory = model.fit(\n    train_dataset,\n    validation_data=val_dataset,\n    epochs=20,\n    callbacks=[early_stopping, model_checkpoint, reduce_lr]\n)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:09:49.991289Z","iopub.execute_input":"2024-10-11T03:09:49.991995Z","iopub.status.idle":"2024-10-11T03:10:04.199252Z","shell.execute_reply.started":"2024-10-11T03:09:49.991951Z","shell.execute_reply":"2024-10-11T03:10:04.192375Z"},"trusted":true},"execution_count":93,"outputs":[{"name":"stdout","text":"Epoch 1/20\n","output_type":"stream"},{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mInvalidArgumentError\u001b[0m                      Traceback (most recent call last)","Cell \u001b[0;32mIn[93], line 11\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# # Train the model with the callbacks\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;66;03m# history = model.fit(\u001b[39;00m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;66;03m#     train_generator,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m      9\u001b[0m \n\u001b[1;32m     10\u001b[0m \u001b[38;5;66;03m# Use this dataset with model training\u001b[39;00m\n\u001b[0;32m---> 11\u001b[0m history \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m     12\u001b[0m \u001b[43m    \u001b[49m\u001b[43mtrain_dataset\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     13\u001b[0m \u001b[43m    \u001b[49m\u001b[43mvalidation_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mval_dataset\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     14\u001b[0m \u001b[43m    \u001b[49m\u001b[43mepochs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m20\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m     15\u001b[0m \u001b[43m    \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mearly_stopping\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel_checkpoint\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mreduce_lr\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m     16\u001b[0m \u001b[43m)\u001b[49m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:122\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    119\u001b[0m     filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n\u001b[1;32m    120\u001b[0m     \u001b[38;5;66;03m# To get the full stack trace, call:\u001b[39;00m\n\u001b[1;32m    121\u001b[0m     \u001b[38;5;66;03m# `keras.config.disable_traceback_filtering()`\u001b[39;00m\n\u001b[0;32m--> 122\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m e\u001b[38;5;241m.\u001b[39mwith_traceback(filtered_tb) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    123\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m    124\u001b[0m     \u001b[38;5;28;01mdel\u001b[39;00m filtered_tb\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/execute.py:53\u001b[0m, in \u001b[0;36mquick_execute\u001b[0;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[1;32m     51\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m     52\u001b[0m   ctx\u001b[38;5;241m.\u001b[39mensure_initialized()\n\u001b[0;32m---> 53\u001b[0m   tensors \u001b[38;5;241m=\u001b[39m pywrap_tfe\u001b[38;5;241m.\u001b[39mTFE_Py_Execute(ctx\u001b[38;5;241m.\u001b[39m_handle, device_name, op_name,\n\u001b[1;32m     54\u001b[0m                                       inputs, attrs, num_outputs)\n\u001b[1;32m     55\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m core\u001b[38;5;241m.\u001b[39m_NotOkStatusException \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m     56\u001b[0m   \u001b[38;5;28;01mif\u001b[39;00m name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n","\u001b[0;31mInvalidArgumentError\u001b[0m: Graph execution error:\n\nDetected at node EagerPyFunc defined at (most recent call last):\n<stack traces unavailable>\nDetected at node EagerPyFunc defined at (most recent call last):\n<stack traces unavailable>\n2 root error(s) found.\n  (0) INVALID_ARGUMENT:  ValueError: Tensor's shape (1920, 1080, 40) is not compatible with supplied shape [1080, 1920, 40].\nTraceback (most recent call last):\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 268, in __call__\n    return func(device, token, args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 146, in __call__\n    outputs = self._call(device, args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 153, in _call\n    ret = self._func(*args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py\", line 643, in wrapper\n    return func(*args, **kwargs)\n\n  File \"/tmp/ipykernel_30/688819844.py\", line 12, in load_data\n    label = preprocess_segmented_image(label_path)  # Preprocess segmented label\n\n  File \"/tmp/ipykernel_30/1165213442.py\", line 39, in preprocess_segmented_image\n    one_hot_label_tensor.set_shape([target_size[0], target_size[1], num_classes])  # Shape: (1080, 1920, num_classes)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/framework/ops.py\", line 551, in set_shape\n    raise ValueError(f\"Tensor's shape {self.shape} is not compatible \"\n\nValueError: Tensor's shape (1920, 1080, 40) is not compatible with supplied shape [1080, 1920, 40].\n\n\n\t [[{{node EagerPyFunc}}]]\n\t [[IteratorGetNext]]\n\t [[IteratorGetNext/_4]]\n  (1) INVALID_ARGUMENT:  ValueError: Tensor's shape (1920, 1080, 40) is not compatible with supplied shape [1080, 1920, 40].\nTraceback (most recent call last):\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 268, in __call__\n    return func(device, token, args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 146, in __call__\n    outputs = self._call(device, args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 153, in _call\n    ret = self._func(*args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py\", line 643, in wrapper\n    return func(*args, **kwargs)\n\n  File \"/tmp/ipykernel_30/688819844.py\", line 12, in load_data\n    label = preprocess_segmented_image(label_path)  # Preprocess segmented label\n\n  File \"/tmp/ipykernel_30/1165213442.py\", line 39, in preprocess_segmented_image\n    one_hot_label_tensor.set_shape([target_size[0], target_size[1], num_classes])  # Shape: (1080, 1920, num_classes)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/framework/ops.py\", line 551, in set_shape\n    raise ValueError(f\"Tensor's shape {self.shape} is not compatible \"\n\nValueError: Tensor's shape (1920, 1080, 40) is not compatible with supplied shape [1080, 1920, 40].\n\n\n\t [[{{node EagerPyFunc}}]]\n\t [[IteratorGetNext]]\n0 successful operations.\n0 derived errors ignored. [Op:__inference_one_step_on_iterator_62391]"],"ename":"InvalidArgumentError","evalue":"Graph execution error:\n\nDetected at node EagerPyFunc defined at (most recent call last):\n<stack traces unavailable>\nDetected at node EagerPyFunc defined at (most recent call last):\n<stack traces unavailable>\n2 root error(s) found.\n  (0) INVALID_ARGUMENT:  ValueError: Tensor's shape (1920, 1080, 40) is not compatible with supplied shape [1080, 1920, 40].\nTraceback (most recent call last):\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 268, in __call__\n    return func(device, token, args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 146, in __call__\n    outputs = self._call(device, args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 153, in _call\n    ret = self._func(*args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py\", line 643, in wrapper\n    return func(*args, **kwargs)\n\n  File \"/tmp/ipykernel_30/688819844.py\", line 12, in load_data\n    label = preprocess_segmented_image(label_path)  # Preprocess segmented label\n\n  File \"/tmp/ipykernel_30/1165213442.py\", line 39, in preprocess_segmented_image\n    one_hot_label_tensor.set_shape([target_size[0], target_size[1], num_classes])  # Shape: (1080, 1920, num_classes)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/framework/ops.py\", line 551, in set_shape\n    raise ValueError(f\"Tensor's shape {self.shape} is not compatible \"\n\nValueError: Tensor's shape (1920, 1080, 40) is not compatible with supplied shape [1080, 1920, 40].\n\n\n\t [[{{node EagerPyFunc}}]]\n\t [[IteratorGetNext]]\n\t [[IteratorGetNext/_4]]\n  (1) INVALID_ARGUMENT:  ValueError: Tensor's shape (1920, 1080, 40) is not compatible with supplied shape [1080, 1920, 40].\nTraceback (most recent call last):\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 268, in __call__\n    return func(device, token, args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 146, in __call__\n    outputs = self._call(device, args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/ops/script_ops.py\", line 153, in _call\n    ret = self._func(*args)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/autograph/impl/api.py\", line 643, in wrapper\n    return func(*args, **kwargs)\n\n  File \"/tmp/ipykernel_30/688819844.py\", line 12, in load_data\n    label = preprocess_segmented_image(label_path)  # Preprocess segmented label\n\n  File \"/tmp/ipykernel_30/1165213442.py\", line 39, in preprocess_segmented_image\n    one_hot_label_tensor.set_shape([target_size[0], target_size[1], num_classes])  # Shape: (1080, 1920, num_classes)\n\n  File \"/opt/conda/lib/python3.10/site-packages/tensorflow/python/framework/ops.py\", line 551, in set_shape\n    raise ValueError(f\"Tensor's shape {self.shape} is not compatible \"\n\nValueError: Tensor's shape (1920, 1080, 40) is not compatible with supplied shape [1080, 1920, 40].\n\n\n\t [[{{node EagerPyFunc}}]]\n\t [[IteratorGetNext]]\n0 successful operations.\n0 derived errors ignored. [Op:__inference_one_step_on_iterator_62391]","output_type":"error"}]},{"cell_type":"code","source":"# Check if TensorFlow is built with GPU support\nprint(\"Is TensorFlow built with GPU support?\", tf.test.is_built_with_cuda())\n\n# List available GPUs\ngpus = tf.config.list_physical_devices('GPU')\nif gpus:\n    print(f\"TensorFlow is using the following GPU(s): {gpus}\")\nelse:\n    print(\"No GPU found. TensorFlow is using the CPU.\")","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:55.892786Z","iopub.status.idle":"2024-10-11T03:06:55.893535Z","shell.execute_reply.started":"2024-10-11T03:06:55.893264Z","shell.execute_reply":"2024-10-11T03:06:55.89329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print device placement for operations\ntf.debugging.set_log_device_placement(True)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:55.898508Z","iopub.status.idle":"2024-10-11T03:06:55.899147Z","shell.execute_reply.started":"2024-10-11T03:06:55.898916Z","shell.execute_reply":"2024-10-11T03:06:55.89894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\ngpus = tf.config.experimental.list_physical_devices('GPU')\nif gpus:\n    try:\n        for gpu in gpus:\n            tf.config.experimental.set_memory_growth(gpu, True)\n        print(\"Memory growth enabled for the GPU(s)\")\n    except RuntimeError as e:\n        print(e)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:55.903972Z","iopub.status.idle":"2024-10-11T03:06:55.904627Z","shell.execute_reply.started":"2024-10-11T03:06:55.904386Z","shell.execute_reply":"2024-10-11T03:06:55.904411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.device('/GPU:0'):  # You can specify GPU:1 or others if needed\n    # Your model training code goes here","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:55.908559Z","iopub.status.idle":"2024-10-11T03:06:55.909184Z","shell.execute_reply.started":"2024-10-11T03:06:55.908956Z","shell.execute_reply":"2024-10-11T03:06:55.90898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:55.910293Z","iopub.status.idle":"2024-10-11T03:06:55.910944Z","shell.execute_reply.started":"2024-10-11T03:06:55.910718Z","shell.execute_reply":"2024-10-11T03:06:55.910742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unset CUDA_VISIBLE_DEVICES","metadata":{"execution":{"iopub.status.busy":"2024-10-11T03:06:55.919473Z","iopub.status.idle":"2024-10-11T03:06:55.921105Z","shell.execute_reply.started":"2024-10-11T03:06:55.920586Z","shell.execute_reply":"2024-10-11T03:06:55.920639Z"},"trusted":true},"execution_count":null,"outputs":[]}]}