{"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\nhttps://www.kaggle.com/code/fnands/a-quick-yolov7-baseline-inference\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":{"papermill":{"duration":0.006433,"end_time":"2023-06-19T05:59:43.630970","exception":false,"start_time":"2023-06-19T05:59:43.624537","status":"completed"},"tags":[]}},{"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.execute_input":"2023-06-19T05:59:43.644420Z","iopub.status.busy":"2023-06-19T05:59:43.643438Z","iopub.status.idle":"2023-06-19T06:00:17.513483Z","shell.execute_reply":"2023-06-19T06:00:17.512380Z"},"papermill":{"duration":33.879469,"end_time":"2023-06-19T06:00:17.516007","exception":false,"start_time":"2023-06-19T05:59:43.636538","status":"completed"},"tags":[]},"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.execute_input":"2023-06-19T06:00:17.532401Z","iopub.status.busy":"2023-06-19T06:00:17.532075Z","iopub.status.idle":"2023-06-19T06:00:17.543099Z","shell.execute_reply":"2023-06-19T06:00:17.542157Z"},"papermill":{"duration":0.0215,"end_time":"2023-06-19T06:00:17.545118","exception":false,"start_time":"2023-06-19T06:00:17.523618","status":"completed"},"tags":[]},"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.execute_input":"2023-06-19T06:00:17.560342Z","iopub.status.busy":"2023-06-19T06:00:17.560073Z","iopub.status.idle":"2023-06-19T06:00:51.403348Z","shell.execute_reply":"2023-06-19T06:00:51.402118Z"},"papermill":{"duration":33.853772,"end_time":"2023-06-19T06:00:51.405954","exception":false,"start_time":"2023-06-19T06:00:17.552182","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/yolov7-weights-and-wheels/yolov7 yolo","metadata":{"execution":{"iopub.execute_input":"2023-06-19T06:00:51.428811Z","iopub.status.busy":"2023-06-19T06:00:51.428464Z","iopub.status.idle":"2023-06-19T06:00:53.555474Z","shell.execute_reply":"2023-06-19T06:00:53.554214Z"},"papermill":{"duration":2.141279,"end_time":"2023-06-19T06:00:53.558031","exception":false,"start_time":"2023-06-19T06:00:51.416752","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yolo.seg.segment import predict","metadata":{"execution":{"iopub.execute_input":"2023-06-19T06:00:53.581237Z","iopub.status.busy":"2023-06-19T06:00:53.580917Z","iopub.status.idle":"2023-06-19T06:00:58.337958Z","shell.execute_reply":"2023-06-19T06:00:58.337020Z"},"papermill":{"duration":4.771438,"end_time":"2023-06-19T06:00:58.340421","exception":false,"start_time":"2023-06-19T06:00:53.568983","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a yaml file as expected by YOLOv7 (and others)\n# 这段代码的作用是将一个YAML格式的文本写入到指定的文件中。\n\n# 首先，定义了一个名为yaml_text的字符串变量，其中包含了一些类名的信息。这些类名被存储在一个名为names的字典中，键为0、1和2,对应的值分别为blood_vessel、glomerulus和unsure。\n\n# 接下来，使用with语句打开一个名为/kaggle/working/hubmap-coco.yaml的文件，并以写入模式('w')打开它。这个文件路径是一个示例路径，实际应用中需要根据实际情况修改。\n\n# 最后，使用write()方法将yaml_text字符串写入到文件中。当with语句块执行完毕后，文件会自动关闭。\n\n\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.execute_input":"2023-06-19T06:00:58.366032Z","iopub.status.busy":"2023-06-19T06:00:58.364805Z","iopub.status.idle":"2023-06-19T06:00:58.370663Z","shell.execute_reply":"2023-06-19T06:00:58.369842Z"},"papermill":{"duration":0.021234,"end_time":"2023-06-19T06:00:58.372747","exception":false,"start_time":"2023-06-19T06:00:58.351513","status":"completed"},"tags":[]},"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.execute_input":"2023-06-19T06:00:58.395287Z","iopub.status.busy":"2023-06-19T06:00:58.394430Z","iopub.status.idle":"2023-06-19T06:00:58.401608Z","shell.execute_reply":"2023-06-19T06:00:58.400700Z"},"papermill":{"duration":0.020588,"end_time":"2023-06-19T06:00:58.403644","exception":false,"start_time":"2023-06-19T06:00:58.383056","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n这段代码定义了一个名为encode_binary_mask的函数，它接受一个二进制掩码(numpy数组)作为输入，并将其转换为OID挑战编码的ASCII文本。\n\n首先，检查输入掩码的数据类型是否为布尔型，如果不是，则抛出一个ValueError异常。\n\n使用np.squeeze()函数去除掩码数组中的单维度条目。\n\n检查掩码数组的形状是否为二维，如果不是，则抛出一个ValueError异常。\n\n将输入掩码转换为预期的COCO API输入格式，即将其重塑为三维数组，其中第一个维度表示行数，第二个维度表示列数，第三个维度表示通道数(在这里我们只有一个通道)。然后将数组的数据类型转换为无符号8位整数(np.uint8),并将其转换为Fortran连续数组。\n\n使用coco_mask.encode()函数对转换后的掩码进行RLE编码，得到编码后的掩码。\n\n使用zlib.compress()函数对编码后的掩码进行压缩，并使用base64.b64encode()函数对其进行Base64编码。\n\n最后，返回Base64编码后的字符串。\n'''\ndef 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.execute_input":"2023-06-19T06:00:58.427173Z","iopub.status.busy":"2023-06-19T06:00:58.425611Z","iopub.status.idle":"2023-06-19T06:00:58.433338Z","shell.execute_reply":"2023-06-19T06:00:58.432476Z"},"papermill":{"duration":0.021129,"end_time":"2023-06-19T06:00:58.435299","exception":false,"start_time":"2023-06-19T06:00:58.414170","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n这段代码的作用是从一个JSONL文件中读取数据，并将其转换为Python对象列表。\n\n首先，使用with open()语句打开指定的JSONL文件(路径为'/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl'),并将其赋值给变量json_file。这里使用了'r'模式，表示以只读方式打开文件。\n\n接下来，使用list()函数将文件中的每一行都读取出来，并存储到变量json_list中。这个列表包含了JSONL文件中的所有行。\n\n然后，创建一个空列表tiles_dicts,用于存储从JSON字符串转换而来的Python对象。\n\n最后，使用for循环遍历json_list中的每个JSON字符串，并使用json.loads()函数将其转换为Python对象。这个对象会被添加到tiles_dicts列表中。最终，tiles_dicts列表中就包含了所有从JSONL文件中读取出来的Python对象。\n'''\n# 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.execute_input":"2023-06-19T06:00:58.458250Z","iopub.status.busy":"2023-06-19T06:00:58.456872Z","iopub.status.idle":"2023-06-19T06:01:03.188466Z","shell.execute_reply":"2023-06-19T06:01:03.187508Z"},"papermill":{"duration":4.745248,"end_time":"2023-06-19T06:01:03.190969","exception":false,"start_time":"2023-06-19T06:00:58.445721","status":"completed"},"tags":[]},"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.execute_input":"2023-06-19T06:01:03.214745Z","iopub.status.busy":"2023-06-19T06:01:03.213140Z","iopub.status.idle":"2023-06-19T06:01:03.220833Z","shell.execute_reply":"2023-06-19T06:01:03.219960Z"},"papermill":{"duration":0.021158,"end_time":"2023-06-19T06:01:03.222876","exception":false,"start_time":"2023-06-19T06:01:03.201718","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n这段代码定义了一个名为get_glomerulus_mask的函数，它接受两个参数：一个字典类型的注释(annotations)和一个元组类型的掩码形状(mask_shape),默认值为(512, 512)。该函数的作用是将肾小球标签转换为布尔掩码。\n\n首先，创建一个与指定掩码形状相同、数据类型为无符号8位整数(np.uint8)的全1矩阵(mask)。\n\n然后，遍历注释字典中的每个注释。如果注释的类型为“glomerulus”，则获取其坐标(coords),并使用OpenCV库的cv2.fillPoly()函数在掩码上填充多边形区域，颜色为0。\n\n最后，将掩码矩阵的数据类型转换为布尔型，并返回。\n'''\ndef 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.execute_input":"2023-06-19T06:01:03.245315Z","iopub.status.busy":"2023-06-19T06:01:03.244568Z","iopub.status.idle":"2023-06-19T06:01:03.250673Z","shell.execute_reply":"2023-06-19T06:01:03.249693Z"},"papermill":{"duration":0.01969,"end_time":"2023-06-19T06:01:03.253040","exception":false,"start_time":"2023-06-19T06:01:03.233350","status":"completed"},"tags":[]},"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.execute_input":"2023-06-19T06:01:03.275375Z","iopub.status.busy":"2023-06-19T06:01:03.275099Z","iopub.status.idle":"2023-06-19T06:01:03.337147Z","shell.execute_reply":"2023-06-19T06:01:03.336251Z"},"papermill":{"duration":0.076028,"end_time":"2023-06-19T06:01:03.339476","exception":false,"start_time":"2023-06-19T06:01:03.263448","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"weights:预训练权重文件的路径。\n\nsource:数据集的路径。\n\ndata:数据集的名称。\n\nproject:项目名称。\n\nimgsz:推理图像的大小(高度，宽度),默认为(640, 640)。\n\nconf_thres:置信度阈值，默认为0.15。\n\niou_thres:非极大值抑制(NMS)IOU阈值，默认为0.45。\n\nmax_det:每张图片的最大检测数量，默认为1000。\n\ndevice:使用的设备，可以是GPU(如0、1、2、3)或CPU,默认为空字符串。\n\nview_img:是否显示结果，默认为False。\n\nsave_txt:是否将结果保存到*.txt文件中，默认为False。\n\nsave_conf:是否在--save-txt标签中保存置信度，默认为False。\n\nsave_crop:是否保存裁剪后的预测框，默认为False。\n\nnosave:是否不保存图像/视频，默认为False。\n\nclasses:过滤类别，例如--class 0,或者--class 0 2 3,默认为None。\n\nagnostic_nms:类无关NMS,默认为False。\n\naugment:增强推理，默认为False。\n\nvisualize:可视化特征，默认为False。\n\nupdate:更新所有模型，默认为False。\n\nname:将结果保存到项目/名称中，默认为'exp'。\n\nexist_ok:现有项目/名称是否允许，不递增，默认为False。\n\nline_thickness:边界框厚度(像素),默认为3。\n\nhide_labels:隐藏标签，默认为False。\n\nhide_conf:隐藏置信度，默认为False。\n\nhalf:使用FP16半精度推理，默认为False。\n\ndnn:使用OpenCV DNN进行ONNX推理，默认为False。","metadata":{}},{"cell_type":"code","source":"'''\n这段代码定义了一个名为segment的函数，用于对图像进行目标检测和分割。函数接受多个参数，包括模型权重、输入数据、输出目录、推理大小、置信度阈值、IOU阈值、最大检测数、设备等。\n\n函数的主要功能如下：\n\n读取注释文件，获取图像的标注信息。\n\n根据输入参数创建模型实例。\n\n对输入图像进行预处理，包括调整大小、归一化等操作。\n\n使用模型进行推理，得到预测结果。\n\n对预测结果进行非极大值抑制(NMS),保留有效结果。\n\n对每个有效结果进行分割，生成掩码和编码后的掩码。\n\n将结果写入CSV文件，包括图像ID、预测字符串、置信度、编码后的掩码等信息。\n'''\ndef 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.execute_input":"2023-06-19T06:01:03.362389Z","iopub.status.busy":"2023-06-19T06:01:03.362109Z","iopub.status.idle":"2023-06-19T06:01:03.380699Z","shell.execute_reply":"2023-06-19T06:01:03.379870Z"},"papermill":{"duration":0.03226,"end_time":"2023-06-19T06:01:03.382738","exception":false,"start_time":"2023-06-19T06:01:03.350478","status":"completed"},"tags":[]},"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/wpr-bestpt/yolov7-fine-tune/yolov7-fine-tune/weights/best.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.execute_input":"2023-06-19T06:01:03.404442Z","iopub.status.busy":"2023-06-19T06:01:03.404160Z","iopub.status.idle":"2023-06-19T06:01:16.578893Z","shell.execute_reply":"2023-06-19T06:01:16.576620Z"},"papermill":{"duration":13.188093,"end_time":"2023-06-19T06:01:16.580997","exception":false,"start_time":"2023-06-19T06:01:03.392904","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cat submission.csv","metadata":{"execution":{"iopub.execute_input":"2023-06-19T06:01:16.608603Z","iopub.status.busy":"2023-06-19T06:01:16.607235Z","iopub.status.idle":"2023-06-19T06:01:17.593960Z","shell.execute_reply":"2023-06-19T06:01:17.592763Z"},"papermill":{"duration":1.001739,"end_time":"2023-06-19T06:01:17.596707","exception":false,"start_time":"2023-06-19T06:01:16.594968","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.0114,"end_time":"2023-06-19T06:01:17.619579","exception":false,"start_time":"2023-06-19T06:01:17.608179","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.01142,"end_time":"2023-06-19T06:01:17.642423","exception":false,"start_time":"2023-06-19T06:01:17.631003","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}