{"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.63097","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":{"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":[],"scrolled":true,"execution":{"iopub.status.busy":"2023-06-26T06:04:29.298287Z","iopub.execute_input":"2023-06-26T06:04:29.298653Z","iopub.status.idle":"2023-06-26T06:05:03.231668Z","shell.execute_reply.started":"2023-06-26T06:04:29.298622Z","shell.execute_reply":"2023-06-26T06:05:03.230458Z"},"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":{"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":{"iopub.status.busy":"2023-06-26T06:05:03.235124Z","iopub.execute_input":"2023-06-26T06:05:03.236018Z","iopub.status.idle":"2023-06-26T06:05:03.249097Z","shell.execute_reply.started":"2023-06-26T06:05:03.235983Z","shell.execute_reply":"2023-06-26T06:05:03.248099Z"},"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":{"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":[],"scrolled":true,"execution":{"iopub.status.busy":"2023-06-26T06:05:03.250640Z","iopub.execute_input":"2023-06-26T06:05:03.250985Z","iopub.status.idle":"2023-06-26T06:05:36.305747Z","shell.execute_reply.started":"2023-06-26T06:05:03.250960Z","shell.execute_reply":"2023-06-26T06:05:36.304521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/yolov7-weights-and-wheels/yolov7 yolo","metadata":{"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":{"iopub.status.busy":"2023-06-26T06:05:36.308379Z","iopub.execute_input":"2023-06-26T06:05:36.308781Z","iopub.status.idle":"2023-06-26T06:05:38.814009Z","shell.execute_reply.started":"2023-06-26T06:05:36.308739Z","shell.execute_reply":"2023-06-26T06:05:38.812744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yolo.seg.segment import predict","metadata":{"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":{"iopub.status.busy":"2023-06-26T06:05:38.817948Z","iopub.execute_input":"2023-06-26T06:05:38.818372Z","iopub.status.idle":"2023-06-26T06:05:42.627166Z","shell.execute_reply.started":"2023-06-26T06:05:38.818339Z","shell.execute_reply":"2023-06-26T06:05:42.626197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a yaml file as expected by YOLOv7 (and others)\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":{"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":{"iopub.status.busy":"2023-06-26T06:05:42.628794Z","iopub.execute_input":"2023-06-26T06:05:42.629473Z","iopub.status.idle":"2023-06-26T06:05:42.637674Z","shell.execute_reply.started":"2023-06-26T06:05:42.629438Z","shell.execute_reply":"2023-06-26T06:05:42.636719Z"},"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":{"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":{"iopub.status.busy":"2023-06-26T06:05:42.639250Z","iopub.execute_input":"2023-06-26T06:05:42.639990Z","iopub.status.idle":"2023-06-26T06:05:42.657454Z","shell.execute_reply.started":"2023-06-26T06:05:42.639956Z","shell.execute_reply":"2023-06-26T06:05:42.656387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import typing as t","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:05:42.658963Z","iopub.execute_input":"2023-06-26T06:05:42.659837Z","iopub.status.idle":"2023-06-26T06:05:42.667381Z","shell.execute_reply.started":"2023-06-26T06:05:42.659804Z","shell.execute_reply":"2023-06-26T06:05:42.666435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#############################################################################################################\ndef encode_binary_mask(mask: np.ndarray) -> t.Text:\n    \"\"\"Converts a binary mask into OID challenge encoding ascii text.\"\"\"\n\n    # check input mask --\n    #if mask.dtype != np.bool:\n    if mask.dtype != 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\n\ndef dilate_predict_mask(out_mask):\n    for i in range(len(out_mask)):\n        kernel = np.ones(shape=(3, 3), dtype=np.uint8)\n        out_mask[i] = cv2.dilate(out_mask[i], kernel, 3)\n    return out_mask","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:05:42.669010Z","iopub.execute_input":"2023-06-26T06:05:42.669445Z","iopub.status.idle":"2023-06-26T06:05:42.679835Z","shell.execute_reply.started":"2023-06-26T06:05:42.669413Z","shell.execute_reply":"2023-06-26T06:05:42.678728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_one(model, image_file, device):\n    im0 = cv2.imread(image_file)  # BGR\n\n    # Resize and pad image while meeting stride-multiple constraints\n    #im = letterbox(im0, self.img_size, stride=self.stride, auto=self.auto)[0]  # padded resize\n    im = im0\n    im = im.transpose((2, 0, 1))[::-1]  # HWC to CHW, BGR to RGB\n    im = np.ascontiguousarray(im)  # contiguous\n\n    im = torch.from_numpy(im).to(device)\n    im = im.float()  # uint8 to fp16/32\n    im /= 255  # 0 - 255 to 0.0 - 1.0\n    im = im[None]  # expand for batch dim\n\n\n    # inference\n    pred, out = model(im, augment=False, visualize=False)\n    proto = out[1]\n    #print(pred.shape)\n    #pred.shape: torch.Size([1, 16128, 40])\n\n    # NMS\n    pred = non_max_suppression(pred, conf_thres=0.001, iou_thres=0.6, classes=0, agnostic=False, max_det=1000, nm=32) #nm = num_mask?\n\n    out_mask = []\n    out_conf = []\n    for i, det in enumerate(pred):  # per image\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            out_conf = confs.data.cpu().numpy()\n\n            # https://www.kaggle.com/competitions/hubmap-hacking-the-human-vasculature/discussion/416901\n            for mask, confidence, classification in zip(masks, confs, clasf):\n                binary_mask = mask.cpu().numpy() \n                out_mask.append(binary_mask)\n  \n    return out_mask, out_conf","metadata":{"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":{"iopub.status.busy":"2023-06-26T06:05:42.681487Z","iopub.execute_input":"2023-06-26T06:05:42.681886Z","iopub.status.idle":"2023-06-26T06:05:42.696294Z","shell.execute_reply.started":"2023-06-26T06:05:42.681855Z","shell.execute_reply":"2023-06-26T06:05:42.695382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom glob import glob","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:05:42.697898Z","iopub.execute_input":"2023-06-26T06:05:42.698426Z","iopub.status.idle":"2023-06-26T06:05:42.710041Z","shell.execute_reply.started":"2023-06-26T06:05:42.698394Z","shell.execute_reply":"2023-06-26T06:05:42.709204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#############################################################################################################\nmode = 'submit' #'submit' #debug\n\nif mode=='debug':\n    image_dir = '/kaggle/input/hubmap-hacking-the-human-vasculature/train'\n    image_id = glob(f'{image_dir}/*.tif')\n    image_id = [f.split('/')[-1][:-4] for f in image_id] #[:25]\n    \n\nif mode=='submit': \n    image_dir = '/kaggle/input/hubmap-hacking-the-human-vasculature/test' \n    valid_df = pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv')\n    #image_id = valid_df['id'].values\n    \n    image_id = glob(f'{image_dir}/*.tif')\n    image_id = [f.split('/')[-1][:-4] for f in image_id]\n    \nprint('image_dir', image_dir)\nprint(len(image_id),image_id )\n ","metadata":{"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":{"iopub.status.busy":"2023-06-26T06:20:28.748388Z","iopub.execute_input":"2023-06-26T06:20:28.748763Z","iopub.status.idle":"2023-06-26T06:20:28.762662Z","shell.execute_reply.started":"2023-06-26T06:20:28.748735Z","shell.execute_reply":"2023-06-26T06:20:28.761404Z"},"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","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:14:58.618393Z","iopub.execute_input":"2023-06-26T06:14:58.618772Z","iopub.status.idle":"2023-06-26T06:14:58.627258Z","shell.execute_reply.started":"2023-06-26T06:14:58.618742Z","shell.execute_reply":"2023-06-26T06:14:58.625973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class 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#         \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\ndef 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.70,  # 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\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-26T06:20:01.711924Z","iopub.execute_input":"2023-06-26T06:20:01.712455Z","iopub.status.idle":"2023-06-26T06:20:01.765304Z","shell.execute_reply.started":"2023-06-26T06:20:01.712418Z","shell.execute_reply":"2023-06-26T06:20:01.764266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#-------------------------------------------------\n\nmodel_file=['/kaggle/input/25-50-epochs-32batch-size/yolov7-fine-tune/yolov7-fine-tune/weights/best.pt',\n            '/kaggle/input/32-batch-iou-0-6-train/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/best.pt']\ndata_yaml='/kaggle/working/hubmap-predict.yaml'\n\n# 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(data_yaml, 'w') as text_file:\n    text_file.write(yaml_text)\n\ndevice = select_device('0') \nmodel = DetectMultiBackend(model_file, device=device, dnn=False, data=data_yaml, fp16=False)\nmodel.warmup(imgsz=(1,3,512,512))  \nprint('model.training', model.model.training)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:20:38.937908Z","iopub.execute_input":"2023-06-26T06:20:38.938422Z","iopub.status.idle":"2023-06-26T06:20:41.337502Z","shell.execute_reply.started":"2023-06-26T06:20:38.938389Z","shell.execute_reply":"2023-06-26T06:20:41.336473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline \nimport matplotlib\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:20:59.307383Z","iopub.execute_input":"2023-06-26T06:20:59.307773Z","iopub.status.idle":"2023-06-26T06:20:59.313694Z","shell.execute_reply.started":"2023-06-26T06:20:59.307741Z","shell.execute_reply":"2023-06-26T06:20:59.312666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#-------------------------------------------------\n \nsubmission=[] \n#https://www.kaggle.com/code/itsuki9180/hubmap-inference\nfor t,id in enumerate(image_id):\n    print(t,id) \n    prediction_string = ''\n    try: \n        image_file = f'{image_dir}/{id}.tif' \n        mask, conf = predict_one(model, image_file, device)\n        \n        mask = dilate_predict_mask(mask)\n\n        all = np.zeros((512,512), dtype=np.uint8)\n        if len(mask)>0:\n            num_mask = len(mask)\n            for i in range(num_mask):\n                m = mask[i]>0\n                #if all[m].mean() > 0.7: continue\n                all[m] = 1\n\n                e = encode_binary_mask(m)\n                if i == 0:\n                    prediction_string = f'0 {conf[i]} {e.decode(\"utf-8\")}'\n                else:\n                    prediction_string += f' 0 {conf[i]} {e.decode(\"utf-8\")}'\n    except:\n        pass\n    \n    if t==0:\n        print('prediction_string')\n        print(prediction_string)\n        #print(prediction_string)\n        \n#         image = cv2.imread(image_file)\n#         mask = all/(all.max()+0.001)\n#         plt.imshow(image)\n#         plt.imshow(mask)\n#         plt.show()\n        \n  \n    submission.append({\n        'id':id,\n        'height':512,\n        'width':512,\n        'prediction_string':prediction_string,\n    })    \n    #print(prediction_string)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:20:59.697043Z","iopub.execute_input":"2023-06-26T06:20:59.698082Z","iopub.status.idle":"2023-06-26T06:20:59.802932Z","shell.execute_reply.started":"2023-06-26T06:20:59.698022Z","shell.execute_reply":"2023-06-26T06:20:59.801960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame(submission)\nsubmission_df.to_csv('submission.csv',index=False)\nprint(submission_df)\nprint('SUBMIT OK !!!')\n\n# '''\n# /kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif\n# (12, 512, 512) 1 uint8\n# [    0.83434     0.76198     0.75391     0.69363     0.66402     0.64325     0.62769     0.61784     0.54618     0.45178     0.31751     0.25086]\n\n# '''","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:21:00.296996Z","iopub.execute_input":"2023-06-26T06:21:00.297914Z","iopub.status.idle":"2023-06-26T06:21:00.312801Z","shell.execute_reply.started":"2023-06-26T06:21:00.297881Z","shell.execute_reply":"2023-06-26T06:21:00.311301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","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":[]}},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2023-06-26T06:05:55.652415Z","iopub.status.idle":"2023-06-26T06:05:55.653246Z","shell.execute_reply.started":"2023-06-26T06:05:55.652983Z","shell.execute_reply":"2023-06-26T06:05:55.653004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}