{"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 uninstall -y opencv-python","metadata":{"execution":{"iopub.status.busy":"2023-07-01T03:49:25.054671Z","iopub.execute_input":"2023-07-01T03:49:25.055151Z","iopub.status.idle":"2023-07-01T03:49:28.510023Z","shell.execute_reply.started":"2023-07-01T03:49:25.055112Z","shell.execute_reply":"2023-07-01T03:49:28.508813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -qq /kaggle/input/yolov8-requirments/opencv_python-4.7.0.72-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip install -qq /kaggle/input/yolov8-requirments/ultralytics-8.0.117-py3-none-any.whl\n!pip install -qq /kaggle/input/mmdetection/pycocotools-2.0-cp310-cp310-linux_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2023-07-01T03:49:28.512317Z","iopub.execute_input":"2023-07-01T03:49:28.512989Z","iopub.status.idle":"2023-07-01T03:51:07.050070Z","shell.execute_reply.started":"2023-07-01T03:49:28.512944Z","shell.execute_reply":"2023-07-01T03:51:07.048794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport cv2\nimport pandas as pd\nimport base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\nfrom ultralytics import YOLO\nimport matplotlib.pyplot as plt\nimport typing as t\nimport time\nfrom skimage.morphology import binary_dilation\nimport glob\n\nsys.path.append(\"/kaggle/input/weighted-boxes-fusion/Weighted-Boxes-Fusion-master/ensemble_boxes/\")\n# import ensemble_boxes_nms\nimport ensemble_boxes_wbf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-01T03:51:07.052236Z","iopub.execute_input":"2023-07-01T03:51:07.052654Z","iopub.status.idle":"2023-07-01T03:51:19.450005Z","shell.execute_reply.started":"2023-07-01T03:51:07.052609Z","shell.execute_reply":"2023-07-01T03:51:19.448947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # From https://www.kaggle.com/stainsby/fast-tested-rle\n# def rle_decode(mask_rle, shape=(520, 704)):\n#     '''\n#     mask_rle: run-length as string formated (start length)\n#     shape: (height,width) 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)  # Needed to align to RLE direction\n\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.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\n\ndef encode_binary_mask(mask: np.ndarray) -> t.Text:\n    # check input mask --\n    if mask.dtype != 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-07-01T03:51:19.453159Z","iopub.execute_input":"2023-07-01T03:51:19.453920Z","iopub.status.idle":"2023-07-01T03:51:19.464233Z","shell.execute_reply.started":"2023-07-01T03:51:19.453867Z","shell.execute_reply":"2023-07-01T03:51:19.463247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_mask(mask, target_size=(512, 512)):\n    resized_mask = cv2.resize(mask, target_size, interpolation=cv2.INTER_NEAREST)\n    return resized_mask","metadata":{"execution":{"iopub.status.busy":"2023-07-01T03:51:19.465890Z","iopub.execute_input":"2023-07-01T03:51:19.466544Z","iopub.status.idle":"2023-07-01T03:51:19.482691Z","shell.execute_reply.started":"2023-07-01T03:51:19.466487Z","shell.execute_reply":"2023-07-01T03:51:19.481618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = glob.glob('/kaggle/input/hubmap-hacking-the-human-vasculature/test/*')\n\n\nsub = pd.DataFrame()\n\nidxes = []\nheights = []\nwidths = []\npredict_string = []\n\nstart_time = time.time()\n\nmodel = YOLO(\"/kaggle/input/yolov8/hubmap-vasculature2class-seg1536-best.pt\")\n# model2 = YOLO(\"/kaggle/input/yolov8/hubmap-vasculature1class-seg1920-best.pt\")\nfor file in files:\n    idx = file.split('/')[-1][:-4]\n    idxes.append(idx)\n    img = cv2.imread(file)\n    height, width, _ = img.shape[:3]\n    heights.append(height)\n    widths.append(width)\n    \n    \n#     sizes = [1920, 1920]\n    \n#     boxes_list = []\n#     scores_list = []\n#     labels_list = []\n#     weights = [1, 1]\n#     iou_thr = 0.5\n#     skip_box_thr = 0.0001\n\n    \n#     for sz in sizes:\n#         img = cv2.resize(img, (sz, sz))\n#         results = model.predict(img,conf=0.1, device=0)\n#         r = ''\n#         _classes = results[0].boxes.cls.cpu().numpy()\n#         confs = results[0].boxes.conf[_classes==0].cpu()\n#         label = _classes[_classes==0]\n#         boxes = results[0].boxes[_classes==0].xyxy.cpu().numpy()/1920\n#         boxes_list.append(boxes)\n#         scores_list.append(confs)\n#         labels_list.append(label)\n    \n    \n#     boxes, scores, labels = ensemble_boxes_wbf.weighted_boxes_fusion(boxes_list, scores_list, labels_list, weights=weights, iou_thr=iou_thr)\n#     print(scores)\n    \n    \n\n#     print(labels_list)\n#     print(results[0])\n    sz = 1920\n    img = cv2.resize(img, (sz, \n                          sz))\n    results = model.predict(img,conf=0.01, device=0)\n    \n    r = ''\n    _classes = results[0].boxes.cls.cpu().numpy()\n    confs = results[0].boxes.conf[_classes==0].cpu()\n\n    _class = 0\n    masks = results[0].masks[_classes==0].data.cpu().numpy()\n    for index, conf in  enumerate(confs):\n        mask = masks[index]\n        mask = resize_mask(mask,target_size=(width, height))\n        threshold = 0.5\n        mask = (mask > threshold)\n        mask = binary_dilation(mask)\n        encoded_mask = encode_binary_mask(mask)\n        \n        if index == 0:\n            r += f\"{_class} {conf} {encoded_mask.decode('utf-8')}\"\n        else:\n            r += f\" {_class} {conf} {encoded_mask.decode('utf-8')}\"\n    \n    predict_string.append(r)\n\n    \nsub['id'] = idxes\nsub['height'] = heights\nsub['width'] = widths\nsub['prediction_string'] = predict_string","metadata":{"execution":{"iopub.status.busy":"2023-07-01T03:52:46.992152Z","iopub.execute_input":"2023-07-01T03:52:46.992537Z","iopub.status.idle":"2023-07-01T03:52:50.243277Z","shell.execute_reply.started":"2023-07-01T03:52:46.992490Z","shell.execute_reply":"2023-07-01T03:52:50.242351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-01T03:52:51.262589Z","iopub.execute_input":"2023-07-01T03:52:51.263227Z","iopub.status.idle":"2023-07-01T03:52:51.287554Z","shell.execute_reply.started":"2023-07-01T03:52:51.263194Z","shell.execute_reply":"2023-07-01T03:52:51.286397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# res_plotted = results[0].plot()","metadata":{"execution":{"iopub.status.busy":"2023-07-01T03:53:00.707460Z","iopub.execute_input":"2023-07-01T03:53:00.708169Z","iopub.status.idle":"2023-07-01T03:53:00.985602Z","shell.execute_reply.started":"2023-07-01T03:53:00.708136Z","shell.execute_reply":"2023-07-01T03:53:00.983370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(res_plotted)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T03:53:01.989761Z","iopub.execute_input":"2023-07-01T03:53:01.990140Z","iopub.status.idle":"2023-07-01T03:53:03.437270Z","shell.execute_reply.started":"2023-07-01T03:53:01.990109Z","shell.execute_reply":"2023-07-01T03:53:03.436247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}