{"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":"!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-06-17T03:59:11.407486Z","iopub.execute_input":"2023-06-17T03:59:11.407894Z","iopub.status.idle":"2023-06-17T03:59:44.849087Z","shell.execute_reply.started":"2023-06-17T03:59:11.407864Z","shell.execute_reply":"2023-06-17T03:59:44.847917Z"},"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","metadata":{"execution":{"iopub.status.busy":"2023-06-17T03:59:44.851577Z","iopub.execute_input":"2023-06-17T03:59:44.852008Z","iopub.status.idle":"2023-06-17T03:59:44.865919Z","shell.execute_reply.started":"2023-06-17T03:59:44.851944Z","shell.execute_reply":"2023-06-17T03:59:44.864907Z"},"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-06-17T03:59:44.867406Z","iopub.execute_input":"2023-06-17T03:59:44.867816Z","iopub.status.idle":"2023-06-17T04:00:17.653982Z","shell.execute_reply.started":"2023-06-17T03:59:44.867780Z","shell.execute_reply":"2023-06-17T04:00:17.652786Z"},"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-06-17T04:00:17.657300Z","iopub.execute_input":"2023-06-17T04:00:17.657671Z","iopub.status.idle":"2023-06-17T04:00:19.546413Z","shell.execute_reply.started":"2023-06-17T04:00:17.657632Z","shell.execute_reply":"2023-06-17T04:00:19.545133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yolo.seg.segment import predict","metadata":{"execution":{"iopub.status.busy":"2023-06-17T04:00:19.548489Z","iopub.execute_input":"2023-06-17T04:00:19.548872Z","iopub.status.idle":"2023-06-17T04:00:20.516357Z","shell.execute_reply.started":"2023-06-17T04:00:19.548831Z","shell.execute_reply":"2023-06-17T04:00:20.515396Z"},"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-06-17T04:00:20.517597Z","iopub.execute_input":"2023-06-17T04:00:20.518440Z","iopub.status.idle":"2023-06-17T04:00:20.523302Z","shell.execute_reply.started":"2023-06-17T04:00:20.518406Z","shell.execute_reply":"2023-06-17T04:00:20.522433Z"},"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\n\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, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh)\nfrom utils.plots import Annotator, colors, save_one_box\nfrom utils.segment.general import process_mask, 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-06-17T04:00:20.524460Z","iopub.execute_input":"2023-06-17T04:00:20.525222Z","iopub.status.idle":"2023-06-17T04:00:20.534862Z","shell.execute_reply.started":"2023-06-17T04:00:20.525190Z","shell.execute_reply":"2023-06-17T04:00:20.534012Z"},"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\n    # compress and base64 encoding --\n    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n    base64_str = base64.b64encode(binary_str)\n    return base64_str","metadata":{"execution":{"iopub.status.busy":"2023-06-17T04:00:20.536015Z","iopub.execute_input":"2023-06-17T04:00:20.537174Z","iopub.status.idle":"2023-06-17T04:00:20.545982Z","shell.execute_reply.started":"2023-06-17T04:00:20.537143Z","shell.execute_reply":"2023-06-17T04:00:20.545115Z"},"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-06-17T04:00:20.547210Z","iopub.execute_input":"2023-06-17T04:00:20.547624Z","iopub.status.idle":"2023-06-17T04:00:25.341088Z","shell.execute_reply.started":"2023-06-17T04:00:20.547592Z","shell.execute_reply":"2023-06-17T04:00:25.340153Z"},"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-06-17T04:00:25.344621Z","iopub.execute_input":"2023-06-17T04:00:25.345083Z","iopub.status.idle":"2023-06-17T04:00:25.351568Z","shell.execute_reply.started":"2023-06-17T04:00:25.345048Z","shell.execute_reply":"2023-06-17T04:00:25.350685Z"},"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-06-17T04:00:25.352944Z","iopub.execute_input":"2023-06-17T04:00:25.353771Z","iopub.status.idle":"2023-06-17T04:00:25.362153Z","shell.execute_reply.started":"2023-06-17T04:00:25.353739Z","shell.execute_reply":"2023-06-17T04:00:25.361137Z"},"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, non_max_suppression, 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        # Usage:\n        #   PyTorch:              weights = *.pt\n        #   TorchScript:                    *.torchscript\n        #   ONNX Runtime:                   *.onnx\n        #   ONNX OpenCV DNN:                *.onnx with --dnn\n        #   OpenVINO:                       *.xml\n        #   CoreML:                         *.mlmodel\n        #   TensorRT:                       *.engine\n        #   TensorFlow SavedModel:          *_saved_model\n        #   TensorFlow GraphDef:            *.pb\n        #   TensorFlow Lite:                *.tflite\n        #   TensorFlow Edge TPU:            *_edgetpu.tflite\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        elif jit:  # TorchScript\n            LOGGER.info(f'Loading {w} for TorchScript inference...')\n            extra_files = {'config.txt': ''}  # model metadata\n            model = torch.jit.load(w, _extra_files=extra_files)\n            model.half() if fp16 else model.float()\n            if extra_files['config.txt']:  # load metadata dict\n                d = json.loads(extra_files['config.txt'],\n                               object_hook=lambda d: {int(k) if k.isdigit() else k: v\n                                                      for k, v in d.items()})\n                stride, names = int(d['stride']), d['names']\n        elif dnn:  # ONNX OpenCV DNN\n            LOGGER.info(f'Loading {w} for ONNX OpenCV DNN inference...')\n            check_requirements(('opencv-python>=4.5.4',))\n            net = cv2.dnn.readNetFromONNX(w)\n        elif onnx:  # ONNX Runtime\n            LOGGER.info(f'Loading {w} for ONNX Runtime inference...')\n            cuda = torch.cuda.is_available() and device.type != 'cpu'\n            check_requirements(('onnx', 'onnxruntime-gpu' if cuda else 'onnxruntime'))\n            import onnxruntime\n            providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider']\n            session = onnxruntime.InferenceSession(w, providers=providers)\n            meta = session.get_modelmeta().custom_metadata_map  # metadata\n            if 'stride' in meta:\n                stride, names = int(meta['stride']), eval(meta['names'])\n        elif xml:  # OpenVINO\n            LOGGER.info(f'Loading {w} for OpenVINO inference...')\n            check_requirements(('openvino',))  # requires openvino-dev: https://pypi.org/project/openvino-dev/\n            from openvino.runtime import Core, Layout, get_batch\n            ie = Core()\n            if not Path(w).is_file():  # if not *.xml\n                w = next(Path(w).glob('*.xml'))  # get *.xml file from *_openvino_model dir\n            network = ie.read_model(model=w, weights=Path(w).with_suffix('.bin'))\n            if network.get_parameters()[0].get_layout().empty:\n                network.get_parameters()[0].set_layout(Layout(\"NCHW\"))\n            batch_dim = get_batch(network)\n            if batch_dim.is_static:\n                batch_size = batch_dim.get_length()\n            executable_network = ie.compile_model(network, device_name=\"CPU\")  # device_name=\"MYRIAD\" for Intel NCS2\n            output_layer = next(iter(executable_network.outputs))\n            meta = Path(w).with_suffix('.yaml')\n            if meta.exists():\n                stride, names = self._load_metadata(meta)  # load metadata\n        elif engine:  # TensorRT\n            LOGGER.info(f'Loading {w} for TensorRT inference...')\n            import tensorrt as trt  # https://developer.nvidia.com/nvidia-tensorrt-download\n            check_version(trt.__version__, '7.0.0', hard=True)  # require tensorrt>=7.0.0\n            if device.type == 'cpu':\n                device = torch.device('cuda:0')\n            Binding = namedtuple('Binding', ('name', 'dtype', 'shape', 'data', 'ptr'))\n            logger = trt.Logger(trt.Logger.INFO)\n            with open(w, 'rb') as f, trt.Runtime(logger) as runtime:\n                model = runtime.deserialize_cuda_engine(f.read())\n            context = model.create_execution_context()\n            bindings = OrderedDict()\n            fp16 = False  # default updated below\n            dynamic = False\n            for index in range(model.num_bindings):\n                name = model.get_binding_name(index)\n                dtype = trt.nptype(model.get_binding_dtype(index))\n                if model.binding_is_input(index):\n                    if -1 in tuple(model.get_binding_shape(index)):  # dynamic\n                        dynamic = True\n                        context.set_binding_shape(index, tuple(model.get_profile_shape(0, index)[2]))\n                    if dtype == np.float16:\n                        fp16 = True\n                shape = tuple(context.get_binding_shape(index))\n                im = torch.from_numpy(np.empty(shape, dtype=dtype)).to(device)\n                bindings[name] = Binding(name, dtype, shape, im, int(im.data_ptr()))\n            binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items())\n            batch_size = bindings['images'].shape[0]  # if dynamic, this is instead max batch size\n        elif coreml:  # CoreML\n            LOGGER.info(f'Loading {w} for CoreML inference...')\n            import coremltools as ct\n            model = ct.models.MLModel(w)\n        else:  # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)\n            if saved_model:  # SavedModel\n                LOGGER.info(f'Loading {w} for TensorFlow SavedModel inference...')\n                import tensorflow as tf\n                keras = False  # assume TF1 saved_model\n                model = tf.keras.models.load_model(w) if keras else tf.saved_model.load(w)\n            elif pb:  # GraphDef https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt\n                LOGGER.info(f'Loading {w} for TensorFlow GraphDef inference...')\n                import tensorflow as tf\n\n                def wrap_frozen_graph(gd, inputs, outputs):\n                    x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=\"\"), [])  # wrapped\n                    ge = x.graph.as_graph_element\n                    return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs))\n\n                gd = tf.Graph().as_graph_def()  # graph_def\n                with open(w, 'rb') as f:\n                    gd.ParseFromString(f.read())\n                frozen_func = wrap_frozen_graph(gd, inputs=\"x:0\", outputs=\"Identity:0\")\n            elif tflite or edgetpu:  # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python\n                try:  # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu\n                    from tflite_runtime.interpreter import Interpreter, load_delegate\n                except ImportError:\n                    import tensorflow as tf\n                    Interpreter, load_delegate = tf.lite.Interpreter, tf.lite.experimental.load_delegate,\n                if edgetpu:  # Edge TPU https://coral.ai/software/#edgetpu-runtime\n                    LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...')\n                    delegate = {\n                        'Linux': 'libedgetpu.so.1',\n                        'Darwin': 'libedgetpu.1.dylib',\n                        'Windows': 'edgetpu.dll'}[platform.system()]\n                    interpreter = Interpreter(model_path=w, experimental_delegates=[load_delegate(delegate)])\n                else:  # Lite\n                    LOGGER.info(f'Loading {w} for TensorFlow Lite inference...')\n                    interpreter = Interpreter(model_path=w)  # load TFLite model\n                interpreter.allocate_tensors()  # allocate\n                input_details = interpreter.get_input_details()  # inputs\n                output_details = interpreter.get_output_details()  # outputs\n            elif tfjs:\n                raise NotImplementedError('ERROR: YOLOv5 TF.js inference is not supported')\n            else:\n                raise NotImplementedError(f'ERROR: {w} is not a supported format')\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        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        elif self.jit:  # TorchScript\n            y = self.model(im)[0]\n        elif self.dnn:  # ONNX OpenCV DNN\n            im = im.cpu().numpy()  # torch to numpy\n            self.net.setInput(im)\n            y = self.net.forward()\n        elif self.onnx:  # ONNX Runtime\n            im = im.cpu().numpy()  # torch to numpy\n            y = self.session.run([self.session.get_outputs()[0].name], {self.session.get_inputs()[0].name: im})[0]\n        elif self.xml:  # OpenVINO\n            im = im.cpu().numpy()  # FP32\n            y = self.executable_network([im])[self.output_layer]\n        elif self.engine:  # TensorRT\n            if self.dynamic and im.shape != self.bindings['images'].shape:\n                i_in, i_out = (self.model.get_binding_index(x) for x in ('images', 'output'))\n                self.context.set_binding_shape(i_in, im.shape)  # reshape if dynamic\n                self.bindings['images'] = self.bindings['images']._replace(shape=im.shape)\n                self.bindings['output'].data.resize_(tuple(self.context.get_binding_shape(i_out)))\n            s = self.bindings['images'].shape\n            assert im.shape == s, f\"input size {im.shape} {'>' if self.dynamic else 'not equal to'} max model size {s}\"\n            self.binding_addrs['images'] = int(im.data_ptr())\n            self.context.execute_v2(list(self.binding_addrs.values()))\n            y = self.bindings['output'].data\n        elif self.coreml:  # CoreML\n            im = im.permute(0, 2, 3, 1).cpu().numpy()  # torch BCHW to numpy BHWC shape(1,320,192,3)\n            im = Image.fromarray((im[0] * 255).astype('uint8'))\n            # im = im.resize((192, 320), Image.ANTIALIAS)\n            y = self.model.predict({'image': im})  # coordinates are xywh normalized\n            if 'confidence' in y:\n                box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]])  # xyxy pixels\n                conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float)\n                y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1)\n            else:\n                k = 'var_' + str(sorted(int(k.replace('var_', '')) for k in y)[-1])  # output key\n                y = y[k]  # output\n        else:  # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)\n            im = im.permute(0, 2, 3, 1).cpu().numpy()  # torch BCHW to numpy BHWC shape(1,320,192,3)\n            if self.saved_model:  # SavedModel\n                y = (self.model(im, training=False) if self.keras else self.model(im)).numpy()\n            elif self.pb:  # GraphDef\n                y = self.frozen_func(x=self.tf.constant(im)).numpy()\n            else:  # Lite or Edge TPU\n                input, output = self.input_details[0], self.output_details[0]\n                int8 = input['dtype'] == np.uint8  # is TFLite quantized uint8 model\n                if int8:\n                    scale, zero_point = input['quantization']\n                    im = (im / scale + zero_point).astype(np.uint8)  # de-scale\n                self.interpreter.set_tensor(input['index'], im)\n                self.interpreter.invoke()\n                y = self.interpreter.get_tensor(output['index'])\n                if int8:\n                    scale, zero_point = output['quantization']\n                    y = (y.astype(np.float32) - zero_point) * scale  # re-scale\n            y[..., :4] *= [w, h, w, h]  # xywh normalized to pixels\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-06-17T04:00:25.363840Z","iopub.execute_input":"2023-06-17T04:00:25.364284Z","iopub.status.idle":"2023-06-17T04:00:25.427648Z","shell.execute_reply.started":"2023-06-17T04:00:25.364252Z","shell.execute_reply":"2023-06-17T04:00:25.426754Z"},"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.25,  # confidence threshold\n        iou_thres=0.45,  # NMS IOU threshold\n        max_det=1000,  # 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\n        device = select_device(device)\n        \n        single_model = DetectMultiBackend(weights[0], device=device, dnn=dnn, data=data, fp16=half)\n        model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)\n        stride, names, pt = model.stride, model.names, model.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        model.warmup(imgsz=(1 if pt else bs, 3, *imgsz))  # warmup\n        seen, windows, dt = 0, [], (Profile(), Profile(), Profile())\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=imgsz).astype(bool)\n                \n                \n            with dt[0]:\n                im = torch.from_numpy(im).to(device)\n                im = im.half() if model.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            # Inference\n            with dt[1]:\n                single_pred, out = single_model(im, augment=augment, visualize=visualize)\n                visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False\n                pred, _ = model(im, augment=augment, visualize=visualize)\n                #print(pred.shape)\n                proto = out[1]\n            # NMS\n            with dt[2]:\n                pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det, nm=32)\n            for i, det in enumerate(pred):  # per image\n                seen += 1\n\n                if len(det):\n                    masks = process_mask(proto[i], det[:, 6:], det[:, :4], im.shape[2:], upsample=True)  # HWC\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                        kernel = np.ones(shape=(3, 3), dtype=np.uint8)\n                        binary_mask = cv2.dilate(binary_mask, kernel, 3)\n                        \n                        binary_mask = binary_mask.astype(bool)\n                        binary_mask = binary_mask & glomerulus_mask\n                        \n                        kernel = np.ones(shape=(3, 3))\n\n                        encoded_mask = encode_binary_mask(binary_mask)\n                        sub_file.write(f'{int(classification)} {confidence} {encoded_mask.decode()} ' )\n        \n            sub_file.write('\\n')","metadata":{"execution":{"iopub.status.busy":"2023-06-17T04:00:25.429184Z","iopub.execute_input":"2023-06-17T04:00:25.429590Z","iopub.status.idle":"2023-06-17T04:00:25.450568Z","shell.execute_reply.started":"2023-06-17T04:00:25.429558Z","shell.execute_reply":"2023-06-17T04:00:25.449742Z"},"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=(512, 512), \n            classes=0,\n            weights=['/kaggle/input/yolov7-weights-and-wheels/yolov7-fine-tune/yolov7-fine-tune/weights/best.pt',\n                     '/kaggle/input/hubmap-yolov7-weights/yolo_v7_best_fold0.pt',\n                     '/kaggle/input/yolov7-weights-and-wheels/yolov7-fine-tune/yolov7-fine-tune/weights/last.pt'],\n            name='yolov7-predict',\n            project='yolov7-predict',\n            exist_ok=True,\n            nosave=True,\n            save_txt=True,\n            view_img=True,\n            )","metadata":{"execution":{"iopub.status.busy":"2023-06-17T04:00:25.453660Z","iopub.execute_input":"2023-06-17T04:00:25.453946Z","iopub.status.idle":"2023-06-17T04:00:37.627174Z","shell.execute_reply.started":"2023-06-17T04:00:25.453915Z","shell.execute_reply":"2023-06-17T04:00:37.625020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-06-17T04:00:37.628674Z","iopub.execute_input":"2023-06-17T04:00:37.629171Z","iopub.status.idle":"2023-06-17T04:00:38.709460Z","shell.execute_reply.started":"2023-06-17T04:00:37.629134Z","shell.execute_reply":"2023-06-17T04:00:38.708287Z"},"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":[]}]}