{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\n\n# import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-02T01:39:56.448640Z","iopub.execute_input":"2023-07-02T01:39:56.449073Z","iopub.status.idle":"2023-07-02T01:39:56.463094Z","shell.execute_reply.started":"2023-07-02T01:39:56.449024Z","shell.execute_reply":"2023-07-02T01:39:56.461654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install --no-index --no-deps /kaggle/input/pycocotools206/wheels/*.whl","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:39:56.856726Z","iopub.execute_input":"2023-07-02T01:39:56.857440Z","iopub.status.idle":"2023-07-02T01:39:56.862290Z","shell.execute_reply.started":"2023-07-02T01:39:56.857403Z","shell.execute_reply":"2023-07-02T01:39:56.861127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nimport torch\n# from pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\nimport pandas as pd\nimport torchvision.transforms as T\nfrom torch.utils.data import Dataset\nimport cv2\nimport yaml\nimport json\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport glob","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:39:57.193579Z","iopub.execute_input":"2023-07-02T01:39:57.193945Z","iopub.status.idle":"2023-07-02T01:40:00.856239Z","shell.execute_reply.started":"2023-07-02T01:39:57.193898Z","shell.execute_reply":"2023-07-02T01:40:00.855238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if torch.cuda.is_available(): \n    device = \"cuda\" \nelse: \n    device = \"cpu\" ","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:00.858353Z","iopub.execute_input":"2023-07-02T01:40:00.859007Z","iopub.status.idle":"2023-07-02T01:40:00.888004Z","shell.execute_reply.started":"2023-07-02T01:40:00.858969Z","shell.execute_reply":"2023-07-02T01:40:00.887265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as transforms\n\nmean = [0.485, 0.456, 0.406]  # Mean values for RGB channels\nstd = [0.229, 0.224, 0.225]  # Standard deviation values for RGB channels\n\n# Define the transformation pipeline including normalization\ntransform = transforms.Compose([\n    transforms.ToTensor(),  # Convert the image to a tensor, also changes ordering of channels\n    transforms.Normalize(mean, std)  # Normalize the tensor\n])","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:00.889532Z","iopub.execute_input":"2023-07-02T01:40:00.890212Z","iopub.status.idle":"2023-07-02T01:40:00.903610Z","shell.execute_reply.started":"2023-07-02T01:40:00.890174Z","shell.execute_reply":"2023-07-02T01:40:00.902606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class HubMapDataset(torch.utils.data.Dataset):\n    def __init__ (self, test_path, transform, img_size=512): #Either img_size or string messes up\n         \n        self.all_test_img_paths = glob.glob(test_path+'*.tif')\n        self.transform= transform\n        self.img_size= img_size #512 if not working\n        self.name_indices = [os.path.splitext(os.path.basename(i))[0] for i in self.all_test_img_paths]\n        \n\n    def __len__(self):\n        return len(self.all_test_img_paths)\n        \n    def resize(self,img, interp):\n            return cv2.resize(img, (self.img_size, self.img_size), interpolation=interp)\n        \n    def __getitem__(self, idx):\n            img_path= self.all_test_img_paths[idx]\n            img= cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB) #return np array of pixels \n            #img = img/255 #normalize pixels (0-1)\n            \n            \n            if img.shape != (self.img_size, self.img_size, 3):\n                img = self.resize(img,cv2.INTER_NEAREST)\n                    \n            if self.transform:\n                img= transform(img)\n            \n            name = self.name_indices[idx]\n        \n            return img, name","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:00.907231Z","iopub.execute_input":"2023-07-02T01:40:00.908116Z","iopub.status.idle":"2023-07-02T01:40:00.917993Z","shell.execute_reply.started":"2023-07-02T01:40:00.908084Z","shell.execute_reply":"2023-07-02T01:40:00.916905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_PATH = '/kaggle/input/hubmap-hacking-the-human-vasculature/test/'\ntest_dataset = HubMapDataset(TEST_PATH, transform)\ntest_dl = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False)\ntest_dataset[0]","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:00.919246Z","iopub.execute_input":"2023-07-02T01:40:00.919747Z","iopub.status.idle":"2023-07-02T01:40:01.104855Z","shell.execute_reply.started":"2023-07-02T01:40:00.919713Z","shell.execute_reply":"2023-07-02T01:40:01.103926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import AutoImageProcessor, Mask2FormerConfig, Mask2FormerModel, Mask2FormerForUniversalSegmentation","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:01.106301Z","iopub.execute_input":"2023-07-02T01:40:01.106632Z","iopub.status.idle":"2023-07-02T01:40:10.935269Z","shell.execute_reply.started":"2023-07-02T01:40:01.106599Z","shell.execute_reply":"2023-07-02T01:40:10.934303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id2label = {0: 'unsure', 1: 'blood_vessel', 2: 'glomerulus'}\n\n# Initializing a Mask2Former facebook/mask2former-swin-small-coco-instance configuration\nconfiguration = Mask2FormerConfig(id2label=id2label)\n\n# Initializing a model (with random weights) from the facebook/mask2former-swin-small-coco-instance style configuration\nmodel = Mask2FormerForUniversalSegmentation(configuration).to('cuda')\n\n# Accessing the model configuration\nconfiguration = model.config\n\n# processor = AutoImageProcessor.from_pretrained(\"facebook/mask2former-swin-tiny-cityscapes-semantic\")","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:10.936746Z","iopub.execute_input":"2023-07-02T01:40:10.937103Z","iopub.status.idle":"2023-07-02T01:40:18.310957Z","shell.execute_reply.started":"2023-07-02T01:40:10.937068Z","shell.execute_reply":"2023-07-02T01:40:18.309031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"processor = AutoImageProcessor.from_pretrained(\"/kaggle/input/mask2former-swin-tiny-cityscapes-semantic/\")","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:18.317107Z","iopub.execute_input":"2023-07-02T01:40:18.319733Z","iopub.status.idle":"2023-07-02T01:40:18.344392Z","shell.execute_reply.started":"2023-07-02T01:40:18.319697Z","shell.execute_reply":"2023-07-02T01:40:18.343407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load('/kaggle/input/hubmap/50_epochs_model.pth', map_location=torch.device('cpu')))","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:18.349066Z","iopub.execute_input":"2023-07-02T01:40:18.351408Z","iopub.status.idle":"2023-07-02T01:40:21.692679Z","shell.execute_reply.started":"2023-07-02T01:40:18.351373Z","shell.execute_reply":"2023-07-02T01:40:21.691708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:21.696519Z","iopub.execute_input":"2023-07-02T01:40:21.698364Z","iopub.status.idle":"2023-07-02T01:40:21.701827Z","shell.execute_reply.started":"2023-07-02T01:40:21.698337Z","shell.execute_reply":"2023-07-02T01:40:21.700949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def get_pred_string(objs):\n\n#     string = \"\"\n#     for i, item in enumerate(objs):\n#         mask = np.zeros((512,512), np.bool8)\n#         mask[item>0] = 1\n#         encoded_mask = encode_binary_mask(mask).decode(\"utf-8\")\n\n#         if i == 0:\n#             string += f\"0 1.0 {encoded_mask}\"\n#         else:\n#             string += f\" 0 1.0 {encoded_mask}\"\n        \n#     return string","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:21.703371Z","iopub.execute_input":"2023-07-02T01:40:21.703805Z","iopub.status.idle":"2023-07-02T01:40:21.717299Z","shell.execute_reply.started":"2023-07-02T01:40:21.703771Z","shell.execute_reply":"2023-07-02T01:40:21.716213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class MyBestModel:\n#     @staticmethod\n#     def generate_masks(num_masks: int) -> list[dict, ...]:\n#         masks = []\n#         for _ in range(num_masks):\n#             mask = torch.randint(0, 2, (1, 512, 512))\n#             confidence = round(float(torch.rand(1)[0]), 2)\n#             masks.append({\"mask\": mask, \"confidence\": confidence})\n#         return masks\n        \n#     def __call__(self, image) -> list[dict, ...]:\n#         num_masks = torch.randint(1, 5, (1, 1))\n#         masks = self.generate_masks(num_masks)\n#         return masks","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:21.718818Z","iopub.execute_input":"2023-07-02T01:40:21.719208Z","iopub.status.idle":"2023-07-02T01:40:21.729942Z","shell.execute_reply.started":"2023-07-02T01:40:21.719174Z","shell.execute_reply":"2023-07-02T01:40:21.729025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.eval()\n# with torch.no_grad():\n#     outputs = model(torch.stack([transform(test_img).to(device).reshape(3,512,512)]))","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:21.731360Z","iopub.execute_input":"2023-07-02T01:40:21.731693Z","iopub.status.idle":"2023-07-02T01:40:21.741890Z","shell.execute_reply.started":"2023-07-02T01:40:21.731662Z","shell.execute_reply":"2023-07-02T01:40:21.740953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nheights = []\nwidths = []\nprediction_strings = []\npre_res = dict()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:40:21.743439Z","iopub.execute_input":"2023-07-02T01:40:21.743853Z","iopub.status.idle":"2023-07-02T01:40:21.753192Z","shell.execute_reply.started":"2023-07-02T01:40:21.743818Z","shell.execute_reply":"2023-07-02T01:40:21.752137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\nwith torch.no_grad():\n    for img, idx in test_dl:\n        print(img.shape)\n        print(idx)\n        img = img.to(device)\n        pred = model(img)\n        predicted_semantic_map = processor.post_process_instance_segmentation(pred, target_sizes=[(512,512)])[0]\n        print(predicted_semantic_map)\n        \n        pre_res[idx] = predicted_semantic_map\n        \n#         pred_string = ''\n        \n#         for d in predicted_semantic_map['segments_info']:\n#             mask = predicted_semantic_map['segmentation']==d['id']\n#             mask = mask.cpu().numpy().astype(np.bool)\n#             score = d['score']\n#             encoded = encode_binary_mask(mask)\n#             if d['id']==0:\n#                 pred_string += f\"0 {score} {encoded.decode('utf-8')}\"\n\n#             else:\n#                 pred_string += f\" 0 {score} {encoded.decode('utf-8')}\"\n        b, c, h, w = img.shape\n        ids.append(idx[0])\n        heights.append(h)\n        widths.append(w)\n#         prediction_strings.append(pred_string)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:42:51.900853Z","iopub.execute_input":"2023-07-02T01:42:51.901234Z","iopub.status.idle":"2023-07-02T01:42:52.057008Z","shell.execute_reply.started":"2023-07-02T01:42:51.901203Z","shell.execute_reply":"2023-07-02T01:42:52.055154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img= cv2.cvtColor(cv2.imread('/kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif'), cv2.COLOR_BGR2RGB) #return np array of pixels \nplt.imshow(img)\nplt.title(\"Image\")\nplt.show()\n\n# predicted_semantic_map = processor.post_process_instance_segmentation(outputs, target_sizes=[(512,512)])[0]\ncolor_segmentation_map = predicted_semantic_map['segmentation']\nfor d in predicted_semantic_map['segments_info']:\n    color_segmentation_map[color_segmentation_map==d['id']] = 50+50*d['label_id']\n#     color_segmentation_map = color_segmentation_map.abs()\n#     print(color_segmentation_map)\nplt.imshow(img.reshape(512,512,3))\nplt.imshow(color_segmentation_map, alpha=0.6)\nplt.title(\"Predictions\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T01:42:52.271094Z","iopub.execute_input":"2023-07-02T01:42:52.271469Z","iopub.status.idle":"2023-07-02T01:42:53.079731Z","shell.execute_reply.started":"2023-07-02T01:42:52.271437Z","shell.execute_reply":"2023-07-02T01:42:53.078881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip uninstall numpy\n!pip install --no-index --no-deps /kaggle/input/pycocotools206/wheels/*.whl","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pycocotools import _mask as coco_mask\nimport numpy as np","metadata":{"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 != 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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx in pre_res:\n\n    predicted_semantic_map = pre_res[idx]\n\n    pred_string = ''\n\n    for d in predicted_semantic_map['segments_info']:\n        print(d)\n        mask = predicted_semantic_map['segmentation']==d['id']\n        mask = mask.cpu().numpy().astype(np.bool)\n        score = d['score']\n        encoded = encode_binary_mask(mask)\n        if d['id']==0:\n            pred_string += f\"0 {score} {encoded.decode('utf-8')}\"\n\n        else:\n            pred_string += f\" 0 {score} {encoded.decode('utf-8')}\"\n#         print(pred_string)\n    prediction_strings.append(pred_string)","metadata":{"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\")\nsubmission","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicted_semantic_map = processor.post_process_instance_segmentation(outputs, target_sizes=[(512,512)])[0]\n# predicted_semantic_map['segmentation'][350]\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}