{"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\nfrom skimage.morphology import binary_dilation\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-07-02T04:42:25.517605Z","iopub.execute_input":"2023-07-02T04:42:25.518053Z","iopub.status.idle":"2023-07-02T04:42:32.474165Z","shell.execute_reply.started":"2023-07-02T04:42:25.518027Z","shell.execute_reply":"2023-07-02T04:42:32.473187Z"},"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":{"scrolled":true,"execution":{"iopub.status.busy":"2023-07-02T04:42:32.477290Z","iopub.execute_input":"2023-07-02T04:42:32.478097Z","iopub.status.idle":"2023-07-02T04:43:26.861789Z","shell.execute_reply.started":"2023-07-02T04:42:32.478055Z","shell.execute_reply":"2023-07-02T04:43:26.860441Z"},"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-07-02T04:43:26.864588Z","iopub.execute_input":"2023-07-02T04:43:26.866008Z","iopub.status.idle":"2023-07-02T04:43:26.885866Z","shell.execute_reply.started":"2023-07-02T04:43:26.865893Z","shell.execute_reply":"2023-07-02T04:43:26.884560Z"},"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-07-02T04:43:26.889266Z","iopub.execute_input":"2023-07-02T04:43:26.891023Z","iopub.status.idle":"2023-07-02T04:43:26.900918Z","shell.execute_reply.started":"2023-07-02T04:43:26.890985Z","shell.execute_reply":"2023-07-02T04:43:26.899931Z"},"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-07-02T04:43:26.902500Z","iopub.execute_input":"2023-07-02T04:43:26.903046Z","iopub.status.idle":"2023-07-02T04:43:27.178419Z","shell.execute_reply.started":"2023-07-02T04:43:26.903011Z","shell.execute_reply":"2023-07-02T04:43:27.177431Z"},"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-07-02T04:43:27.179640Z","iopub.execute_input":"2023-07-02T04:43:27.180014Z","iopub.status.idle":"2023-07-02T04:43:27.199739Z","shell.execute_reply.started":"2023-07-02T04:43:27.179975Z","shell.execute_reply":"2023-07-02T04:43:27.198867Z"},"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-07-02T04:43:27.201059Z","iopub.execute_input":"2023-07-02T04:43:27.201448Z","iopub.status.idle":"2023-07-02T04:43:27.226873Z","shell.execute_reply.started":"2023-07-02T04:43:27.201385Z","shell.execute_reply":"2023-07-02T04:43:27.226042Z"},"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-02T04:43:27.228359Z","iopub.execute_input":"2023-07-02T04:43:27.228798Z","iopub.status.idle":"2023-07-02T04:43:27.298111Z","shell.execute_reply.started":"2023-07-02T04:43:27.228767Z","shell.execute_reply":"2023-07-02T04:43:27.296958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**code for the MASK RCNN CASCADING**","metadata":{}},{"cell_type":"code","source":"import torch\nimport numpy as np\n\ndef calculate_iou(masks1, masks2):\n    # Flatten masks\n    masks1 = masks1.view(masks1.size(0), -1)\n    masks2 = masks2.view(masks2.size(0), -1)\n\n    num_instances1 = masks1.size(0)\n\n    iou = torch.zeros(num_instances1)\n\n    for i in range(num_instances1):\n        intersection = torch.logical_and(masks1[i], masks2[0]).sum().float()\n        union = torch.logical_or(masks1[i], masks2[0]).sum().float()\n\n        iou[i] = intersection / union\n\n    return iou\n","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:27.299847Z","iopub.execute_input":"2023-07-02T04:43:27.300522Z","iopub.status.idle":"2023-07-02T04:43:27.309250Z","shell.execute_reply.started":"2023-07-02T04:43:27.300484Z","shell.execute_reply":"2023-07-02T04:43:27.308182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:27.315411Z","iopub.execute_input":"2023-07-02T04:43:27.316088Z","iopub.status.idle":"2023-07-02T04:43:27.320707Z","shell.execute_reply.started":"2023-07-02T04:43:27.316056Z","shell.execute_reply":"2023-07-02T04:43:27.319652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def maskrcnn_cascade_ensemble(models, image):\n    # Initialize ensemble predictions with the first model's predictions\n    models[0] = models[0].cuda()\n    ensemble_predictions = models[0](image)\n\n    # Perform ensemble predictions for each subsequent model\n    for i in range(1, len(models)):\n        model = models[i]\n        model = model.cuda()\n\n        # Get the predictions from the current model\n        predictions = model(image)\n\n        # Iterate over each prediction and update the ensemble predictions\n        for key in [0]:\n            # Perform the ensemble update only for overlapping instances\n            masks = predictions[key]['masks'].cpu()\n            scores = predictions[key]['scores'].cpu()\n            boxes = predictions[key]['boxes'].cpu()\n            labels = predictions[key]['labels'].cpu()\n#             print(masks.shape)\n\n            # Iterate over each instance in the ensemble predictions\n            for j in range(len(ensemble_predictions[key]['masks'])):\n                ensemble_mask = ensemble_predictions[key]['masks'][j].cpu()\n                ensemble_score = ensemble_predictions[key]['scores'][j].cpu()\n                ensemble_box = ensemble_predictions[key]['boxes'][j].cpu()\n                ensemble_label = ensemble_predictions[key]['labels'][j].cpu()\n\n                # Calculate IoU between the ensemble mask and the current instance masks\n                iou = calculate_iou(masks, ensemble_mask)\n\n                # Find the best matching instance in terms of IoU\n                best_match_index = iou.argmax()\n\n                if iou[best_match_index] > 0.6:\n                    # Update the ensemble instance with the current instance's information\n                    # Update the ensemble instance with the current instance's information\n                    ensemble_mask = masks[best_match_index] if scores[best_match_index] > ensemble_score else ensemble_mask\n                    ensemble_score = max(scores[best_match_index], ensemble_score)\n                    ensemble_box = boxes[best_match_index] if scores[best_match_index] > ensemble_score else ensemble_box\n                    ensemble_label = labels[best_match_index] if scores[best_match_index] > ensemble_score else ensemble_label\n                    \n                # Update the ensemble predictions with the updated instance\n                ensemble_predictions[key]['masks'][j] = ensemble_mask\n                ensemble_predictions[key]['scores'][j] = ensemble_score\n                ensemble_predictions[key]['boxes'][j] = ensemble_box\n                ensemble_predictions[key]['labels'][j] = ensemble_label\n                \n\n# #         predictions = predictions.cpu()\n#         masks= masks.to('cpu')\n#         labels=labels.to('cpu')\n#         boxes= boxes.to('cpu')\n#         scores= scores.to('cpu')\n#         ensemble_mask= ensemble_mask.to('cpu')\n#         ensemble_label=ensemble_label.to('cpu')\n#         ensemble_box= ensemble_box.to('cpu')\n#         ensemble_score= ensemble_score.to('cpu')\n        \n        del predictions, masks, labels, boxes , scores, ensemble_mask, ensemble_label, ensemble_box , ensemble_score\n        torch.cuda.empty_cache()\n        gc.collect()\n\n        # Empty the CUDA cache\n        torch.cuda.empty_cache()\n        \n    return ensemble_predictions","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:27.322024Z","iopub.execute_input":"2023-07-02T04:43:27.322340Z","iopub.status.idle":"2023-07-02T04:43:27.338899Z","shell.execute_reply.started":"2023-07-02T04:43:27.322315Z","shell.execute_reply":"2023-07-02T04:43:27.337904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Number of classes for the instance segmentation task\nnum_classes = 2\n# Number of folds\nnum_folds = 2\n","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:27.340512Z","iopub.execute_input":"2023-07-02T04:43:27.340909Z","iopub.status.idle":"2023-07-02T04:43:27.352029Z","shell.execute_reply.started":"2023-07-02T04:43:27.340862Z","shell.execute_reply":"2023-07-02T04:43:27.351076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\n","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:27.353240Z","iopub.execute_input":"2023-07-02T04:43:27.353729Z","iopub.status.idle":"2023-07-02T04:43:27.362365Z","shell.execute_reply.started":"2023-07-02T04:43:27.353697Z","shell.execute_reply":"2023-07-02T04:43:27.361435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load and initialize the trained models for each fold\nmodels = []\nfor fold in range(num_folds):\n    if(fold==0):\n        model = get_model_instance_segmentation(num_classes)\n        model.load_state_dict(torch.load(f'/kaggle/input/hubmap-well-tuned/well_tuned_weight.pth'))\n        model.eval()\n#     model= model.cuda()\n        model = nn.DataParallel(model, device_ids=[0,1])\n    \n        models.append(model)\n        \n    if(fold==1):\n        model = get_model_instance_segmentation(num_classes)\n        model.load_state_dict(torch.load(f'/kaggle/input/hubmap-train/fold_0_epoch29.pth'))\n        model.eval()\n#     model= model.cuda()\n        model = nn.DataParallel(model, device_ids=[0,1])\n    \n        models.append(model)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:27.364371Z","iopub.execute_input":"2023-07-02T04:43:27.364778Z","iopub.status.idle":"2023-07-02T04:43:34.975582Z","shell.execute_reply.started":"2023-07-02T04:43:27.364745Z","shell.execute_reply":"2023-07-02T04:43:34.974519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Perform Mask-RCNN Cascade ensemble predictions\n# ensemble_predictions = maskrcnn_cascade_ensemble(models, image)\n\n# # Access the ensemble predictions\n# boxes = ensemble_predictions['boxes']\n# labels = ensemble_predictions['labels']\n# masks = ensemble_predictions['masks']\n# scores = ensemble_predictions['scores']","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:34.977391Z","iopub.execute_input":"2023-07-02T04:43:34.977800Z","iopub.status.idle":"2023-07-02T04:43:34.982675Z","shell.execute_reply.started":"2023-07-02T04:43:34.977766Z","shell.execute_reply":"2023-07-02T04:43:34.981524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def load_fold_model(num_classes, fold):\n#     model = get_model_instance_segmentation(num_classes)\n#     model.load_state_dict(torch.load(f'/kaggle/input/hubmap-train-5folds/fold_{fold}_epoch5.pth'))\n#     model.eval()\n#     return model","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:34.984569Z","iopub.execute_input":"2023-07-02T04:43:34.985388Z","iopub.status.idle":"2023-07-02T04:43:34.992473Z","shell.execute_reply.started":"2023-07-02T04:43:34.985352Z","shell.execute_reply":"2023-07-02T04:43:34.991498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def ensemble_prediction(input_data, ensemble_models):\n#     predictions_masks = []\n#     predictions_scores = []\n\n#     for model in ensemble_models:\n#         model.to(device)\n#         model.eval()\n#         with torch.no_grad():\n#             output = model(input_data)\n# #         print(output[0]['masks'].shape)\n#         predictions_masks.append(torch.Tensor(output[0]['masks']))\n#         predictions_scores.append(torch.Tensor(output[0]['scores']))\n# #     print(len(predictions_masks))\n#     # Aggregate the predictions (e.g., take the average or majority vote)\n# #     ensemble_output_masks = torch.mean(torch.stack(predictions_masks), dim=0)\n# #     ensemble_output_scores = torch.mean(torch.stack(predictions_scores), dim=0)\n#     ensemble_output_masks = torch.cat(predictions_masks, dim=0)\n#     ensemble_output_scores = torch.cat(predictions_scores, dim=0)    \n\n#     return ensemble_output_masks , ensemble_output_scores","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:34.995907Z","iopub.execute_input":"2023-07-02T04:43:34.996173Z","iopub.status.idle":"2023-07-02T04:43:35.004180Z","shell.execute_reply.started":"2023-07-02T04:43:34.996150Z","shell.execute_reply":"2023-07-02T04:43:35.002913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# # Create a list to store the ensemble models\n# ensemble_models = []\n\n# # Load each fold model and add it to the ensemble\n# for fold in range(num_folds):\n#     model = load_fold_model(num_classes, fold)\n#     ensemble_models.append(model)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.005922Z","iopub.execute_input":"2023-07-02T04:43:35.006481Z","iopub.status.idle":"2023-07-02T04:43:35.018183Z","shell.execute_reply.started":"2023-07-02T04:43:35.006449Z","shell.execute_reply":"2023-07-02T04:43:35.017221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(ensemble_models)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.019584Z","iopub.execute_input":"2023-07-02T04:43:35.020162Z","iopub.status.idle":"2023-07-02T04:43:35.029056Z","shell.execute_reply.started":"2023-07-02T04:43:35.020129Z","shell.execute_reply":"2023-07-02T04:43:35.028031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = build_ensemble_model()\n# model.to(device)\n# # model.load_state_dict(torch.load('/kaggle/input/5folds-trained-usme-fold0/fold_0_epoch5.pth'))\n# # model.eval()\n# # print()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.030558Z","iopub.execute_input":"2023-07-02T04:43:35.031064Z","iopub.status.idle":"2023-07-02T04:43:35.038632Z","shell.execute_reply.started":"2023-07-02T04:43:35.031032Z","shell.execute_reply":"2023-07-02T04:43:35.037611Z"},"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-07-02T04:43:35.040133Z","iopub.execute_input":"2023-07-02T04:43:35.040554Z","iopub.status.idle":"2023-07-02T04:43:35.055176Z","shell.execute_reply.started":"2023-07-02T04:43:35.040521Z","shell.execute_reply":"2023-07-02T04:43:35.054220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nheights = []\nwidths = []\nprediction_strings = []","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.056644Z","iopub.execute_input":"2023-07-02T04:43:35.057232Z","iopub.status.idle":"2023-07-02T04:43:35.061786Z","shell.execute_reply.started":"2023-07-02T04:43:35.057199Z","shell.execute_reply":"2023-07-02T04:43:35.060673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# type(img)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.063429Z","iopub.execute_input":"2023-07-02T04:43:35.063899Z","iopub.status.idle":"2023-07-02T04:43:35.072645Z","shell.execute_reply.started":"2023-07-02T04:43:35.063867Z","shell.execute_reply":"2023-07-02T04:43:35.071556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for img, idx in test_dl:\n#     img = img.to(device)\n#     pred = ensemble_prediction(img, ensemble_models)\n#     break","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.074245Z","iopub.execute_input":"2023-07-02T04:43:35.074663Z","iopub.status.idle":"2023-07-02T04:43:35.081364Z","shell.execute_reply.started":"2023-07-02T04:43:35.074611Z","shell.execute_reply":"2023-07-02T04:43:35.080546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pred[1].shape","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.082990Z","iopub.execute_input":"2023-07-02T04:43:35.083473Z","iopub.status.idle":"2023-07-02T04:43:35.090902Z","shell.execute_reply.started":"2023-07-02T04:43:35.083440Z","shell.execute_reply":"2023-07-02T04:43:35.089877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pred[1].shape","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.092445Z","iopub.execute_input":"2023-07-02T04:43:35.092871Z","iopub.status.idle":"2023-07-02T04:43:35.100501Z","shell.execute_reply.started":"2023-07-02T04:43:35.092841Z","shell.execute_reply":"2023-07-02T04:43:35.099357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mask=pred[0]['masks'][0].detach().permute(1,2,0).cpu().numpy()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.102327Z","iopub.execute_input":"2023-07-02T04:43:35.102733Z","iopub.status.idle":"2023-07-02T04:43:35.110542Z","shell.execute_reply.started":"2023-07-02T04:43:35.102704Z","shell.execute_reply":"2023-07-02T04:43:35.109491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mask = np.where(mask>0.5, 1, 0).astype(np.bool)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.118635Z","iopub.execute_input":"2023-07-02T04:43:35.118891Z","iopub.status.idle":"2023-07-02T04:43:35.122927Z","shell.execute_reply.started":"2023-07-02T04:43:35.118869Z","shell.execute_reply":"2023-07-02T04:43:35.121865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# score = pred[0]['scores'][0].detach().cpu().numpy()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.124860Z","iopub.execute_input":"2023-07-02T04:43:35.125528Z","iopub.status.idle":"2023-07-02T04:43:35.134542Z","shell.execute_reply.started":"2023-07-02T04:43:35.125493Z","shell.execute_reply":"2023-07-02T04:43:35.133682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# encoded = encode_binary_mask(mask)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.136186Z","iopub.execute_input":"2023-07-02T04:43:35.136617Z","iopub.status.idle":"2023-07-02T04:43:35.143443Z","shell.execute_reply.started":"2023-07-02T04:43:35.136533Z","shell.execute_reply":"2023-07-02T04:43:35.142531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# encoded.decode('utf-8')","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.145282Z","iopub.execute_input":"2023-07-02T04:43:35.145751Z","iopub.status.idle":"2023-07-02T04:43:35.153016Z","shell.execute_reply.started":"2023-07-02T04:43:35.145715Z","shell.execute_reply":"2023-07-02T04:43:35.152110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open('/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl', 'r') as json_file:\n#     json_list = list(json_file)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.154753Z","iopub.execute_input":"2023-07-02T04:43:35.155159Z","iopub.status.idle":"2023-07-02T04:43:35.162258Z","shell.execute_reply.started":"2023-07-02T04:43:35.155128Z","shell.execute_reply":"2023-07-02T04:43:35.161202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tiles_dicts = []\n# for json_str in json_list:\n#     tiles_dicts.append(json.loads(json_str))","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.163423Z","iopub.execute_input":"2023-07-02T04:43:35.163734Z","iopub.status.idle":"2023-07-02T04:43:35.172031Z","shell.execute_reply.started":"2023-07-02T04:43:35.163708Z","shell.execute_reply":"2023-07-02T04:43:35.170970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tile_meta_df = pd.read_csv(\"/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.173386Z","iopub.execute_input":"2023-07-02T04:43:35.173855Z","iopub.status.idle":"2023-07-02T04:43:35.181820Z","shell.execute_reply.started":"2023-07-02T04:43:35.173804Z","shell.execute_reply":"2023-07-02T04:43:35.180936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # mask = np.zeros((512, 512), dtype=np.float32)\n# for annot in tiles_dicts[0]['annotations']:\n#     cords = annot['coordinates']\n#     if annot['type'] == \"blood_vessel\":\n#         for cd in cords:\n#             rr, cc = np.array([i[1] for i in cd]), np.asarray([i[0] for i in cd])\n#             mask[rr, cc] = 1\n            \n# plt.imshow(mask)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.183709Z","iopub.execute_input":"2023-07-02T04:43:35.184181Z","iopub.status.idle":"2023-07-02T04:43:35.191197Z","shell.execute_reply.started":"2023-07-02T04:43:35.184147Z","shell.execute_reply":"2023-07-02T04:43:35.190389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Access the ensemble predictions\n# boxes = ensemble_predictions['boxes']\n# labels = ensemble_predictions['labels']\n# masks = ensemble_predictions['masks']\n# scores = ensemble_predictions['scores']","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.193083Z","iopub.execute_input":"2023-07-02T04:43:35.193436Z","iopub.status.idle":"2023-07-02T04:43:35.200024Z","shell.execute_reply.started":"2023-07-02T04:43:35.193404Z","shell.execute_reply":"2023-07-02T04:43:35.199269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\n# Get the current GPU memory usage\nallocated_memory = torch.cuda.memory_allocated()\n\nprint(\"Current GPU memory usage:\", allocated_memory)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:35.201263Z","iopub.execute_input":"2023-07-02T04:43:35.202257Z","iopub.status.idle":"2023-07-02T04:43:35.211739Z","shell.execute_reply.started":"2023-07-02T04:43:35.202224Z","shell.execute_reply":"2023-07-02T04:43:35.210762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img, idx in test_dl:\n    \n    img= img.cuda()\n    ensemble_predictions = maskrcnn_cascade_ensemble(models, img)\n    \n    \n    pred_string = ''\n#     fig, ax = plt.subplots()\n    for m in range(len(ensemble_predictions[0]['masks'])):\n        mask = ensemble_predictions[0]['masks'][m].detach().permute(1,2,0).cpu().numpy()\n#         if(m!=0 and ensemble_predictions[0]['scores'][m]<0.90):\n#             continue\n        mask = np.where(mask>0.2, 1, 0).astype(np.bool)\n        mask= binary_dilation(mask)\n        \n#         mask= mask.reshape(512,512)\n#         ax.plot(mask)\n        score = ensemble_predictions[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#         m+=15\n#     plt.show()\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)\n    \n#     img=img.cpu()\n    del img, ensemble_predictions\n    torch.cuda.empty_cache()\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:44:13.515940Z","iopub.execute_input":"2023-07-02T04:44:13.517000Z","iopub.status.idle":"2023-07-02T04:44:16.413699Z","shell.execute_reply.started":"2023-07-02T04:44:13.516961Z","shell.execute_reply":"2023-07-02T04:44:16.412436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# array = tiff.imread('/kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif')\n# plt.imshow(array)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:44:05.026353Z","iopub.execute_input":"2023-07-02T04:44:05.026740Z","iopub.status.idle":"2023-07-02T04:44:05.031449Z","shell.execute_reply.started":"2023-07-02T04:44:05.026706Z","shell.execute_reply":"2023-07-02T04:44:05.030369Z"},"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(1,len(sample[0]['masks'])))]\n# img = 0\n# for i in top10:\n#     img += i\n#     img = np.clip(img, 0, 1)\n# # img/=10\n# plt.imshow(img)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:44:05.033076Z","iopub.execute_input":"2023-07-02T04:44:05.033755Z","iopub.status.idle":"2023-07-02T04:44:05.042389Z","shell.execute_reply.started":"2023-07-02T04:44:05.033721Z","shell.execute_reply":"2023-07-02T04:44:05.041489Z"},"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-07-02T04:44:05.045973Z","iopub.execute_input":"2023-07-02T04:44:05.046293Z","iopub.status.idle":"2023-07-02T04:44:05.068426Z","shell.execute_reply.started":"2023-07-02T04:44:05.046267Z","shell.execute_reply":"2023-07-02T04:44:05.067407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_string.count(\"0 \")","metadata":{"execution":{"iopub.status.busy":"2023-07-02T04:43:55.058773Z","iopub.execute_input":"2023-07-02T04:43:55.059149Z","iopub.status.idle":"2023-07-02T04:43:55.066350Z","shell.execute_reply.started":"2023-07-02T04:43:55.059113Z","shell.execute_reply":"2023-07-02T04:43:55.065254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}