{"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":"markdown","source":"#### A Quick YOLOv7 Baseline [Inference Edition]\n\nThe original Code From [here](https://www.kaggle.com/code/fnands/a-quick-yolov7-baseline-inference)\n\nTODO: Try Model Soups like in [here](https://www.kaggle.com/code/bachngoh/hubmap-2023-detectron2-model-soups)","metadata":{}},{"cell_type":"code","source":"weights_list = [ \\\n                '/kaggle/input/20230629-2328-yolov7-fine-tune-5fold-exp05-v3/yolov7-fine-tune_fold0/weights/best.pt',\n                '/kaggle/input/20230629-2328-yolov7-fine-tune-5fold-exp05-v3/yolov7-fine-tune_fold1/weights/best.pt',\n#                 '/kaggle/input/20230629-2328-yolov7-fine-tune-5fold-exp05-v3/yolov7-fine-tune_fold2/weights/best.pt',\n#                 '/kaggle/input/20230629-2328-yolov7-fine-tune-5fold-exp05-v3/yolov7-fine-tune_fold3/weights/best.pt',\n                '/kaggle/input/20230629-2328-yolov7-fine-tune-5fold-exp05-v3/yolov7-fine-tune_fold4/weights/best.pt',\n                ]\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:06:51.914551Z","iopub.execute_input":"2023-07-29T07:06:51.914922Z","iopub.status.idle":"2023-07-29T07:06:51.921751Z","shell.execute_reply.started":"2023-07-29T07:06:51.914882Z","shell.execute_reply":"2023-07-29T07:06:51.920685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/pycocotools/ /kaggle/working/pycocotools\n!pip install /kaggle/working/pycocotools/pycocotools-2.0.6  --no-index --find-links=/kaggle/working/pycocotools/ ","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:06:51.924339Z","iopub.execute_input":"2023-07-29T07:06:51.925179Z","iopub.status.idle":"2023-07-29T07:07:24.018227Z","shell.execute_reply.started":"2023-07-29T07:06:51.925141Z","shell.execute_reply":"2023-07-29T07:07:24.016763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/d/ccvipchenbin/ensemble-boxes/ensemble_boxes-1.0.7-py3-none-any.whl  --no-deps","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:07:24.021051Z","iopub.execute_input":"2023-07-29T07:07:24.023181Z","iopub.status.idle":"2023-07-29T07:07:46.352645Z","shell.execute_reply.started":"2023-07-29T07:07:24.023116Z","shell.execute_reply":"2023-07-29T07:07:46.351211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nfrom typing import Text, Dict, Tuple\nimport zlib\n\nfrom ensemble_boxes import weighted_boxes_fusion","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:07:46.356123Z","iopub.execute_input":"2023-07-29T07:07:46.356594Z","iopub.status.idle":"2023-07-29T07:07:46.363165Z","shell.execute_reply.started":"2023-07-29T07:07:46.356546Z","shell.execute_reply":"2023-07-29T07:07:46.362103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/yolov7-weights-and-wheels/yolo_wheel/yolov7-0.0.1-py37.py38.py39-none-any.whl --no-index --find-links=/kaggle/input/yolov7-weights-and-wheels/yolo_wheel","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:07:46.367823Z","iopub.execute_input":"2023-07-29T07:07:46.368838Z","iopub.status.idle":"2023-07-29T07:07:59.726499Z","shell.execute_reply.started":"2023-07-29T07:07:46.368797Z","shell.execute_reply":"2023-07-29T07:07:59.725189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/yolov7-weights-and-wheels/yolov7 yolo","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:07:59.728618Z","iopub.execute_input":"2023-07-29T07:07:59.729479Z","iopub.status.idle":"2023-07-29T07:08:01.129496Z","shell.execute_reply.started":"2023-07-29T07:07:59.729430Z","shell.execute_reply":"2023-07-29T07:08:01.127981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yolo.seg.segment import predict","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:01.132069Z","iopub.execute_input":"2023-07-29T07:08:01.132554Z","iopub.status.idle":"2023-07-29T07:08:01.138278Z","shell.execute_reply.started":"2023-07-29T07:08:01.132506Z","shell.execute_reply":"2023-07-29T07:08:01.137128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a yaml file as expected by YOLOv7 (and others)\nyaml_text = \"\"\"\n# class names\nnames: \n  0: blood_vessel\n  1: glomerulus\n  2: unsure\n\"\"\"\nwith open('/kaggle/working/hubmap-coco.yaml', 'w') as text_file:\n    text_file.write(yaml_text)","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:01.140112Z","iopub.execute_input":"2023-07-29T07:08:01.140844Z","iopub.status.idle":"2023-07-29T07:08:01.154143Z","shell.execute_reply.started":"2023-07-29T07:08:01.140804Z","shell.execute_reply":"2023-07-29T07:08:01.152958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import argparse\nimport os\nimport platform\nimport sys\nfrom pathlib import Path\nimport json\n\nimport torch\nimport torch.backends.cudnn as cudnn\nimport matplotlib.pyplot as plt\n\n\nfrom models.common import DetectMultiBackend\nfrom utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams\nfrom utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,\n                           increment_path,  print_args, scale_coords, strip_optimizer, xyxy2xywh)\nfrom utils.plots import Annotator, colors, save_one_box\nfrom utils.segment.general import scale_masks\nfrom utils.segment.plots import plot_masks\nfrom utils.torch_utils import select_device, smart_inference_mode","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:01.156418Z","iopub.execute_input":"2023-07-29T07:08:01.156805Z","iopub.status.idle":"2023-07-29T07:08:01.168521Z","shell.execute_reply.started":"2023-07-29T07:08:01.156769Z","shell.execute_reply":"2023-07-29T07:08:01.167439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def encode_binary_mask(mask: np.ndarray) -> 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#     print(encoded_mask)\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-29T07:08:01.170229Z","iopub.execute_input":"2023-07-29T07:08:01.170830Z","iopub.status.idle":"2023-07-29T07:08:01.185255Z","shell.execute_reply.started":"2023-07-29T07:08:01.170792Z","shell.execute_reply":"2023-07-29T07:08:01.184105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read .jsonl file and convert it to a list of dicts\n# The dicts contain IDs, class names and segmentation masks\n# from https://www.kaggle.com/code/leonidkulyk/eda-hubmap-hhv-interactive-annotations\nwith open('/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl', 'r') as json_file:\n    json_list = list(json_file)\n    \ntiles_dicts = []\nfor json_str in json_list:\n    tiles_dicts.append(json.loads(json_str))","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:01.186735Z","iopub.execute_input":"2023-07-29T07:08:01.187292Z","iopub.status.idle":"2023-07-29T07:08:05.509281Z","shell.execute_reply.started":"2023-07-29T07:08:01.187254Z","shell.execute_reply":"2023-07-29T07:08:05.508194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_of_tiles = {}\nfor tile in tiles_dicts:\n    dict_of_tiles[tile['id']] = tile['annotations']","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:05.510731Z","iopub.execute_input":"2023-07-29T07:08:05.511091Z","iopub.status.idle":"2023-07-29T07:08:05.798688Z","shell.execute_reply.started":"2023-07-29T07:08:05.511056Z","shell.execute_reply":"2023-07-29T07:08:05.797282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_glomerulus_mask(annotations: Dict, mask_shape: Tuple = (512, 512)) -> np.ndarray:\n    \"\"\" Converts glomerulus labels into boolean mask \"\"\"\n    mask = np.ones(shape=mask_shape, dtype=np.uint8)\n    \n    for annotation in annotations: \n        if annotation['type'] == 'glomerulus':            \n            coords = np.array(annotation['coordinates'])\n            cv2.fillPoly(mask, pts=coords, color=0)\n        \n\n    return mask.astype(bool)\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:05.800638Z","iopub.execute_input":"2023-07-29T07:08:05.801484Z","iopub.status.idle":"2023-07-29T07:08:05.814389Z","shell.execute_reply.started":"2023-07-29T07:08:05.801427Z","shell.execute_reply":"2023-07-29T07:08:05.813135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# YOLOv5 🚀 by Ultralytics, GPL-3.0 license\n\"\"\"\nCommon modules\n\"\"\"\n\nimport json\nimport math\nimport platform\nimport warnings\nfrom collections import OrderedDict, namedtuple\nfrom copy import copy\nfrom pathlib import Path\nimport sys\nsys.path.insert(0,'.')\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport requests\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom PIL import Image\nfrom torch.cuda import amp\n\nfrom utils.dataloaders import exif_transpose, letterbox\nfrom utils.general import (LOGGER, ROOT, Profile, check_requirements, check_suffix, check_version, colorstr,\n                           increment_path, make_divisible, scale_coords, xywh2xyxy, xyxy2xywh,\n                           yaml_load)\nfrom utils.plots import Annotator, colors, save_one_box\nfrom utils.torch_utils import copy_attr, smart_inference_mode\n\nclass DetectMultiBackend(nn.Module):\n    # YOLOv5 MultiBackend class for python inference on various backends\n    def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False, data=None, fp16=False, fuse=True):\n\n        from models.experimental import attempt_download, attempt_load  # scoped to avoid circular import\n\n        super().__init__()\n        w = str(weights[0] if isinstance(weights, list) else weights)\n        pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs = self._model_type(w)  # get backend\n        w = attempt_download(w)  # download if not local\n        fp16 &= pt or jit or onnx or engine  # FP16\n        stride = 32  # default stride\n\n        if pt:  # PyTorch\n            model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)\n            stride = max(int(model.stride.max()), 32)  # model stride\n            names = model.module.names if hasattr(model, 'module') else model.names  # get class names\n            model.half() if fp16 else model.float()\n            self.model = model  # explicitly assign for to(), cpu(), cuda(), half()\n            #segmentation_model = type(model.model[-1]).__name__ in ['Segment', 'ISegment', 'IRSegment']\n            segmentation_model = True\n\n        # class names\n        if 'names' not in locals():\n            names = yaml_load(data)['names'] if data else {i: f'class{i}' for i in range(999)}\n        if names[0] == 'n01440764' and len(names) == 1000:  # ImageNet\n            names = yaml_load(ROOT / 'data/ImageNet.yaml')['names']  # human-readable names\n\n        self.__dict__.update(locals())  # assign all variables to self\n\n    def forward(self, im, augment=False, visualize=False, val=False):\n        # YOLOv5 MultiBackend inference\n        b, ch, h, w = im.shape  # batch, channel, height, width\n        \n        if self.fp16 and im.dtype != torch.float16:\n            im = im.half()  # to FP16\n\n        if self.pt:  # PyTorch\n            y = self.model(im, augment=augment, visualize=visualize) if augment or visualize else self.model(im)\n            if isinstance(y, tuple) and not self.segmentation_model:\n                y = y[0]\n\n        if isinstance(y, np.ndarray):\n            y = torch.tensor(y, device=self.device)\n        return (y, []) if val else y\n\n    def warmup(self, imgsz=(1, 3, 640, 640)):\n        # Warmup model by running inference once\n        warmup_types = self.pt, self.jit, self.onnx, self.engine, self.saved_model, self.pb\n        if any(warmup_types) and self.device.type != 'cpu':\n            im = torch.empty(*imgsz, dtype=torch.half if self.fp16 else torch.float, device=self.device)  # input\n            for _ in range(2 if self.jit else 1):  #\n                self.forward(im)  # warmup\n\n    @staticmethod\n    def _model_type(p='path/to/model.pt'):\n        # Return model type from model path, i.e. path='path/to/model.onnx' -> type=onnx\n        from export import export_formats\n        suffixes = list(export_formats().Suffix) + ['.xml']  # export suffixes\n        check_suffix(p, suffixes)  # checks\n        p = Path(p).name  # eliminate trailing separators\n        pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, xml2 = (s in p for s in suffixes)\n        xml |= xml2  # *_openvino_model or *.xml\n        tflite &= not edgetpu  # *.tflite\n        return pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs\n\n    @staticmethod\n    def _load_metadata(f='path/to/meta.yaml'):\n        # Load metadata from meta.yaml if it exists\n        d = yaml_load(f)\n        return d['stride'], d['names']  # assign stride, names\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:05.819085Z","iopub.execute_input":"2023-07-29T07:08:05.820122Z","iopub.status.idle":"2023-07-29T07:08:05.847489Z","shell.execute_reply.started":"2023-07-29T07:08:05.820072Z","shell.execute_reply":"2023-07-29T07:08:05.846336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport torch\nimport torch.nn.functional as F\n\ndef crop(masks, boxes):\n    \"\"\"\n    \"Crop\" predicted masks by zeroing out everything not in the predicted bbox.\n    Vectorized by Chong (thanks Chong).\n\n    Args:\n        - masks should be a size [h, w, n] tensor of masks\n        - boxes should be a size [n, 4] tensor of bbox coords in relative point form\n    \"\"\"\n\n    n, h, w = masks.shape\n    x1, y1, x2, y2 = torch.chunk(boxes[:, :, None], 4, 1)  # x1 shape(1,1,n)\n    r = torch.arange(w, device=masks.device, dtype=x1.dtype)[None, None, :]  # rows shape(1,w,1)\n    c = torch.arange(h, device=masks.device, dtype=x1.dtype)[None, :, None]  # cols shape(h,1,1)\n\n    return masks * ((r >= x1) * (r < x2) * (c >= y1) * (c < y2))\n\n\ndef process_mask(protos, masks_in, bboxes, shape ,upsample=False, th=0.5):\n    \"\"\"\n    Crop before upsample.\n    proto_out: [mask_dim, mask_h, mask_w]\n    out_masks: [n, mask_dim], n is number of masks after nms\n    bboxes: [n, 4], n is number of masks after nms\n    shape:input_image_size, (h, w)\n\n    return: h, w, n\n    \"\"\"\n\n    c, mh, mw = protos.shape  # CHW\n    ih, iw = shape\n    masks = (masks_in @ protos.float().view(c, -1)).sigmoid().view(-1, mh, mw)  # CHW\n\n    downsampled_bboxes = bboxes.clone()\n    downsampled_bboxes[:, 0] *= mw / iw\n    downsampled_bboxes[:, 2] *= mw / iw\n    downsampled_bboxes[:, 3] *= mh / ih\n    downsampled_bboxes[:, 1] *= mh / ih\n\n    masks = crop(masks, downsampled_bboxes)  # CHW\n    if upsample:\n        masks = F.interpolate(masks[None], shape, mode='bilinear', align_corners=False)[0]  # CHW\n    return masks.gt_(th)","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:05.849264Z","iopub.execute_input":"2023-07-29T07:08:05.849749Z","iopub.status.idle":"2023-07-29T07:08:05.866842Z","shell.execute_reply.started":"2023-07-29T07:08:05.849713Z","shell.execute_reply":"2023-07-29T07:08:05.865759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nimport torchvision\n\ndef non_max_suppression(\n        prediction,\n        conf_thres=0.25,\n        iou_thres=0.45,\n        classes=None,\n        agnostic=False,\n        multi_label=False,\n        labels=(),\n        max_det=300,\n        nm=0,  # number of masks\n):\n    \"\"\"Non-Maximum Suppression (NMS) on inference results to reject overlapping detections\n\n    Returns:\n         list of detections, on (n,6) tensor per image [xyxy, conf, cls]\n    \"\"\"\n\n    bs = prediction.shape[0]  # batch size\n    nc = prediction.shape[2] - nm - 5  # number of classes\n    xc = prediction[..., 4] > conf_thres  # candidates\n\n    print(\"nms nc:\",nc)\n    \n    # Checks\n    assert 0 <= conf_thres <= 1, f'Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0'\n    assert 0 <= iou_thres <= 1, f'Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0'\n\n    # Settings\n    # min_wh = 2  # (pixels) minimum box width and height\n    max_wh = 7680  # (pixels) maximum box width and height\n    max_nms = 30000  # maximum number of boxes into torchvision.ops.nms()\n    time_limit = 0.5 + 0.05 * bs  # seconds to quit after\n    redundant = True  # require redundant detections\n    multi_label &= nc > 1  # multiple labels per box (adds 0.5ms/img)\n    merge = False  # use merge-NMS\n\n    t = time.time()\n    mi = 5 + nc  # mask start index\n    output = [torch.zeros((0, 6 + nm), device=prediction.device)] * bs\n    output_idx = [torch.zeros((0, 1), device=prediction.device)] * bs\n    \n    for xi, x in enumerate(prediction):  # image index, image inference\n        print(\"1nms:\",xi)\n        print(\"2nms:\",x.shape)# 25200,40\n \n        # 追加: 元のインデックス情報を保存するためのインデックス列を追加\n        original_indices = torch.arange(x.shape[0], device=x.device).float().view(-1, 1)\n        print(\"nms:\",original_indices.shape)# 25200,40\n\n        # Apply constraints\n        x = x[xc[xi]]  # confidence\n#         print(original_indices)\n        \n        # If none remain process next image\n        if not x.shape[0]:\n            continue\n\n        # Compute conf\n        x[:, 5:] *= x[:, 4:5]  # conf = obj_conf * cls_conf\n\n        # 最後に追加\n        x = torch.cat((x, original_indices[xc[xi]]), 1)\n        print(\"3nms:\",x.shape)\n        \n        # Box/Mask\n        box = xywh2xyxy(x[:, :4])  # center_x, center_y, width, height) to (x1, y1, x2, y2)\n        mask = x[:, mi:]  # zero columns if no masks\n\n        print(\"3.5nms:\",x.shape)\n        # Detections matrix nx6 (xyxy, conf, cls)\n        if multi_label:\n            i, j = (x[:, 5:mi] > conf_thres).nonzero(as_tuple=False).T\n            x = torch.cat((box[i], x[i, 5 + j, None], j[:, None].float(), mask[i]), 1)\n            print(\"nms: multi_label\")\n        else:  # best class only\n            conf, j = x[:, 5:mi].max(1, keepdim=True)\n            x = torch.cat((box, conf, j.float(), mask), 1)[conf.view(-1) > conf_thres]\n        \n        print(\"4nms:\",x.shape)\n        # Filter by class\n        if classes is not None:\n            x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)]\n\n        # Apply finite constraint\n        # if not torch.isfinite(x).all():\n        #     x = x[torch.isfinite(x).all(1)]\n\n        # Check shape\n        n = x.shape[0]  # number of boxes\n        if not n:  # no boxes\n            continue\n        elif n > max_nms:  # excess boxes\n            x = x[x[:, 4].argsort(descending=True)[:max_nms]]  # sort by confidence\n        else:\n            x = x[x[:, 4].argsort(descending=True)]  # sort by confidence\n\n        # Batched NMS\n        c = x[:, 5:6] * (0 if agnostic else max_wh)  # classes\n        boxes, scores = x[:, :4] + c, x[:, 4]  # boxes (offset by class), scores\n        i = torchvision.ops.nms(boxes, scores, iou_thres)  # NMS\n        \n        if i.shape[0] > max_det:  # limit detections\n            i = i[:max_det]\n  \n        # 追加: original_indices を NMS で選択されたものだけ取得\n#         print(original_indices.shape)\n#         output[xi] = torch.cat((x[i], original_indices), 1) \n        output_idx[xi] = x[i,-1]\n#         print(original_indices)\n        print(\"nms:\",x.shape)\n        output[xi] = x[i,:-1]\n        if (time.time() - t) > time_limit:\n            LOGGER.warning(f'WARNING: NMS time limit {time_limit:.3f}s exceeded')\n            break  # time limit exceeded\n\n    return output, output_idx","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:05.868817Z","iopub.execute_input":"2023-07-29T07:08:05.869706Z","iopub.status.idle":"2023-07-29T07:08:05.897503Z","shell.execute_reply.started":"2023-07-29T07:08:05.869654Z","shell.execute_reply":"2023-07-29T07:08:05.896352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def segment(\n        weights,\n        source,\n        data,\n        project,\n        imgsz=(640, 640),  # inference size (height, width)\n        conf_thres=0.001,  # confidence threshold\n        iou_thres=0.55,  # NMS IOU threshold\n        max_det=2000,  # maximum detections per image\n        device='',  # cuda device, i.e. 0 or 0,1,2,3 or cpu\n        view_img=False,  # show results\n        save_txt=False,  # save results to *.txt\n        save_conf=False,  # save confidences in --save-txt labels\n        save_crop=False,  # save cropped prediction boxes\n        nosave=False,  # do not save images/videos\n        classes=None,  # filter by class: --class 0, or --class 0 2 3\n        agnostic_nms=False,  # class-agnostic NMS\n        augment=False,  # augmented inference\n        visualize=False,  # visualize features\n        update=False,  # update all models\n        name='exp',  # save results to project/name\n        exist_ok=False,  # existing project/name ok, do not increment\n        line_thickness=3,  # bounding box thickness (pixels)\n        hide_labels=False,  # hide labels\n        hide_conf=False,  # hide confidences\n        half=False,  # use FP16 half-precision inference\n        dnn=False,  # use OpenCV DNN for ONNX inference\n    ):\n    with open('/kaggle/working/submission.csv', 'w') as sub_file:\n        # Write header\n        sub_file.write('id,height,width,prediction_string\\n')\n\n        device = select_device(device)\n        \n        models = []\n        for weight in weights:\n            print(weight)\n            models.append(DetectMultiBackend(weight, device=device, dnn=dnn, data=data, fp16=half))\n        \n#         model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)\n        \n        stride, names, pt = models[0].stride, models[0].names, models[0].pt\n        imgsz = check_img_size(imgsz, s=stride)  # check image size\n\n        dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)\n        bs = 1  # batch_size\n\n        models[0].warmup(imgsz=(1 if pt else bs, 3, *imgsz))  # warmup\n        seen, windows, dt = 0, [], (Profile(), Profile(), Profile())\n        \n        for path, im, im0s, vid_cap, s in dataset: \n            # Write id and size\n            image_id = Path(path).stem\n            sub_file.write(f'{image_id},512,512,')\n            \n            if image_id in dict_of_tiles:\n                annotations = dict_of_tiles[image_id]\n                glomerulus_mask = get_glomerulus_mask(annotations)\n            else:\n                annotations = []\n                glomerulus_mask = np.ones(shape=(512,512)).astype(bool)\n                \n            print(im.shape)\n            im = torch.from_numpy(im).to(device)\n            im = im.half() if models[0].fp16 else im.float()  # uint8 to fp16/32\n            im /= 255  # 0 - 255 to 0.0 - 1.0\n            if len(im.shape) == 3:\n                im = im[None]  # expand for batch dim\n\n                \n            ###############################\n            # Inference\n            preds=[]\n            protos=[]\n            model_ind=[]\n            for iii, model in enumerate(models):\n                # pred\n                pred, out = model(im, augment=False, visualize=visualize)\n                proto = out[1]\n                print(pred.shape)\n                print(proto.shape)\n                \n                model_ind.append(torch.ones((pred.shape[1]))*iii)\n                preds.append(pred)\n                protos.append(proto)\n            \n            ###############################\n            # concat\n            pred = torch.cat(preds, dim=1)\n            model_ind = torch.cat(model_ind)\n            print(\"model_ind.shape:\", model_ind.shape)\n            \n            print(\"pre nms:\",pred[0].shape) # torch.Size([82, 38])\n            print(\"pre nms:\",proto[0].shape) # torch.Size([82, 38])\n            \n            # NMS\n            pred, output_idx = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det, nm=32)\n\n            print(\"after nms:\",pred[0].shape) # torch.Size([82, 38])\n            print(\"after nms:\",proto[0].shape) # torch.Size([82, 38])\n            print(\"after nms:\",output_idx[0].shape) # torch.Size([82, 38])\n            \n            debug_ = np.zeros((512,512,3))\n#             print(proto.shape)\n            for i, (det_all, bbox_ind) in enumerate(zip(pred, output_idx)):  # per image\n                seen += 1\n                if len(det_all):\n                    bbox_ind = bbox_ind.long().cpu()\n                    model_ind = model_ind.cpu()\n                    print(\"bbox_ind.shape:\", bbox_ind.shape)\n                    print(\"model_ind.shape:\", model_ind.shape)\n                    model_ind_pick = model_ind[bbox_ind]\n                    print(\"model_ind_pick:\", model_ind_pick.shape)\n                    print(\"det_all:\",det_all.shape)\n                    \n                    for unique_id in torch.unique(model_ind_pick):\n                        print(\"unique_id:\",unique_id)\n                        model_ind_pick_unique = torch.where(model_ind_pick == unique_id)[0]\n                        print(\"model_ind_pick_unique:\",model_ind_pick_unique.shape)\n                        \n                        det = det_all[model_ind_pick_unique, :]\n                        #proto = protos[0]\n                        proto = protos[unique_id.long().cpu().numpy()]\n\n                        masks = process_mask(proto[i], det[:, 6:], det[:, :4], im.shape[2:], upsample=True)  # HWC\n                        bboxs = det[:, :4]\n                        confs = det[:, 4]\n                        clasf = det[:, 5]\n\n                        for mask, confidence, classification in zip(masks, confs, clasf):\n                            binary_mask = mask.cpu().numpy()\n\n                            binary_mask = binary_mask.astype(np.uint8)    \n                            width = 512\n                            height = 512\n                            binary_mask = cv2.resize(binary_mask, (width, height), interpolation=cv2.INTER_NEAREST)\n                            \n#                             # pp ---------\n                            num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(binary_mask*255)\n                            if len(stats)==1:\n                                continue \n                            max_label = 1 + np.argmax(stats[1:, cv2.CC_STAT_AREA])\n                            # ラベル画像を作成\n                            binary_mask = np.where(labels == max_label, 1, 0).astype('uint8')\n                            # pp ---------\n                            \n                            binary_mask = binary_mask.astype(bool)\n                            binary_mask = binary_mask & glomerulus_mask\n\n                            encoded_mask = encode_binary_mask(binary_mask)\n                            sub_file.write(f'{int(classification)} {confidence} {encoded_mask.decode()} ' )\n                            debug_[:,:,1] += binary_mask.astype(np.uint8)*16\n            \n            cv2.imwrite(\"debug_.png\",debug_)\n            sub_file.write('\\n')\n","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:10:59.326540Z","iopub.execute_input":"2023-07-29T07:10:59.327119Z","iopub.status.idle":"2023-07-29T07:10:59.361505Z","shell.execute_reply.started":"2023-07-29T07:10:59.327081Z","shell.execute_reply":"2023-07-29T07:10:59.360157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segment(source='/kaggle/input/hubmap-hacking-the-human-vasculature/test',\n            data='/kaggle/working/hubmap-coco.yaml',\n            imgsz=(800, 800), \n            classes=0,\n            weights=weights_list,\n            name='yolov7-predict',\n            project='yolov7-predict',\n            exist_ok=True,\n            nosave=True,\n            save_txt=True,\n            view_img=True,\n            augment=False,\n            )","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:12:19.675517Z","iopub.execute_input":"2023-07-29T07:12:19.676534Z","iopub.status.idle":"2023-07-29T07:12:24.018454Z","shell.execute_reply.started":"2023-07-29T07:12:19.676495Z","shell.execute_reply":"2023-07-29T07:12:24.017365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-07-29T07:08:10.091895Z","iopub.execute_input":"2023-07-29T07:08:10.095828Z","iopub.status.idle":"2023-07-29T07:08:11.399404Z","shell.execute_reply.started":"2023-07-29T07:08:10.095780Z","shell.execute_reply":"2023-07-29T07:08:11.397970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}