{"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":"# About this notebook\n\n- no weights sharing(share only inference pipeline)\n- use `mmdet==3.0.0`\n\n# 0.install mmdets\n\n- packages from this [notebook](https://www.kaggle.com/code/yukkyo/mmdetpkgs)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"!pip install --no-index --find-links=/kaggle/input/mmdetpkgs/openmim openmim > /dev/null 2>&1\n!pip install --no-index --find-links=/kaggle/input/mmdetpkgs/mmengine mmengine > /dev/null 2>&1\n!pip install --no-index --find-links=/kaggle/input/mmdetpkgs/mmcvwheel mmcv > /dev/null 2>&1\n!pip install --no-index --find-links=/kaggle/input/mmdetpkgs/pycocotoolswheel pycocotools > /dev/null 2>&1\n!pip install --no-index --find-links=/kaggle/input/mmdetpkgs/mmdet mmdet > /dev/null 2>&1","metadata":{"execution":{"iopub.status.busy":"2023-07-10T23:39:40.996708Z","iopub.execute_input":"2023-07-10T23:39:40.996976Z","iopub.status.idle":"2023-07-10T23:40:42.512708Z","shell.execute_reply.started":"2023-07-10T23:39:40.996952Z","shell.execute_reply":"2023-07-10T23:40:42.511460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip freeze | grep mmdet","metadata":{"execution":{"iopub.status.busy":"2023-07-10T23:41:13.075868Z","iopub.execute_input":"2023-07-10T23:41:13.076245Z","iopub.status.idle":"2023-07-10T23:41:16.050476Z","shell.execute_reply.started":"2023-07-10T23:41:13.076213Z","shell.execute_reply":"2023-07-10T23:41:16.049266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Sample predict by mmdet","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport mmcv\nfrom mmengine.config import Config\nfrom mmdet.registry import VISUALIZERS\nfrom mmdet.apis import init_detector, inference_detector\n\n# default config & weight\n# config_file = '/kaggle/input/mmdetpkgs/rtmdet_tiny_8xb32-300e_coco.py'\n# checkpoint_file = '/kaggle/input/mmdetpkgs/rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth'\n# img_path = '/kaggle/input/mmdetpkgs/demo.jpg'\n\n# my config and weight\nconfig_file = '/kaggle/input/hubmap3-weights/008.py'\ncheckpoint_file = '/kaggle/input/hubmap3-weights/008_fold_1_epoch_30.pth'\nimg_path = '/kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif'\n\n# model init\ncfg = Config.fromfile(config_file)\nmodel = init_detector(config_file, checkpoint_file, device='cuda:0')  # cpu or cuda:0\n\n# inference\nimg = mmcv.imread(img_path)\nresult = inference_detector(model, img)\n\n# visualize result\nimg = mmcv.imconvert(img, 'bgr', 'rgb')\nvisualizer = VISUALIZERS.build(model.cfg.visualizer)\nvisualizer.dataset_meta = model.dataset_meta\nvisualizer.add_datasample(\n    name='result',\n    image=img,\n    data_sample=result,\n    draw_gt=False,\n)\nplt.figure(figsize=(12, 8))\nplt.imshow(visualizer.get_image())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-10T23:42:15.095603Z","iopub.execute_input":"2023-07-10T23:42:15.095982Z","iopub.status.idle":"2023-07-10T23:42:33.323383Z","shell.execute_reply.started":"2023-07-10T23:42:15.095951Z","shell.execute_reply":"2023-07-10T23:42:33.322218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Make submission","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\nfrom collections import defaultdict\nimport pandas as pd\nimport numpy as np\n\n# for mmdet\nimport mmcv\nfrom mmengine.config import Config\nfrom mmdet.apis import init_detector, inference_detector\n\n# for encode mask\nimport base64\nimport zlib\nfrom pycocotools import _mask as coco_mask\nfrom skimage.morphology import binary_dilation\n\ndef encode_binary_mask(mask: np.ndarray):\n    \"\"\"Converts a binary mask into OID challenge encoding ascii text.\"\"\"\n    # check input mask --\n    if mask.dtype != bool:  # np.bool is deplicated\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-10T23:43:55.097301Z","iopub.execute_input":"2023-07-10T23:43:55.097695Z","iopub.status.idle":"2023-07-10T23:43:55.108301Z","shell.execute_reply.started":"2023-07-10T23:43:55.097665Z","shell.execute_reply":"2023-07-10T23:43:55.107203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_id_blood_vessel = 0  # only single class for this competition\nimg_size = 512\nimg_dir = Path(\"/kaggle/input/hubmap-hacking-the-human-vasculature/test\")\nimg_paths = list(img_dir.glob(\"*.tif\"))\n\nconfig_file = '/kaggle/input/hubmap3-weights/008.py'\ncheckpoint_file = '/kaggle/input/hubmap3-weights/008_fold_1_epoch_30.pth'\n\ncfg = Config.fromfile(config_file)\n# cfg.test_dataloader.dataset.pipeline[1]['scale'] = (1280, 1280)  # we can change test pipeline\nmodel = init_detector(cfg, checkpoint_file, device='cuda:0')  # cpu or device='cuda:0'\n\ndict_df = defaultdict(list)\nfor i_path in img_paths:\n    img = mmcv.imread(str(i_path))\n    \n    # todo: use multi models and ensemble\n    # About DetDataSample: https://mmdetection.readthedocs.io/en/latest/_modules/mmdet/structures/det_data_sample.html\n    result = inference_detector(model, img)\n    \n    # extract blood vessel masks for multi class models\n    indxs = (result.pred_instances.labels == class_id_blood_vessel)\n    pred_scores = result.pred_instances.scores[indxs].cpu().numpy()\n    pred_masks = result.pred_instances.masks[indxs].cpu().numpy()\n    \n    # dilation\n    # https://www.kaggle.com/code/itsuki9180/hubmap-inference\n    # https://www.kaggle.com/competitions/hubmap-hacking-the-human-vasculature/discussion/416901\n    pred_masks = [binary_dilation(m) for m in pred_masks]\n    \n    # masks -> string\n    pred_strings = \" \".join([\n        f\"{class_id_blood_vessel} {score_tmp} {encode_binary_mask(mask_tmp).decode()}\"\n        for score_tmp, mask_tmp in zip(pred_scores, pred_masks)\n    ])\n\n    dict_df[\"id\"].append(i_path.stem)\n    dict_df[\"height\"].append(img_size)\n    dict_df[\"width\"].append(img_size)\n    dict_df[\"prediction_string\"].append(pred_strings)\n\ndf_sub = pd.DataFrame(dict_df)\ndf_sub.to_csv(\"submission.csv\", index=False)\ndf_sub.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-03T02:16:58.91624Z","iopub.execute_input":"2023-07-03T02:16:58.916605Z","iopub.status.idle":"2023-07-03T02:17:01.186438Z","shell.execute_reply.started":"2023-07-03T02:16:58.916575Z","shell.execute_reply":"2023-07-03T02:17:01.18546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head -n 2 submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-07-03T02:17:05.735223Z","iopub.execute_input":"2023-07-03T02:17:05.73558Z","iopub.status.idle":"2023-07-03T02:17:06.72557Z","shell.execute_reply.started":"2023-07-03T02:17:05.735552Z","shell.execute_reply":"2023-07-03T02:17:06.7243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}