{"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":"markdown","source":"# HuBMAP - Microvascular Instance Segmentation\n\n### Competition Goal\n- segment instances of microvascular structures : \n    1. Capillaries\n    2. Arterioles,\n    3. Venules\n\n- Data : 2D PAS-stained histology image from Healty humann kidney tissue\n\n- Dataset Structures :\n\n    - __{train|test}/__ : Folders containing TIFF images of the tiles. Each tile is 512x512 in size.\n    - __polygons.jsonl__ : Polygonal segmentation masks in JSONL format, available for Dataset 1 and Dataset 2. Each line gives JSON annotations for a single image with:\n        - __id__ : Identifies the corresponding image in train/\n        - __annotations__ : A list of mask annotations with:\n        - __type__ : Identifies the type of structure annotated:\n            - __blood_vessel__ : The target structure. Your goal in this competition is to predict these kinds of masks on the test set.\n            - __glomerulus__ : A capillary ball structure in the kidney. These parts of the images were excluded from blood vessel annotation. You should ensure none of your test set predictions occur within glomerulus structures as they will be counted as false positives. Annotations are provided for test set tiles.\n            - __unsure__ : A structure the expert annotators cannot confidently distinguish as a blood vessel.\n        - __coordinates__ A list of polygon coordinates defining the segmentation mask.\n    - __tile_meta.csv__ Metadata for each image.\n        - __source_wsi__ Identifies the WSI this tile was extracted from.\n        - __{i|j}__ The location of the upper-left corner within the WSI where the tile was extracted.\n        - __dataset__ The dataset this tile belongs to, as described above.\n    - __wsi_meta.csv__ Metadata for the Whole Slide Images the tiles were extracted from.\n        - __source_wsi__ Identifies the WSI.\n        - __age, sex, race, height, weight, and bmi__ demographic information about the tissue donor.\n    - __sample_submission.csv__ A sample submission file in the correct format. See the Evaluation page for more details.\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:25.492788Z","iopub.execute_input":"2023-05-26T16:54:25.493228Z","iopub.status.idle":"2023-05-26T16:54:25.725647Z","shell.execute_reply.started":"2023-05-26T16:54:25.493175Z","shell.execute_reply":"2023-05-26T16:54:25.724180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring Meta Data","metadata":{}},{"cell_type":"code","source":"tile_meta = pd.read_csv(\"/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv\")\nwsi_meta = pd.read_csv(\"/kaggle/input/hubmap-hacking-the-human-vasculature/wsi_meta.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:25.727823Z","iopub.execute_input":"2023-05-26T16:54:25.728391Z","iopub.status.idle":"2023-05-26T16:54:25.785629Z","shell.execute_reply.started":"2023-05-26T16:54:25.728345Z","shell.execute_reply":"2023-05-26T16:54:25.784502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tile_meta.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:25.787164Z","iopub.execute_input":"2023-05-26T16:54:25.787779Z","iopub.status.idle":"2023-05-26T16:54:25.820301Z","shell.execute_reply.started":"2023-05-26T16:54:25.787742Z","shell.execute_reply":"2023-05-26T16:54:25.819477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:25.822823Z","iopub.execute_input":"2023-05-26T16:54:25.823441Z","iopub.status.idle":"2023-05-26T16:54:25.839814Z","shell.execute_reply.started":"2023-05-26T16:54:25.823405Z","shell.execute_reply":"2023-05-26T16:54:25.838791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting Image Data","metadata":{}},{"cell_type":"code","source":"tiff_file = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train/0006ff2aa7cd.tif\"\ntiff_img = cv2.imread(tiff_file)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:25.841148Z","iopub.execute_input":"2023-05-26T16:54:25.841508Z","iopub.status.idle":"2023-05-26T16:54:25.892610Z","shell.execute_reply.started":"2023-05-26T16:54:25.841473Z","shell.execute_reply":"2023-05-26T16:54:25.891868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5,5))\nplt.imshow(tiff_img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:25.894311Z","iopub.execute_input":"2023-05-26T16:54:25.894974Z","iopub.status.idle":"2023-05-26T16:54:26.296647Z","shell.execute_reply.started":"2023-05-26T16:54:25.894936Z","shell.execute_reply":"2023-05-26T16:54:26.295869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring Polygon JSONL file","metadata":{}},{"cell_type":"code","source":"import json\nimport os\nimport numpy as np\nfrom tqdm import tqdm, notebook\nfrom torch.utils.data import Dataset, DataLoader","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:26.297850Z","iopub.execute_input":"2023-05-26T16:54:26.298124Z","iopub.status.idle":"2023-05-26T16:54:29.629392Z","shell.execute_reply.started":"2023-05-26T16:54:26.298100Z","shell.execute_reply":"2023-05-26T16:54:29.628531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class HubMAP_Dataset(Dataset):\n    def __init__(self, jsonPath, image_dir, augments = False, train=True):\n        # read jsonl file, \n        with open(jsonPath) as json_file:\n            json_list = list(json_file)\n        \n        # loop through the list, to create Dataframe of json Data\n        dataset = []\n        for json_str in notebook.tqdm(json_list, desc=\"Reading Json Data\"):\n            result = json.loads(json_str)\n            \n            annotations = result['annotations']\n            row = {}\n            for ann in annotations:\n                row = {}\n                row[\"id\"] = result[\"id\"]\n                row[\"type\"] = ann[\"type\"]\n                row[\"coordinates\"] = ann[\"coordinates\"]\n                dataset.append(row)\n        \n        # define dataset, to make it easier to get...\n        self.dataset = pd.DataFrame(dataset, columns=[\"id\", \"type\", \"coordinates\"])                      \n        self.train = train\n        self.image_dir = image_dir\n        \n    def __getitem__(self, idx):\n        \n        data = self.dataset.iloc[idx]\n        \n        imageLoc = os.path.join(self.image_dir,data.id+\".tif\")\n        img = cv2.imread(imageLoc)\n        \n        type_struct = data.type\n        coord = data.coordinates[0]\n        \n        # create mask array\n        mask = np.zeros((512, 512), dtype=np.float32)\n        points = np.array(coord)\n        points = points.reshape((1, -1, 2))\n        mask = cv2.fillPoly(mask, pts=points, color=(255))    \n        \n        return img, type_struct, mask\n            \n    def __len__(self):\n        return len(self.dataset)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:29.630618Z","iopub.execute_input":"2023-05-26T16:54:29.631629Z","iopub.status.idle":"2023-05-26T16:54:29.642273Z","shell.execute_reply.started":"2023-05-26T16:54:29.631599Z","shell.execute_reply":"2023-05-26T16:54:29.641363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"jsonL = \"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\"\nimage_dir = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train\"\n\ntrainDataset = HubMAP_Dataset(jsonPath=jsonL, augments=False, image_dir=image_dir, train=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:29.645022Z","iopub.execute_input":"2023-05-26T16:54:29.645551Z","iopub.status.idle":"2023-05-26T16:54:34.953720Z","shell.execute_reply.started":"2023-05-26T16:54:29.645525Z","shell.execute_reply":"2023-05-26T16:54:34.952641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainDataset.dataset.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:34.956863Z","iopub.execute_input":"2023-05-26T16:54:34.957256Z","iopub.status.idle":"2023-05-26T16:54:35.162704Z","shell.execute_reply.started":"2023-05-26T16:54:34.957230Z","shell.execute_reply":"2023-05-26T16:54:35.161705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize Image and Mask","metadata":{}},{"cell_type":"code","source":"img, type_struct, mask = trainDataset[0]\n\nm = (mask >= 255) * 1\nimg_mask = np.zeros((512,512,3), dtype=np.uint8)\n\nimg_mask[:,:,0] = img[:,:,0] * m\nimg_mask[:,:,1] = img[:,:,1] * m\nimg_mask[:,:,2] = img[:,:,2] * m","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:35.164154Z","iopub.execute_input":"2023-05-26T16:54:35.164482Z","iopub.status.idle":"2023-05-26T16:54:35.199236Z","shell.execute_reply.started":"2023-05-26T16:54:35.164455Z","shell.execute_reply":"2023-05-26T16:54:35.198182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,50))\n\nplt.subplot(1,3,1)\nplt.imshow(img)\nplt.axis(\"off\")\n\nplt.subplot(1,3,2)\nplt.imshow(mask)\nplt.title(type_struct)\nplt.axis(\"off\")\n\nplt.subplot(1,3,3)\nplt.imshow(img_mask)\nplt.title(type_struct)\nplt.axis(\"off\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:35.200457Z","iopub.execute_input":"2023-05-26T16:54:35.200852Z","iopub.status.idle":"2023-05-26T16:54:35.726311Z","shell.execute_reply.started":"2023-05-26T16:54:35.200818Z","shell.execute_reply":"2023-05-26T16:54:35.725506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset Version 2 - Instance Mask","metadata":{"execution":{"iopub.status.busy":"2023-05-26T14:20:36.675775Z","iopub.execute_input":"2023-05-26T14:20:36.676805Z","iopub.status.idle":"2023-05-26T14:20:36.681640Z","shell.execute_reply.started":"2023-05-26T14:20:36.676766Z","shell.execute_reply":"2023-05-26T14:20:36.680390Z"}}},{"cell_type":"code","source":"class HubMAP_Datasetv2(Dataset):\n    def __init__(self, jsonPath, image_dir, augment = False, train=True):\n        # read jsonl file, \n        with open(jsonPath) as json_file:\n            self.json_list = list(json_file)\n            \n        self.image_dir = image_dir\n        self.train=train\n        self.augment=augment\n        \n    def __getitem__(self, idx):\n        # read json\n        json_str = self.json_list[idx]\n        result = json.loads(json_str)\n        \n        # read image\n        imgPath = os.path.join(self.image_dir,result[\"id\"]+\".tif\")\n        img = cv2.imread(imgPath)\n        \n        # mask image\n        target = np.zeros((512,512), dtype=np.uint8)        \n        mask = np.zeros((512,512,3), dtype=np.uint8)        \n        annotations = result['annotations']\n        for ann in annotations:            \n            type_struct = ann[\"type\"]\n            coords = ann[\"coordinates\"]        \n            points = np.array(coords)\n            points = points.reshape((1, -1, 2))                        \n            row, col = np.array([c[1] for c in coords]), np.array([c[0] for c in coords])                \n\n            if type_struct == \"blood_vessel\":\n                mask = cv2.fillPoly(mask, pts=points, color=(0,255,0)) # B\n                target[row, col] = 1\n            elif type_struct == \"glomerulus\":\n                mask = cv2.fillPoly(mask, pts=points, color=(0,0,255)) # R\n                target[row, col] = 2\n            else:\n                mask = cv2.fillPoly(mask, pts=points, color=(255,0,0)) # G            \n#                 target[row, col] = 3\n        \n        return img, target, mask\n            \n    def __len__(self):\n        return len(self.json_list)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:35.727494Z","iopub.execute_input":"2023-05-26T16:54:35.728205Z","iopub.status.idle":"2023-05-26T16:54:35.740729Z","shell.execute_reply.started":"2023-05-26T16:54:35.728177Z","shell.execute_reply":"2023-05-26T16:54:35.739970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"jsonL = \"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\"\nimage_dir = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train\"\n\ntrainDataset = HubMAP_Datasetv2(jsonPath=jsonL, augment=False, image_dir=image_dir, train=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:35.741911Z","iopub.execute_input":"2023-05-26T16:54:35.742744Z","iopub.status.idle":"2023-05-26T16:54:35.799906Z","shell.execute_reply.started":"2023-05-26T16:54:35.742708Z","shell.execute_reply":"2023-05-26T16:54:35.798965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img, target, mask = trainDataset[0]","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:35.800925Z","iopub.execute_input":"2023-05-26T16:54:35.801196Z","iopub.status.idle":"2023-05-26T16:54:35.819794Z","shell.execute_reply.started":"2023-05-26T16:54:35.801173Z","shell.execute_reply":"2023-05-26T16:54:35.818926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)\nret2, m_t = cv2.threshold(m, 0, 255, cv2.THRESH_BINARY)\nimg2 = cv2.bitwise_and(img, img, mask=m_t)\nimg3 = img * mask","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:35.820805Z","iopub.execute_input":"2023-05-26T16:54:35.821131Z","iopub.status.idle":"2023-05-26T16:54:35.851560Z","shell.execute_reply.started":"2023-05-26T16:54:35.821108Z","shell.execute_reply":"2023-05-26T16:54:35.850814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,30))\n\nplt.subplot(1,3,1)\nplt.imshow(img)\nplt.axis(\"off\")\n\nplt.subplot(1,3,2)\nplt.imshow(img2)\nplt.axis(\"off\")\n\nplt.subplot(1,3,3)\nplt.imshow(img3)\nplt.axis(\"off\")\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:54:35.853730Z","iopub.execute_input":"2023-05-26T16:54:35.854796Z","iopub.status.idle":"2023-05-26T16:54:36.333882Z","shell.execute_reply.started":"2023-05-26T16:54:35.854740Z","shell.execute_reply":"2023-05-26T16:54:36.332871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Implement UNET","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}