{"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":"### YOLO v8 train & inference\n\nWe use the YOLO V8 model for this competition because it can execute the object detection and segmentation at the same time.  \nBecause of this notebook is online, we can't submit this directly.  ","metadata":{}},{"cell_type":"code","source":"!cp -r /kaggle/input/hydra-core /kaggle/working\n!mv /kaggle/working/hydra-core/antlr4-python3-runtime-4.9.3.tar.gz.tmp /kaggle/working/hydra-core/antlr4-python3-runtime-4.9.3.tar.gz\n!ls /kaggle/working/hydra-core","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:49:11.355954Z","iopub.execute_input":"2023-06-08T06:49:11.356460Z","iopub.status.idle":"2023-06-08T06:49:14.482901Z","shell.execute_reply.started":"2023-06-08T06:49:11.356425Z","shell.execute_reply":"2023-06-08T06:49:14.481629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/working/hydra-core/*","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:49:14.486519Z","iopub.execute_input":"2023-06-08T06:49:14.486888Z","iopub.status.idle":"2023-06-08T06:49:47.687188Z","shell.execute_reply.started":"2023-06-08T06:49:14.486857Z","shell.execute_reply":"2023-06-08T06:49:47.685981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.insert(0, '/kaggle/input/ultralytics-31may23/ultralytics-main')\nsys.path.insert(0, '/kaggle/input/python-opcounter/pytorch-OpCounter-master')","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:49:47.689779Z","iopub.execute_input":"2023-06-08T06:49:47.690168Z","iopub.status.idle":"2023-06-08T06:49:47.696146Z","shell.execute_reply.started":"2023-06-08T06:49:47.690130Z","shell.execute_reply":"2023-06-08T06:49:47.695183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:49:47.699167Z","iopub.execute_input":"2023-06-08T06:49:47.700158Z","iopub.status.idle":"2023-06-08T06:49:47.710152Z","shell.execute_reply.started":"2023-06-08T06:49:47.700116Z","shell.execute_reply":"2023-06-08T06:49:47.709062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Install hydra-core-1.2.0 package\n\n# !pip install /kaggle/input/hydracore120py3/hydra_core-1.2.0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:49:47.711597Z","iopub.execute_input":"2023-06-08T06:49:47.711987Z","iopub.status.idle":"2023-06-08T06:49:47.718734Z","shell.execute_reply.started":"2023-06-08T06:49:47.711957Z","shell.execute_reply":"2023-06-08T06:49:47.717699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Install ultralytic package\n# import os\n# !mkdir /kaggle/working/packages\n# !cp -r /kaggle/input/ultralytics/* /kaggle/working/packages\n# os.chdir(\"/kaggle/working/packages/ultralytics/\")\n# !python setup.py install\n# !pip install . --no-index --find-links /kaggle/working/packages/\n# os.chdir(\"/kaggle/working\")","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:49:47.720443Z","iopub.execute_input":"2023-06-08T06:49:47.720856Z","iopub.status.idle":"2023-06-08T06:49:47.728307Z","shell.execute_reply.started":"2023-06-08T06:49:47.720825Z","shell.execute_reply":"2023-06-08T06:49:47.727385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install pycocotools package\nimport 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-08T06:49:47.729987Z","iopub.execute_input":"2023-06-08T06:49:47.730584Z","iopub.status.idle":"2023-06-08T06:50:26.724374Z","shell.execute_reply.started":"2023-06-08T06:49:47.730550Z","shell.execute_reply":"2023-06-08T06:50:26.722684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport os\nimport pandas as pd\nimport numpy as np\nimport tifffile as tiff\nimport cv2\nimport matplotlib.pyplot as plt\n\nfrom pathlib import Path\nfrom glob import glob\nfrom collections import defaultdict\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\nfrom IPython.display import Image as show_image\n\nimport ultralytics\nfrom ultralytics import YOLO\n\nimport torch\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nultralytics.checks()","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2023-06-08T06:50:26.726763Z","iopub.execute_input":"2023-06-08T06:50:26.727152Z","iopub.status.idle":"2023-06-08T06:50:26.753783Z","shell.execute_reply.started":"2023-06-08T06:50:26.727105Z","shell.execute_reply":"2023-06-08T06:50:26.751711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set parameters","metadata":{}},{"cell_type":"markdown","source":"### Hyper parameters","metadata":{}},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n\nIMAGE_SIZE = 512\nBATCH_SIZE = 16\nEPOCHS = 10\n\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.755816Z","iopub.execute_input":"2023-06-08T06:50:26.756178Z","iopub.status.idle":"2023-06-08T06:50:26.764431Z","shell.execute_reply.started":"2023-06-08T06:50:26.756144Z","shell.execute_reply":"2023-06-08T06:50:26.763457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_path = '/kaggle/input/hubmap-yolo/yolo8s-v7.pt'","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.771497Z","iopub.execute_input":"2023-06-08T06:50:26.772152Z","iopub.status.idle":"2023-06-08T06:50:26.777286Z","shell.execute_reply.started":"2023-06-08T06:50:26.772116Z","shell.execute_reply":"2023-06-08T06:50:26.775764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Directories","metadata":{}},{"cell_type":"code","source":"# File path settings\nBASE_DIR = Path('/kaggle/input/hubmap-hacking-the-human-vasculature')\n# BASE_DIR = Path('')\n\ntest_paths = glob(f'{BASE_DIR}/test/*')","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.779548Z","iopub.execute_input":"2023-06-08T06:50:26.780337Z","iopub.status.idle":"2023-06-08T06:50:26.791218Z","shell.execute_reply.started":"2023-06-08T06:50:26.780306Z","shell.execute_reply":"2023-06-08T06:50:26.790207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get ready for reverting to actual instance segmentation","metadata":{}},{"cell_type":"code","source":"def revert_label_file(id_path):\n    \"\"\"\n    Convert label txt file to coordinates\n    parameters\n    ----------\n    id_path: str\n        path where label txt file is saved\n    \"\"\"\n    with open(id_path) as f:\n        lines = f.readlines()\n    \n    coordinates = []\n    confidences = []\n    for line in lines:\n        line = np.array(line.strip()[2:].split()).astype('float')\n        if len(line) % 2:\n            confidence = line[-1]\n            line = line[:-1]\n            confidences.append(confidence)\n        coordinate = (\n            line.reshape(-1,2)*IMAGE_SIZE\n        ).round().astype(int)\n        coordinates.append(coordinate)\n    return coordinates, confidences\n\n\ndef coordinate_to_instance(coordinate):\n    mask = np.zeros((IMAGE_SIZE, IMAGE_SIZE), dtype=np.float32)\n    cv2.fillPoly(mask, [coordinate.reshape(-1, 1, 2)], 1)\n    return mask","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.792983Z","iopub.execute_input":"2023-06-08T06:50:26.793751Z","iopub.status.idle":"2023-06-08T06:50:26.804410Z","shell.execute_reply.started":"2023-06-08T06:50:26.793719Z","shell.execute_reply":"2023-06-08T06:50:26.803485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## RLE ","metadata":{}},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\nfrom skimage.measure import regionprops_table, label, regionprops","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.807177Z","iopub.execute_input":"2023-06-08T06:50:26.807775Z","iopub.status.idle":"2023-06-08T06:50:26.822580Z","shell.execute_reply.started":"2023-06-08T06:50:26.807744Z","shell.execute_reply":"2023-06-08T06:50:26.819285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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    if mask.dtype != bool:\n        raise ValueError(\"encode_binary_mask expects a binary mask, received dtype == %s\" % mask.dtype)\n\n    mask = np.squeeze(mask)\n    if len(mask.shape) != 2:\n        raise ValueError(\"encode_binary_mask expects a 2d mask, received shape == %s\" % 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    \n    base64_str = base64.b64encode(binary_str)\n    return base64_str\n","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.823936Z","iopub.execute_input":"2023-06-08T06:50:26.825670Z","iopub.status.idle":"2023-06-08T06:50:26.841581Z","shell.execute_reply.started":"2023-06-08T06:50:26.825636Z","shell.execute_reply":"2023-06-08T06:50:26.840587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction","metadata":{}},{"cell_type":"code","source":"# ckpt = torch.load('runs/segment/train2/weights/best.pt', map_location='cpu')","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.844947Z","iopub.execute_input":"2023-06-08T06:50:26.845352Z","iopub.status.idle":"2023-06-08T06:50:26.857582Z","shell.execute_reply.started":"2023-06-08T06:50:26.845316Z","shell.execute_reply":"2023-06-08T06:50:26.856713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ckpt.keys()","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.859091Z","iopub.execute_input":"2023-06-08T06:50:26.859736Z","iopub.status.idle":"2023-06-08T06:50:26.865954Z","shell.execute_reply.started":"2023-06-08T06:50:26.859705Z","shell.execute_reply":"2023-06-08T06:50:26.865252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ckpt['train_args']","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.867396Z","iopub.execute_input":"2023-06-08T06:50:26.868068Z","iopub.status.idle":"2023-06-08T06:50:26.875547Z","shell.execute_reply.started":"2023-06-08T06:50:26.868038Z","shell.execute_reply":"2023-06-08T06:50:26.874867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_paths","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.876801Z","iopub.execute_input":"2023-06-08T06:50:26.877441Z","iopub.status.idle":"2023-06-08T06:50:26.887459Z","shell.execute_reply.started":"2023-06-08T06:50:26.877410Z","shell.execute_reply":"2023-06-08T06:50:26.886450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_path","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.888841Z","iopub.execute_input":"2023-06-08T06:50:26.889262Z","iopub.status.idle":"2023-06-08T06:50:26.898631Z","shell.execute_reply.started":"2023-06-08T06:50:26.889227Z","shell.execute_reply":"2023-06-08T06:50:26.897531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n# ## checking what happens during actual submission\n# trained_model = YOLO(model_path)\n# predictions = list(trained_model.predict(test_paths*64, device=0, iou=0.6, agnostic_nms=True)) #, save=True, conf=0.6, save_txt=True, save_conf=True))","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.900484Z","iopub.execute_input":"2023-06-08T06:50:26.900915Z","iopub.status.idle":"2023-06-08T06:50:26.907191Z","shell.execute_reply.started":"2023-06-08T06:50:26.900884Z","shell.execute_reply":"2023-06-08T06:50:26.906488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predictions[0].masks.data","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.908345Z","iopub.execute_input":"2023-06-08T06:50:26.908955Z","iopub.status.idle":"2023-06-08T06:50:26.916639Z","shell.execute_reply.started":"2023-06-08T06:50:26.908923Z","shell.execute_reply":"2023-06-08T06:50:26.915696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.919480Z","iopub.execute_input":"2023-06-08T06:50:26.920150Z","iopub.status.idle":"2023-06-08T06:50:26.925601Z","shell.execute_reply.started":"2023-06-08T06:50:26.920079Z","shell.execute_reply":"2023-06-08T06:50:26.924924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n\n# loader = DataLoader(test_paths*650, batch_size=64, shuffle=False)\n# all_predictions = []\n# ## checking what happens during actual submission\n# trained_model = YOLO(model_path)\n# cnt = 0\n# for test_spl in tqdm(loader):\n#     predictions = list(trained_model.predict(test_spl, device=0, iou=0.6, agnostic_nms=True)) #, save=True, conf=0.6, save_txt=True, save_conf=True))\n#     cnt += 1\n#     all_predictions.extend(predictions)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-06-08T06:50:26.926760Z","iopub.execute_input":"2023-06-08T06:50:26.927654Z","iopub.status.idle":"2023-06-08T06:50:26.934678Z","shell.execute_reply.started":"2023-06-08T06:50:26.927612Z","shell.execute_reply":"2023-06-08T06:50:26.933971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nloader = DataLoader(test_paths, batch_size=64, shuffle=False)\npredictions = []\n## checking what happens during actual submission\ntrained_model = YOLO(model_path)\ncnt = 0\nfor test_spl in tqdm(loader):\n    predictions_spl = list(trained_model.predict(test_spl, device=0, iou=0.6, agnostic_nms=True)) #, save=True, conf=0.6, save_txt=True, save_conf=True))\n    cnt += 1\n    predictions.extend(predictions_spl)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:26.935817Z","iopub.execute_input":"2023-06-08T06:50:26.936665Z","iopub.status.idle":"2023-06-08T06:50:27.234300Z","shell.execute_reply.started":"2023-06-08T06:50:26.936633Z","shell.execute_reply":"2023-06-08T06:50:27.233356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trained_model = YOLO(model_path)\n# predictions = list(trained_model.predict(test_paths)) #, save=True, conf=0.6, save_txt=True, save_conf=True))\n# # predictions = list(trained_model.predict(test_paths, save=True)) #, save=True, conf=0.6, save_txt=True, save_conf=True))","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.236000Z","iopub.execute_input":"2023-06-08T06:50:27.236713Z","iopub.status.idle":"2023-06-08T06:50:27.241132Z","shell.execute_reply.started":"2023-06-08T06:50:27.236679Z","shell.execute_reply":"2023-06-08T06:50:27.239934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bb_intersection_over_union(boxA, boxB):\n    # determine the (x, y)-coordinates of the intersection rectangle\n    xA = max(boxA[0], boxB[0])\n    yA = max(boxA[1], boxB[1])\n    xB = min(boxA[2], boxB[2])\n    yB = min(boxA[3], boxB[3])\n\n    # compute the area of intersection rectangle\n    interArea = abs(max((xB - xA, 0)) * max((yB - yA), 0))\n    if interArea == 0:\n        return 0\n    # compute the area of both the prediction and ground-truth\n    # rectangles\n    boxAArea = abs((boxA[2] - boxA[0]) * (boxA[3] - boxA[1]))\n    boxBArea = abs((boxB[2] - boxB[0]) * (boxB[3] - boxB[1]))\n\n    # compute the intersection over union by taking the intersection\n    # area and dividing it by the sum of prediction + ground-truth\n    # areas - the interesection area\n    iou = interArea / float(boxAArea + boxBArea - interArea)\n\n    # return the intersection over union value\n    return iou","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.242922Z","iopub.execute_input":"2023-06-08T06:50:27.243749Z","iopub.status.idle":"2023-06-08T06:50:27.252793Z","shell.execute_reply.started":"2023-06-08T06:50:27.243716Z","shell.execute_reply":"2023-06-08T06:50:27.251703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_instance(polygon, im_size):\n    # Create an empty mask of the same size as the image\n    mask = np.zeros(im_size, dtype=np.float32)\n    lines = np.array(polygon)\n    lines = lines.reshape(-1, 1, 2)\n    # Draw the polygon on the mask\n    cv2.fillPoly(mask, [lines], 1) #255)\n    return mask.astype('bool')\n\n\ndef process_pred(prediction, threshold=0.5):\n    confidences = prediction.boxes.conf.cpu()\n    seg_coordinates = prediction.masks.xy\n    \n    mask = 0\n    for i, (seg_coordinate, seg_confidence) in enumerate(zip(seg_coordinates, confidences)):\n        seg_instance = coordinate_to_instance(seg_coordinate.astype('int'))\n        binmask = seg_instance.astype('bool')\n        mask += binmask\n    \n    label_img = label(mask.astype('bool'))\n    tb = regionprops_table(label_img, properties=['bbox', 'coords'])\n    \n    tt = pd.DataFrame(tb)\n#     tt['x1'] = tb['bbox-1']\n#     tt['y1'] = tb['bbox-0']\n#     tt['x2'] = tb['bbox-3']\n#     tt['y2'] = tb['bbox-2']\n#     tt = tt.values\n    connected = tt[['bbox-1', 'bbox-0', 'bbox-3', 'bbox-2']].values\n    \n    bb = prediction.boxes.xyxy.cpu()\n    \n    tt['intersects'] = [[bb_intersection_over_union(tt_k, bb_k) for bb_k in bb] for tt_k in connected]\n    tt['confidences'] = [(confidences[np.nonzero(intersect)[0]]**0.5).mean()**2 for intersect in tt['intersects']]\n    tt['seg_instances'] = tt['coords'].apply(lambda x: get_instance(x[:, ::-1], mask.shape))\n    tt = tt[tt['confidences'] >= threshold]\n    return tt['seg_instances'], tt['confidences']","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.255401Z","iopub.execute_input":"2023-06-08T06:50:27.255725Z","iopub.status.idle":"2023-06-08T06:50:27.268246Z","shell.execute_reply.started":"2023-06-08T06:50:27.255701Z","shell.execute_reply":"2023-06-08T06:50:27.267109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# seg_instances, confidences = process_pred(prediction, 0.3)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.274845Z","iopub.execute_input":"2023-06-08T06:50:27.275107Z","iopub.status.idle":"2023-06-08T06:50:27.281100Z","shell.execute_reply.started":"2023-06-08T06:50:27.275084Z","shell.execute_reply":"2023-06-08T06:50:27.279911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for inst, conf in zip(seg_instances, confidences):\n#     plt.imshow(inst)\n#     plt.title(conf)\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.282410Z","iopub.execute_input":"2023-06-08T06:50:27.282808Z","iopub.status.idle":"2023-06-08T06:50:27.290228Z","shell.execute_reply.started":"2023-06-08T06:50:27.282776Z","shell.execute_reply":"2023-06-08T06:50:27.289264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_result(prediction, threshold=0.4):\n    confidences = prediction.boxes.conf.cpu()\n    seg_coordinates = prediction.masks.xy\n    \n    ## avoiding intersecting predictions\n    mask = 0\n    # conf_img = 0\n    confs = []\n    for i, (seg_coordinate, seg_confidence) in enumerate(zip(seg_coordinates, confidences)):\n        seg_instance = coordinate_to_instance(seg_coordinate.astype('int'))\n        binmask = seg_instance.astype('bool')\n        mask += binmask\n\n        conf_mask = seg_instance * seg_confidence.numpy()\n    #     conf_img += conf_mask\n        confs.append(conf_mask)\n\n    cc = np.stack(confs)\n    cc[cc == 0] = np.nan\n\n    cc = np.nanmax(cc, axis=0)\n    cc[np.isnan(cc)] = 0 \n    \n    ##\n    label_img = label(mask.astype('bool'))\n    tb = pd.DataFrame(regionprops_table(label_img, \n                                        intensity_image=cc, #conf_img, \n                                        properties=['bbox', 'image', 'image_intensity']))\n    tb['confidences'] = tb.image_intensity.apply(np.unique)\n\n\n    def ret_inst(row):\n        mm = np.zeros((512,512), dtype=np.float32)\n        mm[row['bbox-0']:row['bbox-2'], row['bbox-1']:row['bbox-3']] = row.image_intensity == row.confidences.max()\n        return mm.astype('bool')\n\n    tb['instance'] = tb.apply(ret_inst, axis=1)\n    tb['confidence'] = tb.confidences.map(np.max)\n\n    tb['coded'] = tb.instance.apply(lambda binmask: encode_binary_mask(binmask).decode('utf-8'))\n#     tb['pred_string'] = tb.apply(lambda row: f\"0 {row['confidence']} {row['coded']}\", axis=1)\n    tb['pred_string'] = tb.apply(lambda row: f\"0 1.0 {row['coded']}\", axis=1)\n    \n    tb_used = tb[tb.confidence > threshold]\n    \n    prediction_mask = np.sum(tb_used.instance.values).astype('bool')\n    prediction_string = ' '.join(tb_used['pred_string'].values)\n    return prediction_string, prediction_mask","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.292847Z","iopub.execute_input":"2023-06-08T06:50:27.293818Z","iopub.status.idle":"2023-06-08T06:50:27.307065Z","shell.execute_reply.started":"2023-06-08T06:50:27.293788Z","shell.execute_reply":"2023-06-08T06:50:27.306258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold = 0.3","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.308612Z","iopub.execute_input":"2023-06-08T06:50:27.309427Z","iopub.status.idle":"2023-06-08T06:50:27.319585Z","shell.execute_reply.started":"2023-06-08T06:50:27.309394Z","shell.execute_reply":"2023-06-08T06:50:27.318717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = [os.path.splitext(os.path.basename(test_path))[0] for test_path in test_paths]\n\nheights = []\nwidths = []\nprediction_strings = []\n\nfor k in range(len(ids)):\n    prediction = predictions[k]\n    h, w = prediction.orig_shape\n    pred_string, pred_mask = get_result(prediction, threshold)\n    plt.imshow(pred_mask)\n    plt.show()\n    heights.append(h)\n    widths.append(w)\n    \n    prediction_strings.append(pred_string)\n\nsubmission = pd.DataFrame()\nsubmission['id'] = ids\nsubmission['height'] = heights\nsubmission['width'] = widths\nsubmission['prediction_string'] = prediction_strings\nsubmission = submission.set_index('id')\nsubmission.to_csv(\"submission.csv\")\n\nprint(submission)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.321336Z","iopub.execute_input":"2023-06-08T06:50:27.321765Z","iopub.status.idle":"2023-06-08T06:50:27.729133Z","shell.execute_reply.started":"2023-06-08T06:50:27.321733Z","shell.execute_reply":"2023-06-08T06:50:27.727405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ids = [os.path.splitext(os.path.basename(test_path))[0] for test_path in test_paths]\n\n# heights = []\n# widths = []\n# prediction_strings = []\n\n# for k in range(len(ids)):\n#     prediction = predictions[k]\n    \n# #     confidences = prediction.boxes.conf.cpu()\n# #     seg_coordinates = prediction.masks.xy  #result.masks.segments\n# #     pred_string = \"\"\n# #     mask = 0\n# #     for i, (seg_coordinate, seg_confidence) in enumerate(zip(seg_coordinates, confidences)):\n# #         seg_instance = coordinate_to_instance(seg_coordinate.astype('int'))\n# #         binmask = seg_instance.astype('bool')\n# #         mask += binmask\n# #         plt.imshow(binmask)\n# #         plt.title(f\"conf == {seg_confidence**0.5}\")\n# #         plt.show()\n# #         encoded = encode_binary_mask(binmask)\n        \n# #         if i == 0:\n# #             pred_string += f\"0 {seg_confidence:0.4f} {encoded.decode('utf-8')}\"\n# #         else:\n# #             pred_string += f\" 0 {seg_confidence:0.4f} {encoded.decode('utf-8')}\"\n    \n#     seg_instances, confidences = process_pred(prediction, threshold)\n#     pred_string = \"\"\n#     mask = 0\n#     for i, (binmask, seg_confidence) in enumerate(zip(seg_instances, confidences)):\n#         mask += binmask\n#         plt.imshow(binmask)\n#         plt.title(f\"conf == {seg_confidence**0.5}\")\n#         plt.show()\n#         encoded = encode_binary_mask(binmask)\n        \n# #         if i == 0:\n# #             pred_string += f\"0 {seg_confidence**0.5:0.4f} {encoded.decode('utf-8')}\"\n# #         else:\n# #             pred_string += f\" 0 {seg_confidence**0.5:0.4f} {encoded.decode('utf-8')}\"\n            \n#         if i == 0:\n#             pred_string += f\"0 1.0 {encoded.decode('utf-8')}\"\n#         else:\n#             pred_string += f\" 0 1.0 {encoded.decode('utf-8')}\"\n            \n#     h, w = prediction.orig_shape\n    \n#     heights.append(h)\n#     widths.append(w)\n    \n#     prediction_strings.append(pred_string)\n\n# submission = pd.DataFrame()\n# submission['id'] = ids\n# submission['height'] = heights\n# submission['width'] = widths\n# submission['prediction_string'] = prediction_strings\n# submission = submission.set_index('id')\n# submission.to_csv(\"submission.csv\")\n\n# print(submission)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.731245Z","iopub.execute_input":"2023-06-08T06:50:27.731690Z","iopub.status.idle":"2023-06-08T06:50:27.739100Z","shell.execute_reply.started":"2023-06-08T06:50:27.731638Z","shell.execute_reply":"2023-06-08T06:50:27.737563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(mask)","metadata":{"execution":{"iopub.status.busy":"2023-06-08T06:50:27.740798Z","iopub.execute_input":"2023-06-08T06:50:27.741397Z","iopub.status.idle":"2023-06-08T06:50:27.752877Z","shell.execute_reply.started":"2023-06-08T06:50:27.741285Z","shell.execute_reply":"2023-06-08T06:50:27.751698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}