{"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":"# Goal of the competition\n\nSegment instances of microvascular structures (capillaries, arteriols, venules) using the computer vision - object detection model\n\n### Medical terms\n\n1. Capillaries are delicate blood vessels that exist throughout your body. They transport blood, nutrients and oxygen to cells in your organs and body systems. \n2. An arteriole is a small-diameter blood vessel in the microcirculation that extends and branches out from an artery and leads to capillaries.\n3. Venules are the smallest veins and receive blood from capillaries. They also play a role in the exchange of oxygen and nutrients for water products.\n4. Histology is the study of the microscopic structure of cells and tissues of organisms. \n5. Whole slide imaging  (WSI), also known as virtual microscopy, refers to scanning a complete microscope slide and creating a single high-resolution digital file. This is commonly achieved by capturing many small high-resolution image tiles or strips and then montaging them to create a full image of a histological section.\n\n### Data\n\n**Polygons JSON file**: polygonal segmentation masks for dataset 1 & dataset 2. \n\nid: identifies corresponding image in train data\n\nannotations: list of mask annotations with blood vessel 'type', capillary ball structure 'glomerulus', 'unsure' - whether blood vessel, polygonal coordinates defining segmentation mask.\n\n**tile_metadata**: meta data for each image\n\nsource_wsi: from which WSI this image extracted\n\n{i|j} : location of the upper-left corner within the WSI where the image was extracted\n\ndataset: the dataset the image belongs to \n\n**wsi_metadata**:\n\nsource_wsi: the WSI\n\nage, sex, race, height, weight, and bmi of the tissue donor\n\n**Train data**: 2D PAS-strained histology images from healthy human kidney tissue slides.\n\nNumber of images: 7033 \n\nSize: 512x512 \n\n**Test data**:\na sample image from the 650 images of private test data\n\n### SUBMISSION\n\nEvaluated by Average Precision over confidence scores. Segmentation is calculated using IoU with a threshold of 0.6.\n\nsubmission file columns - id, height, width, prediction_string\n\nprediction_string is an instance segmentation mask and detection score (confidence). \n\nprediction_string format - 0 {confidence} {EncodedMask}\n\n\n### Logic\n\nsegment the microvascular structures (blood vessels) to understand how they are arranged in the tissues","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-19T17:37:21.566052Z","iopub.execute_input":"2023-07-19T17:37:21.566476Z","iopub.status.idle":"2023-07-19T17:37:21.579839Z","shell.execute_reply.started":"2023-07-19T17:37:21.566443Z","shell.execute_reply":"2023-07-19T17:37:21.578538Z"}}},{"cell_type":"markdown","source":"### Microstructures Image","metadata":{}},{"cell_type":"code","source":"from IPython.display import Image\nImage(\"../input/microstructures/capillary.png\", width=600)","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:21:52.845877Z","iopub.execute_input":"2023-07-20T13:21:52.846356Z","iopub.status.idle":"2023-07-20T13:21:52.872925Z","shell.execute_reply.started":"2023-07-20T13:21:52.846318Z","shell.execute_reply":"2023-07-20T13:21:52.871789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport json\nimport matplotlib\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport cv2 as cv\nmatplotlib.rcParams['figure.figsize'] = (5, 5)","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:21:52.875116Z","iopub.execute_input":"2023-07-20T13:21:52.876234Z","iopub.status.idle":"2023-07-20T13:21:52.931694Z","shell.execute_reply.started":"2023-07-20T13:21:52.876189Z","shell.execute_reply":"2023-07-20T13:21:52.930434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path = '../input/hubmap-hacking-the-human-vasculature/train'\ntrain_images =[]\nfor path in os.listdir(train_images_path):\n    # check if current path is a file\n    if os.path.isfile(os.path.join(train_images_path, path)):\n        train_images.append(os.path.join(train_images_path, path))\n        \nprint(\"Total number of 2D PAS-strained histology images in train data:\",len(train_images))","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:21:52.933300Z","iopub.execute_input":"2023-07-20T13:21:52.933735Z","iopub.status.idle":"2023-07-20T13:22:02.374865Z","shell.execute_reply.started":"2023-07-20T13:21:52.933702Z","shell.execute_reply":"2023-07-20T13:22:02.373929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Read some train images","metadata":{}},{"cell_type":"code","source":"def read_images(image_path):\n    img = cv.imread(image_path,cv.IMREAD_COLOR)\n    rgb_img = cv.cvtColor(img, cv.COLOR_BGRA2RGBA)\n    plt.imshow(rgb_img)","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:02.377021Z","iopub.execute_input":"2023-07-20T13:22:02.377557Z","iopub.status.idle":"2023-07-20T13:22:02.382298Z","shell.execute_reply.started":"2023-07-20T13:22:02.377525Z","shell.execute_reply":"2023-07-20T13:22:02.381297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images[0]","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:02.384089Z","iopub.execute_input":"2023-07-20T13:22:02.385609Z","iopub.status.idle":"2023-07-20T13:22:02.402488Z","shell.execute_reply.started":"2023-07-20T13:22:02.385560Z","shell.execute_reply":"2023-07-20T13:22:02.401616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = [20,10])\nplt.subplot(141);read_images(train_images[0]); plt.title('id: '+ train_images[0].split('/')[-1])\nplt.subplot(142);read_images(train_images[1]); plt.title('id: '+ train_images[1].split('/')[-1])\nplt.subplot(143);read_images(train_images[2]); plt.title('id: '+ train_images[2].split('/')[-1])\nplt.subplot(144);read_images(train_images[3]); plt.title('id: '+ train_images[3].split('/')[-1])\nstop =1\n","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:02.404022Z","iopub.execute_input":"2023-07-20T13:22:02.404682Z","iopub.status.idle":"2023-07-20T13:22:03.990922Z","shell.execute_reply.started":"2023-07-20T13:22:02.404649Z","shell.execute_reply":"2023-07-20T13:22:03.989497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Display some images from image data (merged data)","metadata":{}},{"cell_type":"code","source":"image_metadata = pd.read_csv(\"../input/hubmap-hacking-the-human-vasculature/tile_meta.csv\")\nimage_metadata.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:03.992354Z","iopub.execute_input":"2023-07-20T13:22:03.992719Z","iopub.status.idle":"2023-07-20T13:22:04.019581Z","shell.execute_reply.started":"2023-07-20T13:22:03.992687Z","shell.execute_reply":"2023-07-20T13:22:04.017781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi_data = pd.read_csv(\"/kaggle/input/hubmap-hacking-the-human-vasculature/wsi_meta.csv\")\nwsi_data","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:04.021570Z","iopub.execute_input":"2023-07-20T13:22:04.022202Z","iopub.status.idle":"2023-07-20T13:22:04.055084Z","shell.execute_reply.started":"2023-07-20T13:22:04.022156Z","shell.execute_reply":"2023-07-20T13:22:04.053776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_data = image_metadata.merge(wsi_data,how='left',on='source_wsi').fillna(0)\nimage_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:04.058223Z","iopub.execute_input":"2023-07-20T13:22:04.058634Z","iopub.status.idle":"2023-07-20T13:22:04.106089Z","shell.execute_reply.started":"2023-07-20T13:22:04.058602Z","shell.execute_reply":"2023-07-20T13:22:04.104457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in image_data['id'][:3]:\n    plt.figure(figsize = [5,5])\n    read_images(('../input/hubmap-hacking-the-human-vasculature/train/')+i+'.tif'); plt.title('id: '+i)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:04.107903Z","iopub.execute_input":"2023-07-20T13:22:04.108922Z","iopub.status.idle":"2023-07-20T13:22:05.642271Z","shell.execute_reply.started":"2023-07-20T13:22:04.108885Z","shell.execute_reply":"2023-07-20T13:22:05.640800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Test Image","metadata":{}},{"cell_type":"code","source":"test_image_path = '../input/hubmap-hacking-the-human-vasculature/test'\ntest_image =[]\n\nfor path in os.listdir(test_image_path):\n    if os.path.isfile(os.path.join(test_image_path, path)):\n        test_image.append(os.path.join(test_image_path, path))\n        \nprint(\"Total images in test data:\",len(test_image))","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:05.643863Z","iopub.execute_input":"2023-07-20T13:22:05.644781Z","iopub.status.idle":"2023-07-20T13:22:05.653314Z","shell.execute_reply.started":"2023-07-20T13:22:05.644742Z","shell.execute_reply":"2023-07-20T13:22:05.652044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image2 = cv.imread(test_image[0], cv.IMREAD_COLOR)\nprint(\"image size\",image2.shape)\nplt.imshow(image2[:,:,::-1], cmap='viridis')\nstop=1","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:05.654929Z","iopub.execute_input":"2023-07-20T13:22:05.655687Z","iopub.status.idle":"2023-07-20T13:22:06.108093Z","shell.execute_reply.started":"2023-07-20T13:22:05.655638Z","shell.execute_reply":"2023-07-20T13:22:06.106625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's explore the polygons JSON file","metadata":{}},{"cell_type":"code","source":"with open('/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl','r') as f:\n    polygons_data = [json.loads(line) for line in f]\n\n#print(isinstance(polygons_data, dict))\nprint(\"Number of files with image ids :\",len(polygons_data))","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:06.109878Z","iopub.execute_input":"2023-07-20T13:22:06.110336Z","iopub.status.idle":"2023-07-20T13:22:10.406386Z","shell.execute_reply.started":"2023-07-20T13:22:06.110298Z","shell.execute_reply":"2023-07-20T13:22:10.405020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(polygons_data[0])","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:10.408190Z","iopub.execute_input":"2023-07-20T13:22:10.408725Z","iopub.status.idle":"2023-07-20T13:22:10.414465Z","shell.execute_reply.started":"2023-07-20T13:22:10.408678Z","shell.execute_reply":"2023-07-20T13:22:10.413329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"polygon_list =[]\n\nfor i in polygons_data:\n    polygon_list.append(\n        {'id': i['id'],\n         'annotations':i['annotations']\n        }\n    )\nprint(len(polygon_list[0]['annotations']))\n        ","metadata":{"execution":{"iopub.status.busy":"2023-07-20T13:22:10.416296Z","iopub.execute_input":"2023-07-20T13:22:10.416766Z","iopub.status.idle":"2023-07-20T13:22:10.432102Z","shell.execute_reply.started":"2023-07-20T13:22:10.416720Z","shell.execute_reply":"2023-07-20T13:22:10.430705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}