{"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 numpy as np\nimport cv2\nimport pandas as pd\nfrom glob import glob\nimport torch.nn as nn\nimport json\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchmetrics import AveragePrecision\nimport torch\nimport torchvision\nimport torchvision.transforms as T\nimport albumentations as A \nfrom albumentations.pytorch.transforms import ToTensorV2\nimport os\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm\nimport torch.nn.functional as F\nimport time\nimport base64\nimport typing as t\nimport zlib","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-09T09:43:39.042093Z","iopub.execute_input":"2023-07-09T09:43:39.042573Z","iopub.status.idle":"2023-07-09T09:43:53.557384Z","shell.execute_reply.started":"2023-07-09T09:43:39.042531Z","shell.execute_reply":"2023-07-09T09:43:53.555373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /tmp/pip/cache/\n!cp ../input/smpmodels/pretrainedmodels-0.7.4.tar.gz /tmp/pip/cache/pretrainedmodels-0.7.4.tar.gz\n!cp ../input/smpmodels/efficientnet_pytorch-0.6.3.tar.gz /tmp/pip/cache/efficientnet_pytorch-0.6.3.tar.gz\n\n!cp ../input/smpmodels/segmentation_models_pytorch-0.2.1-py3-none-any.whl /tmp/pip/cache/\n\n\n!pip install ../input/smpmodels/timm-0.4.12-py3-none-any.whl\n!pip install --no-index --find-links /tmp/pip/cache/ segmentation-models-pytorch","metadata":{"execution":{"iopub.status.busy":"2023-07-09T09:43:53.559194Z","iopub.execute_input":"2023-07-09T09:43:53.559552Z","iopub.status.idle":"2023-07-09T09:44:45.831679Z","shell.execute_reply.started":"2023-07-09T09:43:53.559520Z","shell.execute_reply":"2023-07-09T09:44:45.830471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!mkdir /kaggle/working/packages\n!cp -r /kaggle/input/pycocotools/* /kaggle/working/packages\nos.chdir(\"/kaggle/working/packages/pycocotools-2.0.6/\")\n!python setup.py install\n!pip install . --no-index --find-links /kaggle/working/packages/\nos.chdir(\"/kaggle/working\")\nfrom pycocotools import _mask as coco_mask","metadata":{"execution":{"iopub.status.busy":"2023-07-09T09:44:45.835113Z","iopub.execute_input":"2023-07-09T09:44:45.835522Z","iopub.status.idle":"2023-07-09T09:45:34.636236Z","shell.execute_reply.started":"2023-07-09T09:44:45.835482Z","shell.execute_reply":"2023-07-09T09:45:34.635019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Files \n__SAMPLE_SBMISSION_PATH = \"/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv\"\n__TILE_META_PATH = \"/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv\"\n__WSI_META_PATH = \"/kaggle/input/hubmap-hacking-the-human-vasculature/wsi_meta.csv\"\n__ANNOTATION_PATH = \"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\"\n\n# Folders\n__TRAIN_PATH = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train\"\n__TEST_PATH = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test\"","metadata":{"execution":{"iopub.status.busy":"2023-07-09T09:45:34.639328Z","iopub.execute_input":"2023-07-09T09:45:34.639728Z","iopub.status.idle":"2023-07-09T09:45:34.651182Z","shell.execute_reply.started":"2023-07-09T09:45:34.639692Z","shell.execute_reply":"2023-07-09T09:45:34.649948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import segmentation_models_pytorch as smp","metadata":{"execution":{"iopub.status.busy":"2023-07-09T09:45:34.652669Z","iopub.execute_input":"2023-07-09T09:45:34.653337Z","iopub.status.idle":"2023-07-09T09:45:35.924207Z","shell.execute_reply.started":"2023-07-09T09:45:34.653306Z","shell.execute_reply":"2023-07-09T09:45:35.923213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')","metadata":{"execution":{"iopub.status.busy":"2023-07-09T09:45:35.925828Z","iopub.execute_input":"2023-07-09T09:45:35.926205Z","iopub.status.idle":"2023-07-09T09:45:35.957200Z","shell.execute_reply.started":"2023-07-09T09:45:35.926170Z","shell.execute_reply":"2023-07-09T09:45:35.956105Z"},"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":"2023-07-09T09:45:35.959009Z","iopub.execute_input":"2023-07-09T09:45:35.959721Z","iopub.status.idle":"2023-07-09T09:45:35.974557Z","shell.execute_reply.started":"2023-07-09T09:45:35.959687Z","shell.execute_reply":"2023-07-09T09:45:35.973526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_model = smp.Unet(encoder_name='se_resnext50_32x4d',encoder_weights=None)\n_model.to(device)\nmodel_path = '/kaggle/input/hubmap-checkpoint/model (3).pth'\nprint(model_path)\nstate = torch.load(model_path, map_location=device)\n_model.load_state_dict(state)\n_model.eval()","metadata":{"execution":{"iopub.status.busy":"2023-07-09T09:46:31.633788Z","iopub.execute_input":"2023-07-09T09:46:31.634731Z","iopub.status.idle":"2023-07-09T09:46:33.807362Z","shell.execute_reply.started":"2023-07-09T09:46:31.634688Z","shell.execute_reply":"2023-07-09T09:46:33.806421Z"},"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            image = self.__get_image(path)\n            image = image.to(device)\n            image = image.unsqueeze(0)\n            masks = self.__forward(image)\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: torch.tensor):\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: torch.tensor, prediction_string: str) -> str:\n        if torch.is_tensor(masks):\n            for outputs in masks:\n                mask = outputs.reshape(-1,512,512)\n                mask = (mask > 0.55).cpu().numpy()\n                base64_str = self.__encoder(mask)\n                confidence = 1.0\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) -> torch.Tensor:\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":"2023-07-09T09:48:38.344434Z","iopub.execute_input":"2023-07-09T09:48:38.345112Z","iopub.status.idle":"2023-07-09T09:48:38.371839Z","shell.execute_reply.started":"2023-07-09T09:48:38.345074Z","shell.execute_reply":"2023-07-09T09:48:38.370848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = Submission(dirpath=__TEST_PATH, model=_model)\nsub.submit()","metadata":{"execution":{"iopub.status.busy":"2023-07-09T09:48:41.104602Z","iopub.execute_input":"2023-07-09T09:48:41.105542Z","iopub.status.idle":"2023-07-09T09:48:41.360474Z","shell.execute_reply.started":"2023-07-09T09:48:41.105499Z","shell.execute_reply":"2023-07-09T09:48:41.358919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-09T09:46:52.332640Z","iopub.execute_input":"2023-07-09T09:46:52.333018Z","iopub.status.idle":"2023-07-09T09:46:52.347617Z","shell.execute_reply.started":"2023-07-09T09:46:52.332988Z","shell.execute_reply":"2023-07-09T09:46:52.346475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}