{"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":"%env CUDA_HOME=/usr/local/cuda-11.8/","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-07-12T06:26:59.746097Z","iopub.execute_input":"2023-07-12T06:26:59.746527Z","iopub.status.idle":"2023-07-12T06:26:59.759533Z","shell.execute_reply.started":"2023-07-12T06:26:59.746488Z","shell.execute_reply":"2023-07-12T06:26:59.758613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/maskdino-sourcecode /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-07-12T06:26:59.761568Z","iopub.execute_input":"2023-07-12T06:26:59.762232Z","iopub.status.idle":"2023-07-12T06:27:02.03147Z","shell.execute_reply.started":"2023-07-12T06:26:59.762199Z","shell.execute_reply":"2023-07-12T06:27:02.030124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/maskdino-sourcecode/MaskDINO/maskdino/modeling/pixel_decoder/ops","metadata":{"execution":{"iopub.status.busy":"2023-07-12T06:27:02.033448Z","iopub.execute_input":"2023-07-12T06:27:02.033859Z","iopub.status.idle":"2023-07-12T06:27:02.043174Z","shell.execute_reply.started":"2023-07-12T06:27:02.033821Z","shell.execute_reply":"2023-07-12T06:27:02.041525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!sh make.sh","metadata":{"execution":{"iopub.status.busy":"2023-07-12T06:27:02.046809Z","iopub.execute_input":"2023-07-12T06:27:02.047469Z","iopub.status.idle":"2023-07-12T06:27:56.22795Z","shell.execute_reply.started":"2023-07-12T06:27:02.047434Z","shell.execute_reply":"2023-07-12T06:27:56.226769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/maskdino-sourcecode/MaskDINO/","metadata":{"execution":{"iopub.status.busy":"2023-07-12T06:27:56.229757Z","iopub.execute_input":"2023-07-12T06:27:56.230148Z","iopub.status.idle":"2023-07-12T06:27:56.240137Z","shell.execute_reply.started":"2023-07-12T06:27:56.23011Z","shell.execute_reply":"2023-07-12T06:27:56.238967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install /kaggle/input/detectron2-wheel-cpu/detectron2/detectron2-0.6-cp310-cp310-linux_x86_64.whl --no-index --find-links=/kaggle/input/detectron2-wheel-cpu/detectron2","metadata":{"execution":{"iopub.status.busy":"2023-07-12T06:27:56.24186Z","iopub.execute_input":"2023-07-12T06:27:56.242735Z","iopub.status.idle":"2023-07-12T06:27:56.253006Z","shell.execute_reply.started":"2023-07-12T06:27:56.242626Z","shell.execute_reply":"2023-07-12T06:27:56.251963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/detectron2-wheel/detectron2/detectron2-0.6-cp310-cp310-linux_x86_64.whl --no-index --find-links=/kaggle/input/detectron2-wheel/detectron2","metadata":{"execution":{"iopub.status.busy":"2023-07-12T06:27:56.255211Z","iopub.execute_input":"2023-07-12T06:27:56.256451Z","iopub.status.idle":"2023-07-12T06:28:11.363366Z","shell.execute_reply.started":"2023-07-12T06:27:56.256407Z","shell.execute_reply":"2023-07-12T06:28:11.362145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/ensemble-boxes /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-07-12T06:28:11.36568Z","iopub.execute_input":"2023-07-12T06:28:11.366092Z","iopub.status.idle":"2023-07-12T06:28:12.490735Z","shell.execute_reply.started":"2023-07-12T06:28:11.366052Z","shell.execute_reply":"2023-07-12T06:28:12.489436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -e /kaggle/working/ensemble-boxes","metadata":{"execution":{"iopub.status.busy":"2023-07-12T06:28:12.492562Z","iopub.execute_input":"2023-07-12T06:28:12.493178Z","iopub.status.idle":"2023-07-12T06:28:27.268032Z","shell.execute_reply.started":"2023-07-12T06:28:12.493137Z","shell.execute_reply":"2023-07-12T06:28:27.26693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat /opt/conda/lib/python3.10/site-packages/ensemble-boxes.egg-link","metadata":{"execution":{"iopub.status.busy":"2023-07-12T06:28:27.271717Z","iopub.execute_input":"2023-07-12T06:28:27.272201Z","iopub.status.idle":"2023-07-12T06:28:28.28415Z","shell.execute_reply.started":"2023-07-12T06:28:27.272164Z","shell.execute_reply":"2023-07-12T06:28:28.282905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%writefile submit.py\nimport sys\nsys.path.append('/opt/conda/lib/python3.10/site-packages/MultiScaleDeformableAttention-1.0-py3.10-linux-x86_64.egg')\nsys.path.append('/kaggle/working/ensemble-boxes')\nfrom detectron2.config import get_cfg\nfrom detectron2.engine.defaults import DefaultPredictor\nfrom detectron2.projects.deeplab import add_deeplab_config\nfrom maskdino import add_maskdino_config\nfrom ensemble_boxes import *\nimport os\n\ndef setup_cfg(config_file):\n    # load config from file and command-line arguments\n    cfg = get_cfg()\n    add_deeplab_config(cfg)\n    add_maskdino_config(cfg)\n    cfg.merge_from_file(config_file)\n#     cfg.freeze()\n    return cfg\n\nconfig_file = \"/kaggle/input/maskdino-swin-data1/config.yaml\"\nmdl_path = \"/kaggle/input/maskdinoweights\"\nDATA_PATH = \"/kaggle/input/hubmap-hacking-the-human-vasculature\"\nMODELS = []\nBEST_MODELS =[]\nSUBM_PATH = f'{DATA_PATH}/test'\n\nbest_model = [\n        \"/kaggle/input/maskdino-swin-data1/model_0008469.pth\"\n             ]\n\nfor b_m in best_model:\n    model_name=b_m\n    BEST_MODELS.append(model_name)\n#     cfg = get_cfg()\n    cfg = setup_cfg(config_file)\n    cfg.MODEL.WEIGHTS = model_name\n    cfg.INPUT.IMAGE_SIZE = 1408\n#     print(cfg)\n    MODELS.append(DefaultPredictor(cfg))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T07:35:32.754123Z","iopub.execute_input":"2023-07-12T07:35:32.754504Z","iopub.status.idle":"2023-07-12T07:35:36.668696Z","shell.execute_reply.started":"2023-07-12T07:35:32.754472Z","shell.execute_reply":"2023-07-12T07:35:36.667694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt\n\nimport base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\nimport time\nfrom detectron2.data import detection_utils as utils\n\nNMS = True\nIOU_TH = .6\n\nTHSS = [[.7, .01], [.7, .01], [.7, .01], [.7, .01], [.7, .01]]\n# MIN_PIXELS = [60, 60]\n\ndef nms_predictions(classes, scores, bboxes, masks, \n                    iou_th=.5, shape=(1408, 1408)):\n    he, wd = shape[0], shape[1]\n    boxes_list = [[[x[0] / wd, x[1] / he, x[2] / wd, x[3] / he] for x in bboxes]]\n    scores_list = [[x for x in scores]]\n    classes_list = [[x for x in classes]]\n    nms_bboxes, nms_scores, nms_classes = non_maximum_weighted(\n        boxes_list, \n        scores_list, \n        classes_list, \n        weights=None,\n        iou_thr=IOU_TH,\n        skip_box_thr=0.0001,\n    )\n    nms_masks = []\n    for s in nms_scores:\n        nms_masks.append(masks[scores.index(s)])\n    nms_scores, nms_classes, nms_masks = zip(*sorted(zip(nms_scores, nms_classes, nms_masks), reverse=True))\n    return nms_classes, nms_scores, nms_masks\n\ndef ensemble_preds(file_name, path, models, ths):\n    img = utils.read_image(f'{path}/{file_name}', format=\"BGR\")\n    \n    outputs = []\n    \n    classes = []\n    scores = []\n    bboxes = []\n    masks = []\n    \n    for i, model in enumerate(models):\n        img_resize = cv2.resize(img, (1408, 1408))\n#         img_resize = img\n        output = model(img_resize)\n#         for j in len(output['instances'].pred_classes.cpu().numpy().tolist()):\n            \n#         print(output)\n#         outputs.append(output)\n        \n        pred_classes = output['instances'].pred_classes.cpu().numpy().tolist()\n        pred_scores = output['instances'].scores.cpu().numpy().tolist()\n#         print(pred_scores)\n        pred_boxes = output['instances'].pred_boxes.tensor.cpu().numpy().tolist()\n        pred_masks = output['instances'].pred_masks.cpu().numpy()\n        for j in range(len(pred_classes)):\n            pred_class = 1\n            score = pred_scores[j]\n            if score < ths[i][pred_class]:\n                continue\n            \n            mask = np.squeeze(cv2.resize(np.expand_dims(pred_masks[j], 2),dsize=(512,512)))\n            classes.append(pred_class)\n            scores.append(score)\n            bboxes.append(pred_boxes[j])\n            masks.append(mask)\n        \n    return classes, scores, bboxes, masks\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\n\ntest_path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test\"\nsample_submission = pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv')\nids = []\nheights = []\nwidths = []\nprediction_strings = []\ntest_names = os.listdir(SUBM_PATH)\nfor test_name in test_names:\n    h, w = 512, 512\n    t1 = time.time()\n    classes, scores, bboxes, masks = ensemble_preds(\n        file_name=test_name, \n        path=SUBM_PATH, \n        models=MODELS, \n        ths=THSS\n    )\n    t2 = time.time()\n    print(\"Infer time: \", t2 - t1)\n    if NMS:\n        classes, scores, masks = nms_predictions(\n            classes, \n            scores, \n            bboxes, \n            masks, \n            iou_th=IOU_TH\n        )\n    pred_string = \"\"\n    mask_img = 0\n    t = 0\n    for i, mask in enumerate(masks):\n        if int(classes[i]) != 1: continue\n        \n        mask = np.where(mask>0.5, 1, 0).astype(np.bool)\n#         print(mask.shape)\n        mask = np.array(mask, dtype=np.uint8)\n#         mask = cv2.dilate(mask, np.ones((3, 3), dtype=np.uint8), iterations=1)\n        mask = np.array(mask, dtype=bool)\n        if mask.sum() < 60: continue\n        mask_img += mask\n        encoded = encode_binary_mask(mask)\n        if t == 0:\n            pred_string += f\"0 {scores[i]} {encoded.decode('utf-8')}\"\n        else:\n            pred_string += f\" 0 {scores[i]} {encoded.decode('utf-8')}\"\n        t += 1\n    ids.append(test_name.split('.')[0])\n    heights.append(h)\n    widths.append(w)\n    prediction_strings.append(pred_string)\n\n    plt.imshow(mask_img)\n    plt.show()\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(\"/kaggle/working/submission.csv\")\nprint(submission)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T07:19:30.903881Z","iopub.execute_input":"2023-07-12T07:19:30.904257Z","iopub.status.idle":"2023-07-12T07:19:32.104027Z","shell.execute_reply.started":"2023-07-12T07:19:30.904226Z","shell.execute_reply":"2023-07-12T07:19:32.103Z"},"trusted":true},"execution_count":null,"outputs":[]}]}