{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"none","dataSources":[{"sourceId":13333,"databundleVersionId":862146,"sourceType":"competition"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\n\n%matplotlib inline\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2024-08-31T20:28:21.178877Z","iopub.execute_input":"2024-08-31T20:28:21.179962Z","iopub.status.idle":"2024-08-31T20:28:21.186911Z","shell.execute_reply.started":"2024-08-31T20:28:21.179910Z","shell.execute_reply":"2024-08-31T20:28:21.185835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RANDOM_SEED = 2906\n\ndef seed_everything(seed: int):  \n    np.random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    \nseed_everything(RANDOM_SEED)","metadata":{"execution":{"iopub.status.busy":"2024-08-31T20:28:21.189003Z","iopub.execute_input":"2024-08-31T20:28:21.189333Z","iopub.status.idle":"2024-08-31T20:28:21.199121Z","shell.execute_reply.started":"2024-08-31T20:28:21.189300Z","shell.execute_reply":"2024-08-31T20:28:21.198139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load data:","metadata":{}},{"cell_type":"code","source":"train_folder = '/kaggle/input/understanding_cloud_organization/train_images'\ntest_folder = '/kaggle/input/understanding_cloud_organization/test_images'\ntrain_df = pd.read_csv('/kaggle/input/understanding_cloud_organization/train.csv')\n\nsplitted = train_df['Image_Label'].str.split(pat='_', expand=True)\n\ntrain_df['Image'] = splitted[0]\ntrain_df['Label'] = splitted[1]\n\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-31T20:28:21.200226Z","iopub.execute_input":"2024-08-31T20:28:21.200594Z","iopub.status.idle":"2024-08-31T20:28:23.416218Z","shell.execute_reply.started":"2024-08-31T20:28:21.200559Z","shell.execute_reply":"2024-08-31T20:28:23.415278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-08-31T20:28:23.417369Z","iopub.execute_input":"2024-08-31T20:28:23.417674Z","iopub.status.idle":"2024-08-31T20:28:23.437489Z","shell.execute_reply.started":"2024-08-31T20:28:23.417641Z","shell.execute_reply":"2024-08-31T20:28:23.436641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.iloc[:, 3].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-31T20:28:23.440026Z","iopub.execute_input":"2024-08-31T20:28:23.440351Z","iopub.status.idle":"2024-08-31T20:28:23.456220Z","shell.execute_reply.started":"2024-08-31T20:28:23.440309Z","shell.execute_reply":"2024-08-31T20:28:23.455188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Drawing the edges:","metadata":{}},{"cell_type":"code","source":"def get_image(folder, idx: int):\n    if idx < 0 or idx >= len(folder):\n        raise IndexError('Index is out of range')\n    image_dir = os.listdir(folder)\n    image_name = image_dir[idx]\n    image_path = os.path.join(folder, image_name)\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    return image, image_name\n\ndef get_edge(image, threshold1, threshold2):\n    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    edge_image = cv2.Canny(gray_image, threshold1, threshold2)\n    return edge_image","metadata":{"execution":{"iopub.status.busy":"2024-08-31T20:28:23.457276Z","iopub.execute_input":"2024-08-31T20:28:23.457594Z","iopub.status.idle":"2024-08-31T20:28:23.465090Z","shell.execute_reply.started":"2024-08-31T20:28:23.457560Z","shell.execute_reply":"2024-08-31T20:28:23.464137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold1 = 150\nthreshold2 = 250\n\norig_images = []\nedged_images = []\n\nfor i in range(1, 4):\n    image = get_image(train_folder, i)[0]\n    orig_images.append(image)\n    edge_image = get_edge(image, threshold1, threshold2)\n    edged_images.append(edge_image)\n\nplt.figure(figsize=(8, 10))\nfor i in range(3):\n    plt.subplot(3, 2, 2 * i + 1)\n    plt.imshow(orig_images[i])\n    plt.axis('off')\n    \n    plt.subplot(3, 2, 2 * i + 2)\n    plt.imshow(edged_images[i], cmap='gray')\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-31T20:28:23.466512Z","iopub.execute_input":"2024-08-31T20:28:23.466934Z","iopub.status.idle":"2024-08-31T20:28:25.613335Z","shell.execute_reply.started":"2024-08-31T20:28:23.466886Z","shell.execute_reply":"2024-08-31T20:28:25.612370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get the mask:","metadata":{}},{"cell_type":"code","source":"shape = (1400, 2100)\n\ndef rle_decode(encoded_pixels, shape=shape):\n    encoded_pixels = encoded_pixels.split()\n\n    starts = np.array(encoded_pixels[0::2], dtype=np.int32, copy=False) - 1\n    lengths = np.array(encoded_pixels[1::2], dtype=int, copy=False)\n\n    ends = starts + lengths\n\n    mask = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n    for start, end in zip(starts, ends):\n        mask[start:end] = 1\n    return np.reshape(mask, shape, order='F')","metadata":{"execution":{"iopub.status.busy":"2024-08-31T20:28:25.614687Z","iopub.execute_input":"2024-08-31T20:28:25.615129Z","iopub.status.idle":"2024-08-31T20:28:25.622321Z","shell.execute_reply.started":"2024-08-31T20:28:25.615089Z","shell.execute_reply":"2024-08-31T20:28:25.621378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20, 15))\nfor j, image_name in enumerate(np.random.choice(train_df['Image'].unique(), 2)):\n    for i, (idx, row) in enumerate(train_df.loc[train_df['Image'] == image_name].iterrows()):\n        image_name = row['Image']\n        label = row['Label']\n        ax = fig.add_subplot(4, 4, j * 4 + i + 1, xticks=[], yticks=[])\n        image = cv2.imread(f'{train_folder}/{image_name}')\n        plt.imshow(image)\n        encoded_pixels = row['EncodedPixels']\n        try:\n            mask = rle_decode(encoded_pixels)\n        except:\n            mask = np.zeros((1400, 2100))\n        plt.imshow(mask, alpha=0.25)\n        ax.set_title(f\"Image: {image_name}. Label: {label}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-31T20:28:25.623611Z","iopub.execute_input":"2024-08-31T20:28:25.623919Z","iopub.status.idle":"2024-08-31T20:28:33.276511Z","shell.execute_reply.started":"2024-08-31T20:28:25.623885Z","shell.execute_reply":"2024-08-31T20:28:33.275463Z"},"trusted":true},"execution_count":null,"outputs":[]}]}