{"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":{"execution":{"iopub.status.busy":"2023-06-27T08:03:21.514019Z","iopub.execute_input":"2023-06-27T08:03:21.514622Z","iopub.status.idle":"2023-06-27T08:03:21.522497Z","shell.execute_reply.started":"2023-06-27T08:03:21.514571Z","shell.execute_reply":"2023-06-27T08:03:21.521453Z"},"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-27T08:03:21.524627Z","iopub.execute_input":"2023-06-27T08:03:21.525283Z","iopub.status.idle":"2023-06-27T08:04:00.178483Z","shell.execute_reply.started":"2023-06-27T08:03:21.525250Z","shell.execute_reply":"2023-06-27T08:04:00.177316Z"},"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-27T08:04:00.180185Z","iopub.execute_input":"2023-06-27T08:04:00.180559Z","iopub.status.idle":"2023-06-27T08:04:00.189534Z","shell.execute_reply.started":"2023-06-27T08:04:00.180514Z","shell.execute_reply":"2023-06-27T08:04:00.188634Z"},"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-27T08:04:00.192450Z","iopub.execute_input":"2023-06-27T08:04:00.193136Z","iopub.status.idle":"2023-06-27T08:04:00.218113Z","shell.execute_reply.started":"2023-06-27T08:04:00.193104Z","shell.execute_reply":"2023-06-27T08:04:00.217288Z"},"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-27T08:04:00.220217Z","iopub.execute_input":"2023-06-27T08:04:00.220984Z","iopub.status.idle":"2023-06-27T08:04:00.231841Z","shell.execute_reply.started":"2023-06-27T08:04:00.220949Z","shell.execute_reply":"2023-06-27T08:04:00.230889Z"},"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-27T08:04:00.234939Z","iopub.execute_input":"2023-06-27T08:04:00.235226Z","iopub.status.idle":"2023-06-27T08:04:00.242560Z","shell.execute_reply.started":"2023-06-27T08:04:00.235195Z","shell.execute_reply":"2023-06-27T08:04:00.241592Z"},"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-27T08:04:00.245552Z","iopub.execute_input":"2023-06-27T08:04:00.245809Z","iopub.status.idle":"2023-06-27T08:04:00.257129Z","shell.execute_reply.started":"2023-06-27T08:04:00.245787Z","shell.execute_reply":"2023-06-27T08:04:00.256219Z"},"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-27T08:04:00.260309Z","iopub.execute_input":"2023-06-27T08:04:00.260593Z","iopub.status.idle":"2023-06-27T08:04:00.268577Z","shell.execute_reply.started":"2023-06-27T08:04:00.260571Z","shell.execute_reply":"2023-06-27T08:04:00.267561Z"},"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-training/fold_0_epoch6.pth'))\nmodel.eval()\nprint()","metadata":{"execution":{"iopub.status.busy":"2023-06-27T08:04:00.270882Z","iopub.execute_input":"2023-06-27T08:04:00.271255Z","iopub.status.idle":"2023-06-27T08:04:01.247846Z","shell.execute_reply.started":"2023-06-27T08:04:00.271225Z","shell.execute_reply":"2023-06-27T08:04:01.246859Z"},"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-27T08:04:01.251435Z","iopub.execute_input":"2023-06-27T08:04:01.251728Z","iopub.status.idle":"2023-06-27T08:04:01.261086Z","shell.execute_reply.started":"2023-06-27T08:04:01.251703Z","shell.execute_reply":"2023-06-27T08:04:01.260000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nheights = []\nwidths = []\nprediction_strings = []","metadata":{"execution":{"iopub.status.busy":"2023-06-27T08:04:01.263404Z","iopub.execute_input":"2023-06-27T08:04:01.263737Z","iopub.status.idle":"2023-06-27T08:04:01.269418Z","shell.execute_reply.started":"2023-06-27T08:04:01.263713Z","shell.execute_reply":"2023-06-27T08:04:01.268388Z"},"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-27T08:04:01.270906Z","iopub.execute_input":"2023-06-27T08:04:01.271259Z","iopub.status.idle":"2023-06-27T08:04:06.708882Z","shell.execute_reply.started":"2023-06-27T08:04:01.271229Z","shell.execute_reply":"2023-06-27T08:04:06.706405Z"},"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-27T08:04:06.710595Z","iopub.execute_input":"2023-06-27T08:04:06.711243Z","iopub.status.idle":"2023-06-27T08:04:07.068746Z","shell.execute_reply.started":"2023-06-27T08:04:06.711197Z","shell.execute_reply":"2023-06-27T08:04:07.064644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"execution":{"iopub.status.busy":"2023-06-27T08:04:07.069745Z","iopub.execute_input":"2023-06-27T08:04:07.070055Z","iopub.status.idle":"2023-06-27T08:04:07.091762Z","shell.execute_reply.started":"2023-06-27T08:04:07.070025Z","shell.execute_reply":"2023-06-27T08:04:07.090771Z"},"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-27T08:04:07.093112Z","iopub.execute_input":"2023-06-27T08:04:07.093576Z","iopub.status.idle":"2023-06-27T08:04:07.352642Z","shell.execute_reply.started":"2023-06-27T08:04:07.093542Z","shell.execute_reply":"2023-06-27T08:04:07.351709Z"},"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-27T08:04:07.355812Z","iopub.execute_input":"2023-06-27T08:04:07.356517Z","iopub.status.idle":"2023-06-27T08:04:07.388813Z","shell.execute_reply.started":"2023-06-27T08:04:07.356484Z","shell.execute_reply":"2023-06-27T08:04:07.387973Z"},"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":[]}]}