{"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":"!pip install /kaggle/input/pip-install-ultralytics/opencv_python-4.7.0.72-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip install /kaggle/input/pip-install-ultralytics/ultralytics-8.0.109-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:10:29.345061Z","iopub.execute_input":"2023-06-22T03:10:29.345319Z","iopub.status.idle":"2023-06-22T03:11:37.861545Z","shell.execute_reply.started":"2023-06-22T03:10:29.345294Z","shell.execute_reply":"2023-06-22T03:11:37.860403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n!mkdir /kaggle/working/packages\n!cp -r /kaggle/input/pycocotools/* /kaggle/working/packages\nos.chdir(\"/kaggle/working/packages/pycocotools-2.0.6/\")\n!python setup.py install\n!pip install . --no-index --find-links /kaggle/working/packages/\nos.chdir(\"/kaggle/working\")","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:11:37.863820Z","iopub.execute_input":"2023-06-22T03:11:37.864184Z","iopub.status.idle":"2023-06-22T03:12:28.094009Z","shell.execute_reply.started":"2023-06-22T03:11:37.864149Z","shell.execute_reply":"2023-06-22T03:12:28.092832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom glob import glob\nfrom PIL import Image\nfrom skimage import draw\nfrom pathlib import Path\nfrom rasterio import features","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:12:28.096016Z","iopub.execute_input":"2023-06-22T03:12:28.096371Z","iopub.status.idle":"2023-06-22T03:12:28.705363Z","shell.execute_reply.started":"2023-06-22T03:12:28.096334Z","shell.execute_reply":"2023-06-22T03:12:28.704458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ultralytics,torch\nfrom ultralytics import YOLO\nultralytics.checks()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:12:28.707611Z","iopub.execute_input":"2023-06-22T03:12:28.708401Z","iopub.status.idle":"2023-06-22T03:12:39.623124Z","shell.execute_reply.started":"2023-06-22T03:12:28.708365Z","shell.execute_reply":"2023-06-22T03:12:39.622243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_model = YOLO('/kaggle/input/x512e300womosflipudro90mixvafterplds32021ods3tes/best.pt')\n#my_model = YOLO('/kaggle/input/x512e300womosaicflipudrot90mixup/weights/best.pt')\n#results = list(my_model('/kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif', conf=0.001))\nCONF = 0.001\nSIZE = 512\nresults = my_model.predict(\"/kaggle/input/hubmap-hacking-the-human-vasculature/test/\", save=False, imgsz=SIZE, conf=CONF,iou=0.6,save_conf=True,device=0,stream=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:20:14.729704Z","iopub.execute_input":"2023-06-22T03:20:14.730110Z","iopub.status.idle":"2023-06-22T03:20:15.602108Z","shell.execute_reply.started":"2023-06-22T03:20:14.730081Z","shell.execute_reply":"2023-06-22T03:20:15.601115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\n\n\ndef encode_binary_mask(mask: np.ndarray) -> t.Text:\n    \"\"\"Converts a binary mask into OID challenge encoding ascii text.\"\"\"\n\n    # check input mask --\n    if mask.dtype != np.bool_:\n        raise ValueError(\n            \"encode_binary_mask expects a binary mask, received dtype == %s\" %\n            mask.dtype)\n\n    mask = np.squeeze(mask)\n    if len(mask.shape) != 2:\n        raise ValueError(\n            \"encode_binary_mask expects a 2d mask, received shape == %s\" %\n            mask.shape)\n\n    # convert input mask to expected COCO API input --\n    mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)\n    mask_to_encode = mask_to_encode.astype(np.uint8)\n    mask_to_encode = np.asfortranarray(mask_to_encode)\n\n    # RLE encode mask --\n    encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n\n    # compress and base64 encoding --\n    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n    base64_str = base64.b64encode(binary_str)\n    return base64_str","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:20:15.603983Z","iopub.execute_input":"2023-06-22T03:20:15.604421Z","iopub.status.idle":"2023-06-22T03:20:15.613244Z","shell.execute_reply.started":"2023-06-22T03:20:15.604387Z","shell.execute_reply":"2023-06-22T03:20:15.612359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nwith open('/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl', 'r') as json_file:\n    json_list = list(json_file)\n    \ntiles_dicts = []\nfor json_str in json_list:\n    tiles_dicts.append(json.loads(json_str))\ndict_of_tiles = {}\nfor tile in tiles_dicts:\n    dict_of_tiles[tile['id']] = tile['annotations']","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:20:15.614456Z","iopub.execute_input":"2023-06-22T03:20:15.614972Z","iopub.status.idle":"2023-06-22T03:20:19.768280Z","shell.execute_reply.started":"2023-06-22T03:20:15.614940Z","shell.execute_reply":"2023-06-22T03:20:19.767260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_glomerulus_mask(annotations):\n    \"\"\" Converts glomerulus labels into boolean mask \"\"\"\n    mask = np.ones(shape=(512,512), dtype=np.uint8)\n    \n    for annotation in annotations: \n        if annotation['type'] == 'glomerulus':            \n            coords = np.array(annotation['coordinates'])\n            cv2.fillPoly(mask, pts=coords, color=0)\n        \n\n    return mask.astype(bool)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:20:19.770753Z","iopub.execute_input":"2023-06-22T03:20:19.771131Z","iopub.status.idle":"2023-06-22T03:20:19.776959Z","shell.execute_reply.started":"2023-06-22T03:20:19.771097Z","shell.execute_reply":"2023-06-22T03:20:19.775977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids= []\nimport cv2\nprediction_strings = []\nh = []\nw = []\nfor j,result in enumerate(results):\n    img_name = result.path.split(\"/\")[-1].replace(\".tif\",\"\")\n    ids.append(img_name)\n    \n    h.append(result.orig_shape[0])\n    w.append(result.orig_shape[1])\n    \n    \n    #if img_name in dict_of_tiles.keys():\n    #    annotations = dict_of_tiles[img_name]\n    #    glomerulus_mask = get_glomerulus_mask(annotations)\n    #else:\n    #    annotations = []\n    #    glomerulus_mask = np.ones(shape=(512,512)).astype(bool)\n\n\n    \n    try:\n        conf = result.boxes.conf.cpu().numpy()\n        masks = result.masks.data.cpu()\n        if SIZE!=512:\n            masks = torch.nn.functional.interpolate(\n                masks[None,:,:,:],\n                size=512,\n                mode=\"bicubic\",\n                align_corners=False\n            )[0]\n\n        \n        \n        \n        masks = masks.numpy()\n        pred_=[]\n\n        for i in range(len(masks)):\n            c = conf[i]\n            #& \n            rle = encode_binary_mask(np.where((masks[i])>=CONF,True,False)).decode('utf-8')\n            ##  filter out glomerulus_mask\n            #rle = encode_binary_mask(np.where((masks[i])>=CONF,True,False)&glomerulus_mask).decode('utf-8')\n\n            pred_.append(f\"0 {c} {rle}\")\n\n        #f_pred = \n        prediction_strings.append(\" \".join(pred_))\n    except Exception as e:\n        prediction_strings.append(\"\")\n        print(e)\n\n\n\n    \n    \n","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:20:19.778394Z","iopub.execute_input":"2023-06-22T03:20:19.779006Z","iopub.status.idle":"2023-06-22T03:20:20.219842Z","shell.execute_reply.started":"2023-06-22T03:20:19.778973Z","shell.execute_reply":"2023-06-22T03:20:20.218789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n#sub = pd.read_csv(\"/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv\")\nsub = pd.DataFrame()\nsub[\"id\"]=ids\nsub[\"height\"]=h\nsub[\"width\"]=w\nsub[\"prediction_string\"]=prediction_strings\n\nsub.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:20:20.221409Z","iopub.execute_input":"2023-06-22T03:20:20.222520Z","iopub.status.idle":"2023-06-22T03:20:20.235691Z","shell.execute_reply.started":"2023-06-22T03:20:20.222481Z","shell.execute_reply":"2023-06-22T03:20:20.234661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-22T03:20:20.237375Z","iopub.execute_input":"2023-06-22T03:20:20.237865Z","iopub.status.idle":"2023-06-22T03:20:20.254853Z","shell.execute_reply.started":"2023-06-22T03:20:20.237825Z","shell.execute_reply":"2023-06-22T03:20:20.253757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}