{"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-07-30T21:06:48.863269Z","iopub.execute_input":"2023-07-30T21:06:48.86364Z","iopub.status.idle":"2023-07-30T21:06:54.776809Z","shell.execute_reply.started":"2023-07-30T21:06:48.86361Z","shell.execute_reply":"2023-07-30T21:06:54.77586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\ntorch.__version__  #torch 2.0\n#!nvidia-smi   #CUDA Version: 11.4\n! ls /usr/local","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:06:54.778613Z","iopub.execute_input":"2023-07-30T21:06:54.779299Z","iopub.status.idle":"2023-07-30T21:06:55.729709Z","shell.execute_reply.started":"2023-07-30T21:06:54.779264Z","shell.execute_reply":"2023-07-30T21:06:55.728506Z"},"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\n# # Install mmcv and mmdet packages\n# #3.0\n# #!pip install mmcv mmdet --no-index --find-links /kaggle/input/mmdetection/ -q\n# #os.chdir(\"/kaggle/working\")\n# #ytt 2140\n# !pip install '/kaggle/input/mmdetectionv2140/addict-2.4.0-py3-none-any.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/yapf-0.31.0-py2.py3-none-any.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/terminal-0.4.0-py3-none-any.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/terminaltables-3.1.0-py3-none-any.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/mmcv_full-1_3_8-cu110-torch1_7_0/mmcv_full-1.3.8-cp37-cp37m-manylinux1_x86_64.whl' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/pycocotools-2.0.2/pycocotools-2.0.2' --no-deps\n# !pip install '/kaggle/input/mmdetectionv2140/mmpycocotools-12.0.3/mmpycocotools-12.0.3' --no-deps\n\n# !rm -rf mmdetection\n\n# !cp -r /kaggle/input/mmdetectionv2140/mmdetection-2.14.0 /kaggle/working/\n# !mv /kaggle/working/mmdetection-2.14.0 /kaggle/working/mmdetection\n# %cd /kaggle/working/mmdetection\n# !pip install -e .\n\n\n# 必要なライブラリのインストール（オフライン用）\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/addict-2.4.0-py3-none-any.whl\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/yapf-0.32.0-py2.py3-none-any.whl\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/terminal-0.4.0-py3-none-any.whl\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/terminaltables-3.1.10-py2.py3-none-any.whl\n#ytt\n#!pip install /kaggle/input/2023-hhp-mmdet/mmcv_full-1.7.0-cp310-cp310-manylinux1_x86_64_cu113.whl\n!pip install /kaggle/input/mmdet3-wheels/mmcv_full-1.7.1-cp310-cp310-linux_x86_64.whl\n#!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/mmcv_full-1.7.0-cp37-cp37m-linux_x86_64.whl\n#!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/pycocotools-2.0.6-cp37-cp37m-linux_x86_64.whl\n#!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/mmpycocotools-12.0.3-cp37-cp37m-linux_x86_64.whl\n!cp -r /kaggle/input/cbnetv2-repo/cbnet_repo /kaggle/working/\n# !cp -r /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/mmdetection/ /kaggle/working/\n%cd /kaggle/working/cbnet_repo\n!pip install -e . --no-deps\n%cd /kaggle/working/\n\n!pip install /kaggle/input/mmdetection-2-26-0/mmdetection-2-26-0/mmdet-2.26.0-py3-none-any.whl\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:06:55.733052Z","iopub.execute_input":"2023-07-30T21:06:55.733475Z","iopub.status.idle":"2023-07-30T21:11:36.644672Z","shell.execute_reply.started":"2023-07-30T21:06:55.733419Z","shell.execute_reply":"2023-07-30T21:11:36.643521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/enseibleboxes109/ensemble_boxes-1.0.9-py3-none-any.whl ","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:11:36.648944Z","iopub.execute_input":"2023-07-30T21:11:36.650948Z","iopub.status.idle":"2023-07-30T21:12:07.78843Z","shell.execute_reply.started":"2023-07-30T21:11:36.650917Z","shell.execute_reply":"2023-07-30T21:12:07.787288Z"},"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-30T21:12:07.791625Z","iopub.execute_input":"2023-07-30T21:12:07.791969Z","iopub.status.idle":"2023-07-30T21:12:07.807443Z","shell.execute_reply.started":"2023-07-30T21:12:07.79194Z","shell.execute_reply":"2023-07-30T21:12:07.806567Z"},"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-30T21:12:07.808899Z","iopub.execute_input":"2023-07-30T21:12:07.809262Z","iopub.status.idle":"2023-07-30T21:12:07.819911Z","shell.execute_reply.started":"2023-07-30T21:12:07.809229Z","shell.execute_reply":"2023-07-30T21:12:07.818982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile wbf_tracking.py\n\n# coding: utf-8\n\n__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'\n# Modified by Mista G: https://www.kaggle.com/mistag\n\nimport warnings\nimport numpy as np\nfrom numba import jit\n\n@jit(nopython=True)\ndef bb_intersection_over_union(A, B) -> float:\n    xA = max(A[0], B[0])\n    yA = max(A[1], B[1])\n    xB = min(A[2], B[2])\n    yB = min(A[3], B[3])\n\n    # compute the area of intersection rectangle\n    interArea = max(0, xB - xA) * max(0, yB - yA)\n\n    if interArea == 0:\n        return 0.0\n\n    # compute the area of both the prediction and ground-truth rectangles\n    boxAArea = (A[2] - A[0]) * (A[3] - A[1])\n    boxBArea = (B[2] - B[0]) * (B[3] - B[1])\n\n    iou = interArea / float(boxAArea + boxBArea - interArea)\n    return iou\n\n\ndef prefilter_boxes(boxes, scores, labels, weights, thr):\n    # Create dict with boxes stored by its label\n    new_boxes = dict()\n\n    for t in range(len(boxes)):\n\n        if len(boxes[t]) != len(scores[t]):\n            print('Error. Length of boxes arrays not equal to length of scores array: {} != {}'.format(len(boxes[t]), len(scores[t])))\n            sys.exit()\n\n        if len(boxes[t]) != len(labels[t]):\n            print('Error. Length of boxes arrays not equal to length of labels array: {} != {}'.format(len(boxes[t]), len(labels[t])))\n            sys.exit()\n\n        for j in range(len(boxes[t])):\n            score = scores[t][j]\n            if score < thr:\n                continue\n            label = int(labels[t][j])\n            box_part = boxes[t][j]\n            x1 = max(float(box_part[0]), 0.)\n            y1 = max(float(box_part[1]), 0.)\n            x2 = max(float(box_part[2]), 0.)\n            y2 = max(float(box_part[3]), 0.)\n\n            # Box data checks\n            if x2 < x1:\n                warnings.warn('X2 < X1 value in box. Swap them.')\n                x1, x2 = x2, x1\n            if y2 < y1:\n                warnings.warn('Y2 < Y1 value in box. Swap them.')\n                y1, y2 = y2, y1\n            if x1 > 1:\n                warnings.warn('X1 > 1 in box. Set it to 1. Check that you normalize boxes in [0, 1] range.')\n                x1 = 1\n            if x2 > 1:\n                warnings.warn('X2 > 1 in box. Set it to 1. Check that you normalize boxes in [0, 1] range.')\n                x2 = 1\n            if y1 > 1:\n                warnings.warn('Y1 > 1 in box. Set it to 1. Check that you normalize boxes in [0, 1] range.')\n                y1 = 1\n            if y2 > 1:\n                warnings.warn('Y2 > 1 in box. Set it to 1. Check that you normalize boxes in [0, 1] range.')\n                y2 = 1\n            if (x2 - x1) * (y2 - y1) == 0.0:\n                warnings.warn(\"Zero area box skipped: {}.\".format(box_part))\n                continue\n\n            # [label, score, weight, model index, x1, y1, x2, y2]\n            b = [int(label), float(score) * weights[t], weights[t], t, x1, y1, x2, y2]\n            if label not in new_boxes:\n                new_boxes[label] = []\n            new_boxes[label].append(b)\n\n    # Sort each list in dict by score and transform it to numpy array\n    for k in new_boxes:\n        current_boxes = np.array(new_boxes[k])\n        new_boxes[k] = current_boxes[current_boxes[:, 1].argsort()[::-1]]\n\n    return new_boxes\n\n\ndef get_weighted_box(boxes, conf_type='avg'):\n    \"\"\"\n    Create weighted box for set of boxes\n    :param boxes: set of boxes to fuse\n    :param conf_type: type of confidence one of 'avg' or 'max'\n    :return: weighted box (label, score, weight, x1, y1, x2, y2)\n    \"\"\"\n\n    box = np.zeros(8, dtype=np.float32)\n    conf = 0\n    conf_list = []\n    w = 0\n    for b in boxes:\n        box[4:] += (b[1] * b[4:])\n        conf += b[1]\n        conf_list.append(b[1])\n        w += b[2]\n    box[0] = boxes[0][0]\n    if conf_type == 'avg':\n        box[1] = conf / len(boxes)\n    elif conf_type == 'max':\n        box[1] = np.array(conf_list).max()\n    elif conf_type in ['box_and_model_avg', 'absent_model_aware_avg']:\n        box[1] = conf / len(boxes)\n    box[2] = w\n    box[3] = -1 # model index field is retained for consistensy but is not used.\n    box[4:] /= conf\n    return box\n\n\ndef find_matching_box(boxes_list, new_box, match_iou):\n    best_iou = match_iou\n    best_index = -1\n    for i in range(len(boxes_list)):\n        box = boxes_list[i]\n        if box[0] != new_box[0]:\n            continue\n        iou = bb_intersection_over_union(box[4:], new_box[4:])\n        if iou > best_iou:\n            best_index = i\n            best_iou = iou\n\n    return best_index, best_iou\n\n\ndef weighted_boxes_fusion_tracking(boxes_list, scores_list, labels_list, weights=None, iou_thr=0.55, skip_box_thr=0.0, conf_type='avg', allows_overflow=False):\n    '''\n    :param boxes_list: list of boxes predictions from each model, each box is 4 numbers.\n    It has 3 dimensions (models_number, model_preds, 4)\n    Order of boxes: x1, y1, x2, y2. We expect float normalized coordinates [0; 1]\n    :param scores_list: list of scores for each model\n    :param labels_list: list of labels for each model\n    :param weights: list of weights for each model. Default: None, which means weight == 1 for each model\n    :param iou_thr: IoU value for boxes to be a match\n    :param skip_box_thr: exclude boxes with score lower than this variable\n    :param conf_type: how to calculate confidence in weighted boxes. 'avg': average value, 'max': maximum value, 'box_and_model_avg': box and model wise hybrid weighted average, 'absent_model_aware_avg': weighted average that takes into account the absent model.\n    :param allows_overflow: false if we want confidence score not exceed 1.0\n\n    :return: boxes: boxes coordinates (Order of boxes: x1, y1, x2, y2).\n    :return: scores: confidence scores\n    :return: labels: boxes labels\n    :return: wbfo: original boxes coordinates for each fused box\n    '''\n\n    if weights is None:\n        weights = np.ones(len(boxes_list))\n    if len(weights) != len(boxes_list):\n        print('Warning: incorrect number of weights {}. Must be: {}. Set weights equal to 1.'.format(len(weights), len(boxes_list)))\n        weights = np.ones(len(boxes_list))\n    weights = np.array(weights)\n\n    if conf_type not in ['avg', 'max', 'box_and_model_avg', 'absent_model_aware_avg']:\n        print('Unknown conf_type: {}. Must be \"avg\", \"max\" or \"box_and_model_avg\", or \"absent_model_aware_avg\"'.format(conf_type))\n        sys.exit()\n\n    filtered_boxes = prefilter_boxes(boxes_list, scores_list, labels_list, weights, skip_box_thr)\n    if len(filtered_boxes) == 0:\n        return np.zeros((0, 4)), np.zeros((0,)), np.zeros((0,)), np.zeros((0, 4))\n    \n    overall_boxes = []\n    original_boxes = []\n    for label in filtered_boxes:\n        boxes = filtered_boxes[label]\n        new_boxes = []\n        weighted_boxes = []\n        # Clusterize boxes\n        for j in range(0, len(boxes)):\n            index, best_iou = find_matching_box(weighted_boxes, boxes[j], iou_thr)\n            if index != -1:\n                new_boxes[index].append(boxes[j])\n                weighted_boxes[index] = get_weighted_box(new_boxes[index], conf_type)\n            else:\n                new_boxes.append([boxes[j].copy()])\n                weighted_boxes.append(boxes[j].copy())\n        # Rescale confidence based on number of models and boxes\n        original_boxes.append(new_boxes)\n        for i in range(len(new_boxes)):\n            clustered_boxes = np.array(new_boxes[i])\n            if conf_type == 'box_and_model_avg':\n                # weighted average for boxes\n                weighted_boxes[i][1] = weighted_boxes[i][1] * len(clustered_boxes) / weighted_boxes[i][2]\n                # identify unique model index by model index column\n                _, idx = np.unique(clustered_boxes[:, 3], return_index=True)\n                # rescale by unique model weights\n                weighted_boxes[i][1] = weighted_boxes[i][1] *  clustered_boxes[idx, 2].sum() / weights.sum()\n            elif conf_type == 'absent_model_aware_avg':\n                # get unique model index in the cluster\n                models = np.unique(clustered_boxes[:, 3]).astype(int)\n                # create a mask to get unused model weights\n                mask = np.ones(len(weights), dtype=bool)\n                mask[models] = False\n                # absent model aware weighted average\n                weighted_boxes[i][1] = weighted_boxes[i][1] * len(clustered_boxes) / (weighted_boxes[i][2] + weights[mask].sum())\n            elif conf_type == 'max':\n                weighted_boxes[i][1] = weighted_boxes[i][1] / weights.max()\n            elif not allows_overflow:\n                weighted_boxes[i][1] = weighted_boxes[i][1] * min(len(weights), len(clustered_boxes)) / weights.sum()\n            else:\n                weighted_boxes[i][1] = weighted_boxes[i][1] * len(clustered_boxes) / weights.sum()\n        overall_boxes.append(np.array(weighted_boxes))\n    overall_boxes = np.concatenate(overall_boxes, axis=0)\n    sidx = overall_boxes[:, 1].argsort()\n    overall_boxes = overall_boxes[sidx[::-1]]\n    boxes = overall_boxes[:, 4:]\n    scores = overall_boxes[:, 1]\n    labels = overall_boxes[:, 0]\n    # sort originals accoring to wbf\n    original_boxes = original_boxes[0]\n    wbfo = [original_boxes[i] for i in sidx[::-1]]\n    return boxes, scores, labels, wbfo","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:12:07.82231Z","iopub.execute_input":"2023-07-30T21:12:07.822839Z","iopub.status.idle":"2023-07-30T21:12:07.837257Z","shell.execute_reply.started":"2023-07-30T21:12:07.822809Z","shell.execute_reply":"2023-07-30T21:12:07.836184Z"},"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-30T21:12:07.838808Z","iopub.execute_input":"2023-07-30T21:12:07.839196Z","iopub.status.idle":"2023-07-30T21:12:08.085868Z","shell.execute_reply.started":"2023-07-30T21:12:07.839165Z","shell.execute_reply":"2023-07-30T21:12:08.084935Z"},"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-30T21:12:08.087134Z","iopub.execute_input":"2023-07-30T21:12:08.087917Z","iopub.status.idle":"2023-07-30T21:12:08.113213Z","shell.execute_reply.started":"2023-07-30T21:12:08.087884Z","shell.execute_reply":"2023-07-30T21:12:08.112363Z"},"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-30T21:12:08.118755Z","iopub.execute_input":"2023-07-30T21:12:08.119015Z","iopub.status.idle":"2023-07-30T21:12:08.143775Z","shell.execute_reply.started":"2023-07-30T21:12:08.118992Z","shell.execute_reply":"2023-07-30T21:12:08.142914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append('/kaggle/input/einops/einops-master')\n\nsys.path.append(\"../input/pretrained-models-pytorch\")\nsys.path.append(\"../input/efficientnet-pytorch\")\nsys.path.append(\"/kaggle/input/smp-github/segmentation_models.pytorch-master\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:12:08.145246Z","iopub.execute_input":"2023-07-30T21:12:08.145895Z","iopub.status.idle":"2023-07-30T21:12:08.160268Z","shell.execute_reply.started":"2023-07-30T21:12:08.145864Z","shell.execute_reply":"2023-07-30T21:12:08.159427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nprint(sys.path)\n!cp -r /kaggle/input/vitadadapter/hubmap/vitadap/vitadapzip/ ./\nsys.path.insert(1, '/kaggle/working/vitadapzip')\nprint(sys.path)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:12:08.161816Z","iopub.execute_input":"2023-07-30T21:12:08.162582Z","iopub.status.idle":"2023-07-30T21:12:14.466566Z","shell.execute_reply.started":"2023-07-30T21:12:08.16255Z","shell.execute_reply":"2023-07-30T21:12:14.465176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport segmentation_models_pytorch as smp\nimport mmcv_custom\nimport mmdet_custom","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:12:14.468615Z","iopub.execute_input":"2023-07-30T21:12:14.469605Z","iopub.status.idle":"2023-07-30T21:12:20.436003Z","shell.execute_reply.started":"2023-07-30T21:12:14.469561Z","shell.execute_reply":"2023-07-30T21:12:20.435023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import mmdet\nfrom mmdet.apis import init_detector, inference_detector,show_result_pyplot, set_random_seed\nfrom mmdet.models import build_detector\n#print(mmdet.__version__)\n#print(mmcv.__version__)\n#print(mmengine.__version__)\n\nfrom mmdet.models.backbones import *\n# # #check file her\n\nfrom mmcv import Config\n\nfrom mmdet.models.backbones.swin import SwinTransformer\nfrom mmdet.models.backbones import cbnet","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:12:20.43756Z","iopub.execute_input":"2023-07-30T21:12:20.438026Z","iopub.status.idle":"2023-07-30T21:12:21.107436Z","shell.execute_reply.started":"2023-07-30T21:12:20.43799Z","shell.execute_reply":"2023-07-30T21:12:21.10647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import sys\n# print(sys.path)\n# !cp -r /kaggle/input/cbnetv2-repo ./# \n# sys.path.insert(1, '/kaggle/working/cbnet_repo/')\n# print(sys.path)\n\nimport mmdet\nfrom mmdet.apis import init_detector, inference_detector,show_result_pyplot, set_random_seed\nfrom mmdet.models import build_detector\n#print(mmdet.__version__)\n#print(mmcv.__version__)\n#print(mmengine.__version__)\n\nfrom mmdet.models.backbones import *\n# # #check file her\n\nfrom mmcv import Config\n\nfrom mmdet.models.backbones.swin import SwinTransformer\nfrom mmdet.models.backbones import cbnet","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:12:21.108787Z","iopub.execute_input":"2023-07-30T21:12:21.109636Z","iopub.status.idle":"2023-07-30T21:12:21.11618Z","shell.execute_reply.started":"2023-07-30T21:12:21.109601Z","shell.execute_reply":"2023-07-30T21:12:21.115218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import mmdet, mmcv, mmengine\n#from mmengine.config import Config\n#from mmengine.runner import Runner\n#from mmdet.utils import register_all_modules\n#from mmdet.apis import init_detector, inference_detector\n#from mmengine.visualization import Visualizer\n\nfrom mmdet.apis import init_detector, inference_detector,show_result_pyplot, set_random_seed\n\n#print(mmdet.__version__)\n#print(mmcv.__version__)\n#print(mmengine.__version__)\n\n\n# # #check file her\n\nfrom mmcv import Config\n\n\n# configs_path = ['/kaggle/working/test_conf.py','/kaggle/working/test_conf3.py', '/kaggle/working/cbnet_conf.py']\n# ckpt_paths = ['/kaggle/input/ds1pretexp1moreaug-htc50-2048-cv408ps/best_segm_mAP_epoch_21.pth',\n#               '/kaggle/input/ds1pretexp1-htc101-2048-full/detectors_epoch_18.pth',\n#              '/kaggle/input/pretexp1cbnetv2-base-2048-basic-exp1-f5cv437/best_segm_mAP_epoch_21.pth']\n\n\nconfigs_path = [\n#             '/kaggle/input/pretexp3-adaplargehtc-cv411f1/exp1_adaplarge_htc.py',\n                '/kaggle/input/pretexp4-adapbeitv2lhtc-1400-ds2wsiall-ps60-leak/exp4_adapbeitv2l_withps50exp2.py',\n            '/kaggle/input/pretexp4-adapbeitv2lhtc-1400-ds2wsiall-ps50exp2-lo/exp4_adapbeitv2l_withps50exp2.py',\n#                 '/kaggle/input/hubmap-adaplargev1-exp5-pretwsiall-leaky/exp5_adaplarge_htc.py',\n                '/kaggle/input/pretwsiallhtc-resnext101-exp3-augv4-maskloss4/detec101next.py',\n                '/kaggle/input/ds1pretexp1moreaug-htc50-2048-cv408ps/h50psexp1.py',\n#                 '/kaggle/input/exp3-withpret-cblarge-1600-morepretep-ps50exp2/cbnet_large_2048_ps50.py'\n                '/kaggle/input/pretexp1cbnetv2-base-2048-basic-exp1-f5cv437/exp1_cbnet_swinb2.py',\n]\n\nckpt_paths = [\n#     '/kaggle/input/pretexp3-adaplargehtc-cv417f1/best_segm_mAP_epoch_21.pth',\n    '/kaggle/input/pretexp4-adapbeitv2lhtc-1400-ds2wsiall-ps60-leak/best_segm_mAP_epoch_18.pth',\n    '/kaggle/input/pretexp4-adapbeitv2lhtc-1400-ds2wsiall-ps50exp2-lo/best_segm_mAP_epoch_20.pth',\n#     '/kaggle/input/hubmap-adaplargev1-exp5-pretwsiall-leaky/best_segm_mAP_epoch_21.pth',\n            '/kaggle/input/pretwsiallhtc-resnext101-exp3-augv4-maskloss4/best_segm_mAP_epoch_17.pth',\n              '/kaggle/input/ds1pretexp1moreaug-htc50-2048-cv408ps/detectors_epoch_23.pth',\n    '/kaggle/input/pretexp1cbnetv2-base-2048-basic-exp1-f5cv437/best_segm_mAP_epoch_21.pth'\n#     '/kaggle/input/exp3-withpret-cblarge-1600-morepretep-ps50exp2/best_segm_mAP_epoch_19.pth'\n    \n]\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T22:12:25.677137Z","iopub.execute_input":"2023-07-30T22:12:25.677565Z","iopub.status.idle":"2023-07-30T22:12:25.68664Z","shell.execute_reply.started":"2023-07-30T22:12:25.677533Z","shell.execute_reply":"2023-07-30T22:12:25.685532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = []\nfor cfg_path, ckpt in zip(configs_path,ckpt_paths):\n    cfg = Config.fromfile(cfg_path)\n    if 'cbnet' in cfg_path:\n        print('small image for cbnet')\n        cfg.model.test_cfg.rcnn.score_thr = 0.001\n\n        cfg.model.test_cfg.rcnn.max_per_img = 500\n\n        cfg.model.test_cfg.rcnn.nms.iou_threshold=0.5\n        cfg.model.test_cfg.rcnn.mask_thr_binary=0.55\n        cfg.data.test.pipeline[1].img_scale= [(2048, 2048)]#\n        \n    elif 'adap' in cfg_path:\n        print('small image for adap')\n        cfg.model.test_cfg.rcnn.score_thr = 0.001\n\n        cfg.model.test_cfg.rcnn.max_per_img = 500\n        cfg.model.test_cfg.rcnn.nms.type='nms'\n\n        cfg.model.test_cfg.rcnn.nms.iou_threshold=0.5\n        cfg.model.test_cfg.rcnn.mask_thr_binary=0.55\n        cfg.data.test.pipeline[1].img_scale= [(1600,1600),(1400,1400)]#\n        \n    else:\n        cfg.data.test.pipeline[1].img_scale= [(2048,2048)]#\n        cfg.model.test_cfg.rcnn.score_thr = 0.001\n\n        cfg.model.test_cfg.rcnn.max_per_img = 500\n\n        cfg.model.test_cfg.rcnn.nms.iou_threshold=0.5\n        cfg.model.test_cfg.rcnn.mask_thr_binary=0.55\n    cfg.seed = 69\n    set_random_seed(69, deterministic=False)\n\n    print(f'Config:\\n{cfg.data.test.pipeline}')\n    model = init_detector(cfg, ckpt, device=device)  \n    models.append(model)\n    del cfg","metadata":{"execution":{"iopub.status.busy":"2023-07-30T22:12:26.003289Z","iopub.execute_input":"2023-07-30T22:12:26.003642Z","iopub.status.idle":"2023-07-30T22:15:04.900972Z","shell.execute_reply.started":"2023-07-30T22:12:26.003613Z","shell.execute_reply":"2023-07-30T22:15:04.899858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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-30T22:15:04.904076Z","iopub.execute_input":"2023-07-30T22:15:04.904477Z","iopub.status.idle":"2023-07-30T22:15:04.911315Z","shell.execute_reply.started":"2023-07-30T22:15:04.904424Z","shell.execute_reply":"2023-07-30T22:15:04.910381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# all_imgs","metadata":{"execution":{"iopub.status.busy":"2023-07-30T22:15:04.912927Z","iopub.execute_input":"2023-07-30T22:15:04.913947Z","iopub.status.idle":"2023-07-30T22:15:04.9202Z","shell.execute_reply.started":"2023-07-30T22:15:04.913908Z","shell.execute_reply":"2023-07-30T22:15:04.919213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.morphology import binary_dilation\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:14:27.400864Z","iopub.execute_input":"2023-07-30T21:14:27.401208Z","iopub.status.idle":"2023-07-30T21:14:27.407135Z","shell.execute_reply.started":"2023-07-30T21:14:27.401176Z","shell.execute_reply":"2023-07-30T21:14:27.406012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom skimage.morphology import binary_erosion, binary_dilation, binary_opening, binary_closing\n\nMIN_PIXELS = 40\ndef nms_predictions(classes, scores, bboxes, masks, \n                    iou_th=.5, shape=(512, 512), weights=[0.5,0.5]):\n    he, wd = shape[0], shape[1]\n    boxes_list = [[x[0] / wd, x[1] / he, x[2] / wd, x[3] / he]\n                  for x in bboxes]\n    scores_list = [x for x in scores]\n    labels_list = [x for x in classes]\n    nms_bboxes, nms_scores, nms_classes = nms(\n        boxes=[boxes_list], \n        scores=[scores_list], \n        labels=[labels_list], \n        weights=weights,\n        iou_thr=iou_th\n    )\n    nms_masks = []\n    for s in nms_scores:\n        nms_masks.append(masks[scores.index(s)])\n    nms_scores, nms_classes, nms_masks = zip(*sorted(zip(nms_scores, nms_classes, nms_masks), reverse=True))\n    return nms_classes, nms_scores, nms_masks\n\ndef ensemble_pred_masks(masks, min_pixels=MIN_PIXELS, shape=(512, 512)):\n    result = []\n    used = np.zeros(shape, dtype=int) \n\n    prev_masks = []\n    new_masks = []\n    for i, mask in enumerate(masks):\n        # cont, hier = cv2.findContours(np.array(mask,dtype=np.uint8),cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n        # if len(cont)>0:\n            # for cnt in cont:\n            #     convex_mask = cv2.fillConvexPoly(np.zeros_like(np.array(mask,dtype=np.uint8)),points=cnt, color=1)\n            #     fillornot = len(pd.Series((convex_mask==mask).flatten()).value_counts())\n            #     if fillornot>1: #fill\n            #         mask = convex_mask\n\n        \n            # before= mask.sum()\n        mask = binary_erosion(binary_dilation(mask))  #post processing \n            # if before!=mask.sum():\n               # print('after',mask.sum(),'before',before)\n        mask = mask * (1-used)\n        if mask.sum() >= 100: # skip predictions with small area\n                #     used += mask \n            new_masks.append(mask)\n        \n    return new_masks\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:14:27.408608Z","iopub.execute_input":"2023-07-30T21:14:27.408941Z","iopub.status.idle":"2023-07-30T21:14:27.422733Z","shell.execute_reply.started":"2023-07-30T21:14:27.408911Z","shell.execute_reply":"2023-07-30T21:14:27.421814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ensemble_boxes import *","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:14:27.424112Z","iopub.execute_input":"2023-07-30T21:14:27.424912Z","iopub.status.idle":"2023-07-30T21:14:28.027551Z","shell.execute_reply.started":"2023-07-30T21:14:27.424879Z","shell.execute_reply":"2023-07-30T21:14:28.026656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = []\nheights = []\nwidths = []\nprediction_strings = []","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:14:28.028843Z","iopub.execute_input":"2023-07-30T21:14:28.029483Z","iopub.status.idle":"2023-07-30T21:14:28.034827Z","shell.execute_reply.started":"2023-07-30T21:14:28.029426Z","shell.execute_reply":"2023-07-30T21:14:28.033811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import measure\nfrom wbf_tracking import *","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:14:28.036287Z","iopub.execute_input":"2023-07-30T21:14:28.036768Z","iopub.status.idle":"2023-07-30T21:14:28.051208Z","shell.execute_reply.started":"2023-07-30T21:14:28.036736Z","shell.execute_reply":"2023-07-30T21:14:28.050171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ensemble_boxes import *\nMODEL_WEIGHTS = [0.25,0.5,0.25]\ndef bbox_to_key(bbox):\n    return str(np.round(bbox, 6))\n\n\n# Fuse masks that belong to fused boxes\ndef get_wsf_mask(wbf_box, wbf_org, pmasks, pmasks_lkup, thres=0.5):\n    w, h = 512, 512\n    mask = np.zeros((w, h), dtype=np.uint8)\n    for i in range(len(wbf_org)):\n        key = bbox_to_key(wbf_org[i][4:])\n        model = int(wbf_org[i][3])\n        # try:\n        ind = pmasks_lkup[model][key]\n        mask = mask + pmasks[model][ind]\n        # except:\n            # pass\n    # convert thres to integer based on number of boxes\n    threshold = max(1, int(thres*len(wbf_org)))\n    # threshold = 0.0001\n            \n    # remove pixels outside WBF box\n    m2 = np.zeros((w, h), dtype=np.uint8)\n    x1 = max(0, int(h * wbf_box[0]))\n    y1 = max(0, int(w * wbf_box[1]))\n    x2 = min(h, int(h * wbf_box[2]))\n    y2 = min(w, int(w * wbf_box[3]))\n    # print(x1,x2,y1,y2)\n    m2[y1:y2, x1:x2] = 1\n    mask = (mask >= threshold) * m2\n    return mask.astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:14:28.052854Z","iopub.execute_input":"2023-07-30T21:14:28.053288Z","iopub.status.idle":"2023-07-30T21:14:28.064086Z","shell.execute_reply.started":"2023-07-30T21:14:28.053256Z","shell.execute_reply":"2023-07-30T21:14:28.062957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm_ids, subm_masks = [], []\nsample = None\nimport mmcv\n\n\nconfidence_thresholds = {0: 0.5, 1: 0.5, 2: 0.8}\n\nfor img in all_imgs:\n    pred_string = ''\n\n    img_array = mmcv.imread(img,channel_order='rgb')\n    [h, w, c] = img_array.shape \n    \n    \n\n    \n    masks_nms_list = []\n    pred_dict_list=  []\n    score_nms_list = []\n    box_nms_list = []\n    class_nms_list = []\n    \n    \n    for modely in models:\n        \n        pred_dict = {}\n        previous_masks = []\n        classes_nms = []\n        scoresb_nms = []\n        bboxesb_nms = []\n        weightsb_nms = []     \n        \n        result = inference_detector(modely,img)\n\n        c = []\n        for i, classe in enumerate(result[0]):\n            c.append(classe.shape[0])\n        \n        maxclass = np.argwhere(np.array(c)==np.max(c))[0][0]\n        for i, classe in enumerate(result[0]):\n            if i==maxclass: #classe.shape != (0, 5):\n                bbs = classe\n                sgs = result[1][i]\n                # print(sgs.shape)\n                count = 0\n\n                for bb, sg in zip(bbs,sgs):\n                    box = bb[:4]\n                    cnf = bb[4]\n                    box = [box[0] / 512, box[1] / 512, box[2] / 512, box[3] / 512]\n                    \n                    if cnf >= 0.00001:\n                        mask = np.array(sg,dtype=np.uint8)  \n                        previous_masks.append(mask)\n                        scoresb_nms.extend([cnf])\n                        bboxesb_nms.extend([box])\n                        # weights = [weight] * len()\n#                         weightsb_nms.extend([weight])\n                        pred_dict[bbox_to_key(box)] = count\n                        count+= 1\n                        classes_nms = [0] * len(previous_masks)\n    \n            masks_nms_list.append(np.array(previous_masks, dtype=np.uint8))\n            score_nms_list.append(np.array(scoresb_nms))\n            box_nms_list.append(np.array(bboxesb_nms))\n\n            class_nms_list.append(np.array(classes_nms))\n            pred_dict_list.append(pred_dict)\n            \n            \n    wbf_boxes, wbf_scores, _, wbf_originals = weighted_boxes_fusion_tracking(box_nms_list, \n                                                                             score_nms_list, \n                                                                             labels_list=class_nms_list, \n#                                                                              weights = [0.20,0.20,0.2,0.15,0.15,0.1],\n                                                                             weights = [0.25,0.25,0.2,0.2,0.1],\n                                                                             \n                                                                             iou_thr=0.6, \n                                                                             skip_box_thr=0.01)\n    \n    \n    \n    fin_masks = []\n    used = np.zeros((512,512), dtype=int)\n    for i in range(len(wbf_boxes)):\n        \n        mask = get_wsf_mask(wbf_boxes[i], wbf_originals[i], masks_nms_list, pred_dict_list, thres=0.2)\n        fin_masks.append(mask)\n    \n    fin_masks = ensemble_pred_masks(fin_masks) \n\n    m = 0\n    for masky, scory in zip(fin_masks, tuple(wbf_scores)):\n        masky = masky.astype(bool)       \n        \n        encoded = encode_binary_mask(masky)\n        if m==0:\n            pred_string += f\"0 {scory} {encoded.decode('utf-8')}\"\n            m=m+1\n        else:\n            pred_string += f\" 0 {scory} {encoded.decode('utf-8')}\"\n\n            \n    ids.append(os.path.basename(img).split('.')[0])\n    heights.append(h)\n    widths.append(w)\n            \n    prediction_strings.append(pred_string)                    \n\n    ","metadata":{"execution":{"iopub.status.busy":"2023-07-30T22:20:27.61114Z","iopub.execute_input":"2023-07-30T22:20:27.611903Z","iopub.status.idle":"2023-07-30T22:20:30.328149Z","shell.execute_reply.started":"2023-07-30T22:20:27.611859Z","shell.execute_reply":"2023-07-30T22:20:30.325805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# masks_nms","metadata":{"execution":{"iopub.status.busy":"2023-07-30T22:20:30.329125Z","iopub.status.idle":"2023-07-30T22:20:30.329736Z","shell.execute_reply.started":"2023-07-30T22:20:30.329467Z","shell.execute_reply":"2023-07-30T22:20:30.329493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# len(scoresb_nms)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T22:20:30.331749Z","iopub.status.idle":"2023-07-30T22:20:30.332226Z","shell.execute_reply.started":"2023-07-30T22:20:30.331978Z","shell.execute_reply":"2023-07-30T22:20:30.332009Z"},"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-30T22:20:30.33374Z","iopub.status.idle":"2023-07-30T22:20:30.334627Z","shell.execute_reply.started":"2023-07-30T22:20:30.334357Z","shell.execute_reply":"2023-07-30T22:20:30.334385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission.loc['72e40acccadf','prediction_string']","metadata":{"execution":{"iopub.status.busy":"2023-07-30T22:20:30.336085Z","iopub.status.idle":"2023-07-30T22:20:30.336563Z","shell.execute_reply.started":"2023-07-30T22:20:30.336312Z","shell.execute_reply":"2023-07-30T22:20:30.336334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf mmdetection\n!rm -rf packages\n# !rm -rf cbnetv2-repo\n!rm -rf cbnet_repo\n!rm -rf /kaggle/working/vitadapzip","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:15:13.136268Z","iopub.execute_input":"2023-07-30T21:15:13.137003Z","iopub.status.idle":"2023-07-30T21:15:17.392654Z","shell.execute_reply.started":"2023-07-30T21:15:13.136901Z","shell.execute_reply":"2023-07-30T21:15:17.391228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##a","metadata":{"execution":{"iopub.status.busy":"2023-07-30T21:48:43.824937Z","iopub.execute_input":"2023-07-30T21:48:43.825341Z","iopub.status.idle":"2023-07-30T21:48:43.831723Z","shell.execute_reply.started":"2023-07-30T21:48:43.825312Z","shell.execute_reply":"2023-07-30T21:48:43.830576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}