{"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 os, glob\nimport sys\nimport json\nfrom PIL import Image\nfrom collections import Counter\n\nimport numpy as np\nimport pandas as pd\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport torch\nimport cv2\n\nimport pandas as pd\n\nfrom sklearn.model_selection import KFold\n\nsys.path.append(\"/kaggle/input/detection-wheel\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-18T07:15:27.324132Z","iopub.execute_input":"2023-06-18T07:15:27.324607Z","iopub.status.idle":"2023-06-18T07:15:31.956333Z","shell.execute_reply.started":"2023-06-18T07:15:27.324567Z","shell.execute_reply":"2023-06-18T07:15:31.955286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install pycocotools package\nimport os\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 -q\n!pip install . --no-index --find-links /kaggle/working/packages/ -q\nos.chdir(\"/kaggle/working\")","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:15:31.958150Z","iopub.execute_input":"2023-06-18T07:15:31.960009Z","iopub.status.idle":"2023-06-18T07:16:22.883587Z","shell.execute_reply.started":"2023-06-18T07:15:31.959953Z","shell.execute_reply":"2023-06-18T07:16:22.882417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:22.885707Z","iopub.execute_input":"2023-06-18T07:16:22.886083Z","iopub.status.idle":"2023-06-18T07:16:23.949399Z","shell.execute_reply.started":"2023-06-18T07:16:22.886045Z","shell.execute_reply":"2023-06-18T07:16:23.948214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\n\ndef 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-06-18T07:16:23.953174Z","iopub.execute_input":"2023-06-18T07:16:23.953550Z","iopub.status.idle":"2023-06-18T07:16:23.970961Z","shell.execute_reply.started":"2023-06-18T07:16:23.953514Z","shell.execute_reply":"2023-06-18T07:16:23.970066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport torch\nfrom PIL import Image\n\n\nclass PennFudanDataset(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 = tiff.imread(img_path)\n        img = Image.fromarray(array)\n        \n        img, _ = self.transforms(img, img)\n\n        return img, name\n\n    def __len__(self):\n        return len(self.imgs)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:23.972449Z","iopub.execute_input":"2023-06-18T07:16:23.972875Z","iopub.status.idle":"2023-06-18T07:16:23.982408Z","shell.execute_reply.started":"2023-06-18T07:16:23.972842Z","shell.execute_reply":"2023-06-18T07:16:23.981243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\nimport torchvision\nfrom torchvision.models.detection.faster_rcnn import FastRCNNPredictor\nfrom torchvision.models.detection.mask_rcnn import MaskRCNNPredictor\n\ndef get_model_instance_segmentation(num_classes):\n    # load an instance segmentation model pre-trained on COCO\n    model = torchvision.models.detection.maskrcnn_resnet50_fpn_v2(weights=None, weights_backbone=None)\n\n    # get number of input features for the classifier\n    in_features = model.roi_heads.box_predictor.cls_score.in_features\n    # replace the pre-trained head with a new one\n    model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)\n\n    # now get the number of input features for the mask classifier\n    in_features_mask = model.roi_heads.mask_predictor.conv5_mask.in_channels\n    hidden_layer = 256\n    # and replace the mask predictor with a new one\n    model.roi_heads.mask_predictor = MaskRCNNPredictor(in_features_mask,\n                                                       hidden_layer,\n                                                       num_classes)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:23.984004Z","iopub.execute_input":"2023-06-18T07:16:23.984716Z","iopub.status.idle":"2023-06-18T07:16:24.201030Z","shell.execute_reply.started":"2023-06-18T07:16:23.984685Z","shell.execute_reply":"2023-06-18T07:16:24.200052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import transforms as T\n\ndef 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":"2023-06-18T07:16:24.202756Z","iopub.execute_input":"2023-06-18T07:16:24.203071Z","iopub.status.idle":"2023-06-18T07:16:24.221930Z","shell.execute_reply.started":"2023-06-18T07:16:24.203040Z","shell.execute_reply":"2023-06-18T07:16:24.221132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from engine import train_one_epoch, evaluate\nimport utils","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:24.223439Z","iopub.execute_input":"2023-06-18T07:16:24.223757Z","iopub.status.idle":"2023-06-18T07:16:24.245961Z","shell.execute_reply.started":"2023-06-18T07:16:24.223727Z","shell.execute_reply":"2023-06-18T07:16:24.245126Z"},"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-06-18T07:16:24.248421Z","iopub.execute_input":"2023-06-18T07:16:24.248929Z","iopub.status.idle":"2023-06-18T07:16:24.284813Z","shell.execute_reply.started":"2023-06-18T07:16:24.248895Z","shell.execute_reply":"2023-06-18T07:16:24.283679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model_instance_segmentation(num_classes=2)\nmodel.to(device)\nmodel.load_state_dict(torch.load('/kaggle/input/hubmap-train/fold_0_epoch29.pth'))\nmodel.eval()\nprint()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:24.290551Z","iopub.execute_input":"2023-06-18T07:16:24.292554Z","iopub.status.idle":"2023-06-18T07:16:30.411981Z","shell.execute_reply.started":"2023-06-18T07:16:24.292520Z","shell.execute_reply":"2023-06-18T07:16:30.411030Z"},"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 = PennFudanDataset(all_imgs, get_transform(train=False))\ntest_dl = torch.utils.data.DataLoader(\n        dataset_test, batch_size=1, shuffle=False, num_workers=os.cpu_count(), pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:30.413226Z","iopub.execute_input":"2023-06-18T07:16:30.414017Z","iopub.status.idle":"2023-06-18T07:16:30.422926Z","shell.execute_reply.started":"2023-06-18T07:16:30.413983Z","shell.execute_reply":"2023-06-18T07:16:30.422004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nheights = []\nwidths = []\nprediction_strings = []","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:30.424646Z","iopub.execute_input":"2023-06-18T07:16:30.424997Z","iopub.status.idle":"2023-06-18T07:16:30.430001Z","shell.execute_reply.started":"2023-06-18T07:16:30.424966Z","shell.execute_reply":"2023-06-18T07:16:30.428926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = None\nfor img, idx in test_dl:\n    img = img.to(device)\n    pred = model(img)\n    if sample is None: sample=pred\n    pred_string = ''\n    for m in range(len(pred[0]['masks'])):\n        mask = pred[0]['masks'][m].detach().permute(1,2,0).cpu().numpy()\n        mask = np.where(mask>0.5, 1, 0).astype(np.bool)\n        score = pred[0]['scores'][m].detach().cpu().numpy()\n        encoded = encode_binary_mask(mask)\n        if m==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-06-18T07:16:30.431560Z","iopub.execute_input":"2023-06-18T07:16:30.432045Z","iopub.status.idle":"2023-06-18T07:16:34.831229Z","shell.execute_reply.started":"2023-06-18T07:16:30.431873Z","shell.execute_reply":"2023-06-18T07:16:34.830115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"array = tiff.imread('/kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif')\nplt.imshow(array)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:34.833113Z","iopub.execute_input":"2023-06-18T07:16:34.833510Z","iopub.status.idle":"2023-06-18T07:16:35.187678Z","shell.execute_reply.started":"2023-06-18T07:16:34.833468Z","shell.execute_reply":"2023-06-18T07:16:35.186838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:35.188755Z","iopub.execute_input":"2023-06-18T07:16:35.189109Z","iopub.status.idle":"2023-06-18T07:16:35.209840Z","shell.execute_reply.started":"2023-06-18T07:16:35.189073Z","shell.execute_reply":"2023-06-18T07:16:35.208924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top10 = [sample[0]['masks'][i].detach().permute(1,2,0).cpu().numpy() for i in range(min(10,len(sample[0]['masks'])))]\nimg = 0\nfor i in top10:\n    img += i\n    img = np.clip(img, 0, 1)\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:35.211130Z","iopub.execute_input":"2023-06-18T07:16:35.211833Z","iopub.status.idle":"2023-06-18T07:16:35.491158Z","shell.execute_reply.started":"2023-06-18T07:16:35.211792Z","shell.execute_reply":"2023-06-18T07:16:35.490250Z"},"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.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T07:16:35.492571Z","iopub.execute_input":"2023-06-18T07:16:35.493110Z","iopub.status.idle":"2023-06-18T07:16:35.525257Z","shell.execute_reply.started":"2023-06-18T07:16:35.493072Z","shell.execute_reply":"2023-06-18T07:16:35.524384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}