{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":52279,"databundleVersionId":5822112,"sourceType":"competition"},{"sourceId":8022865,"sourceType":"datasetVersion","datasetId":660045},{"sourceId":8036180,"sourceType":"datasetVersion","datasetId":4733640},{"sourceId":8055379,"sourceType":"datasetVersion","datasetId":4750981},{"sourceId":8058391,"sourceType":"datasetVersion","datasetId":4733118}],"dockerImageVersionId":30673,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Cài đặt thư viện","metadata":{}},{"cell_type":"code","source":"# !git clone https://github.com/open-mmlab/mmcv.git\n# !cd mmcv && CUDA_HOME=/usr/local/cuda-12.1 MMCV_WITH_OPS=1 pip wheel --wheel-dir=/kaggle/working/mmdet-mmcv .","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:41:47.393073Z","iopub.execute_input":"2024-04-08T00:41:47.393627Z","iopub.status.idle":"2024-04-08T00:41:47.398764Z","shell.execute_reply.started":"2024-04-08T00:41:47.393591Z","shell.execute_reply":"2024-04-08T00:41:47.397654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !git clone https://github.com/open-mmlab/mmdetection.git\n# !cd mmdetection && pip wheel --wheel-dir=/kaggle/working/mmdet-mmcv .","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:41:47.400987Z","iopub.execute_input":"2024-04-08T00:41:47.401370Z","iopub.status.idle":"2024-04-08T00:41:47.411202Z","shell.execute_reply.started":"2024-04-08T00:41:47.401333Z","shell.execute_reply":"2024-04-08T00:41:47.410355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !git clone https://github.com/open-mmlab/mmpretrain.git\n# !cd mmpretrain && pip wheel --wheel-dir=/kaggle/working/mmpretrain .","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:41:47.412482Z","iopub.execute_input":"2024-04-08T00:41:47.412831Z","iopub.status.idle":"2024-04-08T00:41:47.420728Z","shell.execute_reply.started":"2024-04-08T00:41:47.412804Z","shell.execute_reply":"2024-04-08T00:41:47.419845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !zip -r mmpretrain.zip /kaggle/working/mmpretrain","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:41:47.423027Z","iopub.execute_input":"2024-04-08T00:41:47.423323Z","iopub.status.idle":"2024-04-08T00:41:47.430829Z","shell.execute_reply.started":"2024-04-08T00:41:47.423279Z","shell.execute_reply":"2024-04-08T00:41:47.430019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q --no-deps /kaggle/input/mmdet-mmcv/*.whl\n!pip install -q --no-deps /kaggle/input/mmpretrain/*.whl","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:41:47.431965Z","iopub.execute_input":"2024-04-08T00:41:47.432253Z","iopub.status.idle":"2024-04-08T00:42:30.058333Z","shell.execute_reply.started":"2024-04-08T00:41:47.432228Z","shell.execute_reply":"2024-04-08T00:42:30.057067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Import thư viện","metadata":{}},{"cell_type":"code","source":"import mmcv\n\nfrom mmdet.apis import init_detector, inference_detector\nfrom mmengine import Config\nfrom mmengine.runner import Runner, set_random_seed\nfrom mmengine.visualization import Visualizer\n\nfrom pathlib import Path\nfrom PIL import Image\n\nfrom pycocotools import _mask as coco_mask\nfrom pycocotools.coco import COCO\n\nfrom skimage import io\nfrom skimage.morphology import binary_dilation\n\nfrom tqdm import tqdm\n\nimport base64\nimport cv2\n\nimport glob\nimport json\n\nimport random\nimport matplotlib.pyplot as plt\n\nimport numpy as np\nimport os\nimport pandas as pd\n\nimport torch\n\nimport torchvision.transforms as T\nimport typing as t\n\nimport zlib\nfrom collections import defaultdict\n\nfrom sklearn.model_selection import train_test_split\nfrom copy import deepcopy","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:30.059949Z","iopub.execute_input":"2024-04-08T00:42:30.060259Z","iopub.status.idle":"2024-04-08T00:42:40.347548Z","shell.execute_reply.started":"2024-04-08T00:42:30.060232Z","shell.execute_reply":"2024-04-08T00:42:40.346568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Chuẩn bị dataset","metadata":{}},{"cell_type":"code","source":"# df = pd.read_csv(\"/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv\")\n# df = df[df[\"dataset\"] != 3] # chỉ lấy dataset 1 và 2\n# df.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.348840Z","iopub.execute_input":"2024-04-08T00:42:40.349322Z","iopub.status.idle":"2024-04-08T00:42:40.353383Z","shell.execute_reply.started":"2024-04-08T00:42:40.349277Z","shell.execute_reply":"2024-04-08T00:42:40.352364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Chia ra 2 tập train và validate","metadata":{}},{"cell_type":"code","source":"# df_train, df_val = train_test_split(df, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.354624Z","iopub.execute_input":"2024-04-08T00:42:40.355524Z","iopub.status.idle":"2024-04-08T00:42:40.521340Z","shell.execute_reply.started":"2024-04-08T00:42:40.355497Z","shell.execute_reply":"2024-04-08T00:42:40.520116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.524752Z","iopub.execute_input":"2024-04-08T00:42:40.525380Z","iopub.status.idle":"2024-04-08T00:42:40.549675Z","shell.execute_reply.started":"2024-04-08T00:42:40.525349Z","shell.execute_reply":"2024-04-08T00:42:40.548707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_val.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.553780Z","iopub.execute_input":"2024-04-08T00:42:40.554072Z","iopub.status.idle":"2024-04-08T00:42:40.559034Z","shell.execute_reply.started":"2024-04-08T00:42:40.554048Z","shell.execute_reply":"2024-04-08T00:42:40.558153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Chuyển annotations sang định dạng coco","metadata":{"execution":{"iopub.status.busy":"2024-04-05T13:09:47.162204Z","iopub.execute_input":"2024-04-05T13:09:47.162963Z","iopub.status.idle":"2024-04-05T13:09:47.166709Z","shell.execute_reply.started":"2024-04-05T13:09:47.162931Z","shell.execute_reply":"2024-04-05T13:09:47.165829Z"}}},{"cell_type":"code","source":"# jsonl_file_path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\"\n# data = []\n# with open(jsonl_file_path, \"r\") as file:\n#     for line in file:\n#         data.append(json.loads(line))","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.560391Z","iopub.execute_input":"2024-04-08T00:42:40.560668Z","iopub.status.idle":"2024-04-08T00:42:40.567163Z","shell.execute_reply.started":"2024-04-08T00:42:40.560634Z","shell.execute_reply":"2024-04-08T00:42:40.566296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def polygon_to_segmentation(polygon):\n#     seg = [[]]\n#     for i in polygon:\n#         seg[0].append(i[0])\n#         seg[0].append(i[1])\n#     return seg","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.568319Z","iopub.execute_input":"2024-04-08T00:42:40.568648Z","iopub.status.idle":"2024-04-08T00:42:40.576216Z","shell.execute_reply.started":"2024-04-08T00:42:40.568624Z","shell.execute_reply":"2024-04-08T00:42:40.575454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coco_data_template = {\"info\": {}, \"licenses\": [], \"categories\": [], \"images\": [], \"annotations\": []}\n\n# categories = []\n# for item in tqdm(data, dynamic_ncols=True):\n#     annotations = item[\"annotations\"]\n#     for annotation in annotations:\n#         annotation_type = annotation[\"type\"]\n#         if annotation_type not in categories:\n#             categories.append(annotation_type)\n#             coco_data_template[\"categories\"].append({\"id\": len(categories), \"name\": annotation_type})","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.577270Z","iopub.execute_input":"2024-04-08T00:42:40.577552Z","iopub.status.idle":"2024-04-08T00:42:40.586915Z","shell.execute_reply.started":"2024-04-08T00:42:40.577530Z","shell.execute_reply":"2024-04-08T00:42:40.586114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def to_coco_annotation(df, output_file_path):\n    \n#     coco_data = deepcopy(coco_data_template)\n    \n#     for image_id in tqdm(df[\"id\"], dynamic_ncols=True):\n\n#         image_info = {\"id\": image_id, \"file_name\": image_id + \".tif\", \"height\": 512, \"width\": 512}\n#         coco_data[\"images\"].append(image_info)\n\n#         masks = [i for i in data if i['id'] == image_id]\n#         if (len(masks) == 0):\n#             continue\n\n#         masks = masks[0]['annotations']\n#         masks = [i['coordinates'][0] for i in masks if i['type'] == 'blood_vessel']\n\n#         cat = \"blood_vessel\"\n\n#         for mask in masks:\n\n#             ys, xs = np.array(mask)[:,1], np.array(mask)[:,0]\n\n#             x1, x2 = np.min(xs), np.max(xs)\n#             y1, y2 = np.min(ys), np.max(ys)\n\n#             category_id = 2 # blood_vessel\n\n#             segmentation = polygon_to_segmentation(mask)\n\n#             annotation_info = {\n#                 \"id\": len(coco_data[\"annotations\"]) + 1,\n#                 \"image_id\": image_id,\n#                 \"category_id\": category_id,\n#                 \"segmentation\": segmentation,\n#                 \"bbox\": [int(x1), int(y1), int(x2 - x1 + 1), int(y2 - y1 + 1)],\n#                 \"area\": int(np.sum(mask)),\n#                 \"iscrowd\": 0,\n#             }\n#             coco_data[\"annotations\"].append(annotation_info)\n\n#     with open(output_file_path, \"w\", encoding=\"utf-8\") as output_file:\n#         json.dump(coco_data, output_file, ensure_ascii=True, indent=4)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.588064Z","iopub.execute_input":"2024-04-08T00:42:40.588454Z","iopub.status.idle":"2024-04-08T00:42:40.596131Z","shell.execute_reply.started":"2024-04-08T00:42:40.588429Z","shell.execute_reply":"2024-04-08T00:42:40.595355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to_coco_annotation(df_train, \"/kaggle/working/coco_annotations_train.json\")\n# to_coco_annotation(df_val, \"/kaggle/working/coco_annotations_val.json\")","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.597076Z","iopub.execute_input":"2024-04-08T00:42:40.597440Z","iopub.status.idle":"2024-04-08T00:42:40.609032Z","shell.execute_reply.started":"2024-04-08T00:42:40.597416Z","shell.execute_reply":"2024-04-08T00:42:40.608218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Một chút kiểm tra","metadata":{"execution":{"iopub.status.busy":"2024-04-05T13:13:38.194189Z","iopub.execute_input":"2024-04-05T13:13:38.194860Z","iopub.status.idle":"2024-04-05T13:13:38.198905Z","shell.execute_reply.started":"2024-04-05T13:13:38.194804Z","shell.execute_reply":"2024-04-05T13:13:38.197906Z"}}},{"cell_type":"code","source":"# dataDir = Path(\"/kaggle/input/hubmap-hacking-the-human-vasculature/train\")\n# annFile = Path(\"/kaggle/working/coco_annotations_val.json\")\n\n# coco = COCO(annFile)\n# imgIds = coco.getImgIds()\n\n# print(len(imgIds))\n\n# imgs = coco.loadImgs(random.sample(imgIds, 2))\n# fig, axs = plt.subplots(len(imgs), 2, figsize=(10, 5*len(imgs)))\n\n# for img, ax_row in zip(imgs, axs):\n#     ax = ax_row[0]  # Access the first axis in each row\n#     I = io.imread(dataDir / img[\"file_name\"])\n#     annIds = coco.getAnnIds(imgIds=[img[\"id\"]])\n#     anns = coco.loadAnns(annIds)\n#     ax.imshow(I)\n\n#     ax = ax_row[1]  # Access the second axis in each row\n#     ax.imshow(I)\n#     plt.sca(ax)\n#     coco.showAnns(anns, draw_bbox=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.609955Z","iopub.execute_input":"2024-04-08T00:42:40.610271Z","iopub.status.idle":"2024-04-08T00:42:40.618610Z","shell.execute_reply.started":"2024-04-08T00:42:40.610249Z","shell.execute_reply":"2024-04-08T00:42:40.617600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Config model","metadata":{"execution":{"iopub.status.busy":"2024-04-03T14:28:03.316349Z","iopub.execute_input":"2024-04-03T14:28:03.317201Z"}}},{"cell_type":"code","source":"%%writefile my_config.py\n\nauto_scale_lr = dict(base_batch_size=16, enable=False)\nbackend_args = None\ncheckpoint_file = 'https://download.openmmlab.com/mmclassification/v0/convnext/downstream/convnext-small_3rdparty_32xb128-noema_in1k_20220301-303e75e3.pth'\ncustom_imports = dict(\n    allow_failed_imports=False, imports=[\n        'mmpretrain.models',\n    ])\ndata_root = '/kaggle/input/hubmap-hacking-the-human-vasculature'\ndataset_type = 'CocoDataset'\ndefault_hooks = dict(\n    checkpoint=dict(interval=2, type='CheckpointHook'),\n    logger=dict(interval=100, type='LoggerHook'),\n    param_scheduler=dict(type='ParamSchedulerHook'),\n    sampler_seed=dict(type='DistSamplerSeedHook'),\n    timer=dict(type='IterTimerHook'),\n    visualization=dict(type='DetVisualizationHook'))\ndefault_scope = 'mmdet'\nenv_cfg = dict(\n    cudnn_benchmark=False,\n    dist_cfg=dict(backend='nccl'),\n    mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0))\nload_from = '/kaggle/input/checkpoints/epoch_15.pth'\nlog_level = 'INFO'\nlog_processor = dict(by_epoch=True, type='LogProcessor', window_size=50)\nmax_epochs = 21\nmetainfo = dict(classes=('blood_vessel', ))\nmodel = dict(\n    backbone=dict(\n        arch='small',\n        drop_path_rate=0.6,\n        gap_before_final_norm=False,\n        init_cfg=dict(\n            checkpoint=\n            'https://download.openmmlab.com/mmclassification/v0/convnext/downstream/convnext-small_3rdparty_32xb128-noema_in1k_20220301-303e75e3.pth',\n            prefix='backbone.',\n            type='Pretrained'),\n        layer_scale_init_value=1.0,\n        out_indices=[\n            0,\n            1,\n            2,\n            3,\n        ],\n        type='mmpretrain.ConvNeXt'),\n    data_preprocessor=dict(\n        bgr_to_rgb=True,\n        mean=[\n            123.675,\n            116.28,\n            103.53,\n        ],\n        pad_mask=True,\n        pad_size_divisor=32,\n        std=[\n            58.395,\n            57.12,\n            57.375,\n        ],\n        type='DetDataPreprocessor'),\n    neck=dict(\n        in_channels=[\n            96,\n            192,\n            384,\n            768,\n        ],\n        num_outs=5,\n        out_channels=256,\n        type='FPN'),\n    roi_head=dict(\n        bbox_head=[\n            dict(\n                bbox_coder=dict(\n                    target_means=[\n                        0.0,\n                        0.0,\n                        0.0,\n                        0.0,\n                    ],\n                    target_stds=[\n                        0.1,\n                        0.1,\n                        0.2,\n                        0.2,\n                    ],\n                    type='DeltaXYWHBBoxCoder'),\n                conv_out_channels=256,\n                fc_out_channels=1024,\n                in_channels=256,\n                loss_bbox=dict(loss_weight=10.0, type='GIoULoss'),\n                loss_cls=dict(\n                    loss_weight=1.0,\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False),\n                norm_cfg=dict(requires_grad=True, type='SyncBN'),\n                num_classes=1,\n                num_shared_convs=4,\n                num_shared_fcs=1,\n                reg_class_agnostic=False,\n                reg_decoded_bbox=True,\n                roi_feat_size=7,\n                type='ConvFCBBoxHead'),\n            dict(\n                bbox_coder=dict(\n                    target_means=[\n                        0.0,\n                        0.0,\n                        0.0,\n                        0.0,\n                    ],\n                    target_stds=[\n                        0.05,\n                        0.05,\n                        0.1,\n                        0.1,\n                    ],\n                    type='DeltaXYWHBBoxCoder'),\n                conv_out_channels=256,\n                fc_out_channels=1024,\n                in_channels=256,\n                loss_bbox=dict(loss_weight=10.0, type='GIoULoss'),\n                loss_cls=dict(\n                    loss_weight=1.0,\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False),\n                norm_cfg=dict(requires_grad=True, type='SyncBN'),\n                num_classes=1,\n                num_shared_convs=4,\n                num_shared_fcs=1,\n                reg_class_agnostic=False,\n                reg_decoded_bbox=True,\n                roi_feat_size=7,\n                type='ConvFCBBoxHead'),\n            dict(\n                bbox_coder=dict(\n                    target_means=[\n                        0.0,\n                        0.0,\n                        0.0,\n                        0.0,\n                    ],\n                    target_stds=[\n                        0.033,\n                        0.033,\n                        0.067,\n                        0.067,\n                    ],\n                    type='DeltaXYWHBBoxCoder'),\n                conv_out_channels=256,\n                fc_out_channels=1024,\n                in_channels=256,\n                loss_bbox=dict(loss_weight=10.0, type='GIoULoss'),\n                loss_cls=dict(\n                    loss_weight=1.0,\n                    type='CrossEntropyLoss',\n                    use_sigmoid=False),\n                norm_cfg=dict(requires_grad=True, type='SyncBN'),\n                num_classes=1,\n                num_shared_convs=4,\n                num_shared_fcs=1,\n                reg_class_agnostic=False,\n                reg_decoded_bbox=True,\n                roi_feat_size=7,\n                type='ConvFCBBoxHead'),\n        ],\n        bbox_roi_extractor=dict(\n            featmap_strides=[\n                4,\n                8,\n                16,\n                32,\n            ],\n            out_channels=256,\n            roi_layer=dict(output_size=7, sampling_ratio=0, type='RoIAlign'),\n            type='SingleRoIExtractor'),\n        mask_head=dict(\n            conv_out_channels=256,\n            in_channels=256,\n            loss_mask=dict(\n                loss_weight=1.0, type='CrossEntropyLoss', use_mask=True),\n            num_classes=1,\n            num_convs=4,\n            type='FCNMaskHead'),\n        mask_roi_extractor=dict(\n            featmap_strides=[\n                4,\n                8,\n                16,\n                32,\n            ],\n            out_channels=256,\n            roi_layer=dict(output_size=14, sampling_ratio=0, type='RoIAlign'),\n            type='SingleRoIExtractor'),\n        num_stages=3,\n        stage_loss_weights=[\n            1,\n            0.5,\n            0.25,\n        ],\n        type='CascadeRoIHead'),\n    rpn_head=dict(\n        anchor_generator=dict(\n            ratios=[\n                0.5,\n                1.0,\n                2.0,\n            ],\n            scales=[\n                8,\n            ],\n            strides=[\n                4,\n                8,\n                16,\n                32,\n                64,\n            ],\n            type='AnchorGenerator'),\n        bbox_coder=dict(\n            target_means=[\n                0.0,\n                0.0,\n                0.0,\n                0.0,\n            ],\n            target_stds=[\n                1.0,\n                1.0,\n                1.0,\n                1.0,\n            ],\n            type='DeltaXYWHBBoxCoder'),\n        feat_channels=256,\n        in_channels=256,\n        loss_bbox=dict(\n            beta=0.1111111111111111, loss_weight=1.0, type='SmoothL1Loss'),\n        loss_cls=dict(\n            loss_weight=1.0, type='CrossEntropyLoss', use_sigmoid=True),\n        type='RPNHead'),\n    test_cfg=dict(\n        rcnn=dict(\n            mask_thr_binary=0.5,\n            max_per_img=100,\n            nms=dict(iou_threshold=0.5, type='nms'),\n            score_thr=0.05),\n        rpn=dict(\n            max_per_img=1000,\n            min_bbox_size=0,\n            nms=dict(iou_threshold=0.7, type='nms'),\n            nms_pre=1000)),\n    train_cfg=dict(\n        rcnn=[\n            dict(\n                assigner=dict(\n                    ignore_iof_thr=-1,\n                    match_low_quality=False,\n                    min_pos_iou=0.5,\n                    neg_iou_thr=0.5,\n                    pos_iou_thr=0.5,\n                    type='MaxIoUAssigner'),\n                debug=False,\n                mask_size=28,\n                pos_weight=-1,\n                sampler=dict(\n                    add_gt_as_proposals=True,\n                    neg_pos_ub=-1,\n                    num=512,\n                    pos_fraction=0.25,\n                    type='RandomSampler')),\n            dict(\n                assigner=dict(\n                    ignore_iof_thr=-1,\n                    match_low_quality=False,\n                    min_pos_iou=0.6,\n                    neg_iou_thr=0.6,\n                    pos_iou_thr=0.6,\n                    type='MaxIoUAssigner'),\n                debug=False,\n                mask_size=28,\n                pos_weight=-1,\n                sampler=dict(\n                    add_gt_as_proposals=True,\n                    neg_pos_ub=-1,\n                    num=512,\n                    pos_fraction=0.25,\n                    type='RandomSampler')),\n            dict(\n                assigner=dict(\n                    ignore_iof_thr=-1,\n                    match_low_quality=False,\n                    min_pos_iou=0.7,\n                    neg_iou_thr=0.7,\n                    pos_iou_thr=0.7,\n                    type='MaxIoUAssigner'),\n                debug=False,\n                mask_size=28,\n                pos_weight=-1,\n                sampler=dict(\n                    add_gt_as_proposals=True,\n                    neg_pos_ub=-1,\n                    num=512,\n                    pos_fraction=0.25,\n                    type='RandomSampler')),\n        ],\n        rpn=dict(\n            allowed_border=0,\n            assigner=dict(\n                ignore_iof_thr=-1,\n                match_low_quality=True,\n                min_pos_iou=0.3,\n                neg_iou_thr=0.3,\n                pos_iou_thr=0.7,\n                type='MaxIoUAssigner'),\n            debug=False,\n            pos_weight=-1,\n            sampler=dict(\n                add_gt_as_proposals=False,\n                neg_pos_ub=-1,\n                num=256,\n                pos_fraction=0.5,\n                type='RandomSampler')),\n        rpn_proposal=dict(\n            max_per_img=2000,\n            min_bbox_size=0,\n            nms=dict(iou_threshold=0.7, type='nms'),\n            nms_pre=2000)),\n    type='CascadeRCNN')\nnum_classes = 1\noptim_wrapper = dict(\n    constructor='LearningRateDecayOptimizerConstructor',\n    optimizer=dict(\n        betas=(\n            0.9,\n            0.999,\n        ), lr=0.0002, type='AdamW', weight_decay=0.05),\n    paramwise_cfg=dict(decay_rate=0.7, decay_type='layer_wise', num_layers=12),\n    type='AmpOptimWrapper')\nparam_scheduler = [\n    dict(\n        begin=0, by_epoch=False, end=1000, start_factor=0.001,\n        type='LinearLR'),\n    dict(\n        begin=0,\n        by_epoch=True,\n        end=36,\n        gamma=0.1,\n        milestones=[\n            27,\n            33,\n        ],\n        type='MultiStepLR'),\n]\nresume = False\ntest_cfg = dict(type='TestLoop')\ntest_dataloader = dict(\n    batch_size=1,\n    dataset=dict(\n        ann_file='/kaggle/working/coco_annotations_val.json',\n        backend_args=None,\n        data_prefix=dict(img='train/'),\n        data_root='/kaggle/input/hubmap-hacking-the-human-vasculature',\n        metainfo=dict(classes=('blood_vessel', )),\n        pipeline=[\n            dict(backend_args=None, type='LoadImageFromFile'),\n            dict(keep_ratio=True, scale=(\n                512,\n                512,\n            ), type='Resize'),\n            dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n            dict(\n                meta_keys=(\n                    'img_id',\n                    'img_path',\n                    'ori_shape',\n                    'img_shape',\n                    'scale_factor',\n                ),\n                type='PackDetInputs'),\n        ],\n        test_mode=True,\n        type='CocoDataset'),\n    drop_last=False,\n    num_workers=2,\n    persistent_workers=True,\n    sampler=dict(shuffle=False, type='DefaultSampler'))\ntest_evaluator = dict(\n    ann_file='/kaggle/working/coco_annotations_val.json',\n    backend_args=None,\n    format_only=False,\n    metric=[\n        'bbox',\n        'segm',\n    ],\n    type='CocoMetric')\ntest_pipeline = [\n    dict(backend_args=None, type='LoadImageFromFile'),\n    dict(keep_ratio=True, scale=(\n        512,\n        512,\n    ), type='Resize'),\n    dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n    dict(\n        meta_keys=(\n            'img_id',\n            'img_path',\n            'ori_shape',\n            'img_shape',\n            'scale_factor',\n        ),\n        type='PackDetInputs'),\n]\ntrain_ann_file = '/kaggle/working/coco_annotations_train.json'\ntrain_cfg = dict(max_epochs=21, type='EpochBasedTrainLoop', val_interval=1)\ntrain_dataloader = dict(\n    batch_sampler=dict(type='AspectRatioBatchSampler'),\n    batch_size=2,\n    dataset=dict(\n        ann_file='/kaggle/working/coco_annotations_train.json',\n        backend_args=None,\n        data_prefix=dict(img='train/'),\n        data_root='/kaggle/input/hubmap-hacking-the-human-vasculature',\n        filter_cfg=dict(filter_empty_gt=True, min_size=32),\n        metainfo=dict(classes=('blood_vessel', )),\n        pipeline=[\n            dict(backend_args=None, type='LoadImageFromFile'),\n            dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n            dict(prob=0.5, type='RandomFlip'),\n            dict(\n                transforms=[\n                    [\n                        dict(\n                            keep_ratio=True,\n                            scales=[\n                                (\n                                    480,\n                                    1333,\n                                ),\n                                (\n                                    512,\n                                    1333,\n                                ),\n                                (\n                                    544,\n                                    1333,\n                                ),\n                                (\n                                    576,\n                                    1333,\n                                ),\n                                (\n                                    608,\n                                    1333,\n                                ),\n                                (\n                                    640,\n                                    1333,\n                                ),\n                                (\n                                    672,\n                                    1333,\n                                ),\n                                (\n                                    704,\n                                    1333,\n                                ),\n                                (\n                                    736,\n                                    1333,\n                                ),\n                                (\n                                    768,\n                                    1333,\n                                ),\n                                (\n                                    800,\n                                    1333,\n                                ),\n                            ],\n                            type='RandomChoiceResize'),\n                    ],\n                    [\n                        dict(\n                            keep_ratio=True,\n                            scales=[\n                                (\n                                    400,\n                                    1333,\n                                ),\n                                (\n                                    500,\n                                    1333,\n                                ),\n                                (\n                                    600,\n                                    1333,\n                                ),\n                            ],\n                            type='RandomChoiceResize'),\n                        dict(\n                            allow_negative_crop=True,\n                            crop_size=(\n                                384,\n                                600,\n                            ),\n                            crop_type='absolute_range',\n                            type='RandomCrop'),\n                        dict(\n                            keep_ratio=True,\n                            scales=[\n                                (\n                                    480,\n                                    1333,\n                                ),\n                                (\n                                    512,\n                                    1333,\n                                ),\n                                (\n                                    544,\n                                    1333,\n                                ),\n                                (\n                                    576,\n                                    1333,\n                                ),\n                                (\n                                    608,\n                                    1333,\n                                ),\n                                (\n                                    640,\n                                    1333,\n                                ),\n                                (\n                                    672,\n                                    1333,\n                                ),\n                                (\n                                    704,\n                                    1333,\n                                ),\n                                (\n                                    736,\n                                    1333,\n                                ),\n                                (\n                                    768,\n                                    1333,\n                                ),\n                                (\n                                    800,\n                                    1333,\n                                ),\n                            ],\n                            type='RandomChoiceResize'),\n                    ],\n                ],\n                type='RandomChoice'),\n            dict(type='PackDetInputs'),\n        ],\n        type='CocoDataset'),\n    num_workers=2,\n    persistent_workers=True,\n    sampler=dict(shuffle=True, type='DefaultSampler'))\ntrain_pipeline = [\n    dict(backend_args=None, type='LoadImageFromFile'),\n    dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n    dict(prob=0.5, type='RandomFlip'),\n    dict(\n        transforms=[\n            [\n                dict(\n                    keep_ratio=True,\n                    scales=[\n                        (\n                            480,\n                            1333,\n                        ),\n                        (\n                            512,\n                            1333,\n                        ),\n                        (\n                            544,\n                            1333,\n                        ),\n                        (\n                            576,\n                            1333,\n                        ),\n                        (\n                            608,\n                            1333,\n                        ),\n                        (\n                            640,\n                            1333,\n                        ),\n                        (\n                            672,\n                            1333,\n                        ),\n                        (\n                            704,\n                            1333,\n                        ),\n                        (\n                            736,\n                            1333,\n                        ),\n                        (\n                            768,\n                            1333,\n                        ),\n                        (\n                            800,\n                            1333,\n                        ),\n                    ],\n                    type='RandomChoiceResize'),\n            ],\n            [\n                dict(\n                    keep_ratio=True,\n                    scales=[\n                        (\n                            400,\n                            1333,\n                        ),\n                        (\n                            500,\n                            1333,\n                        ),\n                        (\n                            600,\n                            1333,\n                        ),\n                    ],\n                    type='RandomChoiceResize'),\n                dict(\n                    allow_negative_crop=True,\n                    crop_size=(\n                        384,\n                        600,\n                    ),\n                    crop_type='absolute_range',\n                    type='RandomCrop'),\n                dict(\n                    keep_ratio=True,\n                    scales=[\n                        (\n                            480,\n                            1333,\n                        ),\n                        (\n                            512,\n                            1333,\n                        ),\n                        (\n                            544,\n                            1333,\n                        ),\n                        (\n                            576,\n                            1333,\n                        ),\n                        (\n                            608,\n                            1333,\n                        ),\n                        (\n                            640,\n                            1333,\n                        ),\n                        (\n                            672,\n                            1333,\n                        ),\n                        (\n                            704,\n                            1333,\n                        ),\n                        (\n                            736,\n                            1333,\n                        ),\n                        (\n                            768,\n                            1333,\n                        ),\n                        (\n                            800,\n                            1333,\n                        ),\n                    ],\n                    type='RandomChoiceResize'),\n            ],\n        ],\n        type='RandomChoice'),\n    dict(type='PackDetInputs'),\n]\nval_cfg = dict(type='ValLoop')\nval_dataloader = dict(\n    batch_size=1,\n    dataset=dict(\n        ann_file='/kaggle/working/coco_annotations_val.json',\n        backend_args=None,\n        data_prefix=dict(img='train/'),\n        data_root='/kaggle/input/hubmap-hacking-the-human-vasculature',\n        metainfo=dict(classes=('blood_vessel', )),\n        pipeline=[\n            dict(backend_args=None, type='LoadImageFromFile'),\n            dict(keep_ratio=True, scale=(\n                512,\n                512,\n            ), type='Resize'),\n            dict(type='LoadAnnotations', with_bbox=True, with_mask=True),\n            dict(\n                meta_keys=(\n                    'img_id',\n                    'img_path',\n                    'ori_shape',\n                    'img_shape',\n                    'scale_factor',\n                ),\n                type='PackDetInputs'),\n        ],\n        test_mode=True,\n        type='CocoDataset'),\n    drop_last=False,\n    num_workers=2,\n    persistent_workers=True,\n    sampler=dict(shuffle=False, type='DefaultSampler'))\nval_evaluator = dict(\n    ann_file='/kaggle/working/coco_annotations_val.json',\n    backend_args=None,\n    format_only=False,\n    metric=[\n        'bbox',\n        'segm',\n    ],\n    type='CocoMetric')\nvis_backends = [\n    dict(type='LocalVisBackend'),\n]\nvisualizer = dict(\n    name='visualizer',\n    type='DetLocalVisualizer',\n    vis_backends=[\n        dict(type='LocalVisBackend'),\n        dict(type='TensorboardVisBackend'),\n    ])\nwork_dir = '/kaggle/working/checkpoints_and_logs'","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.619724Z","iopub.execute_input":"2024-04-08T00:42:40.619973Z","iopub.status.idle":"2024-04-08T00:42:40.644571Z","shell.execute_reply.started":"2024-04-08T00:42:40.619951Z","shell.execute_reply":"2024-04-08T00:42:40.643637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training phase","metadata":{"execution":{"iopub.status.busy":"2024-04-04T16:29:45.994089Z","iopub.execute_input":"2024-04-04T16:29:45.995185Z","iopub.status.idle":"2024-04-04T16:29:45.999727Z","shell.execute_reply.started":"2024-04-04T16:29:45.995149Z","shell.execute_reply":"2024-04-04T16:29:45.998691Z"}}},{"cell_type":"code","source":"# !cat {config}","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.645558Z","iopub.execute_input":"2024-04-08T00:42:40.645824Z","iopub.status.idle":"2024-04-08T00:42:40.656468Z","shell.execute_reply.started":"2024-04-08T00:42:40.645791Z","shell.execute_reply":"2024-04-08T00:42:40.655695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !python /kaggle/input/mmdetection/tools/train.py my_config.py","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.657420Z","iopub.execute_input":"2024-04-08T00:42:40.657672Z","iopub.status.idle":"2024-04-08T00:42:40.666033Z","shell.execute_reply.started":"2024-04-08T00:42:40.657650Z","shell.execute_reply":"2024-04-08T00:42:40.665313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load checkpoint","metadata":{"execution":{"iopub.status.busy":"2024-04-07T03:09:13.286920Z","iopub.execute_input":"2024-04-07T03:09:13.287743Z","iopub.status.idle":"2024-04-07T03:09:13.291866Z","shell.execute_reply.started":"2024-04-07T03:09:13.287712Z","shell.execute_reply":"2024-04-07T03:09:13.290894Z"}}},{"cell_type":"code","source":"cfg = Config.fromfile('my_config.py')\ncheckpoint_file = '/kaggle/input/checkpoints/epoch_33.pth'\nmodel = init_detector(cfg, checkpoint_file, device='cuda')","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:40.666977Z","iopub.execute_input":"2024-04-08T00:42:40.667203Z","iopub.status.idle":"2024-04-08T00:42:52.598896Z","shell.execute_reply.started":"2024-04-08T00:42:40.667183Z","shell.execute_reply":"2024-04-08T00:42:52.598046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Kiểm tra một chút","metadata":{"execution":{"iopub.status.busy":"2024-04-04T03:14:46.099628Z","iopub.execute_input":"2024-04-04T03:14:46.100243Z","iopub.status.idle":"2024-04-04T03:14:46.104905Z","shell.execute_reply.started":"2024-04-04T03:14:46.100208Z","shell.execute_reply":"2024-04-04T03:14:46.103611Z"}}},{"cell_type":"code","source":"# image = mmcv.imread('/kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif',channel_order='rgb')\n\n# new_result = inference_detector(model, image)\n\n# ## extract boxes, masks, scores, and lables :\n# pred_bboxes = new_result.pred_instances.bboxes\n# pred_labels = new_result.pred_instances.labels\n# pred_scores = new_result.pred_instances.scores\n# pred_masks = new_result.pred_instances.masks\n\n# ## lets filter them using a threshold\n# confidence_threshold = 0.8\n# # Move tensors to CPU and convert to numpy\n# pred_scores_np = pred_scores.cpu().numpy()\n# # Identify the indices that satisfy the threshold\n# filtered_indices = np.where(pred_scores_np > confidence_threshold)[0]\n# # Use these indices to filter the predictions\n# filtered_bboxes = pred_bboxes[filtered_indices].cpu().numpy()\n# filtered_labels = pred_labels[filtered_indices].cpu().numpy()\n# filtered_scores = pred_scores_np[filtered_indices]\n# filtered_masks = pred_masks[filtered_indices].cpu().numpy()\n\n# visualizer = Visualizer(image=image)\n\n# # draw multiple bboxes\n# # single bbox formatted as [xyxy]\n# visualizer.draw_bboxes(filtered_bboxes, edge_colors='r', line_widths=3, line_styles = '--')\n# # to draw a box formatted as [xyxy]\n# # visualizer.draw_bboxes(torch.tensor([[33, 120, 209, 220], [72, 13, 179, 147]]))\n\n# # visualizer.draw_binary_masks(new_result.pred_instances.masks) ## this also works\n# visualizer.draw_binary_masks(filtered_masks, colors=(255, 150, 50), alphas=0.35)\n\n# visualizer.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:52.600162Z","iopub.execute_input":"2024-04-08T00:42:52.600534Z","iopub.status.idle":"2024-04-08T00:42:52.611028Z","shell.execute_reply.started":"2024-04-08T00:42:52.600498Z","shell.execute_reply":"2024-04-08T00:42:52.609845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predicting and submitting","metadata":{}},{"cell_type":"code","source":"class MyDataset(torch.utils.data.Dataset):\n    def __init__(self, imgs, transforms):\n        self.transforms = transforms\n        # load all image files, sorting them to\n        # ensure that they are aligned\n        self.imgs = imgs\n        self.name_indices = [os.path.splitext(os.path.basename(i))[0] for i in imgs]\n\n    def __getitem__(self, idx):\n        # load images and masks\n        img_path = self.imgs[idx]\n        name = self.name_indices[idx]\n        array = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)\n        img = Image.fromarray(array)\n        \n        img = self.transforms(img)\n\n        return img, name\n\n    def __len__(self):\n        return len(self.imgs)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:52.612202Z","iopub.execute_input":"2024-04-08T00:42:52.612552Z","iopub.status.idle":"2024-04-08T00:42:52.624961Z","shell.execute_reply.started":"2024-04-08T00:42:52.612516Z","shell.execute_reply":"2024-04-08T00:42:52.623875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_transform(train):\n    transforms = []\n    transforms.append(T.PILToTensor())\n    transforms.append(T.ConvertImageDtype(torch.float))\n    return T.Compose(transforms)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:52.626050Z","iopub.execute_input":"2024-04-08T00:42:52.626348Z","iopub.status.idle":"2024-04-08T00:42:52.638568Z","shell.execute_reply.started":"2024-04-08T00:42:52.626301Z","shell.execute_reply":"2024-04-08T00:42:52.637364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_imgs = glob.glob('/kaggle/input/hubmap-hacking-the-human-vasculature/test/*.tif')\ndataset_test = MyDataset(all_imgs, get_transform(train=False))","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:52.639791Z","iopub.execute_input":"2024-04-08T00:42:52.640109Z","iopub.status.idle":"2024-04-08T00:42:52.656285Z","shell.execute_reply.started":"2024-04-08T00:42:52.640079Z","shell.execute_reply":"2024-04-08T00:42:52.655352Z"},"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    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":"2024-04-08T00:42:52.657299Z","iopub.execute_input":"2024-04-08T00:42:52.659486Z","iopub.status.idle":"2024-04-08T00:42:52.668969Z","shell.execute_reply.started":"2024-04-08T00:42:52.659452Z","shell.execute_reply":"2024-04-08T00:42:52.667822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_df = defaultdict(list)\n\nfor img_path in all_imgs:\n    \n    pred_string = ''\n\n    img = mmcv.imread(img_path,channel_order='rgb')\n    \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.cpu().numpy()\n    pred_masks = result.pred_instances.masks.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\"0 {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(img_path.split('/')[-1][:-4])\n    dict_df[\"height\"].append(512)\n    dict_df[\"width\"].append(512)\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":"2024-04-08T00:42:52.670518Z","iopub.execute_input":"2024-04-08T00:42:52.670875Z","iopub.status.idle":"2024-04-08T00:42:54.456820Z","shell.execute_reply.started":"2024-04-08T00:42:52.670841Z","shell.execute_reply":"2024-04-08T00:42:54.455945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat submission.csv","metadata":{"execution":{"iopub.status.busy":"2024-04-08T00:42:54.458276Z","iopub.execute_input":"2024-04-08T00:42:54.458920Z","iopub.status.idle":"2024-04-08T00:42:55.450495Z","shell.execute_reply.started":"2024-04-08T00:42:54.458883Z","shell.execute_reply":"2024-04-08T00:42:55.449542Z"},"trusted":true},"execution_count":null,"outputs":[]}]}