{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":13333,"databundleVersionId":862146,"sourceType":"competition"}],"dockerImageVersionId":30513,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport io\nimport cv2\nimport time\nimport math\nimport numpy as np\nimport pandas as pd\nimport albumentations as A\nimport matplotlib.pyplot as plt\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-28T10:28:40.660377Z","iopub.execute_input":"2023-12-28T10:28:40.661212Z","iopub.status.idle":"2023-12-28T10:28:43.768293Z","shell.execute_reply.started":"2023-12-28T10:28:40.661171Z","shell.execute_reply":"2023-12-28T10:28:43.767271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"work_dir = \"/kaggle/working/\"\ndata_dir = \"../input/understanding_cloud_organization\"\ntrain_csv_path = os.path.join(data_dir,'train.csv')\ntest_csv_path = os.path.join(data_dir,\"sample_submission.csv\")\ntrain_image_path = os.path.join(data_dir,'train_images')\ntest_image_path = os.path.join(data_dir,'test_images')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:28:43.770221Z","iopub.execute_input":"2023-12-28T10:28:43.770877Z","iopub.status.idle":"2023-12-28T10:28:43.777194Z","shell.execute_reply.started":"2023-12-28T10:28:43.770810Z","shell.execute_reply":"2023-12-28T10:28:43.775990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class COLOR:\n    PURPLE = '\\033[95m'\n    CYAN = '\\033[96m'\n    DARKCYAN = '\\033[36m'\n    BLUE = '\\033[94m'\n    GREEN = '\\033[92m'\n    YELLOW = '\\033[93m'\n    RED = '\\033[91m'\n    BOLD = '\\033[1m'\n    UNDERLINE = '\\033[4m'\n    END = '\\033[0m'","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:28:43.778865Z","iopub.execute_input":"2023-12-28T10:28:43.779996Z","iopub.status.idle":"2023-12-28T10:28:43.793219Z","shell.execute_reply.started":"2023-12-28T10:28:43.779928Z","shell.execute_reply":"2023-12-28T10:28:43.792333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading and Preaparing the Dataframe","metadata":{}},{"cell_type":"markdown","source":"`Most of the Idea of this section has been taken from this notebook` - https://www.kaggle.com/code/ekhtiar/tf-tutorial-semantic-segmentation-with-u-net","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(train_csv_path).fillna(-1)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:28:43.796503Z","iopub.execute_input":"2023-12-28T10:28:43.797556Z","iopub.status.idle":"2023-12-28T10:28:48.719713Z","shell.execute_reply.started":"2023-12-28T10:28:43.797513Z","shell.execute_reply":"2023-12-28T10:28:48.718311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Image_Id'] = train_df['Image_Label'].apply(lambda x: x.split('_')[0])\ntrain_df['Label'] = train_df['Image_Label'].apply(lambda x: x.split('_')[1])\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:28:48.721074Z","iopub.execute_input":"2023-12-28T10:28:48.724113Z","iopub.status.idle":"2023-12-28T10:28:48.769489Z","shell.execute_reply.started":"2023-12-28T10:28:48.724072Z","shell.execute_reply":"2023-12-28T10:28:48.768398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['Label_EncodedPixels'] = train_df.apply(lambda row: (row['Label'], row['EncodedPixels']), axis = 1)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:28:48.770933Z","iopub.execute_input":"2023-12-28T10:28:48.771270Z","iopub.status.idle":"2023-12-28T10:28:49.170552Z","shell.execute_reply.started":"2023-12-28T10:28:48.771241Z","shell.execute_reply":"2023-12-28T10:28:49.169406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped_EncodedPixels = train_df.groupby('Image_Id')['Label_EncodedPixels'].apply(list)\ngrouped_EncodedPixels.head()\ngrouped_EncodedPixels.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:28:49.171874Z","iopub.execute_input":"2023-12-28T10:28:49.172240Z","iopub.status.idle":"2023-12-28T10:28:49.387798Z","shell.execute_reply.started":"2023-12-28T10:28:49.172211Z","shell.execute_reply":"2023-12-28T10:28:49.386737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = grouped_EncodedPixels.to_frame().reset_index()\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:49.990884Z","iopub.execute_input":"2023-12-28T10:29:49.991359Z","iopub.status.idle":"2023-12-28T10:29:50.017267Z","shell.execute_reply.started":"2023-12-28T10:29:49.991325Z","shell.execute_reply":"2023-12-28T10:29:50.015868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['Fish', 'Flower', 'Gravel', 'Sugar']\n\nfor label in labels:\n    train_df = train_df.assign(**{label: 0})\nfor index, row in train_df.iterrows():\n    for item in row['Label_EncodedPixels']:\n        label, value = item\n        if value == -1:\n            bool_value = 0\n        else:\n            bool_value = 1\n        train_df.loc[index, label] = bool_value\n\ntrain_df['classes'] = train_df.apply(lambda row: [col for col in labels if row[col] == 1], axis=1)\n\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:50.825986Z","iopub.execute_input":"2023-12-28T10:29:50.827161Z","iopub.status.idle":"2023-12-28T10:29:54.058053Z","shell.execute_reply.started":"2023-12-28T10:29:50.827085Z","shell.execute_reply":"2023-12-28T10:29:54.056757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:54.060217Z","iopub.execute_input":"2023-12-28T10:29:54.060580Z","iopub.status.idle":"2023-12-28T10:29:54.082543Z","shell.execute_reply.started":"2023-12-28T10:29:54.060548Z","shell.execute_reply":"2023-12-28T10:29:54.081133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# printing out indexes (in dataframe) of some images having all 4 types of mask\nfor ix,item in enumerate(train_df['Label_EncodedPixels'][:100]):\n    c1=item[0][-1]!=-1\n    c2=item[1][-1]!=-1\n    c3=item[2][-1]!=-1\n    c4=item[3][-1]!=-1\n    if c1 and c2 and c3 and c4:\n        print(ix) ","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:54.083913Z","iopub.execute_input":"2023-12-28T10:29:54.084286Z","iopub.status.idle":"2023-12-28T10:29:54.092021Z","shell.execute_reply.started":"2023-12-28T10:29:54.084255Z","shell.execute_reply":"2023-12-28T10:29:54.090679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[18][\"Image_Id\"]","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:54.094768Z","iopub.execute_input":"2023-12-28T10:29:54.095197Z","iopub.status.idle":"2023-12-28T10:29:54.110803Z","shell.execute_reply.started":"2023-12-28T10:29:54.095164Z","shell.execute_reply":"2023-12-28T10:29:54.109623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# printing out indexes (in image dir) of some images having all 4 types of mask\nfor ix,item in enumerate(os.listdir(train_image_path)):\n    if item == train_df.loc[18][\"Image_Id\"]:\n        print(ix)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:54.112862Z","iopub.execute_input":"2023-12-28T10:29:54.113318Z","iopub.status.idle":"2023-12-28T10:29:55.533727Z","shell.execute_reply.started":"2023-12-28T10:29:54.113276Z","shell.execute_reply":"2023-12-28T10:29:55.532684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for j,item in enumerate(train_df['Label_EncodedPixels'][:100]):\n    c1=item[0][-1]!=-1\n    c2=item[1][-1]!=-1\n    c3=item[2][-1]!=-1\n    c4=item[3][-1]!=-1\n    if c1 and c2 and c3 and c4:\n        for ix,item in enumerate(os.listdir(train_image_path)):\n            if item == train_df.loc[j][\"Image_Id\"]:\n                print(ix)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:55.535633Z","iopub.execute_input":"2023-12-28T10:29:55.535959Z","iopub.status.idle":"2023-12-28T10:29:59.477778Z","shell.execute_reply.started":"2023-12-28T10:29:55.535931Z","shell.execute_reply":"2023-12-28T10:29:59.476620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INDEX = 5390","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:59.478997Z","iopub.execute_input":"2023-12-28T10:29:59.479345Z","iopub.status.idle":"2023-12-28T10:29:59.487502Z","shell.execute_reply.started":"2023-12-28T10:29:59.479317Z","shell.execute_reply":"2023-12-28T10:29:59.485988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indexes =  [0, 1, 2, 3]\nlabels = ['Fish', 'Flower', 'Gravel', 'Sugar']\ncolors = ['maroon', 'darkblue', 'purple','teal']\ncolormaps = ['PuRd_r', 'Blues_r', 'Purples_r','winter_r']\nrgb_colors = [(56, 255, 255),(255, 70, 90),(48, 255, 99),(255, 255, 102)]\n\nlabel_to_idx = dict(zip(labels,indexes))\nidx_to_label = dict(zip(indexes,labels))\n\nlabel_to_color = dict(zip(labels,colors))\nidx_to_color = dict(zip(indexes,colors))\n\nlabel_to_rgb_color =  dict(zip(labels,rgb_colors))\nidx_to_rgb_color = dict(zip(indexes,rgb_colors))\n\nlabel_to_colormap = dict(zip(labels,colormaps))\nidx_to_colormap = dict(zip(indexes,colormaps))","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:59.489902Z","iopub.execute_input":"2023-12-28T10:29:59.490442Z","iopub.status.idle":"2023-12-28T10:29:59.501497Z","shell.execute_reply.started":"2023-12-28T10:29:59.490399Z","shell.execute_reply":"2023-12-28T10:29:59.500022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"def count_nonzero_pixel_from_rle(rle_string, height, width):\n    \"\"\"\n    Counts non-zero pixels in a RLE-encoded mask without decoding.\n\n    Args:\n        rle_string (str): RLE-encoded mask string.\n        height (int): Height of the mask.\n        width (int): Width of the mask.\n\n    Returns:\n        int: Number of non-zero pixels in the mask.\n    \"\"\"\n\n    if rle_string == -1:\n        return 0  # Empty mask\n\n    nonzero_count = 0\n    rle_numbers = [int(num_string) for num_string in rle_string.split(' ')]\n    rle_pairs = np.array(rle_numbers).reshape(-1, 2)\n\n    for index, length in rle_pairs:\n        if length > 0:  # Only count non-zero runs\n            nonzero_count += length\n\n    return nonzero_count","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:59.503001Z","iopub.execute_input":"2023-12-28T10:29:59.503343Z","iopub.status.idle":"2023-12-28T10:29:59.513746Z","shell.execute_reply.started":"2023-12-28T10:29:59.503315Z","shell.execute_reply":"2023-12-28T10:29:59.512431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Classwise Pixel Count","metadata":{}},{"cell_type":"code","source":"%%time \n\nclass_wise_pixel_count = {\n    \"Fish\":0,\n    \"Flower\":0,\n    \"Gravel\":0,\n    \"Sugar\":0\n}\n\nimg_width = 2100\nimg_height = 1400\n\nfish_count = 0\nflower_count = 0 \ngravel_count = 0\nsugar_count = 0\n\nfor ix, item in train_df.iterrows():\n    rle = item[\"Label_EncodedPixels\"]\n    class_wise_pixel_count[\"Fish\"] += count_nonzero_pixel_from_rle(rle[0][1], img_height, img_width)\n    class_wise_pixel_count[\"Flower\"] += count_nonzero_pixel_from_rle(rle[1][1], img_height, img_width)\n    class_wise_pixel_count[\"Gravel\"] += count_nonzero_pixel_from_rle(rle[2][1], img_height, img_width)\n    class_wise_pixel_count[\"Sugar\"] += count_nonzero_pixel_from_rle(rle[3][1], img_height, img_width)\n    \n\nprint(class_wise_pixel_count)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:29:59.515623Z","iopub.execute_input":"2023-12-28T10:29:59.516737Z","iopub.status.idle":"2023-12-28T10:30:48.399475Z","shell.execute_reply.started":"2023-12-28T10:29:59.516688Z","shell.execute_reply":"2023-12-28T10:30:48.397899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(num=None, figsize=(18, 6), dpi=80, facecolor='w', edgecolor='k')\nfig.suptitle('Classwise Pixel Count', fontsize=20)\nfig.tight_layout();\n\nax = plt.subplot(1,2,1)\nbar = plt.bar(class_wise_pixel_count.keys(), class_wise_pixel_count.values(), color=colors);\nfor rect in bar:\n    height = rect.get_height()\n    plt.text(rect.get_x() + rect.get_width()/2, height, '%.3E' % height,\n             ha='center', va='bottom',fontsize=10)\nplt.xlabel(\"Cloud Types\");\nplt.ylabel(\"Total Pixels\");\n\nax = plt.subplot(1,2,2)\nplt.pie(class_wise_pixel_count.values(),\n        labels=class_wise_pixel_count.keys(),\n        autopct='%1.1f%%',\n        explode=[0.1,0,0,0],\n        colors=colors,\n        shadow=True, \n        startangle=0);\n\nprint(COLOR.BOLD +COLOR.GREEN+ \"Observation: The pixel distribution of the classes is somewhat balanced.\" + COLOR.END)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:30:54.844925Z","iopub.execute_input":"2023-12-28T10:30:54.845347Z","iopub.status.idle":"2023-12-28T10:30:55.328332Z","shell.execute_reply.started":"2023-12-28T10:30:54.845314Z","shell.execute_reply":"2023-12-28T10:30:55.327072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pixel Distribution","metadata":{}},{"cell_type":"code","source":"total_pixels = img_height*img_width*len(train_df)\nmask_pixels = sum(class_wise_pixel_count.values())\n\npixel_distribution = {\"Background\":(total_pixels-mask_pixels)/total_pixels*100,\n                       \"Fish\":class_wise_pixel_count[\"Fish\"]/total_pixels*100,\n                       \"Flower\":class_wise_pixel_count[\"Flower\"]/total_pixels*100,\n                       \"Gravel\":class_wise_pixel_count[\"Gravel\"]/total_pixels*100,\n                       \"Sugar\":class_wise_pixel_count[\"Sugar\"]/total_pixels*100,\n                       }\n\nprint(pixel_distribution)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:30:58.519355Z","iopub.execute_input":"2023-12-28T10:30:58.519744Z","iopub.status.idle":"2023-12-28T10:30:58.528208Z","shell.execute_reply.started":"2023-12-28T10:30:58.519714Z","shell.execute_reply":"2023-12-28T10:30:58.526768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(num=None, figsize=(18, 6), dpi=80, facecolor='w', edgecolor='k')\nfig.suptitle('Pixel Distribution', fontsize=24)\nfig.tight_layout();\n\nax = plt.subplot(1,2,1)\nbar = plt.bar(pixel_distribution.keys(), pixel_distribution.values(), color=[\"darkslategrey\"]+colors);\nfor rect in bar:\n    height = rect.get_height()\n    plt.text(rect.get_x() + rect.get_width()/2, height, '%.3f %%' % height,\n             ha='center', va='bottom',fontsize=10)\n    \nplt.xlabel(\"Pixel Types\");\nplt.ylabel(\"Parcentage of Pixels\");\n\nax = plt.subplot(1,2,2)\nplt.pie(pixel_distribution.values(),\n        labels=pixel_distribution.keys(),\n        autopct='%1.1f%%',\n        explode=[0.1,0,0,0,0],\n        shadow=True,\n        colors=[\"darkslategrey\"]+colors,\n        startangle=180);\n\n\ncomment = '''Observation: The pixel distribution of the overall dataset is not balanced,\n             most of the pixels does not have any associated label.'''\n\nprint(COLOR.BOLD +COLOR.GREEN+ comment + COLOR.END)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:31:01.379827Z","iopub.execute_input":"2023-12-28T10:31:01.380273Z","iopub.status.idle":"2023-12-28T10:31:02.034775Z","shell.execute_reply.started":"2023-12-28T10:31:01.380242Z","shell.execute_reply":"2023-12-28T10:31:02.033558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Class Frequency","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(num=None, figsize=(12, 6), dpi=80, facecolor='w', edgecolor='k')\nfig.tight_layout();\n\nclass_frequency = dict(train_df[labels].sum())\nbar = plt.bar(class_frequency.keys(),class_frequency.values(), color=colors)\nfor rect in bar:\n    height = rect.get_height()\n    plt.text(rect.get_x() + rect.get_width()/2, height, height,\n             ha='center', va='bottom',fontsize=10)\n    \nplt.xlabel(\"Labels\",fontsize=16);\nplt.ylabel('Frequency',fontsize=16)\nplt.title(\"Mask Class Frequency\",fontsize=20)\n\nprint(COLOR.BOLD +COLOR.GREEN+ \"Observation: The Dataset is somewhat balanced for classifcation tasks.\" + COLOR.END)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:31:05.828102Z","iopub.execute_input":"2023-12-28T10:31:05.828872Z","iopub.status.idle":"2023-12-28T10:31:06.174390Z","shell.execute_reply.started":"2023-12-28T10:31:05.828812Z","shell.execute_reply":"2023-12-28T10:31:06.173200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Class Frequency per Image","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(num=None, figsize=(12, 6), dpi=80, facecolor='w', edgecolor='k')\nfig.tight_layout();\n\nclass_frequency_per_image = dict(train_df[\"classes\"].apply(len).value_counts())\n\nbar = plt.bar(class_frequency_per_image.keys(), class_frequency_per_image.values(), color=colors);\nfor rect in bar:\n    height = rect.get_height()\n    plt.text(rect.get_x() + rect.get_width()/2, height, height,\n             ha='center', va='bottom',fontsize=10)\nplt.xlabel(\"No. of Labels in a Single Image\",fontsize=16);\nplt.xticks(ticks=[1,2,3,4]);\nplt.ylabel(\"Frequency\",fontsize=16);\nplt.title(\"Class Frequency per Image\",fontsize=20);\n\nprint(COLOR.BOLD +COLOR.GREEN+ \"Observation: Most of the images have 2 labels.\" + COLOR.END)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:31:09.973277Z","iopub.execute_input":"2023-12-28T10:31:09.973711Z","iopub.status.idle":"2023-12-28T10:31:10.329180Z","shell.execute_reply.started":"2023-12-28T10:31:09.973676Z","shell.execute_reply":"2023-12-28T10:31:10.327912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Average Mask Area","metadata":{}},{"cell_type":"code","source":"#Avarage Area per mask\navarage_mask_area = dict()\n\nfor key in class_frequency.keys():\n    avarage_mask_area[key] = class_wise_pixel_count[key]//class_frequency[key]\n    \nfig = plt.figure(num=None, figsize=(12, 6), dpi=80, facecolor='w', edgecolor='k')\nfig.tight_layout();\n\nbar = plt.bar(avarage_mask_area.keys(), avarage_mask_area.values(), color=colors);\nfor rect in bar:\n    height = rect.get_height()\n    plt.text(rect.get_x() + rect.get_width()/2, height, '%.3E'%height,\n             ha='center', va='bottom',fontsize=10)\n    \nplt.xlabel(\"Labels\",fontsize=16);\nplt.ylabel(\"Average no. of Pixels per mask\",fontsize=16);\nplt.title(\"Average Mask Area\",fontsize=20);\n\nprint(COLOR.BOLD +COLOR.GREEN+ \"Observation: Avarage area(pixel count) for each mask are somewhat close.\" + COLOR.END)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:31:25.947341Z","iopub.execute_input":"2023-12-28T10:31:25.947751Z","iopub.status.idle":"2023-12-28T10:31:26.296107Z","shell.execute_reply.started":"2023-12-28T10:31:25.947721Z","shell.execute_reply":"2023-12-28T10:31:26.294770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Class Combination Frequency","metadata":{}},{"cell_type":"code","source":"from itertools import combinations\nclasses = labels\ncombinations_list = list(combinations(classes, 1)) + list(combinations(classes, 2)) + list(combinations(classes, 3)) + list(combinations(classes, 4))\n\nlabel_counts = {}\nfor combination in combinations_list:\n    count = train_df[train_df[\"classes\"].apply(lambda x: set(combination).issubset(x))].shape[0]\n    label_counts[tuple(combination)] = count\n    \n\ndef remove_chars_iter(subj,):\n    chars = [\")\",\"(\",\"'\"]\n    subj = str(subj)\n    sc = set(chars)\n    return ''.join([c for c in subj if c not in sc]);\n\n#remove_chars_iter(list(label_counts.keys())[5])\n\n    \nlabel_counts","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:31:29.453191Z","iopub.execute_input":"2023-12-28T10:31:29.453588Z","iopub.status.idle":"2023-12-28T10:31:29.555611Z","shell.execute_reply.started":"2023-12-28T10:31:29.453558Z","shell.execute_reply":"2023-12-28T10:31:29.554771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(num=None, figsize=(12, 6), dpi=80, facecolor='w', edgecolor='k')\nfig.tight_layout();\n\n\nbar = plt.barh(list(map(remove_chars_iter,label_counts.keys())), label_counts.values());\n\nfor rect in bar:\n    width = rect.get_width()\n    plt.text(width-width/3, rect.get_y() + rect.get_height()/5, width,\n             ha='center', va='bottom',fontsize=8)\n    \nplt.xlabel(\"Frequency (No. of Images)\", fontsize=16);\nplt.ylabel(\"Class Combiantions\", fontsize=16);\nplt.title(\"Class Combination Frequency\", fontsize=20);","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:31:44.075542Z","iopub.execute_input":"2023-12-28T10:31:44.075985Z","iopub.status.idle":"2023-12-28T10:31:44.589981Z","shell.execute_reply.started":"2023-12-28T10:31:44.075953Z","shell.execute_reply":"2023-12-28T10:31:44.588863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing the Images\n`PIL or CV2 Image (image_width, image_height) == Numpy Array (image_height, image_width)`","metadata":{}},{"cell_type":"code","source":"def batchDataLoader(image_dir,img_w= 512, img_h=512, num_channel =4, Batch_Size=32):\n    \n    while True:\n        k=0\n        image_ids = os.listdir(image_dir)\n        num_batches = math.ceil(len(image_ids)/Batch_Size)\n        \n        for batch_no in range(1,num_batches+1): \n            if batch_no < num_batches:\n                batch_size = Batch_Size\n                batch_image_ids = image_ids[k:k+batch_size]\n                image_batch = np.zeros((batch_size, img_h, img_w, num_channel),dtype=np.uint8)\n                for i in range(batch_size):\n                    path = os.path.join(image_dir, image_ids[i])\n                    img = cv2.imread(path)\n                    img =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n                    image_batch[i] = img\n            # for the last batch which could be fractional\n            if batch_no == num_batches:\n                batch_image_ids = image_ids[k:]\n                batch_size = len(batch_image_ids)\n                image_batch = np.zeros((batch_size, img_h, img_w, num_channel),dtype=np.uint8)\n                for i in range(batch_size):\n                    path = os.path.join(image_dir, image_ids[i])\n                    img = cv2.imread(path)\n                    img =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n                    image_batch[i] = img\n            \n            k = k+batch_size\n            print(f\"batch_no = {batch_no}\")\n            yield image_batch","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:31:53.600727Z","iopub.execute_input":"2023-12-28T10:31:53.601204Z","iopub.status.idle":"2023-12-28T10:31:53.612668Z","shell.execute_reply.started":"2023-12-28T10:31:53.601167Z","shell.execute_reply":"2023-12-28T10:31:53.611755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_width = 2100\nimg_height = 1400\nnum_channel = 3\nBATCH_SIZE = 32\ncurrent_batch = batchDataLoader(train_image_path,img_width,img_height, num_channel, BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:31:54.028261Z","iopub.execute_input":"2023-12-28T10:31:54.028917Z","iopub.status.idle":"2023-12-28T10:31:54.034064Z","shell.execute_reply.started":"2023-12-28T10:31:54.028886Z","shell.execute_reply":"2023-12-28T10:31:54.032677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = next(current_batch)\nprint(images.shape)\nplt.figure(figsize=(24,8))\nfor i in range(8):\n    ax = plt.subplot(2,4, i+1)\n    plt.imshow(images[i])\n    plt.axis(\"off\");","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:31:54.497311Z","iopub.execute_input":"2023-12-28T10:31:54.498442Z","iopub.status.idle":"2023-12-28T10:32:02.411534Z","shell.execute_reply.started":"2023-12-28T10:31:54.498399Z","shell.execute_reply":"2023-12-28T10:32:02.410115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing the Segmentation Masks","metadata":{}},{"cell_type":"markdown","source":"`rle_to_mask function source` - https://www.kaggle.com/robertkag/rle-to-mask-converter","metadata":{}},{"cell_type":"code","source":"def rle_to_mask(rle_string, height, width):\n    '''\n    convert RLE(run length encoding) string to numpy array\n\n    Parameters: \n    rle_string (str): string of rle encoded mask\n    height (int): height of the mask\n    width (int): width of the mask \n\n    Returns: \n    numpy.array: numpy array of the mask\n    '''\n    \n    rows, cols = height, width\n    \n    if rle_string == -1:\n        return np.zeros((height,width))\n    else:\n        rle_numbers = [int(num_string) for num_string in rle_string.split(' ')]\n        rle_pairs = np.array(rle_numbers).reshape(-1,2)\n        img = np.zeros(rows*cols, dtype=np.uint8)\n        for index, length in rle_pairs:\n            index -= 1\n            img[index:index+length] = 255\n        img = img.reshape(cols,rows)\n        img = img.T\n        img = img/255.0\n        return img","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:32:05.441155Z","iopub.execute_input":"2023-12-28T10:32:05.441712Z","iopub.status.idle":"2023-12-28T10:32:05.450987Z","shell.execute_reply.started":"2023-12-28T10:32:05.441676Z","shell.execute_reply":"2023-12-28T10:32:05.449970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = '0011165.jpg'\npath = os.path.join(train_image_path,image_id)\nimg = cv2.imread(path)\nimg =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nrle = list(train_df[train_df['Image_Id'] == image_id]['Label_EncodedPixels'])[0][0][1]\n\nm = rle_to_mask(rle,img_height,img_width)\nm = cv2.resize(m, (384,256),interpolation=cv2.INTER_LINEAR)\nm = (m>0).astype(int)\nplt.imshow(m)\nprint(m.shape)\nprint(np.unique(m))\nprint(np.argwhere(m==1)[0])","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:32:13.740491Z","iopub.execute_input":"2023-12-28T10:32:13.740924Z","iopub.status.idle":"2023-12-28T10:32:14.188235Z","shell.execute_reply.started":"2023-12-28T10:32:13.740877Z","shell.execute_reply":"2023-12-28T10:32:14.187056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_masks_by_img_id(dataframe, image_id):\n    masks = np.zeros((img_height,img_width,4))\n    rle_masks = list(dataframe[dataframe['Image_Id'] == image_id]['Label_EncodedPixels'])[0]\n    fish_mask = rle_to_mask(rle_masks[0][1], img_height, img_width)\n    flower_mask = rle_to_mask(rle_masks[1][1], img_height, img_width)\n    gravel_mask = rle_to_mask(rle_masks[2][1], img_height, img_width)\n    sugar_mask = rle_to_mask(rle_masks[3][1], img_height, img_width)\n    mask_list = [fish_mask,flower_mask,gravel_mask,sugar_mask]\n    for ix, mask in enumerate(mask_list):\n        masks[:,:,ix] = mask\n    return masks","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:32:14.379864Z","iopub.execute_input":"2023-12-28T10:32:14.380895Z","iopub.status.idle":"2023-12-28T10:32:14.389060Z","shell.execute_reply.started":"2023-12-28T10:32:14.380854Z","shell.execute_reply":"2023-12-28T10:32:14.387862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = os.listdir(train_image_path)[1]\n#image_id = 'f516a20.jpg'\nmasks = get_masks_by_img_id(train_df, image_id)\nmasks.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:32:17.961183Z","iopub.execute_input":"2023-12-28T10:32:17.961627Z","iopub.status.idle":"2023-12-28T10:32:18.145699Z","shell.execute_reply.started":"2023-12-28T10:32:17.961591Z","shell.execute_reply":"2023-12-28T10:32:18.144550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = os.listdir(train_image_path)[INDEX]\n#image_id = 'f516a20.jpg'\nmasks = get_masks_by_img_id(train_df, image_id)\nprint(image_id)\nplt.figure(figsize=(24,4))\nfor ix in range(masks.shape[-1]):\n    ax = plt.subplot(1,4, ix+1)\n    plt.imshow(masks[:,:,ix],cmap=None)\n    plt.axis(\"off\");","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:32:21.206448Z","iopub.execute_input":"2023-12-28T10:32:21.206919Z","iopub.status.idle":"2023-12-28T10:32:22.939572Z","shell.execute_reply.started":"2023-12-28T10:32:21.206879Z","shell.execute_reply":"2023-12-28T10:32:22.938420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing the Images with Segmentation Masks","metadata":{}},{"cell_type":"code","source":"from matplotlib import font_manager\nfont_prop = font_manager.FontProperties(size=16,weight=\"semibold\",stretch=\"condensed\")\n\ndef draw_label_on_mask(mask, label, obj=plt):\n    '''\n    Function to add labels to the image.\n    '''\n    if np.sum(mask) > 0:\n        y,x = 0,0\n        y,x = np.argwhere(mask==1)[0]\n        y,x = y+50,x+20      \n        obj.text(x,y,label,color='white',fontproperties=font_prop)\n    return None","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:32:45.195341Z","iopub.execute_input":"2023-12-28T10:32:45.195742Z","iopub.status.idle":"2023-12-28T10:32:45.203675Z","shell.execute_reply.started":"2023-12-28T10:32:45.195711Z","shell.execute_reply":"2023-12-28T10:32:45.202277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = os.listdir(train_image_path)[INDEX]\npath = os.path.join(train_image_path,image_id)\nimg = cv2.imread(path)\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n#img = cv2.resize(img,(384,256))\nimg = img.astype(np.float32)\nimg = img/255.0\ndpi = 100\n#plt.figure(figsize=(img_width/dpi, img_height/dpi), dpi=dpi)\n#img -= img.mean()\n#img /= img.std()\n#standarization changes the color\n#print(img.shape)\nplt.imshow(img);\nplt.axis('off');\n#plt.savefig(f\"{work_dir}clouds.png\",transparent=True,bbox_inches='tight', pad_inches=0)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:33:13.069151Z","iopub.execute_input":"2023-12-28T10:33:13.069574Z","iopub.status.idle":"2023-12-28T10:33:14.178756Z","shell.execute_reply.started":"2023-12-28T10:33:13.069540Z","shell.execute_reply":"2023-12-28T10:33:14.177873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = os.listdir(train_image_path)[INDEX]\npath = os.path.join(train_image_path,image_id)\nimg = cv2.imread(path)\nimg =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nmasks = get_masks_by_img_id(train_df, image_id)\nlabel = \"Flower\"\nmask = masks[:,:,label_to_idx[label]]\nmask = np.clip(mask,0,1)\nmask = np.ma.masked_where(mask == 0, mask)\ndpi = 100\n#plt.figure(figsize=(img_width/dpi, img_height/dpi), dpi=dpi)\nplt.imshow(img)\nplt.imshow(mask,alpha=0.7,cmap=label_to_colormap[label])\ndraw_label_on_mask(mask,label)\nplt.axis('off');\n#plt.savefig(f\"{work_dir}{label}_mask.png\",transparent=True,bbox_inches='tight', pad_inches=0)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:33:26.076398Z","iopub.execute_input":"2023-12-28T10:33:26.076811Z","iopub.status.idle":"2023-12-28T10:33:27.519001Z","shell.execute_reply.started":"2023-12-28T10:33:26.076779Z","shell.execute_reply":"2023-12-28T10:33:27.517899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = os.listdir(train_image_path)[INDEX]\nmasks = get_masks_by_img_id(train_df, image_id)\nmasks = (masks[:,:,0], masks[:,:,1],masks[:,:,2],masks[:,:,3])\n\npath = os.path.join(train_image_path,image_id)\nimg = cv2.imread(path)\nimg =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\ncolormaps = ['PuRd_r', 'Blues_r', 'Purples_r','winter_r'] # colormap_r = inverse colormap\nmask_labels = ['Fish', 'Flower', 'Gravel', 'Sugar']\nplt.figure(figsize=(15,10))\nfor i,(mask,cmap,label)in enumerate(zip(masks,colormaps,mask_labels)):\n    mask = np.clip(mask,0,1)\n    mask = np.ma.masked_where(mask == 0, mask)\n    ax = plt.subplot(2,2, i+1)\n    plt.imshow(img)\n    plt.imshow(mask,alpha=0.7,cmap=cmap)\n    draw_label_on_mask(mask,label)\n    plt.axis(\"off\")\n    #cv2.imwrite(f\"{label}.jpg\",cv2.cvtColor(img, cv2.COLOR_RGB2BGR))","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:34:00.554019Z","iopub.execute_input":"2023-12-28T10:34:00.554467Z","iopub.status.idle":"2023-12-28T10:34:05.548324Z","shell.execute_reply.started":"2023-12-28T10:34:00.554437Z","shell.execute_reply":"2023-12-28T10:34:05.546968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = os.listdir(train_image_path)[INDEX]\nmasks = get_masks_by_img_id(train_df, image_id)\nmasks = (masks[:,:,0], masks[:,:,1],masks[:,:,2],masks[:,:,3])\n\npath = os.path.join(train_image_path,image_id)\nimg = cv2.imread(path)\nimg =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\ncolormaps = ['PuRd_r', 'Blues_r', 'Purples_r','winter_r']\nmask_labels = ['Fish', 'Flower', 'Gravel', 'Sugar']\ndpi = 100\nplt.figure(figsize=(img_width/dpi, img_height/dpi), dpi=dpi)\nplt.imshow(img)\nfor i,(mask,cmap,label) in enumerate(zip(masks,colormaps,mask_labels)):\n    mask = np.clip(mask,0,1)\n    mask = np.ma.masked_where(mask == 0, mask)\n    plt.imshow(mask,alpha=0.7,cmap=cmap) # colormap_r = inverse colormap\n    draw_label_on_mask(mask,label)\n    plt.axis(\"off\")\n#plt.savefig(f\"{work_dir}cloud_masks.png\",transparent=True,bbox_inches='tight', pad_inches=0)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:34:18.648184Z","iopub.execute_input":"2023-12-28T10:34:18.648628Z","iopub.status.idle":"2023-12-28T10:34:24.597082Z","shell.execute_reply.started":"2023-12-28T10:34:18.648591Z","shell.execute_reply":"2023-12-28T10:34:24.595892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = os.listdir(train_image_path)[INDEX]\npath = os.path.join(train_image_path,image_id)\nimg = cv2.imread(path)\nimg =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nmasks = get_masks_by_img_id(train_df, image_id)\nlabel = \"Sugar\"\nmask = masks[:,:,label_to_idx[label]]\nmask = np.clip(mask,0,1)\nmask = np.ma.masked_where(mask == 0, mask)\nbbox = cv2.boundingRect(mask.astype(np.uint8))\nprint(bbox)\nbbox = (30, 1050, 2040, 320)\ncv2.rectangle(img, bbox, label_to_rgb_color[label], 10)\ncv2.putText(img, label, (bbox[0], bbox[1] + 30),\n                    cv2.FONT_HERSHEY_SIMPLEX, 2.0, (255,255,255), 6)\n#dpi = 100\n#plt.figure(figsize=(img_width/dpi, img_height/dpi), dpi=dpi)\nplt.imshow(img)\n#draw_label_on_mask(mask,label)\nplt.axis('off');\n#plt.savefig(f\"{work_dir}{label}_cloud_bbox.png\",transparent=True,bbox_inches='tight', pad_inches=0)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:39:21.948557Z","iopub.execute_input":"2023-12-28T10:39:21.949021Z","iopub.status.idle":"2023-12-28T10:39:23.219095Z","shell.execute_reply.started":"2023-12-28T10:39:21.948983Z","shell.execute_reply":"2023-12-28T10:39:23.218050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport matplotlib\n\ndef show_bounding_boxes(image, mask, labels, colors):\n    \"\"\"Shows the bounding boxes surrounding the polygon in the image, and\n    adds labels to the bounding boxes.\n\n    Args:\n    image: The image.\n    mask: The binary polygon mask.\n    labels: The labels of the objects in the mask.\n    colors: A list of colors to use for the bounding boxes.\n\n    Returns:\n    The image with the bounding boxes and labels drawn on it.\n    \"\"\"\n\n    # Find the bounding boxes of the polygon.\n    bounding_boxes = []\n    for i in range(mask.shape[-1]):\n        bbox = cv2.boundingRect(mask[:, :, i])\n        bounding_boxes.append(bbox)\n\n    # Draw the bounding boxes on the image.\n    for bbox, label, color_name in zip(bounding_boxes, labels, colors):\n        #rgb_color = matplotlib.colors.to_rgb(color_name)\n        #rgb_color = tuple(value * 255 for value in rgb_color)\n        rgb_color= color_name\n        cv2.rectangle(image, bbox, rgb_color, 7)\n        cv2.putText(image, label, (bbox[0], bbox[1] + 30),\n                    cv2.FONT_HERSHEY_SIMPLEX, 1.3, (255,255,255), 4)\n\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:35:59.882517Z","iopub.execute_input":"2023-12-28T10:35:59.883374Z","iopub.status.idle":"2023-12-28T10:35:59.893225Z","shell.execute_reply.started":"2023-12-28T10:35:59.883335Z","shell.execute_reply":"2023-12-28T10:35:59.892027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rgb_color = matplotlib.colors.to_rgb('darkblue')\nrgb_color = tuple(value * 255 for value in rgb_color)\nrgb_color","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:36:03.446890Z","iopub.execute_input":"2023-12-28T10:36:03.447319Z","iopub.status.idle":"2023-12-28T10:36:03.454869Z","shell.execute_reply.started":"2023-12-28T10:36:03.447286Z","shell.execute_reply":"2023-12-28T10:36:03.453497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image_id = os.listdir(train_image_path)[INDEX]\n# masks = get_masks_by_img_id(train_df, image_id)\n# masks = masks.astype(np.uint8)\n\n\n# path = os.path.join(train_image_path,image_id)\n# img = cv2.imread(path)\n# img =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n# colors = ['maroon', 'darkblue', 'purple','teal']\n# rgb_colors = [(56, 255, 255),(255, 70, 90),(48, 255, 99),(255, 255, 102)]\n# labels = ['Fish', 'Flower', 'Gravel', 'Sugar']\n\n\n# plt.figure(figsize=(15,10))\n# for i,(mask,cmap,label)in enumerate(zip(masks,colormaps,mask_labels)):\n#     mask = np.clip(mask,0,1)\n#     mask = np.ma.masked_where(mask == 0, mask)\n#     ax = plt.subplot(2,2, i+1)\n#     plt.imshow(img)\n#     plt.imshow(mask,alpha=0.7,cmap=cmap)\n#     draw_label_on_mask(mask,label)\n#     plt.axis(\"off\")\n# #cv2.imwrite(f\"{label}.jpg\",cv2.cvtColor(img, cv2.COLOR_RGB2BGR))","metadata":{"execution":{"iopub.status.busy":"2023-12-27T10:33:30.745182Z","iopub.execute_input":"2023-12-27T10:33:30.745861Z","iopub.status.idle":"2023-12-27T10:33:30.761798Z","shell.execute_reply.started":"2023-12-27T10:33:30.745829Z","shell.execute_reply":"2023-12-27T10:33:30.760632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = os.listdir(train_image_path)[INDEX]\nmasks = get_masks_by_img_id(train_df, image_id)\nmasks = masks.astype(np.uint8)\n\n\npath = os.path.join(train_image_path,image_id)\nimg = cv2.imread(path)\nimg =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\ncolors = ['maroon', 'darkblue', 'purple','teal']\nrgb_colors = [(56, 255, 255),(255, 70, 90),(48, 255, 99),(255, 255, 102)]\nlabels = ['Fish', 'Flower', 'Gravel', 'Sugar']\n\nimg = show_bounding_boxes(img,masks,labels,rgb_colors)\n\ndpi = 100\nplt.figure(figsize=(img_width/dpi, img_height/dpi), dpi=dpi)\n#plt.figure(figsize=(32,8))\nplt.imshow(img)\nplt.axis(\"off\");\n#plt.savefig(f\"{work_dir}All_bbox.png\",transparent=True,bbox_inches='tight', pad_inches=0)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:36:08.899627Z","iopub.execute_input":"2023-12-28T10:36:08.900348Z","iopub.status.idle":"2023-12-28T10:36:11.074908Z","shell.execute_reply.started":"2023-12-28T10:36:08.900313Z","shell.execute_reply":"2023-12-28T10:36:11.073700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_img_with_masks(img,masks,comment=\"\"):\n    \n    colormaps = ['PuRd_r', 'Blues_r', 'Purples_r','winter_r']\n    mask_labels = ['Fish', 'Flower', 'Gravel', 'Sugar']\n    \n    fig, axes = plt.subplots(1,6,figsize=(36,4))\n    axes = axes.ravel()\n    \n    if img.shape[-1]!=3:\n        img_cmap = 'gray'\n    else:\n        img_cmap=None\n        \n    for ix,axis in enumerate(axes):\n        ix = ix%6\n        axis.imshow(img,cmap=img_cmap)\n        axis.axis('off')\n        if ix==0:\n            axis.set_title(\"Main Image\")\n        elif ix==1:\n            for i,(mask,cmap,label) in enumerate(zip(masks,colormaps,mask_labels)):\n                mask = np.clip(mask,0,1)\n                mask = np.ma.masked_where(mask == 0, mask)\n                axis.imshow(mask,alpha=0.7,cmap=cmap)\n                axis.set_title(f\"All the mask {comment}\")\n                draw_label_on_mask(mask,label,axis)\n        elif ix>=2:\n            for i,(mask,cmap,label) in enumerate(zip(masks,colormaps,mask_labels)):\n                mask = np.clip(mask,0,1)\n                mask = np.ma.masked_where(mask == 0, mask)\n                axis = axes[2+i]\n                axis.imshow(mask,alpha=0.4,cmap=cmap)\n                axis.set_title(f\"{label} {comment}\")\n                draw_label_on_mask(mask,label,axis)\n    plt.show()\n    \n    return None","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:39:42.926057Z","iopub.execute_input":"2023-12-28T10:39:42.926533Z","iopub.status.idle":"2023-12-28T10:39:42.940281Z","shell.execute_reply.started":"2023-12-28T10:39:42.926494Z","shell.execute_reply":"2023-12-28T10:39:42.938984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = os.listdir(train_image_path)[INDEX]\npath = os.path.join(train_image_path,image_id)\nimg = cv2.imread(path)\nimg =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nmasks = get_masks_by_img_id(train_df, image_id)\nmasks = (masks[:,:,0], masks[:,:,1],masks[:,:,2],masks[:,:,3])\nshow_img_with_masks(img,masks)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:39:44.506579Z","iopub.execute_input":"2023-12-28T10:39:44.507041Z","iopub.status.idle":"2023-12-28T10:39:55.062353Z","shell.execute_reply.started":"2023-12-28T10:39:44.507002Z","shell.execute_reply":"2023-12-28T10:39:55.061145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = os.listdir(train_image_path)[13:16]\nfor image_id in image_ids:\n    path = os.path.join(train_image_path,image_id)\n    img = cv2.imread(path)\n    img =  cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    masks = get_masks_by_img_id(train_df, image_id)\n    masks = (masks[:,:,0], masks[:,:,1],masks[:,:,2],masks[:,:,3])\n    show_img_with_masks(img,masks)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T10:40:26.963387Z","iopub.execute_input":"2023-12-28T10:40:26.964331Z","iopub.status.idle":"2023-12-28T10:40:58.317527Z","shell.execute_reply.started":"2023-12-28T10:40:26.964290Z","shell.execute_reply":"2023-12-28T10:40:58.316192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}