{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# 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)\nimport tifffile as tiff\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-27T08:59:28.631289Z","iopub.execute_input":"2022-06-27T08:59:28.631776Z","iopub.status.idle":"2022-06-27T08:59:28.813191Z","shell.execute_reply.started":"2022-06-27T08:59:28.631735Z","shell.execute_reply":"2022-06-27T08:59:28.811822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# HANDING FUNCTIONS","metadata":{}},{"cell_type":"code","source":"## We need to decode the mask from encoding column of train.csv\n## https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\ndef mask2rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n \ndef rle2mask(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    #print(starts, ends)\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","metadata":{"execution":{"iopub.status.busy":"2022-06-27T08:59:28.815623Z","iopub.execute_input":"2022-06-27T08:59:28.816007Z","iopub.status.idle":"2022-06-27T08:59:28.827953Z","shell.execute_reply.started":"2022-06-27T08:59:28.815972Z","shell.execute_reply":"2022-06-27T08:59:28.826668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Simple DICE Coefficient Implementation\ndef DICE_COEFF(mask1, mask2):\n    intersect = np.sum(mask1*mask2)\n    sum1 = np.sum(mask1)\n    sum2 = np.sum(mask2)\n    dice = 2*intersect/(sum1+sum2)\n    dice = np.mean(dice)\n    return round(dice, 3)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T09:30:27.539458Z","iopub.execute_input":"2022-06-27T09:30:27.540743Z","iopub.status.idle":"2022-06-27T09:30:27.547568Z","shell.execute_reply.started":"2022-06-27T09:30:27.540682Z","shell.execute_reply":"2022-06-27T09:30:27.546275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shifting the images towards the bottom to keep it simple\ndef return_shifted(mask, shift=5):\n    nmask = np.zeros((mask.shape[0]+shift, mask.shape[1]))\n    nmask[shift:, :] = mask\n    nmask = nmask[:-shift, :]\n    return nmask","metadata":{"execution":{"iopub.status.busy":"2022-06-27T09:31:57.740087Z","iopub.execute_input":"2022-06-27T09:31:57.740691Z","iopub.status.idle":"2022-06-27T09:31:57.746275Z","shell.execute_reply.started":"2022-06-27T09:31:57.740657Z","shell.execute_reply":"2022-06-27T09:31:57.745237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading","metadata":{}},{"cell_type":"code","source":"TRAIN_PATH = \"../input/hubmap-organ-segmentation/train_images\"\nTEST_PATH = \"../input/hubmap-organ-segmentation/test_images\"\n\n# Training Dataset Information\ntrain_df = pd.read_csv(\"../input/hubmap-organ-segmentation/train.csv\")\nprint(f\"Shape of the Training Dataset : {train_df.shape}\")","metadata":{"execution":{"iopub.status.busy":"2022-06-27T08:59:28.829552Z","iopub.execute_input":"2022-06-27T08:59:28.830882Z","iopub.status.idle":"2022-06-27T08:59:29.233009Z","shell.execute_reply.started":"2022-06-27T08:59:28.830814Z","shell.execute_reply":"2022-06-27T08:59:29.231788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-27T08:59:29.235579Z","iopub.execute_input":"2022-06-27T08:59:29.235932Z","iopub.status.idle":"2022-06-27T08:59:29.263896Z","shell.execute_reply.started":"2022-06-27T08:59:29.235902Z","shell.execute_reply":"2022-06-27T08:59:29.262622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image and the Mask","metadata":{}},{"cell_type":"code","source":"img1 = tiff.imread(TRAIN_PATH +'/' + str(train_df.id[4]) + '.tiff')\nimg1.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-27T09:25:38.358368Z","iopub.execute_input":"2022-06-27T09:25:38.358787Z","iopub.status.idle":"2022-06-27T09:25:38.388145Z","shell.execute_reply.started":"2022-06-27T09:25:38.358753Z","shell.execute_reply":"2022-06-27T09:25:38.387087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img1)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T09:25:40.817158Z","iopub.execute_input":"2022-06-27T09:25:40.818243Z","iopub.status.idle":"2022-06-27T09:25:41.958396Z","shell.execute_reply.started":"2022-06-27T09:25:40.818196Z","shell.execute_reply":"2022-06-27T09:25:41.957314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rle = train_df.rle[4]","metadata":{"execution":{"iopub.status.busy":"2022-06-27T09:25:54.814151Z","iopub.execute_input":"2022-06-27T09:25:54.814682Z","iopub.status.idle":"2022-06-27T09:25:54.821043Z","shell.execute_reply.started":"2022-06-27T09:25:54.814631Z","shell.execute_reply":"2022-06-27T09:25:54.819117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = rle2mask(rle, img1.shape[:2])","metadata":{"execution":{"iopub.status.busy":"2022-06-27T09:27:39.245628Z","iopub.execute_input":"2022-06-27T09:27:39.246212Z","iopub.status.idle":"2022-06-27T09:27:39.255309Z","shell.execute_reply.started":"2022-06-27T09:27:39.246180Z","shell.execute_reply":"2022-06-27T09:27:39.254468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check with mask and mask\nYes! You can obtain the answer by easy calculation\n\nLet the number of masked pixels be x . Then the intersection between the two images will also have x pixels.\n\n$\\huge \\frac{2\\cdot x }{x + x}  = 1.0$","metadata":{}},{"cell_type":"code","source":"DICE_COEFF(mask, mask)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T09:30:32.251897Z","iopub.execute_input":"2022-06-27T09:30:32.252333Z","iopub.status.idle":"2022-06-27T09:30:32.290986Z","shell.execute_reply.started":"2022-06-27T09:30:32.252296Z","shell.execute_reply":"2022-06-27T09:30:32.289933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check with Shifted Image","metadata":{}},{"cell_type":"code","source":"sh_mask = return_shifted(mask)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T09:35:40.421100Z","iopub.execute_input":"2022-06-27T09:35:40.421544Z","iopub.status.idle":"2022-06-27T09:35:40.481510Z","shell.execute_reply.started":"2022-06-27T09:35:40.421495Z","shell.execute_reply":"2022-06-27T09:35:40.480611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DICE_COEFF(mask, sh_mask)","metadata":{"execution":{"iopub.status.busy":"2022-06-27T09:35:42.189981Z","iopub.execute_input":"2022-06-27T09:35:42.190740Z","iopub.status.idle":"2022-06-27T09:35:42.266190Z","shell.execute_reply.started":"2022-06-27T09:35:42.190699Z","shell.execute_reply":"2022-06-27T09:35:42.265043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# To be continued","metadata":{}}]}