{"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":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:00.173911Z","iopub.execute_input":"2024-10-12T06:17:00.174991Z","iopub.status.idle":"2024-10-12T06:17:02.115161Z","shell.execute_reply.started":"2024-10-12T06:17:00.174933Z","shell.execute_reply":"2024-10-12T06:17:02.114062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport json\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport PIL.ImageDraw as ImageDraw\nimport PIL.Image as Image\nimport matplotlib.pyplot as plt\nimport cv2\n\nfrom torchvision import transforms\nimport torch\nfrom torch.utils.data import DataLoader,random_split\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\nfrom tqdm import tqdm\nimport logging\nimport torchvision\nfrom transformers import SegformerForSemanticSegmentation, SegformerFeatureExtractor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:02.117289Z","iopub.execute_input":"2024-10-12T06:17:02.118096Z","iopub.status.idle":"2024-10-12T06:17:33.320247Z","shell.execute_reply.started":"2024-10-12T06:17:02.118049Z","shell.execute_reply":"2024-10-12T06:17:33.319196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path=\"/kaggle/input/iitg-ai-overnight-hackathon-2024/dataset/dataset\"\nim_height=128\nim_width=128","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:33.321375Z","iopub.execute_input":"2024-10-12T06:17:33.322276Z","iopub.status.idle":"2024-10-12T06:17:33.327202Z","shell.execute_reply.started":"2024-10-12T06:17:33.322220Z","shell.execute_reply":"2024-10-12T06:17:33.326206Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Load the mask image\nmask_path = '/kaggle/input/iitg-ai-overnight-hackathon-2024/dataset/dataset/labels/201/frame0029_gtFine_labelColors.png'\nmask_img = Image.open(mask_path)\n\n# Convert the mask image to a numpy array\nmask_array = np.array(mask_img)\n\n# Get unique values in the mask\nunique_values = np.unique(mask_array)\nplt.imshow(mask_img)\nplt.title(\"Original Mask Image\")\nplt.show()\n# Print the mask array and unique values\nprint(mask_array.shape)\nprint(\"Mask Array:\\n\", mask_array[1][2])\nprint(\"Unique Values:\", unique_values.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:33.330331Z","iopub.execute_input":"2024-10-12T06:17:33.330764Z","iopub.status.idle":"2024-10-12T06:17:34.214256Z","shell.execute_reply.started":"2024-10-12T06:17:33.330717Z","shell.execute_reply":"2024-10-12T06:17:34.213201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:34.215445Z","iopub.execute_input":"2024-10-12T06:17:34.215764Z","iopub.status.idle":"2024-10-12T06:17:34.269886Z","shell.execute_reply.started":"2024-10-12T06:17:34.215731Z","shell.execute_reply":"2024-10-12T06:17:34.268845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"colors = [\n    (128, 64, 128),   # 1road\n    (250, 170, 160),   # 2parking\n    (81, 0, 81),      # 3drivable fallback\n    (244, 35, 232),   # 4sidewalk\n    (230, 150, 140),  # 5rail track\n    (152, 251, 152),  # 6non-drivable fallback\n    (220, 20, 60),    # 7person\n    (246, 198, 145),  # 8animal\n    (255, 0, 0),      # 9rider\n    (0, 0, 230),      # 10motorcycle\n    (119, 11, 32),    # 11bicycle\n    (255, 204, 54),   # 12autorickshaw\n    (0, 0, 142),      # 13car\n    (0, 0, 70),       # 14truck\n    (0, 0, 90),       # 15caravan\n    (0, 0, 110),      # 16trailer\n    (0, 80, 100),     # 17train\n    (136, 143, 153),  # 18vehicle fallback\n    (220, 190, 40),   # 19curb\n    (102, 102, 156),  # 20wall\n    (190, 153, 153),  # 21fence\n    (180, 165, 180),  # 22guard rail\n    (174, 64, 67),    # 23billboard\n    (220, 220, 0),    # 24traffic sign\n    (250, 170, 30),   # 25traffic light\n    (153, 153, 153),  # 26 pole\n    (169, 187, 214),  # 27obs-str-bar-fallback\n    (70, 70, 70),     # 28building\n    (150, 100, 100),  # 29bridge\n    (150, 120, 90),   # 30tunnel\n    (107, 142, 35),   # 31vegetation\n    (70, 130, 180),   # 32sky\n    (0, 0, 0),        # 33unlabeled\n]\n\n# Create a color to label mapping\ncolor_to_label = {color: i for i, color in enumerate(colors)}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:34.271488Z","iopub.execute_input":"2024-10-12T06:17:34.271817Z","iopub.status.idle":"2024-10-12T06:17:34.283475Z","shell.execute_reply.started":"2024-10-12T06:17:34.271784Z","shell.execute_reply":"2024-10-12T06:17:34.282580Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((im_height, im_width)),     \n    transforms.ToTensor()                    \n])\nmask_transform = transforms.Compose([\n    transforms.Resize((im_height, im_width), interpolation=Image.NEAREST),  # Resize\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:34.284680Z","iopub.execute_input":"2024-10-12T06:17:34.285088Z","iopub.status.idle":"2024-10-12T06:17:34.296494Z","shell.execute_reply.started":"2024-10-12T06:17:34.285040Z","shell.execute_reply":"2024-10-12T06:17:34.295755Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def convert_mask_to_labels(mask_img):\n    \"\"\"Converts a mask image to a tensor of label indices.\"\"\"\n    # Convert the mask image to an array\n    mask_array = np.array(mask_img)\n    \n    # Initialize an empty array for labels\n    label_array = np.zeros(mask_array.shape[:2], dtype=np.int64)\n\n    # Iterate through the colors and map them to labels\n    for color, label in color_to_label.items():\n        label_array[(mask_array[:, :, 0] == color[0]) & \n                    (mask_array[:, :, 1] == color[1]) & \n                    (mask_array[:, :, 2] == color[2])] = label\n\n    return torch.tensor(label_array, dtype=torch.long)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:34.297646Z","iopub.execute_input":"2024-10-12T06:17:34.298017Z","iopub.status.idle":"2024-10-12T06:17:34.312367Z","shell.execute_reply.started":"2024-10-12T06:17:34.297982Z","shell.execute_reply":"2024-10-12T06:17:34.311616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_data(path, transform=None, mask_transform=None, device='cpu', train=True):\n    \"\"\"Loads images and their corresponding masks from the dataset.\"\"\"\n    data_list = []\n    X = []  # Initialize X as a list for images\n    y = []  # Initialize y as a list for masks\n\n    # Get directory IDs for train data\n    dir_ids = next(os.walk(os.path.join(path, 'train')))[1]\n\n    for dir_id in dir_ids:\n        img_ids = next(os.walk(os.path.join(path, 'train', dir_id)))[2]\n\n        for img_id in img_ids:\n            # Construct the image path\n            img_path = os.path.join(path, 'train', dir_id, img_id)\n            img = Image.open(img_path)\n\n            # Apply transformations to the image if provided\n            if transform:\n                x_img = transform(img)  \n            else:\n                x_img = img\n\n            # Ensure the image tensor is on the correct device\n            x_img = x_img.to(device)\n            X.append(x_img)\n\n            if train:\n                # Construct the path for the corresponding mask image\n                mask_id = img_id.replace(\"leftImg8bit.jpg\", \"gtFine_labelColors.png\")\n                mask_path = os.path.join(path, 'labels', dir_id, mask_id)\n\n                # Load the mask image directly\n                mask_img = Image.open(mask_path)\n\n                # Apply mask transformations if provided\n                if mask_transform:\n                    mask_transformed = mask_transform(mask_img)  \n                else:\n                    mask_transformed = mask_img\n\n                # Convert the mask image to labels\n                mask_tensor = convert_mask_to_labels(mask_transformed)\n\n                # Move the mask tensor to the correct device\n                mask_tensor = mask_tensor.to(device)  \n                y.append(mask_tensor)\n\n    if train:\n        data_list.append((torch.stack(X), torch.stack(y))) \n    else:\n        data_list.append(torch.stack(X)) \n\n    return data_list","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:34.313521Z","iopub.execute_input":"2024-10-12T06:17:34.313811Z","iopub.status.idle":"2024-10-12T06:17:34.327616Z","shell.execute_reply.started":"2024-10-12T06:17:34.313778Z","shell.execute_reply":"2024-10-12T06:17:34.326893Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset = get_data(path, transform=transform,mask_transform=mask_transform)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:17:34.332003Z","iopub.execute_input":"2024-10-12T06:17:34.332277Z","iopub.status.idle":"2024-10-12T06:28:44.518756Z","shell.execute_reply.started":"2024-10-12T06:17:34.332233Z","shell.execute_reply":"2024-10-12T06:28:44.517722Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# output_path = '/kaggle/working/dataset.pt'\n# torch.save(dataset, output_path)\n# print(f\"Dataset saved to {output_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:44.520168Z","iopub.execute_input":"2024-10-12T06:28:44.520531Z","iopub.status.idle":"2024-10-12T06:28:44.525240Z","shell.execute_reply.started":"2024-10-12T06:28:44.520488Z","shell.execute_reply":"2024-10-12T06:28:44.524017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# dataset = torch.load(output_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:44.526781Z","iopub.execute_input":"2024-10-12T06:28:44.527113Z","iopub.status.idle":"2024-10-12T06:28:44.549131Z","shell.execute_reply.started":"2024-10-12T06:28:44.527081Z","shell.execute_reply":"2024-10-12T06:28:44.548232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i, (images, masks) in enumerate(dataset):\n    print(f\"  Number of images: {images.size(0)}\")\n    print(f\"  Image shape: {images.shape}\")\n    print(f\"  Mask shape: {masks.shape}\")\n    break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:44.550181Z","iopub.execute_input":"2024-10-12T06:28:44.550603Z","iopub.status.idle":"2024-10-12T06:28:44.559834Z","shell.execute_reply.started":"2024-10-12T06:28:44.550569Z","shell.execute_reply":"2024-10-12T06:28:44.558903Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_values = torch.unique(dataset[0][1])\nnum_unique = unique_values.numel()  \nprint(f\"Number of unique values: {unique_values}\")\nprint(f\"Number of unique values: {num_unique}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:44.561107Z","iopub.execute_input":"2024-10-12T06:28:44.561460Z","iopub.status.idle":"2024-10-12T06:28:49.829031Z","shell.execute_reply.started":"2024-10-12T06:28:44.561423Z","shell.execute_reply":"2024-10-12T06:28:49.828024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def label_to_color(label_mask):\n    \"\"\"Convert label mask back to color image.\"\"\"\n    color_mask = np.zeros((*label_mask.shape, 3), dtype=np.uint8)\n    \n    for label, color in enumerate(colors):\n        color_mask[label_mask == label] = color\n\n    return color_mask","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:49.830699Z","iopub.execute_input":"2024-10-12T06:28:49.831120Z","iopub.status.idle":"2024-10-12T06:28:49.836995Z","shell.execute_reply.started":"2024-10-12T06:28:49.831074Z","shell.execute_reply":"2024-10-12T06:28:49.835893Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"first_image, first_mask = dataset[0]  \nfirst_image = first_image[5].cpu().numpy() \nfirst_mask = first_mask[5].cpu().numpy()  \n\n# Display image\nplt.subplot(1, 2, 1)\nplt.imshow(first_image.transpose(1, 2, 0))  # Transpose to (128, 128, 3) for displaying RGB\nplt.title(\"First Image\")\nplt.axis('off')\n\n# Display mask\nplt.subplot(1, 2, 2)\nplt.imshow(first_mask)\nplt.title(\"First Mask\")\nplt.axis('off')\n\nplt.show()\nplt.subplot(1, 2, 2)\nplt.imshow(label_to_color(first_mask))\nplt.title(\"First Mask\")\nplt.axis('off')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:49.838255Z","iopub.execute_input":"2024-10-12T06:28:49.838683Z","iopub.status.idle":"2024-10-12T06:28:50.305616Z","shell.execute_reply.started":"2024-10-12T06:28:49.838638Z","shell.execute_reply":"2024-10-12T06:28:50.304620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader, random_split\n\nclass CustomDataset(Dataset):\n    def __init__(self, images, masks, feature_extractor):\n        self.images = images  # Expecting tensor of shape [N, C1, H, W]\n        self.masks = masks    # Expecting tensor of shape [N, C2, H, W]\n        self.feature_extractor = feature_extractor\n\n    def __len__(self):\n        return len(self.images)\n\n    def __getitem__(self, idx):\n        image = self.images[idx]  # Should be a tensor\n        mask = self.masks[idx]    # Should be a tensor\n\n        # Preprocess the image using the feature extractor\n        image = self.feature_extractor(image, return_tensors=\"pt\").pixel_values.squeeze(0)\n\n        return image, mask\n\n\n# Create an instance of your custom dataset\nX=dataset[0][0]\ny=dataset[0][1]\n\nfeature_extractor = SegformerFeatureExtractor(align=False, reduce_zero_label=False)\n\nfull_dataset = CustomDataset(X, y, feature_extractor)\n\n# Split dataset into training and testing sets\ntrain_size = int(0.8 * len(full_dataset))\ntest_size = len(full_dataset) - train_size\ntrain_dataset, test_dataset = random_split(full_dataset, [train_size, test_size])\n\n# Create DataLoaders for training and testing\ntrain_loader = DataLoader(train_dataset, batch_size=2, shuffle=True)\ntest_loader = DataLoader(test_dataset, batch_size=2, shuffle=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:50.307051Z","iopub.execute_input":"2024-10-12T06:28:50.307536Z","iopub.status.idle":"2024-10-12T06:28:50.344113Z","shell.execute_reply.started":"2024-10-12T06:28:50.307479Z","shell.execute_reply":"2024-10-12T06:28:50.343251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"batch = next(iter(train_loader))\nimages, masks = batch  # Unpack the batch (images, masks)\n\nprint(\"Images shape:\", images.shape)\nprint(\"Masks shape:\", masks.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:50.345110Z","iopub.execute_input":"2024-10-12T06:28:50.345387Z","iopub.status.idle":"2024-10-12T06:28:50.404998Z","shell.execute_reply.started":"2024-10-12T06:28:50.345356Z","shell.execute_reply":"2024-10-12T06:28:50.404043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# List of labels corresponding to the colors (based on your color comments)\nlabels = [\n    \"road\", \"parking\", \"drivable fallback\", \"sidewalk\", \"rail track\",\n    \"non-drivable fallback\", \"person\", \"animal\", \"rider\", \"motorcycle\",\n    \"bicycle\", \"autorickshaw\", \"car\", \"truck\", \"caravan\", \"trailer\", \"train\", \n    \"vehicle fallback\", \"curb\", \"wall\", \"fence\", \"guard rail\", \"billboard\", \n    \"traffic sign\", \"traffic light\", \"pole\", \"obs-str-bar-fallback\", \"building\", \n    \"bridge\", \"tunnel\", \"vegetation\", \"sky\", \"unlabeled\"\n]\n\n# Create id2label and label2id mappings\nid2label = {i: label for i, label in enumerate(labels)}\nlabel2id = {label: i for i, label in enumerate(labels)}\n\n# Print the mappings\nprint(\"id2label:\", id2label)\nprint(\"label2id:\", label2id)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:50.406312Z","iopub.execute_input":"2024-10-12T06:28:50.406819Z","iopub.status.idle":"2024-10-12T06:28:50.414893Z","shell.execute_reply.started":"2024-10-12T06:28:50.406773Z","shell.execute_reply":"2024-10-12T06:28:50.413826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:50.416251Z","iopub.execute_input":"2024-10-12T06:28:50.416681Z","iopub.status.idle":"2024-10-12T06:28:50.428744Z","shell.execute_reply.started":"2024-10-12T06:28:50.416641Z","shell.execute_reply":"2024-10-12T06:28:50.427805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:50.429910Z","iopub.execute_input":"2024-10-12T06:28:50.430222Z","iopub.status.idle":"2024-10-12T06:28:50.437857Z","shell.execute_reply.started":"2024-10-12T06:28:50.430187Z","shell.execute_reply":"2024-10-12T06:28:50.436897Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# class_weights = calculate_class_weights(train_loader, num_classes=33)\n# class_weights = class_weights.to(device)\n\n# # Print class weights and their shape\n# print(f\"Class weights: {class_weights}\")\n# print(class_weights.shape)  # This should be (num_classes,)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:50.439022Z","iopub.execute_input":"2024-10-12T06:28:50.439514Z","iopub.status.idle":"2024-10-12T06:28:50.448100Z","shell.execute_reply.started":"2024-10-12T06:28:50.439472Z","shell.execute_reply":"2024-10-12T06:28:50.447287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = SegformerForSemanticSegmentation.from_pretrained(\"nvidia/mit-b5\", ignore_mismatched_sizes=True,\n                                                         num_labels=len(id2label), id2label=id2label, label2id=label2id,\n                                                         reshape_last_stage=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:50.449564Z","iopub.execute_input":"2024-10-12T06:28:50.449905Z","iopub.status.idle":"2024-10-12T06:28:54.449790Z","shell.execute_reply.started":"2024-10-12T06:28:50.449842Z","shell.execute_reply":"2024-10-12T06:28:54.449005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = torch.optim.AdamW(model.parameters(), lr=0.00006)\nmodel.to(device)\nprint(\"Model Initialized!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:54.451202Z","iopub.execute_input":"2024-10-12T06:28:54.451519Z","iopub.status.idle":"2024-10-12T06:28:54.875836Z","shell.execute_reply.started":"2024-10-12T06:28:54.451485Z","shell.execute_reply":"2024-10-12T06:28:54.874893Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nnum_epochs = 3  # Number of epochs\nfor epoch in range(1, num_epochs + 1):  # Loop over the dataset multiple times\n    print(\"Epoch:\", epoch)\n    pbar = tqdm(train_loader, desc=f'Epoch {epoch}/{num_epochs}')  # Use train_loader instead of train_dataloader\n    accuracies = []  # List to store accuracies for each batch\n    losses = []  # List to store losses for each batch\n\n    model.train()  # Set the model to training mode\n    running_loss = 0.0  # Initialize running loss for the epoch\n\n    for idx, (images, masks) in enumerate(pbar):\n        images = images.to(device)  # Move images to the correct device\n        masks = masks.to(device)  # Move masks to the correct device\n\n        # Zero the parameter gradients\n        optimizer.zero_grad()\n\n        # Forward pass\n        outputs = model(images, labels=masks)  # Ensure that you are using the right inputs\n\n        # Evaluate\n        upsampled_logits = nn.functional.interpolate(outputs.logits, size=masks.shape[-2:], mode=\"bilinear\", align_corners=False)\n        predicted = upsampled_logits.argmax(dim=1)  # Get the predicted class per pixel\n\n        mask = (masks != 255)  # We don't include the background class (255) for the accuracy calculation\n        pred_labels = predicted[mask].detach().cpu().numpy()\n        true_labels = masks[mask].detach().cpu().numpy()\n\n        # Calculate accuracy\n        accuracy = accuracy_score(pred_labels, true_labels)\n        accuracies.append(accuracy)\n\n        # Calculate loss\n        loss = outputs.loss  # Get the loss from the model output\n        losses.append(loss.item())\n\n        # Backward + optimize\n        loss.backward()\n        optimizer.step()\n\n        # Update progress bar\n        pbar.set_postfix({'Batch': idx, 'Pixel-wise accuracy': sum(accuracies)/len(accuracies), 'Loss': sum(losses)/len(losses)})\n\n    # Print the average loss and accuracy for this epoch\n    avg_accuracy = sum(accuracies) / len(accuracies)\n    avg_loss = sum(losses) / len(losses)\n    print(f\"Train Pixel-wise accuracy: {avg_accuracy:.4f}, Train Loss: {avg_loss:.4f}\")\n\nprint(\"Training complete!\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:28:54.877144Z","iopub.execute_input":"2024-10-12T06:28:54.877462Z","iopub.status.idle":"2024-10-12T06:30:18.833278Z","shell.execute_reply.started":"2024-10-12T06:28:54.877429Z","shell.execute_reply":"2024-10-12T06:30:18.829192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_save_path = '/kaggle/working/model'  # Specify your desired save path\ntorch.save(model, model_save_path)\nprint(f\"Full model saved at {model_save_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:37:01.607394Z","iopub.execute_input":"2024-10-12T06:37:01.608359Z","iopub.status.idle":"2024-10-12T06:37:02.296148Z","shell.execute_reply.started":"2024-10-12T06:37:01.608315Z","shell.execute_reply":"2024-10-12T06:37:02.295001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Set the model to evaluation mode\nmodel.eval()\nall_predicted_masks = []\nall_actual_masks = []\nall_images = []\n\n# Get predictions for all test data\nwith torch.no_grad():  # Disable gradient calculation for inference\n    for idx, (images, masks) in enumerate(test_loader):\n        # Move images to the correct device\n        images = images.to(device)\n        masks = masks.to(device)  # Ensure masks are also moved to the correct device\n\n        # Perform the prediction\n        outputs = model(pixel_values=images)  # Use pixel_values for the Segformer model\n        logits = outputs.logits  # Get the logits from the model output\n\n        # Convert logits to probabilities and then to predicted masks\n        predicted_masks = torch.argmax(logits, dim=1)  # Get the predicted class per pixel\n        actual_masks = masks.cpu()  # Move actual masks to CPU for visualization\n\n        # Append results to lists\n        all_predicted_masks.append(predicted_masks.cpu())  # Move predicted masks to CPU for visualization\n        all_actual_masks.append(actual_masks.cpu())  # Move actual masks to CPU for visualization\n        all_images.append(images.cpu())  # Store original images\n\n# Concatenate results\nall_predicted_masks = torch.cat(all_predicted_masks)  # Shape: [num_samples, height, width]\nall_actual_masks = torch.cat(all_actual_masks)  # Shape: [num_samples, height, width]\nall_images = torch.cat(all_images)  # Shape: [num_samples, channels, height, width]\n\nprint(\"Evaluation complete!\")\nprint(f\"Predicted Masks Shape: {all_predicted_masks.shape}\")\nprint(f\"Actual Masks Shape: {all_actual_masks.shape}\")\nprint(f\"Images Shape: {all_images.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:30:21.019055Z","iopub.execute_input":"2024-10-12T06:30:21.020060Z","iopub.status.idle":"2024-10-12T06:33:08.912165Z","shell.execute_reply.started":"2024-10-12T06:30:21.020015Z","shell.execute_reply":"2024-10-12T06:33:08.911096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def label_to_color(label_mask):\n    \"\"\"Convert label mask back to color image.\"\"\"\n    color_mask = np.zeros((*label_mask.shape, 3), dtype=np.uint64)\n    \n    for label, color in enumerate(colors):\n        color_mask[label_mask == label] = color\n\n    return color_mask","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:33:08.914457Z","iopub.execute_input":"2024-10-12T06:33:08.914903Z","iopub.status.idle":"2024-10-12T06:33:08.920692Z","shell.execute_reply.started":"2024-10-12T06:33:08.914839Z","shell.execute_reply":"2024-10-12T06:33:08.919664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_results(images, actual_masks, predicted_masks, num_samples=3):\n    plt.figure(figsize=(30, 6 * num_samples))  # Adjust the figure size to accommodate 5 columns\n    \n    for i in range(num_samples):\n        # Plot Original Image\n        plt.subplot(num_samples, 5, i * 5 + 1)\n        plt.title(\"Original Image\")\n        plt.imshow(images[i].permute(1, 2, 0).cpu().numpy())  # Use permute to change tensor shape to (H, W, C)\n        plt.axis('off')\n\n        # Plot Actual Mask\n        plt.subplot(num_samples, 5, i * 5 + 2)\n        plt.title(\"Actual Mask (Labels)\")\n        plt.imshow(actual_masks[i].cpu().numpy())  # Display actual mask\n        plt.axis('off')\n\n        # Plot Predicted Mask (as label indices)\n        plt.subplot(num_samples, 5, i * 5 + 3)\n        plt.title(\"Predicted Mask (Labels)\")\n        plt.imshow(predicted_masks[i].cpu().numpy())  # Display predicted mask as label indices\n        plt.axis('off')\n\n        # Plot Actual Mask with Colors\n        plt.subplot(num_samples, 5, i * 5 + 4)\n        plt.title(\"Actual Mask (Colored)\")\n        colorized_actual_mask = label_to_color(actual_masks[i].cpu().numpy())  # Convert actual mask labels to colors\n        plt.imshow(colorized_actual_mask)  # Display the colorized actual mask\n        plt.axis('off')\n\n        # Plot Predicted Mask with Colors\n        plt.subplot(num_samples, 5, i * 5 + 5)\n        plt.title(\"Predicted Mask (Colored)\")\n        colorized_predicted_mask = label_to_color(predicted_masks[i].cpu().numpy()) \n        plt.imshow(colorized_predicted_mask) \n        plt.axis('off')\n\n    plt.tight_layout()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:33:08.921973Z","iopub.execute_input":"2024-10-12T06:33:08.922344Z","iopub.status.idle":"2024-10-12T06:33:08.936350Z","shell.execute_reply.started":"2024-10-12T06:33:08.922301Z","shell.execute_reply":"2024-10-12T06:33:08.935417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot results for the first few images\nplot_results(all_images, all_actual_masks, all_predicted_masks, num_samples=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:33:08.938758Z","iopub.execute_input":"2024-10-12T06:33:08.939189Z","iopub.status.idle":"2024-10-12T06:33:16.196102Z","shell.execute_reply.started":"2024-10-12T06:33:08.939143Z","shell.execute_reply":"2024-10-12T06:33:16.195107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import fbeta_score\ndef calculate_dice_coefficient(predicted_masks, actual_masks, num_classes):\n    dice_coefficients = {}\n    \n    # Convert predicted_masks and actual_masks to tensors if they are not already\n    if isinstance(predicted_masks, np.ndarray):\n        predicted_masks = torch.from_numpy(predicted_masks)\n    if isinstance(actual_masks, np.ndarray):\n        actual_masks = torch.from_numpy(actual_masks)\n\n    overall_intersection = 0.0\n    overall_union = 0.0\n    \n    for class_id in range(num_classes):\n        # Create binary masks for predicted and actual\n        pred_binary = (predicted_masks == class_id).float()\n        true_binary = (actual_masks == class_id).float()\n\n        intersection = (pred_binary * true_binary).sum().item()\n        union = pred_binary.sum().item() + true_binary.sum().item()\n        \n        # Calculate Dice coefficient\n        dice = (2. * intersection) / (union + 1e-6)  # Adding epsilon to avoid division by zero\n        dice_coefficients[class_id] = dice\n        \n        # Update overall intersection and union for calculating overall Dice\n        overall_intersection += intersection\n        overall_union += union\n\n    # Calculate overall Dice coefficient\n    overall_dice = (2. * overall_intersection) / (overall_union + 1e-6)\n    dice_coefficients['Overall'] = overall_dice\n\n    return dice_coefficients\n\n\ndef calculate_f_beta(predicted_masks, actual_masks, num_classes, beta=0.5):\n    f_beta_scores = {}\n    \n    # Convert predicted_masks and actual_masks to tensors if they are not already\n    if isinstance(predicted_masks, np.ndarray):\n        predicted_masks = torch.from_numpy(predicted_masks)\n    if isinstance(actual_masks, np.ndarray):\n        actual_masks = torch.from_numpy(actual_masks)\n\n    overall_precision = 0.0\n    overall_recall = 0.0\n\n    for class_id in range(num_classes):\n        pred_binary = (predicted_masks == class_id).float()\n        true_binary = (actual_masks == class_id).float()\n\n        true_positive = (pred_binary * true_binary).sum().item()\n        false_positive = pred_binary.sum().item() - true_positive\n        false_negative = true_binary.sum().item() - true_positive\n\n        # Calculate precision and recall\n        precision = true_positive / (true_positive + false_positive + 1e-6)  # Adding epsilon to avoid division by zero\n        recall = true_positive / (true_positive + false_negative + 1e-6)  # Adding epsilon to avoid division by zero\n\n        # Calculate F-beta score\n        if precision + recall > 0:\n            f_beta = (1 + beta**2) * (precision * recall) / (beta**2 * precision + recall)\n        else:\n            f_beta = 0.0\n\n        f_beta_scores[class_id] = f_beta\n        \n        # Update overall precision and recall\n        overall_precision += precision\n        overall_recall += recall\n\n    # Calculate overall F-beta score\n    if overall_precision + overall_recall > 0:\n        overall_f_beta = (1 + beta**2) * (overall_precision * overall_recall) / (beta**2 * overall_precision + overall_recall)\n    else:\n        overall_f_beta = 0.0\n\n    f_beta_scores['Overall'] = overall_f_beta\n\n    return f_beta_scores\n\n# Assuming num_classes is defined (i.e., len(id2label))\nnum_classes = len(id2label)  # Assuming id2label is defined and represents your class labels\npredicted_masks = all_predicted_masks.cpu().numpy()  # Convert to NumPy\nactual_masks = all_actual_masks.cpu().numpy()  # Convert to NumPy\n\n# Calculate Dice Coefficient\ndice_coefficients = calculate_dice_coefficient(predicted_masks, actual_masks, num_classes)\nprint(\"Dice Coefficients:\", dice_coefficients)\n\n# Calculate F-beta Score\nf_beta_scores = calculate_f_beta(predicted_masks, actual_masks, num_classes, beta=0.5)\nprint(\"F-beta Scores:\", f_beta_scores)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:33:53.247916Z","iopub.execute_input":"2024-10-12T06:33:53.248317Z","iopub.status.idle":"2024-10-12T06:34:04.494383Z","shell.execute_reply.started":"2024-10-12T06:33:53.248281Z","shell.execute_reply":"2024-10-12T06:34:04.493145Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\n# Load your image\nimage_path = \"/kaggle/input/iitg-ai-overnight-hackathon-2024/dataset/dataset/test/frame0014_leftImg8bit.jpg\"  # Replace with your image path\nimage = Image.open(image_path)  # Load the image\noriginal_size = image.size  # Store the original size (width, height)\n\n# Preprocess the image using your defined transform\ntransform = transforms.Compose([\n    transforms.Resize((im_height, im_width)),  # Resize to model input size\n    transforms.ToTensor()\n])\n\n# Here we need to ensure we add the batch dimension before processing\nimage = transform(image).unsqueeze(0).to(device)  # Add batch dimension and move to device\n# Print image shape after transformation\nprint(f\"Image shape after transform: {image.shape}\")\n\n# Apply feature extractor for preprocessing (assuming feature_extractor is defined)\nimage = feature_extractor(image, return_tensors=\"pt\").pixel_values.to(device)  # Ensure it's on the same device\n\n# Print image shape for debugging\nprint(f\"Image shape after feature extractor: {image.shape}\")\n\n# Measure model size\nmodel_size = sum(p.numel() for p in model.parameters() if p.requires_grad)\nmodel_size_mb = model_size * 4 / (1024 ** 2) \n\n# Inference\nmodel.eval()  # Set the model to evaluation mode\nwith torch.no_grad():\n    start_time = time.time()  # Start the timer\n    outputs = model(pixel_values=image)  # Use pixel_values for the Segformer model\n    logits = outputs.logits  \n\n        # Convert logits to probabilities and then to predicted masks\n    predicted_masks = torch.argmax(logits, dim=1)\n    end_time = time.time()  # End the timer\n\n# Print outputs shape for debugging\nprint(f\"Outputs shape: {logits.shape}\")\n\n# Calculate inference time\ninference_time = end_time - start_time\n\n# Get the predicted mask and resize it to the original size\npredicted_mask = predicted_masks.cpu().numpy()[0]  # Get the predicted mask\npredicted_mask = label_to_color(predicted_mask)\nprint(predicted_mask.shape)\n# Save the resized mask (optional)\npredicted_mask_path = os.path.join(\"predicted_masks\", \"predicted_mask.png\")  # Set your desired output path\nos.makedirs(os.path.dirname(predicted_mask_path), exist_ok=True)\ncv2.imwrite(predicted_mask_path, predicted_mask)\n\n\nprint(f\"Inference Time for Single Image: {inference_time:.4f} seconds\")\nprint(f\"Model Size: {model_size_mb:.2f} MB\")\nprint(f\"Predicted Mask: {predicted_mask_path}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:34:44.730417Z","iopub.execute_input":"2024-10-12T06:34:44.731220Z","iopub.status.idle":"2024-10-12T06:34:44.999776Z","shell.execute_reply.started":"2024-10-12T06:34:44.731177Z","shell.execute_reply":"2024-10-12T06:34:44.998818Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_test_data(test_folder, feature_extractor, transform=None, device='cpu'):\n    test_images = []\n    filenames = []  # List to store filenames\n    original_sizes = []  # List to store original heights and widths\n    \n    # List all image files in the test folder\n    image_files = [f for f in os.listdir(test_folder) if os.path.isfile(os.path.join(test_folder, f))]\n    \n    for image_file in image_files:\n        # Load the image\n        image_path = os.path.join(test_folder, image_file)\n        image = Image.open(image_path).convert(\"RGB\")  # Ensure image is in RGB format\n        \n        # Store the original size (height, width)\n        original_sizes.append(image.size)  # Appends (width, height)\n        \n        # Apply transformations if provided\n        if transform:\n            image = transform(image)\n\n        # Process the image using the feature extractor\n        processed_image = feature_extractor(image, return_tensors=\"pt\").pixel_values.squeeze(0)  # Shape: (C, H, W)\n\n        # Move image tensor to the specified device (e.g., CUDA)\n        processed_image = processed_image.to(device)\n        \n        test_images.append(processed_image)\n        filenames.append(image_file)  # Store the filename\n    \n    return test_images, filenames, original_sizes  # Return images, filenames, and original sizes\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:34:57.363399Z","iopub.execute_input":"2024-10-12T06:34:57.364098Z","iopub.status.idle":"2024-10-12T06:34:57.372759Z","shell.execute_reply.started":"2024-10-12T06:34:57.364057Z","shell.execute_reply":"2024-10-12T06:34:57.371607Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path_test=\"/kaggle/input/iitg-ai-overnight-hackathon-2024/dataset/dataset/test\"\ntest_data,filenames,original_sizes=get_test_data(path_test, feature_extractor,transform=transform)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:34:59.725150Z","iopub.execute_input":"2024-10-12T06:34:59.725891Z","iopub.status.idle":"2024-10-12T06:35:06.305444Z","shell.execute_reply.started":"2024-10-12T06:34:59.725823Z","shell.execute_reply":"2024-10-12T06:35:06.304376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:35:06.307162Z","iopub.execute_input":"2024-10-12T06:35:06.307557Z","iopub.status.idle":"2024-10-12T06:35:06.313992Z","shell.execute_reply.started":"2024-10-12T06:35:06.307519Z","shell.execute_reply":"2024-10-12T06:35:06.312860Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import shutil\n# shutil.rmtree('/kaggle/working/submit_predicted') ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:30:18.850768Z","iopub.status.idle":"2024-10-12T06:30:18.851189Z","shell.execute_reply.started":"2024-10-12T06:30:18.851006Z","shell.execute_reply":"2024-10-12T06:30:18.851024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Set the model to evaluation mode\nmodel.eval()\npredicted_masks_folder = \"/kaggle/working/submit_predicted\"\nall_predicted_masks = []\n\n# Create directory for predicted masks if it doesn't exist\nos.makedirs(predicted_masks_folder, exist_ok=True)\n\n# Assuming test_data is a list of preprocessed images\nbatch_size = 16  # Adjust as needed\n\n# Get predictions for all test data\nwith torch.no_grad():  # Disable gradient calculation for inference\n    for idx in tqdm(range(0, len(test_data), batch_size), desc=\"Processing batches\"):\n        # Create a batch of images\n        images = test_data[idx:idx + batch_size]  # Get a batch of images\n        images = torch.stack(images).to(device)  # Stack them into a tensor and move to device\n\n        # Perform the prediction\n        outputs = model(images)\n\n        # Get predicted masks (logits)\n        predicted_masks = torch.argmax(outputs.logits, dim=1).cpu()  # Shape: (batch_size, height, width)\n        \n        # Append results to the list\n        all_predicted_masks.append(predicted_masks)\n\n        # Save predicted images\n        for j in range(predicted_masks.size(0)):  # Iterate over the batch\n            mask = predicted_masks[j].numpy()  # Convert to NumPy array\n\n            # Get the original filename and change the extension if needed\n            original_filename = filenames[idx + j].replace('_leftImg8bit.jpg', '.png')  # Modify extension for masks\n            \n            # Get original size\n            original_size = original_sizes[idx + j]  # (width, height)\n\n            # Resize the mask (original_size is (width, height))\n            resized_mask = cv2.resize(mask, (original_size[0], original_size[1]), interpolation=cv2.INTER_NEAREST)  # Resize mask\n\n            # Convert label mask to original color after resizing\n            colorized_mask = label_to_color(resized_mask)\n            \n            # Save with the original name\n            mask_path = os.path.join(predicted_masks_folder, original_filename)  # Save with the original name\n            cv2.imwrite(mask_path, colorized_mask)\n\n# Concatenate results\nall_predicted_masks = torch.cat(all_predicted_masks, dim=0)  # Concatenate along the batch dimension\nprint(\"Inference complete! Predicted masks saved.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:35:06.315252Z","iopub.execute_input":"2024-10-12T06:35:06.315729Z","iopub.status.idle":"2024-10-12T06:35:37.233696Z","shell.execute_reply.started":"2024-10-12T06:35:06.315694Z","shell.execute_reply":"2024-10-12T06:35:37.232650Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Define paths to the predicted mask and original image\npredicted_mask_path = \"/kaggle/working/submit_predicted/frame7446.png\"\noriginal_image_path = \"/kaggle/input/iitg-ai-overnight-hackathon-2024/dataset/dataset/test/frame7446_leftImg8bit.jpg\"\n\n# Read the predicted mask\npredicted_mask = cv2.imread(predicted_mask_path)  # Read image without changing color channels\n\n# Read the original image\noriginal_image = cv2.imread(original_image_path)  # Read original test image\n\n# Convert from BGR to RGB since OpenCV loads in BGR format\n# predicted_mask_rgb = cv2.cvtColor(predicted_mask, cv2.COLOR_BGR2RGB)\n# original_image_rgb = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)\n\n# Display the predicted mask\nplt.subplot(1, 2, 1)  # Create a subplot for side-by-side display\nplt.imshow(predicted_mask)  # Use the predicted mask array\nplt.title(\"Predicted Mask\")\nprint(predicted_mask.shape)  # Print the shape of the predicted mask\nplt.axis('off')  # Turn off axis\n\n# Display the original image\nplt.subplot(1, 2, 2)  # Second subplot for the original image\nplt.imshow(original_image)  # Use the original image array\nplt.title(\"Original Image\")\nprint(original_image.shape)  # Print the shape of the original image\nplt.axis('off')  # Turn off axis\n\n# Show both images\nplt.show()\nprint(predicted_mask[10][10])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:35:48.282703Z","iopub.execute_input":"2024-10-12T06:35:48.283418Z","iopub.status.idle":"2024-10-12T06:35:49.093795Z","shell.execute_reply.started":"2024-10-12T06:35:48.283377Z","shell.execute_reply":"2024-10-12T06:35:49.092750Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class 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# Your label definitions here (from your provided list)\n\nlabels = [\n    #       name                     id    csId     csTrainId level4id        level3Id  category           level2Id      level1Id  hasInstances   ignoreInEval   color\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]           \n\n#Function to get label information from pixel color\ndef get_label_by_color(color):\n    for label in labels:\n        if label.color == tuple(color):  # Compare the pixel color to the label color\n            return label\n    return None\n\n# Function to convert the segmented image into polygon-based format and prepare for CSV\ndef image_to_polygon_format(image_path):\n    segmented_image = cv2.imread(image_path)  # Load the segmented image\n    height, width, _ = segmented_image.shape\n\n    objects = []  # To hold the polygon data for each object\n\n    # Loop through all the labels\n    for label in labels:\n        if label.ignoreInEval:\n            continue  # Skip labels that should be ignored\n\n        # Convert the label color to a binary mask\n        mask = cv2.inRange(segmented_image, np.array(label.color), np.array(label.color))\n\n        # Find contours (polygons) in the binary mask\n        contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n\n        for contour in contours:\n            if len(contour) < 3:  # Skip small or invalid polygons\n                continue\n\n            # Simplify the contour to polygon format\n            polygon = contour.reshape(-1, 2).tolist()  # Flatten the contour to a list of points\n\n            # Create the object data for the current label and polygon\n            object_data = {\n                \"label\": label.name,\n                \"polygon\": polygon\n            }\n\n            # Append the object to the result\n            objects.append(object_data)\n\n    return objects\n\n# Function to save to the CSV format as requested\ndef save_to_csv(objects_dict, output_csv_path):\n    # Create a DataFrame with id and objects\n    data = [{\"id\": filename, \"objects\": json.dumps(objects)} for filename, objects in objects_dict.items()]\n    df = pd.DataFrame(data)\n    \n    # Save to CSV\n    df.to_csv(output_csv_path, index=False)\n\n# Example usage:\noutput_csv_path = \"submission.csv\"\n\n# Store results for each image with a unique row ID\nobjects_dict = {}\n\n# Process each image and store the objects for each row ID\nimage_folder = \"/kaggle/working/submit_predicted\"  # Folder with segmented images\nsegmented_image_paths = [os.path.join(image_folder, f) for f in os.listdir(image_folder) if os.path.isfile(os.path.join(image_folder, f))]\n\nfor image_path in segmented_image_paths:\n    # Get the filename without the extension and omit \"gtFine\" and \"labelColors\"\n    filename = os.path.splitext(os.path.basename(image_path))[0]\n    polygon_data = image_to_polygon_format(image_path)\n    objects_dict[filename] = polygon_data  # Use modified filename as the row_id_column_name\n\n# Save the results to CSV\nsave_to_csv(objects_dict, output_csv_path)\n\nprint(f\"Solution file saved to {output_csv_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-12T06:30:18.857249Z","iopub.status.idle":"2024-10-12T06:30:18.857584Z","shell.execute_reply.started":"2024-10-12T06:30:18.857413Z","shell.execute_reply":"2024-10-12T06:30:18.857430Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}