{"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 -qqq /kaggle/input/mmdet3-wheels-ando/addict-2.4.0-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmseg/mmengine-0.8.1-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmdet3-wheels-ando/mmcv-2.0.0-cp310-cp310-linux_x86_64.whl\n!pip install -qqq /kaggle/input/pycocotools-206/wheels/pycocotools-2.0.6-cp310-cp310-linux_x86_64.whl\n!pip install -qqq /kaggle/input/mmdet3-wheels-ando/terminaltables-3.1.10-py2.py3-none-any.whl\n!pip install -qqq /kaggle/input/mmdet3-wheels-ando/mmdet-3.0.0-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmseg/mmsegmentation-1.1.0-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmseg/ordered_set-4.1.0-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmseg/model_index-0.1.11-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmseg/modelindex-0.0.2-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmseg/rich-13.4.2-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmseg/mat4py-0.5.0-py2.py3-none-any.whl\n!pip install -qqq /kaggle/input/mmseg/einops-0.6.1-py3-none-any.whl\n!pip install -qqq /kaggle/input/mmseg/mmpretrain-1.0.0-py2.py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:17:57.264976Z","iopub.execute_input":"2023-07-28T01:17:57.265429Z","iopub.status.idle":"2023-07-28T01:25:22.480615Z","shell.execute_reply.started":"2023-07-28T01:17:57.265382Z","shell.execute_reply":"2023-07-28T01:25:22.479335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport pycocotools.mask as mask_util\nimport typing as t\nimport zlib\nfrom skimage.morphology import binary_dilation\nimport matplotlib.pyplot as plt\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 != 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    base64_str = base64.b64encode(binary_str)\n    return base64_str","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:25:22.484927Z","iopub.execute_input":"2023-07-28T01:25:22.485243Z","iopub.status.idle":"2023-07-28T01:25:23.197247Z","shell.execute_reply.started":"2023-07-28T01:25:22.485212Z","shell.execute_reply":"2023-07-28T01:25:23.196328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:25:23.199101Z","iopub.execute_input":"2023-07-28T01:25:23.199701Z","iopub.status.idle":"2023-07-28T01:25:23.205300Z","shell.execute_reply.started":"2023-07-28T01:25:23.199666Z","shell.execute_reply":"2023-07-28T01:25:23.204333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import mmdet, mmcv, mmengine\nfrom mmengine.config import Config\nfrom mmengine.runner import Runner\nfrom mmdet.utils import register_all_modules, get_test_pipeline_cfg\nfrom mmdet.apis import init_detector, inference_detector\nfrom mmdet.models import DetTTAModel\nfrom mmengine.visualization import Visualizer\nfrom mmcv.transforms import Compose\nfrom mmengine.structures import InstanceData\nimport mmseg\nfrom mmcv.ops import batched_nms\nfrom mmdet.structures.bbox import bbox_flip\n\nimport torch\nimport sys\nsys.path.append(\"/kaggle/input/weighted-boxes-fusion/Weighted-Boxes-Fusion\")\nfrom ensemble_boxes import *\nimport sys\nsys.path.insert(0, '/kaggle/input/ultralytics/ultralytics')\nimport ultralytics\nfrom ultralytics import YOLO\nimport cv2\n\n\nultralytics.checks()\nprint(mmdet.__version__)\nprint(mmcv.__version__)\nprint(mmengine.__version__)\nprint(mmseg.__version__)","metadata":{"id":"-V-93RXwT5UW","outputId":"74770888-ad8e-4148-cdd7-97ddd0aadc6f","execution":{"iopub.status.busy":"2023-07-28T01:25:23.207978Z","iopub.execute_input":"2023-07-28T01:25:23.208381Z","iopub.status.idle":"2023-07-28T01:25:41.197302Z","shell.execute_reply.started":"2023-07-28T01:25:23.208346Z","shell.execute_reply":"2023-07-28T01:25:41.196325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Inference Functions","metadata":{}},{"cell_type":"code","source":"def bbox_to_numpy(result, img_size = 512):\n    bboxes = [bbox.cpu().numpy()/ (img_size) for bbox in result.bboxes]\n    scores = [score.cpu().numpy() for score in result.scores]\n    labels = [label.cpu().numpy() for label in result.labels]\n    return result, bboxes, scores, labels\n\ndef run_wbf(results, img_size, weights, iou_thr, skip_box_thr):\n    dummy_result = InstanceData()\n    bboxes = [result[1] for result in results]\n    scores = [result[2] for result in results]\n    labels = [result[3] for result in results]\n    bboxes, scores, labels = weighted_boxes_fusion(bboxes, scores, labels, weights=weights, iou_thr=iou_thr, skip_box_thr=skip_box_thr)\n    bboxes = bboxes * (img_size)\n    dummy_result.bboxes = torch.from_numpy(bboxes).float().cuda()\n    dummy_result.scores = torch.from_numpy(scores).float().cuda()\n    dummy_result.labels = torch.from_numpy(labels).long().cuda()\n    return dummy_result\n\ndef get_mask_mean(results):\n    out = (results[0].masks / 255)\n    n = len(results)\n    for result in results[1:]:\n        out += (result.masks / 255)\n    return (out / n)\n\ndef mask_ensemble(results, thr=0.5):\n    dummy_result = InstanceData()\n    bboxes = results[0].bboxes\n    labels = results[0].labels\n    scores = results[0].scores\n    masks = get_mask_mean(results)\n    masks = (masks >= thr)\n    dummy_result.bboxes = bboxes\n    dummy_result.scores = scores\n    dummy_result.labels = labels\n    dummy_result.masks = masks\n    return dummy_result\n\ndef bbox_infer_single_model(cfg, model, img, test_pipeline, use_tta, img_size):\n    data = dict(img_path=img, img_id=0)\n    data = test_pipeline(data)\n    if not use_tta:\n        data['inputs'] = [data['inputs']]\n        data['data_samples'] = [data['data_samples']]\n    data = model.data_preprocessor(data, False)\n    batch_inputs, batch_data_samples = data['inputs'], data['data_samples']\n            \n    with torch.no_grad():\n        if use_tta:\n            x = model.extract_feat(batch_inputs)\n            if batch_data_samples[0].get('proposals', None) is None:\n                rpn_results_list = model.rpn_head.predict(\n                    x, batch_data_samples, rescale=False)\n            else:\n                rpn_results_list = [\n                    data_sample.proposals for data_sample in batch_data_samples\n                ]\n            batch_img_metas = [\n                data_samples.metainfo for data_samples in batch_data_samples\n            ]\n            bboxes_result_list = model.roi_head.predict_bbox(\n                x,\n                batch_img_metas,\n                rpn_results_list,\n                rcnn_test_cfg=model.roi_head.test_cfg,\n                rescale=False\n            )\n            aug_bboxes = [x.bboxes for x in bboxes_result_list]\n            aug_scores = [x.scores for x in bboxes_result_list]\n            aug_labels = [x.labels for x in bboxes_result_list]\n            \n            recovered_bboxes = []\n            for bboxes, img_info in zip(aug_bboxes, batch_img_metas):\n                flip = img_info['flip']\n                flip_direction = img_info['flip_direction']\n                if flip:\n                    bboxes = bbox_flip(\n                        bboxes=bboxes,\n                        img_shape=(img_size, img_size),\n                        direction=flip_direction)\n                recovered_bboxes.append(bboxes)\n                \n            merged_bboxes = torch.cat(recovered_bboxes, dim=0)\n            merged_scores = torch.cat(aug_scores, dim=0)\n            merged_labels = torch.cat(aug_labels, dim=0)\n            \n            if len(merged_bboxes) == 0:\n                return bboxes_result_list[0], [batch_img_metas[-1]], batch_inputs[-1:, ...]\n            \n            det_bboxes, keep_idxs = batched_nms(merged_bboxes, merged_scores,\n                                            merged_labels, cfg.tta_model.tta_cfg.nms)\n\n            det_bboxes = det_bboxes[:cfg.tta_model.tta_cfg.max_per_img]\n            det_labels = merged_labels[keep_idxs][:cfg.tta_model.tta_cfg.max_per_img]\n\n            results = InstanceData()\n            _det_bboxes = det_bboxes.clone()\n            results.bboxes = _det_bboxes[:, :-1]\n            results.scores = _det_bboxes[:, -1]\n            results.labels = det_labels\n            \n            return results, [batch_img_metas[-1]], batch_inputs[-1:, ...]\n            \n        else:\n            x = model.extract_feat(batch_inputs)\n            if batch_data_samples[0].get('proposals', None) is None:\n                rpn_results_list = model.rpn_head.predict(\n                    x, batch_data_samples, rescale=False)\n            else:\n                rpn_results_list = [\n                    data_sample.proposals for data_sample in batch_data_samples\n                ]\n            batch_img_metas = [\n                data_samples.metainfo for data_samples in batch_data_samples\n            ]\n            bboxes_result = model.roi_head.predict_bbox(\n                x,\n                batch_img_metas,\n                rpn_results_list,\n                rcnn_test_cfg=model.roi_head.test_cfg,\n                rescale=False\n            )[0]\n            return bboxes_result, batch_img_metas, batch_inputs\n        \ndef get_bboxes_results(img, cfgs, models, test_pipelines, use_tta, img_size):\n    bboxes_results = []\n    batch_inputss = []\n    batch_img_metass = []\n    for cfg, model, test_pipeline in zip(cfgs, models, test_pipelines):\n        # model = init_detector(cfg, checkpoint=ckpt, device=\"cuda:0\")\n        bboxes_result, batch_img_metas, batch_inputs = bbox_infer_single_model(\n            cfg, model, img, test_pipeline, use_tta, img_size)\n        batch_inputss.append(batch_inputs)\n        batch_img_metass.append(batch_img_metas)\n        if bboxes_result.labels.shape[0] != 0:\n            bboxes_results.append(bbox_to_numpy(bboxes_result, img_size))\n                \n    return bboxes_results, batch_inputss, batch_img_metass\n\ndef bbox_deepcopy_with_scale(b, scale):\n    dummy_result = InstanceData()\n    dummy_result.bboxes = b.bboxes * scale\n    dummy_result.scores = b.scores\n    dummy_result.labels = b.labels\n    return dummy_result\n\ndef get_masks_results(models, batch_inputss, batch_img_metass, bboxes_results, thr=0.5):\n    mask_results = []\n        \n    for model, batch_inputs, batch_img_metas in zip(models, batch_inputss, batch_img_metass):\n        # model = init_detector(cfg, checkpoint=ckpt, device=\"cuda:0\")\n        x = model.extract_feat(batch_inputs)\n        with torch.no_grad():\n            results_list = model.roi_head.predict_mask(\n                x, batch_img_metas, bboxes_results, rescale=True)[0]\n        mask_results.append(results_list)\n        del results_list\n        bboxes_results = [bbox_deepcopy_with_scale(bboxes_results[0], batch_img_metass[0][0][\"scale_factor\"][0])]\n        \n    del bboxes_results, batch_img_metass, batch_inputss\n        \n    result = mask_ensemble(mask_results, thr)\n    scores = result.scores.cpu().numpy()\n    masks = result.masks.cpu().numpy()\n    labels = result.labels.cpu().numpy()\n    return scores, masks, labels\n\ndef get_yolo_results(models, img, img_size):\n    bboxes_results = []\n    for i, model in enumerate(models):\n        results = model.predict(\n            img, \n            device=0,\n            conf=0.1,\n            half=False,\n            iou=0.6, \n            agnostic_nms=False,\n            boxes=False,\n            verbose=False,\n            augment=True,\n            nms=True\n        )\n        bboxes = (results[0].boxes.xyxy.cpu().numpy() / (img_size))\n        scores = results[0].boxes.conf.cpu().numpy()\n        labels = results[0].boxes.cls.cpu().numpy()\n        bboxes_results.append((0, bboxes, scores, labels))\n    return bboxes_results\n\ndef custom_inference(ins_models, yolo_models, imgs, use_tta, img_size, annotations, mask_thr=0.5):\n    cfgs = [model.cfg for model in ins_models]\n    test_pipelines = []\n    for cfg in cfgs:\n        cfg = cfg.copy()\n        if use_tta:\n            test_pipeline = cfg.tta_pipline\n        else:\n            test_pipeline = get_test_pipeline_cfg(cfg)\n        test_pipeline = Compose(test_pipeline)\n        test_pipelines.append(test_pipeline)\n    \n    ids = []\n    heights = []\n    widths = []\n    prediction_string = []\n    for i, img_path in enumerate(imgs):\n        img = cv2.imread(img_path)\n        img = cv2.resize(img, (img_size, img_size))\n        id_ = img_path.split(\"/\")[-1][:-4]\n        ids.append(id_)\n        heights.append(512)\n        widths.append(512)\n        \n        # get ensembled bboxes\n        bboxes_results, batch_inputss, batch_img_metass = get_bboxes_results(\n            img_path, cfgs, ins_models, test_pipelines, use_tta, img_size)\n        \n        if len(yolo_models) > 0:\n            yolo_bboxes_results = get_yolo_results(yolo_models, img, img_size)\n            bboxes_results.extend(yolo_bboxes_results)\n        bboxes_results = [run_wbf(bboxes_results, img_size, None, 0.55, 0.001)]\n        \n        # get ensembled masks\n        scores, masks, labels = get_masks_results(ins_models, batch_inputss, batch_img_metass, bboxes_results, mask_thr)\n        \n        g_masks = []\n        if (id_ in annotations) and len(scores) > 0:\n            anns = annotations[id_]\n            g_anns = list(filter(lambda x: x['type'] == 'glomerulus', anns))\n            for ann in g_anns:\n                coord = ann['coordinates']\n                mask = np.zeros((512, 512), dtype=np.uint8)\n                cv2.fillPoly(mask, [np.array(coord)], 1)\n                g_masks.append(mask)\n        \n        del bboxes_results\n        \n        pred_strings = []\n        for score, mask, label in zip(scores, masks, labels):\n            if label == 0:\n                if DILATE:\n                    mask = mask.astype(np.uint8)\n                    kernel = np.ones(shape=(4, 4), dtype=np.uint8)\n                    mask = cv2.dilate(mask, kernel, 4)\n                a = np.sum(mask)\n                x = True\n                if len(g_masks) > 0:\n                    for g_mask in g_masks:\n                        temp = np.sum(g_mask * mask)\n                        if temp/a > 0.5:\n                            x = False\n                            break\n                if x:\n                    mask = mask.astype(bool)\n                    pred_strings.append(\" \".join([\"0\", str(score), encode_binary_mask(mask).decode()]))\n        prediction_string.append(\" \".join(pred_strings))\n        del scores, masks, labels\n        \n    return prediction_string, ids, heights, widths","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:25:41.200821Z","iopub.execute_input":"2023-07-28T01:25:41.201105Z","iopub.status.idle":"2023-07-28T01:25:41.452631Z","shell.execute_reply.started":"2023-07-28T01:25:41.201079Z","shell.execute_reply":"2023-07-28T01:25:41.451581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CFG","metadata":{}},{"cell_type":"code","source":"EXPS = {\n#     \"actinometricsupernegligence\": [\n#         \"/kaggle/input/actinometricsupernegligence/fold1/best_coco_segm_mAP_epoch_10.pth\",\n#         \"/kaggle/input/actinometricsupernegligence/fold2/best_coco_segm_mAP_epoch_6.pth\",\n#         \"/kaggle/input/actinometricsupernegligence/fold3/best_coco_segm_mAP_epoch_10.pth\",\n#         \"/kaggle/input/actinometricsupernegligence/fold4/best_coco_segm_mAP_epoch_10.pth\",\n#     ],\n#     \"nonpreservationintensifies\": [\n#         \"/kaggle/input/nonpreservationintensifies/fold1/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/nonpreservationintensifies/fold2/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/nonpreservationintensifies/fold3/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/nonpreservationintensifies/fold4/best_coco_segm_mAP_epoch_10.pth\",\n#     ],\n#     \"charvetpanos\":[\n#         \"/kaggle/input/charvetpanos/fold1/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/charvetpanos/fold2/best_coco_segm_mAP_epoch_5.pth\",\n#         \"/kaggle/input/charvetpanos/fold3/best_coco_segm_mAP_epoch_10.pth\",\n#         \"/kaggle/input/charvetpanos/fold4/best_coco_segm_mAP_epoch_7.pth\",\n#     ],\n#     \"aliseptalsaccharometer\":[\n#         \"/kaggle/input/aliseptalsaccharometer/fold1/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/aliseptalsaccharometer/fold2/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/aliseptalsaccharometer/fold3/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/aliseptalsaccharometer/fold4/best_coco_segm_mAP_epoch_10.pth\",\n#     ]\n#     \"killikinicincreasingly\":[\n#         \"/kaggle/input/killikinicincreasingly/fold1/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/killikinicincreasingly/fold2/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/killikinicincreasingly/fold3/best_coco_segm_mAP_epoch_10.pth\",\n#         \"/kaggle/input/killikinicincreasingly/fold4/best_coco_segm_mAP_epoch_8.pth\",\n#     ],\n#     (\"conimpressibleness\", \"ins\"):[\n#         \"/kaggle/input/conimpressibleness/fold1/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/conimpressibleness/fold2/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/conimpressibleness/fold3/best_coco_segm_mAP_epoch_10.pth\",\n#         \"/kaggle/input/conimpressibleness/fold4/best_coco_segm_mAP_epoch_3.pth\",\n#     ],\n#     (\"knickknackishrushlit\", \"ins\"):[\n#         \"/kaggle/input/knickknackishrushlit/fold1/best_coco_segm_mAP_epoch_7.pth\",\n#         \"/kaggle/input/knickknackishrushlit/fold2/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/knickknackishrushlit/fold3/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/knickknackishrushlit/fold4/best_coco_segm_mAP_epoch_9.pth\",\n#     ],\n    (\"neurostheniagurges\", \"ins\"):[\n        \"/kaggle/input/neurostheniagurges/fold1/best_coco_segm_mAP_epoch_7.pth\",\n        \"/kaggle/input/neurostheniagurges/fold2/best_coco_segm_mAP_epoch_8.pth\",\n        \"/kaggle/input/neurostheniagurges/fold3/best_coco_segm_mAP_epoch_9.pth\",\n        \"/kaggle/input/neurostheniagurges/fold4/best_coco_segm_mAP_epoch_3.pth\",\n    ],\n#     (\"nonamorouspedogenic\", \"ins\"):[\n#         \"/kaggle/input/nonamorouspedogenic/fold1/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/nonamorouspedogenic/fold2/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/nonamorouspedogenic/fold3/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/nonamorouspedogenic/fold4/best_coco_segm_mAP_epoch_3.pth\",\n#     ],\n#     (\"gebangacockneyland\", \"ins\"):[\n#         \"/kaggle/input/gebangacockneyland/fold1/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/gebangacockneyland/fold2/best_coco_segm_mAP_epoch_6.pth\",\n#         \"/kaggle/input/gebangacockneyland/fold3/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/gebangacockneyland/fold4/best_coco_segm_mAP_epoch_8.pth\",\n#     ],\n#     (\"yolo-test\", \"yolo\"): [\n#         \"/kaggle/input/yolo-test/fold1/weights/best.pt\",\n#         \"/kaggle/input/yolo-test/fold2/weights/best.pt\",\n#         \"/kaggle/input/yolo-test/fold3/weights/best.pt\",\n#         \"/kaggle/input/yolo-test/fold4/weights/best.pt\"\n#     ],\n    (\"muskratsroosters\", \"ins\"):[\n        \"/kaggle/input/muskratsroosters/fold1/best_coco_segm_mAP_epoch_8.pth\",\n        \"/kaggle/input/muskratsroosters/fold2/best_coco_segm_mAP_epoch_8.pth\",\n        \"/kaggle/input/muskratsroosters/fold3/best_coco_segm_mAP_epoch_4.pth\",\n        \"/kaggle/input/muskratsroosters/fold4/best_coco_segm_mAP_epoch_2.pth\",\n    ],\n#     (\"opalescencelebhaft\", \"ins\"):[\n#         \"/kaggle/input/opalescencelebhaft/fold1/best_coco_segm_mAP_epoch_10.pth\",\n#         \"/kaggle/input/opalescencelebhaft/fold2/best_coco_segm_mAP_epoch_11.pth\",\n#         \"/kaggle/input/opalescencelebhaft/fold3/best_coco_segm_mAP_epoch_13.pth\",\n#         \"/kaggle/input/opalescencelebhaft/fold4/best_coco_segm_mAP_epoch_3.pth\",\n#     ],\n#     (\"ramusibustling\", \"ins\"):[\n#         \"/kaggle/input/ramusibustling/fold1/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/ramusibustling/fold2/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/ramusibustling/fold3/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/ramusibustling/fold4/best_coco_segm_mAP_epoch_3.pth\",\n#     ],\n#     (\"na\", \"yolo\"): [\n#         \"/kaggle/input/hubmap-yolo/yolov8_exp_1024/yolov8_exp_1024/fold1/weights/best.pt\",\n#         \"/kaggle/input/hubmap-yolo/yolov8_exp_1024/yolov8_exp_1024/fold2/weights/best.pt\",\n#         \"/kaggle/input/hubmap-yolo/yolov8_exp_1024/yolov8_exp_1024/fold3/weights/best.pt\",\n#         \"/kaggle/input/hubmap-yolo/yolov8_exp_1024/yolov8_exp_1024/fold4/weights/best.pt\",\n#     ],\n#     (\"sensillumhummaul\", \"yolo\"):[\n#         \"/kaggle/input/sensillumhummaul/fold1/weights/best.pt\",\n#         \"/kaggle/input/sensillumhummaul/fold2/weights/best.pt\",\n#         \"/kaggle/input/sensillumhummaul/fold3/weights/best.pt\",\n#         \"/kaggle/input/sensillumhummaul/fold4/weights/best.pt\",\n#     ],\n#     (\"venenousnessbilo\", \"yolo\"):[\n#         \"/kaggle/input/venenousnessbilo/fold1/weights/best.pt\",\n#         \"/kaggle/input/venenousnessbilo/fold2/weights/best.pt\",\n#         \"/kaggle/input/venenousnessbilo/fold3/weights/best.pt\",\n#         \"/kaggle/input/venenousnessbilo/fold4/weights/best.pt\",\n#     ],\n#     (\"nontypicalsonority\", \"yolo\"):[\n#         \"/kaggle/input/nontypicalsonority/fold1/weights/best.pt\",\n#         \"/kaggle/input/nontypicalsonority/fold2/weights/best.pt\",\n#         \"/kaggle/input/nontypicalsonority/fold3/weights/best.pt\",\n#         \"/kaggle/input/nontypicalsonority/fold4/weights/best.pt\",\n#     ],\n#     (\"winerscantholysis\", \"yolo\"):[\n#         \"/kaggle/input/winerscantholysis/fold1/weights/best.pt\",\n#         \"/kaggle/input/winerscantholysis/fold2/weights/best.pt\",\n#         \"/kaggle/input/winerscantholysis/fold3/weights/best.pt\",\n#         \"/kaggle/input/winerscantholysis/fold4/weights/best.pt\",\n#     ],\n#     (\"auriflammeshakeups\", \"yolo\"):[\n#         \"/kaggle/input/auriflammeshakeups/fold1/weights/best.pt\",\n#         \"/kaggle/input/auriflammeshakeups/fold2/weights/best.pt\",\n#         \"/kaggle/input/auriflammeshakeups/fold3/weights/best.pt\",\n#         \"/kaggle/input/auriflammeshakeups/fold4/weights/best.pt\",\n#     ],\n#     (\"badaxeiconophilism\", \"ins\"):[\n#         \"/kaggle/input/badaxeiconophilism/fold1/best_coco_segm_mAP_epoch_7.pth\",\n#     ],\n#     (\"mesioversionyouve\", \"ins\"):[\n#         \"/kaggle/input/mesioversionyouve/fold1/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/mesioversionyouve/fold2/best_coco_segm_mAP_epoch_7.pth\",\n#         \"/kaggle/input/mesioversionyouve/fold3/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/mesioversionyouve/fold4/best_coco_segm_mAP_epoch_3.pth\",\n#     ],\n#     (\"brabagiousmegalops\", \"ins\"):[\n#         \"/kaggle/input/brabagiousmegalops/fold1/best_coco_segm_mAP_epoch_10.pth\",\n#         \"/kaggle/input/brabagiousmegalops/fold2/best_coco_segm_mAP_epoch_5.pth\",\n#         \"/kaggle/input/brabagiousmegalops/fold3/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/brabagiousmegalops/fold4/best_coco_segm_mAP_epoch_2.pth\",\n#     ],\n#     (\"torpedoingfingram\", \"ins\"):[\n#         \"/kaggle/input/torpedoingfingram/fold1/best_coco_segm_mAP_epoch_6.pth\",\n#         \"/kaggle/input/torpedoingfingram/fold2/best_coco_segm_mAP_epoch_4.pth\",\n#         \"/kaggle/input/torpedoingfingram/fold3/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/torpedoingfingram/fold4/best_coco_segm_mAP_epoch_2.pth\",\n#     ],\n#     (\"shebeansreadds\", \"ins\"):[\n#         \"/kaggle/input/shebeansreadds/fold1/best_coco_segm_mAP_epoch_7.pth\",\n#         \"/kaggle/input/shebeansreadds/fold2/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/shebeansreadds/fold3/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/shebeansreadds/fold4/best_coco_segm_mAP_epoch_3.pth\",\n#     ],\n#     (\"autopolyploidyunhalved\", \"ins\"):[\n#         \"/kaggle/input/autopolyploidyunhalved/fold1/best_coco_segm_mAP_epoch_9.pth\",\n#         \"/kaggle/input/autopolyploidyunhalved/fold2/best_coco_segm_mAP_epoch_4.pth\",\n#         \"/kaggle/input/autopolyploidyunhalved/fold3/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/autopolyploidyunhalved/fold4/best_coco_segm_mAP_epoch_3.pth\",\n#     ],\n    (\"amperydecorticator\", \"ins\"):[\n        \"/kaggle/input/amperydecorticator/fold1/best_coco_segm_mAP_epoch_10.pth\",\n        \"/kaggle/input/amperydecorticator/fold2/best_coco_segm_mAP_epoch_4.pth\",\n        \"/kaggle/input/amperydecorticator/fold3/best_coco_segm_mAP_epoch_2.pth\",\n        \"/kaggle/input/amperydecorticator/fold4/best_coco_segm_mAP_epoch_3.pth\",\n    ],\n    (\"reginaetimbang\", \"ins\"):[\n        \"/kaggle/input/reginaetimbang/fold1/best_coco_bbox_mAP_epoch_7.pth\",\n        \"/kaggle/input/reginaetimbang/fold2/best_coco_bbox_mAP_epoch_7.pth\",\n        \"/kaggle/input/reginaetimbang/fold3/best_coco_bbox_mAP_epoch_2.pth\",\n        \"/kaggle/input/reginaetimbang/fold4/best_coco_bbox_mAP_epoch_6.pth\",\n    ],\n    (\"floscularpustulelike\", \"ins\"):[\n        \"/kaggle/input/floscularpustulelike/fold1/best_coco_segm_mAP_epoch_8.pth\",\n        \"/kaggle/input/floscularpustulelike/fold2/best_coco_segm_mAP_epoch_9.pth\",\n        \"/kaggle/input/floscularpustulelike/fold3/best_coco_segm_mAP_epoch_5.pth\",\n        \"/kaggle/input/floscularpustulelike/fold4/best_coco_segm_mAP_epoch_3.pth\",\n    ],\n#     (\"convocationalstalactiform\", \"ins\"):[\n#         \"/kaggle/input/convocationalstalactiform/fold1/best_coco_segm_mAP_epoch_7.pth\",\n#         \"/kaggle/input/convocationalstalactiform/fold2/best_coco_segm_mAP_epoch_8.pth\",\n#         \"/kaggle/input/convocationalstalactiform/fold3/best_coco_segm_mAP_epoch_4.pth\",\n#         \"/kaggle/input/convocationalstalactiform/fold4/best_coco_segm_mAP_epoch_2.pth\",\n#     ],\n    (\"catalinetacomplexive\", \"yolo\"):[\n        \"/kaggle/input/catalinetacomplexive/fold1/weights/best.pt\",\n        \"/kaggle/input/catalinetacomplexive/fold2/weights/best.pt\",\n        \"/kaggle/input/catalinetacomplexive/fold3/weights/best.pt\",\n        \"/kaggle/input/catalinetacomplexive/fold4/weights/best.pt\",\n    ],\n    (\"nontypicalsonority\", \"yolo\"):[\n        \"/kaggle/input/megalosyndactylytalesman/fold1/weights/best.pt\",\n        \"/kaggle/input/megalosyndactylytalesman/fold2/weights/best.pt\",\n        \"/kaggle/input/megalosyndactylytalesman/fold3/weights/best.pt\",\n        \"/kaggle/input/megalosyndactylytalesman/fold4/weights/best.pt\",\n    ],\n    (\"carpogenicrefoment\", \"ins\"):[\n        \"/kaggle/input/mm-hubmap3/carpogenicrefoment/carpogenicrefoment/fold1/best_coco_segm_mAP_epoch_12.pth\",\n        \"/kaggle/input/mm-hubmap3/carpogenicrefoment/carpogenicrefoment/fold2/best_coco_segm_mAP_epoch_19.pth\",\n        \"/kaggle/input/mm-hubmap3/carpogenicrefoment/carpogenicrefoment/fold3/best_coco_segm_mAP_epoch_18.pth\",\n        \"/kaggle/input/mm-hubmap3/carpogenicrefoment/carpogenicrefoment/fold4/best_coco_segm_mAP_epoch_10.pth\",\n    ],\n}","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:25:41.454539Z","iopub.execute_input":"2023-07-28T01:25:41.454892Z","iopub.status.idle":"2023-07-28T01:25:41.480806Z","shell.execute_reply.started":"2023-07-28T01:25:41.454861Z","shell.execute_reply":"2023-07-28T01:25:41.479764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TTA = True\nIMAGE_SIZE = 768\nTTA_CONFIG = {\n    \"tta_model\": dict(\n        type='DetTTAModel',\n        tta_cfg=dict(\n            nms=dict(\n                type=\"nms\",\n                iou_threshold=0.5,\n            ),\n            max_per_img=100\n        )\n    ),\n    \"tta_pipeline\":[\n        dict(\n            type='LoadImageFromFile',\n            backend_args=None\n        ),\n        dict(\n            type='TestTimeAug',\n            transforms=[\n                [\n                    dict(\n                        type='Resize',\n                        scale=(IMAGE_SIZE, IMAGE_SIZE),\n                        keep_ratio=True\n                    ),\n                ],\n                [\n                    dict(\n                        type='RandomFlip', \n                        direction=\"horizontal\",\n                        prob=1.0\n                    ),\n                    dict(\n                        type='RandomFlip', \n                        direction=\"vertical\",\n                        prob=1.0\n                    ),\n                    dict(\n                        type='RandomFlip',\n                        prob=0.0\n                    )\n                ],\n                [\n                    dict(\n                        type='PackDetInputs',\n                        meta_keys=(\n                            'img_id', \n                            'img_path', \n                            'ori_shape',\n                            'img_shape', \n                            'flip',\n                            'scale_factor',\n                            'flip_direction'\n                        )\n                    )\n                ]\n            ]\n        )\n    ]\n}\npath_list = [\"/kaggle/input/hubmap-hacking-the-human-vasculature/test/\"+i for i in os.listdir(\"/kaggle/input/hubmap-hacking-the-human-vasculature/test/\")]\nDILATE = True\nMASK_THR = 0.5\n\nprint(len(path_list))","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:25:41.482446Z","iopub.execute_input":"2023-07-28T01:25:41.482787Z","iopub.status.idle":"2023-07-28T01:25:41.501407Z","shell.execute_reply.started":"2023-07-28T01:25:41.482757Z","shell.execute_reply":"2023-07-28T01:25:41.500459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Main","metadata":{}},{"cell_type":"code","source":"import json\n\njsonl_file_path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\"\ndata = []\nwith open(jsonl_file_path, \"r\") as file:\n    for line in file:\n        data.append(json.loads(line))\n    \ndata = {d[\"id\"]: d[\"annotations\"] for d in data}","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:25:41.502650Z","iopub.execute_input":"2023-07-28T01:25:41.503052Z","iopub.status.idle":"2023-07-28T01:25:45.866201Z","shell.execute_reply.started":"2023-07-28T01:25:41.503016Z","shell.execute_reply":"2023-07-28T01:25:45.865239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ins_models = []\nyolo_models = []\nfor (exp, m_type), ckpts in EXPS.items():\n    if m_type == \"ins\":\n        for fold, ckpt in enumerate(ckpts):\n            if exp == 'cornutetriddler' or exp == 'ambidextroussolipsist' or exp == 'carpogenicrefoment':\n                cfg = Config.fromfile(f\"/kaggle/input/mm-hubmap3/{exp}/{exp}/fold{fold + 1}/config_train_ins.py\")\n                cfg.load_from = \"\"\n                cfg.work_dir = \"/kaggle/working/work_dir_test\"\n                cfg.model.test_cfg.rcnn.mask_thr_binary = -1\n            else:\n                cfg = Config.fromfile(f\"/kaggle/input/{exp}/fold{fold + 1}/config_train_ins.py\")\n                cfg.load_from = \"\"\n                cfg.work_dir = \"/kaggle/working/work_dir_test\"\n                cfg.model.test_cfg.rcnn.mask_thr_binary = -1\n            if TTA:\n                cfg.tta_model = TTA_CONFIG[\"tta_model\"]\n                cfg.tta_pipline = TTA_CONFIG[\"tta_pipeline\"]\n                \n            #ins_models.append([cfg, ckpt])\n            model = init_detector(cfg, checkpoint=ckpt, device=f\"cuda:0\")\n            ins_models.append(model)\n    elif m_type == \"yolo\":\n        for fold, ckpt in enumerate(ckpts):\n            model = YOLO(ckpt)\n            yolo_models.append(model)\n\nprediction_string, ids, heights, widths = custom_inference(ins_models, yolo_models, imgs=path_list, use_tta=TTA, img_size=IMAGE_SIZE, annotations=data, mask_thr=MASK_THR)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:25:45.867790Z","iopub.execute_input":"2023-07-28T01:25:45.868154Z","iopub.status.idle":"2023-07-28T01:29:05.944185Z","shell.execute_reply.started":"2023-07-28T01:25:45.868119Z","shell.execute_reply":"2023-07-28T01:29:05.943069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame({\"id\": ids, \"height\": heights, \"width\": widths, \"prediction_string\": prediction_string})\nsub = sub.set_index(\"id\")\nsub.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:29:05.947627Z","iopub.execute_input":"2023-07-28T01:29:05.948003Z","iopub.status.idle":"2023-07-28T01:29:05.969284Z","shell.execute_reply.started":"2023-07-28T01:29:05.947967Z","shell.execute_reply":"2023-07-28T01:29:05.968378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2023-07-28T01:29:05.972625Z","iopub.execute_input":"2023-07-28T01:29:05.972899Z","iopub.status.idle":"2023-07-28T01:29:05.993308Z","shell.execute_reply.started":"2023-07-28T01:29:05.972876Z","shell.execute_reply":"2023-07-28T01:29:05.992305Z"},"trusted":true},"execution_count":null,"outputs":[]}]}