{"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":"import sys\nimport os\nsys.path.append('/kaggle/input/ultralytics-main')\nfrom ultralytics import YOLO\nimport numpy as np\nimport base64\nimport typing as t\nimport zlib\nimport torch.nn.functional as F\nimport cv2\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom pathlib import Path\nimport glob\nclass CFG:\n    conf = 0.001\n    max_det = 88\n    img_size = 768\n    iou = 0.65\n    th = 0.35\n    score_up = 0\n    is_tta = False\n    checkpoint_path_list = ['best1.pt'] # 0-6:640, 7-10:768\n    checkpoint_path = '/kaggle/input/hubmap-checpoint/'\n#     checkpoint_path_list = os.listdir(checkpoint_path)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-28T06:20:44.281795Z","iopub.execute_input":"2023-07-28T06:20:44.282165Z","iopub.status.idle":"2023-07-28T06:20:44.291053Z","shell.execute_reply.started":"2023-07-28T06:20:44.282133Z","shell.execute_reply":"2023-07-28T06:20:44.289972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/pycocotools/ /kaggle/working/pycocotools\n!pip install /kaggle/working/pycocotools/pycocotools-2.0.6  --no-index --find-links=/kaggle/working/pycocotools/ \nfrom pycocotools import _mask as coco_mask","metadata":{"execution":{"iopub.status.busy":"2023-07-28T06:15:00.046313Z","iopub.execute_input":"2023-07-28T06:15:00.046921Z","iopub.status.idle":"2023-07-28T06:15:42.324252Z","shell.execute_reply.started":"2023-07-28T06:15:00.046886Z","shell.execute_reply":"2023-07-28T06:15:42.322657Z"},"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":"2023-07-28T06:15:42.329626Z","iopub.execute_input":"2023-07-28T06:15:42.331984Z","iopub.status.idle":"2023-07-28T06:15:42.348519Z","shell.execute_reply.started":"2023-07-28T06:15:42.331941Z","shell.execute_reply":"2023-07-28T06:15:42.343846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class YOLO_part:\n    def __init__(self, model_path):\n        self.model = YOLO(model_path)\n        print( self.model)\n    def __call__(self, img_path):\n        results = self.model.predict(img_path, conf=CFG.conf, max_det=CFG.max_det, imgsz=CFG.img_size, iou=CFG.iou)\n        org_shape = results[0].orig_shape\n        try:\n            masks = results[0].masks.data\n            masks = F.interpolate(masks.unsqueeze(0), size=org_shape, mode='bilinear', align_corners=False).squeeze()\n            masks = masks.cpu().numpy()\n        except:\n            print('mask error')\n            masks = np.zeros(org_shape)\n        try:\n            scores = results[0].boxes.conf.cpu().numpy()\n            labels = results[0].boxes.cls.cpu().numpy()\n        except:\n            print('box error')\n            # 未识别到目标，直接设为不确定\n            scores = 1.0\n            labels = 2\n        return masks, scores, labels, org_shape","metadata":{"execution":{"iopub.status.busy":"2023-07-28T06:15:42.352464Z","iopub.execute_input":"2023-07-28T06:15:42.353898Z","iopub.status.idle":"2023-07-28T06:15:42.365841Z","shell.execute_reply.started":"2023-07-28T06:15:42.353863Z","shell.execute_reply":"2023-07-28T06:15:42.364730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''TTA'''\nimport torch\ndef TTA(x, model):\n    # x.shape=(batch,c,h,w)\n    x = x.unsqueeze(0).permute(0,3,1,2)\n    shape = x.shape\n    x = [x, *[torch.rot90(x, k=i, dims=(-2, -1)) for i in range(1, 4)]]\n    org_shape = [512, 512]\n    best_conf = 0\n    best_score = None\n    best_mask = None\n    best_label = None\n    best_deg = 0\n    for i, img in enumerate(x):\n#         print(i, img.shape)\n        img = img.squeeze(0).cpu().numpy().transpose(1,2,0)\n#         print(i, img.shape)\n        masks, scores, labels, org_shape = model(img)\n        now_score = np.max(scores[labels==0])\n        if now_score > best_conf:\n            best_mask = masks\n            best_label = labels\n            best_score = scores\n            best_conf = now_score\n            best_deg = i\n    masks = []\n    for mask in best_mask:\n#         print(mask)\n        mask = torch.from_numpy(mask).unsqueeze(0).unsqueeze(0)\n        mask = torch.rot90(mask, k=-best_deg, dims=(-2, -1)).squeeze().cpu().numpy()\n        masks.append(mask)\n    best_mask = np.array(masks)\n    return best_mask, best_score, best_label, org_shape","metadata":{"execution":{"iopub.status.busy":"2023-07-28T06:15:42.367676Z","iopub.execute_input":"2023-07-28T06:15:42.368617Z","iopub.status.idle":"2023-07-28T06:15:42.382979Z","shell.execute_reply.started":"2023-07-28T06:15:42.368574Z","shell.execute_reply":"2023-07-28T06:15:42.381931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EnsembleModel:\n    def __init__(self):\n        self.models = []\n#         \n    def __call__(self, x):\n#         masks_list = []\n#         scores_list = []\n#         labels_list = []\n        img = cv2.imread(x)\n        org_shape = [512, 512]\n        best_conf = 0\n        best_score = None\n        best_mask = None\n        best_label = None\n        for model in self.models:\n            if CFG.is_tta:\n                masks, scores, labels, org_shape = TTA(torch.from_numpy(img), model)\n            else:\n                masks, scores, labels, org_shape = model(img)\n            now_score = np.max(scores[labels==0])\n            if now_score > best_conf:\n                best_mask = masks\n                best_label = labels\n                best_score = scores\n                best_conf = now_score\n#             masks_list.append(masks)\n#             scores_list.append(scores)\n#             labels_list.append(labels)\n#         masks = np.stack(masks_list, axis=0)\n#         masks = np.resize(masks, (masks.shape[0] * masks.shape[1], org_shape[0], org_shape[1]))\n#         print(masks.shape)\n#         #         masks = np.mean(masks_list, axis=0)\n#         scores = np.resize(np.stack(scores_list, axis=0), (masks.shape[0]))\n#         print(scores.shape)\n# #         scores = np.mean(scores_list, axis=0)\n#         labels = np.resize(np.stack(labels_list, axis=0), (masks.shape[0]))\n#         print(scores.shape)\n# #         labels = np.mean(labels_list, axis=0)\n#         return masks, scores, labels, org_shape\n        return best_mask, best_score, best_label, org_shape\n\n    def add_model(self, model):\n        self.models.append(model)\n\ndef build_ensemble_model():\n    model = EnsembleModel()\n    checkpoint_path_list = CFG.checkpoint_path_list\n#     checkpoint_path_list = CFG.checkpoint_path_list\n    for checkpoint_path in checkpoint_path_list:\n        path = CFG.checkpoint_path + checkpoint_path\n        print(path)\n        _model = YOLO_part(path)\n        model.add_model(_model)\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-07-28T06:15:42.385187Z","iopub.execute_input":"2023-07-28T06:15:42.385958Z","iopub.status.idle":"2023-07-28T06:15:42.399763Z","shell.execute_reply.started":"2023-07-28T06:15:42.385924Z","shell.execute_reply":"2023-07-28T06:15:42.398724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_ensemble_model()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T06:15:42.401760Z","iopub.execute_input":"2023-07-28T06:15:42.402718Z","iopub.status.idle":"2023-07-28T06:15:48.273527Z","shell.execute_reply.started":"2023-07-28T06:15:42.402683Z","shell.execute_reply":"2023-07-28T06:15:48.272557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# File path settings\n# root_paths = '/kaggle/input/hubmap-hacking-the-human-vasculature/test/'\n# img_list = os.listdir(root_paths)\nids = []\nheights = []\nwidths = []\nprediction_strings = []\nreslut_mask = None\nreslut_scores = None\ntest_dir = Path('/kaggle/input/hubmap-hacking-the-human-vasculature/test')\ntest_image_names = glob.glob(str(test_dir / '*.tif'))\nfor filename in test_image_names:\n    pred_string = ''\n    masks, scores, labels, org_shape = model(filename)\n    unique_values, counts = np.unique(masks, return_counts=True)\n    print(\"Unique values:\", unique_values)\n    print(\"Counts:\", counts)\n    masks = (masks > CFG.th).astype(np.float32)\n    unique_values, counts = np.unique(masks, return_counts=True)\n    print(\"Unique values:\", unique_values)\n    print(\"Counts:\", counts)\n    if reslut_mask is None:\n        reslut_mask = masks\n        reslut_scores = scores\n    for i, mask in enumerate(masks):\n        if labels[i] == 0:\n            # 膨胀边缘\n            kernel = np.ones(shape=(3, 3), dtype=np.uint8)\n            binary_mask = cv2.dilate(mask, kernel, 3)\n            binary_mask = binary_mask[:, :, np.newaxis].astype(np.bool)\n#             binary_mask = mask[:, :, np.newaxis].astype(np.bool)\n            score = scores[i] + CFG.score_up\n#             score = 1.0\n            encoded = encode_binary_mask(binary_mask)\n            if i == 0:\n                pred_string += f\"0 {score} {encoded.decode('utf-8')}\"\n            else:\n                pred_string += f\" 0 {score} {encoded.decode('utf-8')}\" \n    ids.append(filename[:-4].split('/')[-1])\n    heights.append(org_shape[0])\n    widths.append(org_shape[1])\n    prediction_strings.append(pred_string)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T06:20:51.229935Z","iopub.execute_input":"2023-07-28T06:20:51.230338Z","iopub.status.idle":"2023-07-28T06:21:00.703348Z","shell.execute_reply.started":"2023-07-28T06:20:51.230303Z","shell.execute_reply":"2023-07-28T06:21:00.702336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_num = len(reslut_mask)\nimgs = [reslut_mask[i] for i in range(mask_num)]\nimg = 0\nfor i in imgs:\n    img += i\n    img = np.clip(img, 0, 1)\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T06:21:00.705292Z","iopub.execute_input":"2023-07-28T06:21:00.705768Z","iopub.status.idle":"2023-07-28T06:21:01.061227Z","shell.execute_reply.started":"2023-07-28T06:21:00.705731Z","shell.execute_reply":"2023-07-28T06:21:01.060302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame()\nsubmission['id'] = ids\nsubmission['height'] = heights\nsubmission['width'] = widths\nsubmission['prediction_string'] = prediction_strings\nsubmission = submission.set_index('id')\nsubmission.to_csv(\"submission.csv\")\n!cat submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-07-28T06:21:07.780673Z","iopub.execute_input":"2023-07-28T06:21:07.781043Z","iopub.status.idle":"2023-07-28T06:21:08.915613Z","shell.execute_reply.started":"2023-07-28T06:21:07.781013Z","shell.execute_reply":"2023-07-28T06:21:08.914300Z"},"trusted":true},"execution_count":null,"outputs":[]}]}