{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","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":6021726,"sourceType":"datasetVersion","datasetId":3446188}],"dockerImageVersionId":30512,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from itertools import chain\nimport json\nimport os\nimport shutil\nfrom tqdm.notebook import tqdm\nfrom colorama import Fore\nimport yaml\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-08-17T18:52:45.417947Z","iopub.execute_input":"2024-08-17T18:52:45.418244Z","iopub.status.idle":"2024-08-17T18:52:45.506727Z","shell.execute_reply.started":"2024-08-17T18:52:45.418220Z","shell.execute_reply":"2024-08-17T18:52:45.505777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class COCODataset:\n    def __init__(self, images_dirpath: str, annotations_filepath: str, length: int = 1633):\n        self.train_size = None\n        self.val_size = None\n        self.length = length\n        self.classes = None\n        self.labels_counter = None\n        self.normalize = None\n        \n        self.images_dirpath = images_dirpath\n        self.annotations_filepath = annotations_filepath\n        self.dataset_dirpath = os.path.join(os.getcwd(), \"dataset\")\n        self.train_dirpath =  os.path.join(self.dataset_dirpath, \"train\")\n        self.val_dirpath =  os.path.join(self.dataset_dirpath, \"val\")\n        self.config_path = os.path.join(self.dataset_dirpath, \"coco.yaml\")\n\n        self.samples = self.parse_jsonl(annotations_filepath)\n        self.classes_dict = {\n            \"blood_vessel\": 0,\n            \"glomerulus\": 1,\n            \"unsure\": 2,\n        }\n\n    def __prepare_dirs(self) -> None:\n        if not os.path.exists(self.dataset_dirpath):\n            os.makedirs(os.path.join(self.train_dirpath, \"images\"), exist_ok=True)\n            os.makedirs(os.path.join(self.train_dirpath, \"labels\"), exist_ok=True)\n            os.makedirs(os.path.join(self.val_dirpath, \"images\"), exist_ok=True)\n            os.makedirs(os.path.join(self.val_dirpath, \"labels\"), exist_ok=True)\n        else:\n            raise RuntimeError(\"Dataset already exists!\")\n\n    def __define_splitratio(self) -> None:\n        self.train_size = round(self.length * self.train_size)\n        self.val_size = self.length - self.train_size\n        assert self.train_size + self.val_size == self.length\n\n    def parse_jsonl(self, path: str) -> list[dict, ...]:\n        with open(path, 'r') as json_file:\n            jsonl_samples = [\n                json.loads(line)\n                for line in tqdm(\n                    json_file, desc=\"Processing polygons\", total=self.length\n                )\n            ]\n        return jsonl_samples\n\n    def __define_paths(self, i: int) -> dict:\n        data_path = self.val_dirpath\n        if i < self.train_size:\n            data_path = self.train_dirpath\n        return {\n            \"images\": os.path.join(data_path, \"images\"),\n            \"labels\": os.path.join(data_path, \"labels\")\n        }\n\n    @staticmethod\n    def __get_label_path(paths_dict: dict, identifier: str) -> str:\n        return os.path.join(\n            paths_dict[\"labels\"],\n            f\"{identifier}.txt\"\n        )\n\n    @staticmethod\n    def __get_image_path(paths_dict: dict, identifier: str) -> str:\n        return os.path.join(\n            paths_dict[\"images\"],\n            f\"{identifier}.tif\"\n        )\n\n    def __copy_image(self, dst_path: str, identifier: str) -> str:\n        shutil.copyfile(\n            os.path.join(self.images_dirpath, f\"{identifier}.tif\"),\n            dst_path\n        )\n\n    def __copy_label(self, annotations: list, dst_path: str) -> None:\n        with open(dst_path, \"w\") as file:\n            for annotation in annotations:\n                coordinates = annotation[\"coordinates\"][0]\n                label = self.classes_dict[annotation[\"type\"]]\n                if label in self.classes:\n                    if coordinates:\n                        if self.normalize:\n                            coordinates = np.array(coordinates) / 512.0\n                        coordinates = \" \".join(map(str, chain(*coordinates)))\n                        file.write(f\"{label} {coordinates}\\n\")\n                        self.labels_counter += 1\n\n    def __splitfolders(self):\n        for i, line in tqdm(\n                enumerate(self.samples),\n                desc=\"Dataset creation\", total=self.length\n        ):\n            self.labels_counter = 0\n            identifier = line[\"id\"]\n            annotations = line[\"annotations\"]\n            paths_dict = self.__define_paths(i)\n\n            dst_image_path = self.__get_image_path(paths_dict, identifier)\n            dst_label_path = self.__get_label_path(paths_dict, identifier)\n\n            self.__copy_image(dst_image_path, identifier)\n            self.__copy_label(annotations, dst_label_path)\n\n            if self.labels_counter == 0:\n                os.remove(dst_image_path)\n                os.remove(dst_label_path)\n\n    def __count_dataset(self) -> dict:\n        train_images = len(os.listdir(os.path.join(self.train_dirpath, \"images\")))\n        train_labels = len(os.listdir(os.path.join(self.train_dirpath, \"labels\")))\n        val_images = len(os.listdir(os.path.join(self.val_dirpath, \"images\")))\n        val_labels = len(os.listdir(os.path.join(self.val_dirpath, \"labels\")))\n        return {\n            \"train_images\": train_images,\n            \"train_labels\": train_labels,\n            \"val_images\": val_images,\n            \"val_labels\": val_labels\n        }\n\n    @staticmethod\n    def __check_sanity(count_dict: dict) -> None:\n        assert count_dict[\"train_images\"] == count_dict[\"train_labels\"]\n        assert count_dict[\"val_images\"] == count_dict[\"val_labels\"]\n\n    def __finalizing(self, count_dict: dict) -> None:\n        assert os.path.exists(self.dataset_dirpath)\n\n        example_structure = [\n            \"dataset\",\n            \"train\", \"labels\", \"images\",\n            \"val\", \"labels\", \"images\"\n        ]\n\n        dir_bone = (\n            dirname.split(\"/\")[-1]\n            for dirname, _, filenames in os.walk(self.dataset_dirpath)\n            if dirname.split(\"/\")[-1] in example_structure\n        )\n\n        try:\n            print(\"\\n~ HuBMAP Dataset Structure ~\\n\")\n            print(\n            f\"\"\"\n          ├── {next(dir_bone)}\n          │   │\n          │   ├── {next(dir_bone)}\n          │   │   └── {next(dir_bone)}\n          │   │   └── {next(dir_bone)}\n          │   │\n          │   ├── {next(dir_bone)}\n          │   │   └── {next(dir_bone)}\n          │   │   └── {next(dir_bone)}\n            \"\"\"\n            )\n        except StopIteration as e:\n            print(e)\n        else:\n            print(Fore.GREEN + \"-> Success\")\n            print(Fore.GREEN + f\"Train dataset: {count_dict['train_images']}\\nVal dataset: {count_dict['val_images']}\")\n\n    def get_config(self) ->dict:\n        names = [\"blood_vessel\", \"glomerulus\", \"unsure\"]\n        return {\n            \"train\": str(self.train_dirpath),\n            \"val\": str(self.val_dirpath),\n            \"names\": [names[i] for i in self.classes]\n        }\n\n    @staticmethod\n    def display_config(config: dict) -> None:\n        print(Fore.BLACK + \"\\n~ HuBMAP Config Structure ~\\n\")\n        print(\n        f\"\"\"\n      │   │\n      │   ├── train\n      │   │   └── {config['train']}/images\n      │   │\n      │   │\n      │   ├── val\n      │   │   └── {config['val']}/images\n      │   │\n      │   │\n      │   ├── names\n      │   │   └── {' '.join(config['names'])}\n        \"\"\"\n        )\n        print(Fore.GREEN + \"-> Success\")\n        print(Fore.GREEN + f\"Number of classes: {len(config['names'])}\"\n                           f\"\\nClasses: {' '.join(config['names'])}\" \n              )\n\n    def write_config(self, config: dict) -> None:\n        with open(self.config_path, mode=\"w\") as f:\n            yaml.safe_dump(stream=f, data=config)\n\n    def __call__(self, train_size: float,\n                 classes: list[int, ...],\n                 make_config: bool = True,\n                 normalize: bool = True\n                ) -> None:\n        \n        self.train_size = train_size\n        self.classes = classes\n        self.normalize = normalize\n        \n        self.__define_splitratio()\n        self.__prepare_dirs()\n        self.__splitfolders()\n        count_dict = self.__count_dataset()\n        self.__check_sanity(count_dict)\n        self.__finalizing(count_dict)\n        \n        if make_config:\n            config = self.get_config()\n            self.write_config(config)\n            self.display_config(config)","metadata":{"execution":{"iopub.status.busy":"2024-08-17T18:52:52.215405Z","iopub.execute_input":"2024-08-17T18:52:52.215752Z","iopub.status.idle":"2024-08-17T18:52:52.345145Z","shell.execute_reply.started":"2024-08-17T18:52:52.215726Z","shell.execute_reply":"2024-08-17T18:52:52.344152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coco = COCODataset(\n    annotations_filepath=\"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\",\n    images_dirpath=\"/kaggle/input/hubmap-hacking-the-human-vasculature/train\",\n) ","metadata":{"execution":{"iopub.status.busy":"2024-08-17T18:53:02.646240Z","iopub.execute_input":"2024-08-17T18:53:02.646877Z","iopub.status.idle":"2024-08-17T18:53:06.342606Z","shell.execute_reply.started":"2024-08-17T18:53:02.646846Z","shell.execute_reply":"2024-08-17T18:53:06.341686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coco(train_size=0.80, classes=[0, 1, 2])","metadata":{"execution":{"iopub.status.busy":"2024-08-17T18:53:19.607999Z","iopub.execute_input":"2024-08-17T18:53:19.608826Z","iopub.status.idle":"2024-08-17T18:53:58.215411Z","shell.execute_reply.started":"2024-08-17T18:53:19.608797Z","shell.execute_reply":"2024-08-17T18:53:58.214554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport os\nimport sys\nfrom colorama import Fore","metadata":{"execution":{"iopub.status.busy":"2024-08-17T18:54:47.784851Z","iopub.execute_input":"2024-08-17T18:54:47.785682Z","iopub.status.idle":"2024-08-17T18:54:47.789745Z","shell.execute_reply.started":"2024-08-17T18:54:47.785650Z","shell.execute_reply":"2024-08-17T18:54:47.788792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SetupPipline:\n    def __init__(self, display: bool = True):\n        self.pycocotools = self.__pycocotools()\n        self.ultralytics = self.__ultralytics()\n        \n    @staticmethod\n    def __ultralytics() -> str:\n        sys.path.append(\"/kaggle/input/hubmap-tools-ultralytics-and-pycocotools/ultralytics/ultralytics\") \n        return \"successfully\"\n        \n    @staticmethod\n    def __pycocotools() -> str:\n        if not os.path.exists(\"/kaggle/working/packages\"):\n            shutil.copytree(\"/kaggle/input/hubmap-tools-ultralytics-and-pycocotools/pycocotools/pycocotools\", \"/kaggle/working/packages\")\n            os.chdir(\"/kaggle/working/packages/pycocotools-2.0.6/\")\n            os.system(\"python setup.py install\")\n            os.system(\"pip install . --no-index --find-links /kaggle/working/packages/\")\n            os.chdir(\"/kaggle/working\")\n            return \"successfully\"\n    \n    def display(self) -> None:\n        print(Fore.GREEN+f\"\\nPycocotools was installed {self.pycocotools}\")\n        print(f\"Ultralytics was installed {self.ultralytics}\"+Fore.WHITE)","metadata":{"execution":{"iopub.status.busy":"2024-08-17T18:54:52.415765Z","iopub.execute_input":"2024-08-17T18:54:52.416136Z","iopub.status.idle":"2024-08-17T18:54:52.423856Z","shell.execute_reply.started":"2024-08-17T18:54:52.416107Z","shell.execute_reply":"2024-08-17T18:54:52.422939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipline = SetupPipline()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-08-17T18:54:55.155688Z","iopub.execute_input":"2024-08-17T18:54:55.156528Z","iopub.status.idle":"2024-08-17T18:55:40.592741Z","shell.execute_reply.started":"2024-08-17T18:54:55.156497Z","shell.execute_reply":"2024-08-17T18:55:40.591909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipline.display()","metadata":{"execution":{"iopub.status.busy":"2024-08-17T18:55:40.594214Z","iopub.execute_input":"2024-08-17T18:55:40.594501Z","iopub.status.idle":"2024-08-17T18:55:40.599446Z","shell.execute_reply.started":"2024-08-17T18:55:40.594478Z","shell.execute_reply":"2024-08-17T18:55:40.598464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pycocotools import _mask as coco_mask \nfrom ultralytics import YOLO","metadata":{"execution":{"iopub.status.busy":"2024-08-17T18:56:24.766085Z","iopub.execute_input":"2024-08-17T18:56:24.766430Z","iopub.status.idle":"2024-08-17T18:56:31.193246Z","shell.execute_reply.started":"2024-08-17T18:56:24.766402Z","shell.execute_reply":"2024-08-17T18:56:31.192319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def main():\n    model = YOLO(\"yolov8x-seg.pt\")\n    model.train(\n        # Project\n        project=\"HuBMAP\",\n        name=\"yolov8x-seg\",\n\n        # Random Seed parameters\n        deterministic=True,\n        seed=43,\n\n        # Data & model parameters\n        data=\"/kaggle/working/dataset/coco.yaml\", \n        save=True,\n        save_period=5,\n        pretrained=True,\n        imgsz=512,\n\n        # Training parameters\n        epochs=20,\n        batch=4,\n        workers=8,\n        val=True,\n        device=0,\n\n        # Optimization parameters\n        lr0=0.018,\n        patience=3,\n        optimizer=\"SGD\",\n        momentum=0.947,\n        weight_decay=0.0005,\n        close_mosaic=3,\n    )\n","metadata":{"execution":{"iopub.status.busy":"2024-08-17T18:56:46.469224Z","iopub.execute_input":"2024-08-17T18:56:46.470039Z","iopub.status.idle":"2024-08-17T18:56:46.476324Z","shell.execute_reply.started":"2024-08-17T18:56:46.470008Z","shell.execute_reply":"2024-08-17T18:56:46.475362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if __name__ == '__main__':\n    main()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-08-17T18:56:56.654368Z","iopub.execute_input":"2024-08-17T18:56:56.654697Z","iopub.status.idle":"2024-08-17T19:29:58.069227Z","shell.execute_reply.started":"2024-08-17T18:56:56.654672Z","shell.execute_reply":"2024-08-17T19:29:58.068066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:34:15.002630Z","iopub.execute_input":"2024-08-17T19:34:15.003017Z","iopub.status.idle":"2024-08-17T19:34:15.009609Z","shell.execute_reply.started":"2024-08-17T19:34:15.002964Z","shell.execute_reply":"2024-08-17T19:34:15.008737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dirlist = os.listdir(\"/kaggle/working/dataset/val/images\")\nprint(dirlist[:5])","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:34:23.314486Z","iopub.execute_input":"2024-08-17T19:34:23.314853Z","iopub.status.idle":"2024-08-17T19:34:23.322804Z","shell.execute_reply.started":"2024-08-17T19:34:23.314823Z","shell.execute_reply":"2024-08-17T19:34:23.321695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO(\"/kaggle/working/HuBMAP/yolov8x-seg/weights/best.pt\")\nhistory = model.predict(\"../working/dataset/val/images/ed6a92a9410c.tif\")[0]\nimage = history.plot()\nplt.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:34:27.293049Z","iopub.execute_input":"2024-08-17T19:34:27.293415Z","iopub.status.idle":"2024-08-17T19:34:28.792378Z","shell.execute_reply.started":"2024-08-17T19:34:27.293385Z","shell.execute_reply":"2024-08-17T19:34:28.791331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"F1_curve = Image.open(\"/kaggle/working/HuBMAP/yolov8x-seg/results.png\")\nplt.figure(figsize=(15,20))\nplt.imshow(F1_curve)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:34:36.075103Z","iopub.execute_input":"2024-08-17T19:34:36.075487Z","iopub.status.idle":"2024-08-17T19:34:36.829862Z","shell.execute_reply.started":"2024-08-17T19:34:36.075455Z","shell.execute_reply":"2024-08-17T19:34:36.828437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"P_curve = Image.open(\"/kaggle/working/HuBMAP/yolov8x-seg/train_batch1.jpg\")\nplt.imshow(P_curve)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:36:52.557752Z","iopub.execute_input":"2024-08-17T19:36:52.558137Z","iopub.status.idle":"2024-08-17T19:36:52.950010Z","shell.execute_reply.started":"2024-08-17T19:36:52.558106Z","shell.execute_reply":"2024-08-17T19:36:52.949059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nimport torch\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\nimport pandas as pd\nimport torchvision.transforms as T\nfrom ultralytics import YOLO\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:36:57.908265Z","iopub.execute_input":"2024-08-17T19:36:57.908939Z","iopub.status.idle":"2024-08-17T19:36:57.915973Z","shell.execute_reply.started":"2024-08-17T19:36:57.908903Z","shell.execute_reply":"2024-08-17T19:36:57.914396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EncodeBinaryMask:\n    @staticmethod\n    def __checking_mask(mask: np.ndarray) -> np.ndarray:\n        if mask.dtype != np.bool:\n            raise ValueError(\n                \"expects a binary mask, received dtype == %s\" %\n                mask.dtype\n            )\n        return mask\n\n    @staticmethod\n    def __convert_mask(mask: np.ndarray):\n        mask_to_encode = mask.astype(np.uint8)\n        mask_to_encode = np.asfortranarray(mask_to_encode)\n        return mask_to_encode\n\n    @staticmethod\n    def __compress_encode(encoded_mask) -> t.Text:\n        binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n        base64_str = base64.b64encode(binary_str)\n        return base64_str\n\n    def __call__(self, mask: np.ndarray) -> t.Text:\n        mask = self.__checking_mask(mask)\n        mask_to_encode = self.__convert_mask(mask)\n        encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n        base64_str = self.__compress_encode(encoded_mask)\n        return base64_str","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:37:02.682029Z","iopub.execute_input":"2024-08-17T19:37:02.682381Z","iopub.status.idle":"2024-08-17T19:37:02.693655Z","shell.execute_reply.started":"2024-08-17T19:37:02.682354Z","shell.execute_reply":"2024-08-17T19:37:02.691811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Submission:\n    def __init__(self, dirpath: str, model: torch.nn.Module):\n        self.__eval_transforms = self.get_transforms()\n        self.__model = model\n        self.__encoder = EncodeBinaryMask()\n        self.__dirpath = dirpath\n        self.__filenames = os.listdir(dirpath)\n        self.height = 512\n        self.width = 512\n        \n        self.__submission_dict = {\n            \"id\": [],\n            \"height\": [],\n            \"width\": [],\n            \"prediction_string\": []\n        }\n        \n        self.submission = None\n    \n    @staticmethod\n    def get_transforms():\n        return T.Compose([\n            T.ToTensor(),\n            T.Resize(size=(512, 512)),\n            T.Normalize(mean=[0.485, 0.456, 0.406],\n                        std=[0.229, 0.224, 0.225])\n        ])\n\n    def __len__(self):\n        return len(self.__filenames)\n\n    def __get_columns(self) -> None:\n        for filename in self.__filenames:\n            path = self.__get_image_path(filename)\n            masks = self.__forward(path)\n            identifier, height, width, prediction_string = self.__get_cells(filename, masks)\n            self.__update_columns(identifier, height, width, prediction_string)\n\n    def __update_columns(self, identifier: str, height: int, width: int, prediction_string: str) -> None:\n        self.__submission_dict[\"id\"].append(identifier)\n        self.__submission_dict[\"height\"].append(height)\n        self.__submission_dict[\"width\"].append(width)\n        self.__submission_dict[\"prediction_string\"].append(prediction_string)\n\n    def __get_cells(self, filename: str, masks: list):\n        prediction_string = \"\"\n        prediction_string = self.__get_prediction_string(masks, prediction_string)\n        identifier = filename.split(\".\")[0]\n        return identifier, self.height, self.width, prediction_string\n\n    def __get_prediction_string(self, masks: list, prediction_string: str) -> str:\n        if masks:\n            for outputs in masks:\n                mask = outputs[\"mask\"]\n                mask = np.where(mask > 0.5, 1, 0).astype(np.bool)\n                base64_str = self.__encoder(mask)\n                confidence = outputs[\"confidence\"]\n                prediction_string += f\"0 {confidence} {base64_str.decode('utf-8')} \"\n        else:\n            return \"\"\n        return prediction_string\n\n    def __get_image_path(self, filename: str) -> str:\n        return os.path.join(\n            self.__dirpath, filename\n        )\n\n    def __get_image(self, path: str) -> torch.Tensor:\n        image = Image.open(path)\n        image = np.asarray(image)\n        image = self.__eval_transforms(image)\n        return image\n\n    def __forward(self, image: torch.tensor) -> list:\n        masks = self.__model(image) \n        return masks \n\n    def submit(self) -> None:\n        if not self.submission:\n            self.__get_columns()\n            self.submission = pd.DataFrame(self.__submission_dict)\n            self.submission = self.submission.set_index('id')\n            self.submission.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:37:06.710927Z","iopub.execute_input":"2024-08-17T19:37:06.711277Z","iopub.status.idle":"2024-08-17T19:37:06.730072Z","shell.execute_reply.started":"2024-08-17T19:37:06.711250Z","shell.execute_reply":"2024-08-17T19:37:06.728880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BestYolo:\n    def __init__(self, conf: float = 0.05):\n        self.model_path = \"/kaggle/working/HuBMAP/yolov8x-seg/weights/best.pt\"\n        self.model = self.get_model()\n        self.conf = conf\n    \n    def get_model(self) -> YOLO:\n        return YOLO(self.model_path)\n    \n    def __call__(self, source) -> list[dict, ...]:\n        sublist = []\n        result = self.model(source)[0]\n        if result.masks:\n            for i in range(len(result.masks.data)):\n                conf = round(float(result.boxes.conf[i]), 2)\n                mask = np.expand_dims(result.masks.data[i].cpu().numpy(), axis=0).transpose(1,2,0)\n            \n                if int(result.boxes.cls[i]) == 0 and conf >= self.conf:\n                    sublist.append({\"mask\": mask, \"confidence\": conf})\n                else:\n                    continue\n            return sublist\n        else:\n            return None","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:37:10.890014Z","iopub.execute_input":"2024-08-17T19:37:10.890347Z","iopub.status.idle":"2024-08-17T19:37:10.900619Z","shell.execute_reply.started":"2024-08-17T19:37:10.890323Z","shell.execute_reply":"2024-08-17T19:37:10.899537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"__TEST_PATH = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test\"\nmodel = BestYolo()\nsub = Submission(dirpath=__TEST_PATH, model=model)\nsub.submit()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-08-17T19:37:18.579431Z","iopub.execute_input":"2024-08-17T19:37:18.579797Z","iopub.status.idle":"2024-08-17T19:37:19.572071Z","shell.execute_reply.started":"2024-08-17T19:37:18.579764Z","shell.execute_reply":"2024-08-17T19:37:19.570109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.submission.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-17T19:37:24.750827Z","iopub.execute_input":"2024-08-17T19:37:24.751339Z","iopub.status.idle":"2024-08-17T19:37:24.767195Z","shell.execute_reply.started":"2024-08-17T19:37:24.751310Z","shell.execute_reply":"2024-08-17T19:37:24.766128Z"},"trusted":true},"execution_count":null,"outputs":[]}]}