{"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-07-17T03:26:15.625599Z","iopub.execute_input":"2022-07-17T03:26:15.626438Z","iopub.status.idle":"2022-07-17T03:26:15.818929Z","shell.execute_reply.started":"2022-07-17T03:26:15.626305Z","shell.execute_reply":"2022-07-17T03:26:15.817544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 도움 함수들","metadata":{}},{"cell_type":"code","source":"## 인코딩 칼럼의 마스크 칼럼을 인코딩해야 한다.\n## https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n\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-07-17T03:26:15.821343Z","iopub.execute_input":"2022-07-17T03:26:15.821719Z","iopub.status.idle":"2022-07-17T03:26:15.834001Z","shell.execute_reply.started":"2022-07-17T03:26:15.821686Z","shell.execute_reply":"2022-07-17T03:26:15.832263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Simple DICE Coefficient Implementation\n# 기본적인 다이스 계수 구현\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) # 2 * 교집합 / 각각의 개별 집합\n    dice = np.mean(dice)\n    return round(dice, 3)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T03:26:15.835401Z","iopub.execute_input":"2022-07-17T03:26:15.835941Z","iopub.status.idle":"2022-07-17T03:26:15.850423Z","shell.execute_reply.started":"2022-07-17T03:26:15.835909Z","shell.execute_reply":"2022-07-17T03:26:15.849450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# IoU 계수\ndef IoU_COEFF(mask1, mask2):\n    intersect = np.sum(mask1*mask2) # 교집합\n    sum1 = np.sum(mask1)\n    sum2 = np.sum(mask2) # 합집합\n    iou = intersect/(sum1+sum2 - intersect) # 2 * 교집합 / 각각의 개별 집합\n    iou = np.mean(iou)\n    return round(iou, 3)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T03:44:27.433302Z","iopub.execute_input":"2022-07-17T03:44:27.433736Z","iopub.status.idle":"2022-07-17T03:44:27.440701Z","shell.execute_reply.started":"2022-07-17T03:44:27.433704Z","shell.execute_reply":"2022-07-17T03:44:27.439595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shifting the images towards the bottom to keep it simple\n# 원본 이미지를 하단으로 정해진 크기만큼 미는, 이동한 이미지\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-07-17T03:26:15.852394Z","iopub.execute_input":"2022-07-17T03:26:15.852926Z","iopub.status.idle":"2022-07-17T03:26:15.863180Z","shell.execute_reply.started":"2022-07-17T03:26:15.852893Z","shell.execute_reply":"2022-07-17T03:26:15.862267Z"},"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-07-17T03:34:05.136114Z","iopub.execute_input":"2022-07-17T03:34:05.136750Z","iopub.status.idle":"2022-07-17T03:34:05.475668Z","shell.execute_reply.started":"2022-07-17T03:34:05.136699Z","shell.execute_reply":"2022-07-17T03:34:05.474420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T03:34:05.477891Z","iopub.execute_input":"2022-07-17T03:34:05.479082Z","iopub.status.idle":"2022-07-17T03:34:05.512141Z","shell.execute_reply.started":"2022-07-17T03:34:05.479032Z","shell.execute_reply":"2022-07-17T03:34:05.510769Z"},"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-07-17T03:34:16.173044Z","iopub.execute_input":"2022-07-17T03:34:16.174230Z","iopub.status.idle":"2022-07-17T03:34:16.868459Z","shell.execute_reply.started":"2022-07-17T03:34:16.174155Z","shell.execute_reply":"2022-07-17T03:34:16.866985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T03:34:19.334623Z","iopub.execute_input":"2022-07-17T03:34:19.335022Z","iopub.status.idle":"2022-07-17T03:34:20.642797Z","shell.execute_reply.started":"2022-07-17T03:34:19.334989Z","shell.execute_reply":"2022-07-17T03:34:20.641436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rle = train_df.rle[4]\nrle","metadata":{"execution":{"iopub.status.busy":"2022-07-17T03:34:30.585946Z","iopub.execute_input":"2022-07-17T03:34:30.586394Z","iopub.status.idle":"2022-07-17T03:34:30.594661Z","shell.execute_reply.started":"2022-07-17T03:34:30.586358Z","shell.execute_reply":"2022-07-17T03:34:30.593460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = rle2mask(rle, img1.shape[:2])\nplt.imshow(mask)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T03:34:49.452651Z","iopub.execute_input":"2022-07-17T03:34:49.453097Z","iopub.status.idle":"2022-07-17T03:34:50.598017Z","shell.execute_reply.started":"2022-07-17T03:34:49.453059Z","shell.execute_reply":"2022-07-17T03:34:50.596776Z"},"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똑같은 이미지, 마스크 레이어의 주사위 계수는 1일 것이다. 한 번 확인하는 과정\n\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-07-17T03:37:37.053016Z","iopub.execute_input":"2022-07-17T03:37:37.054014Z","iopub.status.idle":"2022-07-17T03:37:37.094795Z","shell.execute_reply.started":"2022-07-17T03:37:37.053960Z","shell.execute_reply":"2022-07-17T03:37:37.093673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check with Shifted Image ( 이동한 이미지와의 주사위 계수 확인 )","metadata":{}},{"cell_type":"code","source":"# 아래로 이동한 이미지(shift 5만큼)\nsh_mask = return_shifted(mask)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T03:39:06.203845Z","iopub.execute_input":"2022-07-17T03:39:06.204323Z","iopub.status.idle":"2022-07-17T03:39:06.306379Z","shell.execute_reply.started":"2022-07-17T03:39:06.204289Z","shell.execute_reply":"2022-07-17T03:39:06.305246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dice 계수\nDICE_COEFF(mask, sh_mask)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T03:39:06.735985Z","iopub.execute_input":"2022-07-17T03:39:06.736401Z","iopub.status.idle":"2022-07-17T03:39:06.862255Z","shell.execute_reply.started":"2022-07-17T03:39:06.736369Z","shell.execute_reply":"2022-07-17T03:39:06.860836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#IoU 계수\nIoU_COEFF(mask, sh_mask)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T03:44:52.366671Z","iopub.execute_input":"2022-07-17T03:44:52.367938Z","iopub.status.idle":"2022-07-17T03:44:52.493053Z","shell.execute_reply.started":"2022-07-17T03:44:52.367893Z","shell.execute_reply":"2022-07-17T03:44:52.491911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# To be continued","metadata":{}}]}