{"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-28T06:50:24.922789Z","iopub.execute_input":"2023-06-28T06:50:24.923178Z","iopub.status.idle":"2023-06-28T06:50:59.92876Z","shell.execute_reply.started":"2023-06-28T06:50:24.923151Z","shell.execute_reply":"2023-06-28T06:50:59.927608Z"},"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-28T06:50:59.931169Z","iopub.execute_input":"2023-06-28T06:50:59.931641Z","iopub.status.idle":"2023-06-28T06:50:59.944365Z","shell.execute_reply.started":"2023-06-28T06:50:59.931604Z","shell.execute_reply":"2023-06-28T06:50:59.943409Z"},"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-28T06:50:59.946153Z","iopub.execute_input":"2023-06-28T06:50:59.946792Z","iopub.status.idle":"2023-06-28T06:51:35.326189Z","shell.execute_reply.started":"2023-06-28T06:50:59.946761Z","shell.execute_reply":"2023-06-28T06:51:35.325001Z"},"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-28T06:51:35.329231Z","iopub.execute_input":"2023-06-28T06:51:35.33074Z","iopub.status.idle":"2023-06-28T06:51:37.436347Z","shell.execute_reply.started":"2023-06-28T06:51:35.3307Z","shell.execute_reply":"2023-06-28T06:51:37.435067Z"},"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-28T06:51:37.438814Z","iopub.execute_input":"2023-06-28T06:51:37.439179Z","iopub.status.idle":"2023-06-28T06:51:41.139543Z","shell.execute_reply.started":"2023-06-28T06:51:37.439142Z","shell.execute_reply":"2023-06-28T06:51:41.138635Z"},"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-28T06:51:41.14085Z","iopub.execute_input":"2023-06-28T06:51:41.141388Z","iopub.status.idle":"2023-06-28T06:51:41.149063Z","shell.execute_reply.started":"2023-06-28T06:51:41.141353Z","shell.execute_reply":"2023-06-28T06:51:41.148109Z"},"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-28T06:51:41.150616Z","iopub.execute_input":"2023-06-28T06:51:41.151028Z","iopub.status.idle":"2023-06-28T06:51:41.160861Z","shell.execute_reply.started":"2023-06-28T06:51:41.150992Z","shell.execute_reply":"2023-06-28T06:51:41.159874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import typing as t","metadata":{"execution":{"iopub.status.busy":"2023-06-28T06:51:41.16241Z","iopub.execute_input":"2023-06-28T06:51:41.162748Z","iopub.status.idle":"2023-06-28T06:51:41.17154Z","shell.execute_reply.started":"2023-06-28T06:51:41.162719Z","shell.execute_reply":"2023-06-28T06:51:41.170313Z"},"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-28T06:51:41.173085Z","iopub.execute_input":"2023-06-28T06:51:41.173404Z","iopub.status.idle":"2023-06-28T06:51:41.184434Z","shell.execute_reply.started":"2023-06-28T06:51:41.173374Z","shell.execute_reply":"2023-06-28T06:51:41.183511Z"},"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-28T06:51:41.189402Z","iopub.execute_input":"2023-06-28T06:51:41.190303Z","iopub.status.idle":"2023-06-28T06:51:41.202451Z","shell.execute_reply.started":"2023-06-28T06:51:41.190259Z","shell.execute_reply":"2023-06-28T06:51:41.201469Z"},"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-28T06:51:41.204255Z","iopub.execute_input":"2023-06-28T06:51:41.205304Z","iopub.status.idle":"2023-06-28T06:51:41.21612Z","shell.execute_reply.started":"2023-06-28T06:51:41.205269Z","shell.execute_reply":"2023-06-28T06:51:41.215209Z"},"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-28T06:51:41.217433Z","iopub.execute_input":"2023-06-28T06:51:41.217921Z","iopub.status.idle":"2023-06-28T06:51:41.243709Z","shell.execute_reply.started":"2023-06-28T06:51:41.217892Z","shell.execute_reply":"2023-06-28T06:51:41.242843Z"},"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'\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-28T06:52:03.475348Z","iopub.execute_input":"2023-06-28T06:52:03.476244Z","iopub.status.idle":"2023-06-28T06:52:04.832977Z","shell.execute_reply.started":"2023-06-28T06:52:03.476201Z","shell.execute_reply":"2023-06-28T06:52:04.831965Z"},"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-28T06:52:09.21491Z","iopub.execute_input":"2023-06-28T06:52:09.215287Z","iopub.status.idle":"2023-06-28T06:52:09.223045Z","shell.execute_reply.started":"2023-06-28T06:52:09.215257Z","shell.execute_reply":"2023-06-28T06:52:09.221304Z"},"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[:200])\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-28T06:52:10.781151Z","iopub.execute_input":"2023-06-28T06:52:10.781885Z","iopub.status.idle":"2023-06-28T06:52:11.382854Z","shell.execute_reply.started":"2023-06-28T06:52:10.781848Z","shell.execute_reply":"2023-06-28T06:52:11.381931Z"},"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-28T06:52:11.384752Z","iopub.execute_input":"2023-06-28T06:52:11.385112Z","iopub.status.idle":"2023-06-28T06:52:11.397058Z","shell.execute_reply.started":"2023-06-28T06:52:11.385077Z","shell.execute_reply":"2023-06-28T06:52:11.396023Z"},"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-28T06:51:51.330289Z","iopub.execute_input":"2023-06-28T06:51:51.330702Z","iopub.status.idle":"2023-06-28T06:51:51.34541Z","shell.execute_reply.started":"2023-06-28T06:51:51.330671Z","shell.execute_reply":"2023-06-28T06:51:51.344133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}