{"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":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_datasets as tfds\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport tifffile as tiff \nfrom skimage.transform import resize\n\nBASE_PATH = \"../input/hubmap-organ-segmentation/\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-25T03:20:42.582462Z","iopub.execute_input":"2022-06-25T03:20:42.583279Z","iopub.status.idle":"2022-06-25T03:20:45.230738Z","shell.execute_reply.started":"2022-06-25T03:20:42.583191Z","shell.execute_reply":"2022-06-25T03:20:45.229775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **MetaData Load**","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\ntest = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-25T03:20:45.231868Z","iopub.execute_input":"2022-06-25T03:20:45.232956Z","iopub.status.idle":"2022-06-25T03:20:45.403473Z","shell.execute_reply.started":"2022-06-25T03:20:45.232921Z","shell.execute_reply":"2022-06-25T03:20:45.401956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-06-25T03:20:45.405200Z","iopub.execute_input":"2022-06-25T03:20:45.406791Z","iopub.status.idle":"2022-06-25T03:20:45.447987Z","shell.execute_reply.started":"2022-06-25T03:20:45.406745Z","shell.execute_reply":"2022-06-25T03:20:45.446709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Masking and Unmasking Functions**","metadata":{}},{"cell_type":"code","source":"def 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=(1600,256)):\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    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-25T03:20:45.450978Z","iopub.execute_input":"2022-06-25T03:20:45.451843Z","iopub.status.idle":"2022-06-25T03:20:45.468234Z","shell.execute_reply.started":"2022-06-25T03:20:45.451794Z","shell.execute_reply":"2022-06-25T03:20:45.466828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_id_1 = 9777\nimg_1 = tiff.imread(BASE_PATH + \"train_images/\" + str(img_id_1) + \".tiff\")\nprint(img_1.shape)\n\nimg_id_2 = 9904\nimg_2 = tiff.imread(BASE_PATH + \"train_images/\" + str(img_id_2) + \".tiff\")\nprint(img_2.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T03:20:45.470174Z","iopub.execute_input":"2022-06-25T03:20:45.470981Z","iopub.status.idle":"2022-06-25T03:20:45.517475Z","shell.execute_reply.started":"2022-06-25T03:20:45.470936Z","shell.execute_reply":"2022-06-25T03:20:45.516226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Getting the Mask Image**","metadata":{}},{"cell_type":"code","source":"mask_1 = rle2mask(train[train[\"id\"]==9777][\"rle\"].iloc[-1], (img_1.shape[1], img_1.shape[0]))\nprint(mask_1.shape)\n\nmask_2 = rle2mask(train[train[\"id\"]==9904][\"rle\"].iloc[-1], (img_2.shape[1], img_2.shape[0]))\nprint(mask_2.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T03:20:45.519084Z","iopub.execute_input":"2022-06-25T03:20:45.519759Z","iopub.status.idle":"2022-06-25T03:20:45.588511Z","shell.execute_reply.started":"2022-06-25T03:20:45.519715Z","shell.execute_reply":"2022-06-25T03:20:45.587394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Orginal Image Size Mask with Acutal Image**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(img_1)\nplt.imshow(mask_1, cmap='coolwarm', alpha=0.5)\n\nplt.figure(figsize=(10,10))\nplt.imshow(img_2)\nplt.imshow(mask_2, cmap='coolwarm', alpha=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T03:20:45.589946Z","iopub.execute_input":"2022-06-25T03:20:45.590610Z","iopub.status.idle":"2022-06-25T03:20:50.386306Z","shell.execute_reply.started":"2022-06-25T03:20:45.590569Z","shell.execute_reply":"2022-06-25T03:20:50.385398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Resizing Both Mask Image and Actual Image**","metadata":{}},{"cell_type":"code","source":"print('Image 1 Resizing.......')\nmask_1 = resize(mask_1, (mask_1.shape[1] // 7, mask_1.shape[0] // 7))\nprint(mask_1.shape)\n\nimg_1 = resize(img_1, (img_1.shape[1] // 7, img_1.shape[0] // 7))\nprint(img_1.shape)\n\nprint('\\nImage 2 Resizing.......')\nmask_2 = resize(mask_2, (mask_2.shape[1] // 7, mask_2.shape[0] // 7))\nprint(mask_1.shape)\n\nimg_2 = resize(img_2, (img_2.shape[1] // 7, img_2.shape[0] // 7))\nprint(img_2.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T03:20:50.387414Z","iopub.execute_input":"2022-06-25T03:20:50.388141Z","iopub.status.idle":"2022-06-25T03:20:53.244760Z","shell.execute_reply.started":"2022-06-25T03:20:50.388100Z","shell.execute_reply":"2022-06-25T03:20:53.243511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **Resized Mask Image with Actual Image**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(img_1)\nplt.imshow(mask_1, cmap='coolwarm', alpha=0.5)\n\nplt.figure(figsize=(10,10))\nplt.imshow(img_2)\nplt.imshow(mask_2, cmap='coolwarm', alpha=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-06-25T03:20:53.246266Z","iopub.execute_input":"2022-06-25T03:20:53.246693Z","iopub.status.idle":"2022-06-25T03:20:54.101289Z","shell.execute_reply.started":"2022-06-25T03:20:53.246661Z","shell.execute_reply":"2022-06-25T03:20:54.100176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🥳 If You find this kernal useful Upvote 🥳🎉👏","metadata":{}}]}