{"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":"code","source":"import sys\nsys.path.append(\"/kaggle/input/yolov8/ultralytics-main\")","metadata":{"execution":{"iopub.status.busy":"2023-06-30T20:55:37.098706Z","iopub.execute_input":"2023-06-30T20:55:37.099579Z","iopub.status.idle":"2023-06-30T20:55:37.111810Z","shell.execute_reply.started":"2023-06-30T20:55:37.099543Z","shell.execute_reply":"2023-06-30T20:55:37.110935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport os\nimport pandas as pd\nimport numpy as np\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\n\nfrom pathlib import Path\nfrom glob import glob\nfrom collections import defaultdict\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\nfrom IPython.display import Image as show_image\n\nimport ultralytics\nfrom ultralytics import YOLO\n\nimport torch\n\nultralytics.checks()","metadata":{"execution":{"iopub.status.busy":"2023-06-30T20:55:37.114589Z","iopub.execute_input":"2023-06-30T20:55:37.115259Z","iopub.status.idle":"2023-06-30T20:56:00.333725Z","shell.execute_reply.started":"2023-06-30T20:55:37.115200Z","shell.execute_reply":"2023-06-30T20:56:00.332718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n\nIMAGE_SIZE = 512\nBATCH_SIZE = 16\nEPOCHS = 10\n\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-06-30T20:56:00.336636Z","iopub.execute_input":"2023-06-30T20:56:00.337128Z","iopub.status.idle":"2023-06-30T20:56:00.346118Z","shell.execute_reply.started":"2023-06-30T20:56:00.337100Z","shell.execute_reply":"2023-06-30T20:56:00.344963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nHOME = os.getcwd()\n!mkdir /kaggle/working/packages\n!cp -r /kaggle/input/pycocotools/* /kaggle/working/packages\nos.chdir(\"/kaggle/working/packages/pycocotools-2.0.6/\")\n!python setup.py install\n!pip install . --no-index --find-links /kaggle/working/packages/\nos.chdir(\"/kaggle/working\")\n\nimport base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\nfrom PIL import Image\nimport cv2\nimport pandas as pd\nimport os\nfrom itertools import groupby\nfrom skimage.measure import label, regionprops\n\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 != 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-30T20:56:00.347677Z","iopub.execute_input":"2023-06-30T20:56:00.348627Z","iopub.status.idle":"2023-06-30T20:57:11.266447Z","shell.execute_reply.started":"2023-06-30T20:56:00.348594Z","shell.execute_reply":"2023-06-30T20:57:11.265394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# infer","metadata":{}},{"cell_type":"code","source":"import argparse\nimport os\nimport platform\nimport sys\nfrom pathlib import Path\nimport json\nimport pandas as pd\nimport torch\nimport torch.backends.cudnn as cudnn\nimport typing as t\nimport torch\nprint('torch',torch.__version__)\n\nimport pandas as pd\nimport numpy as np\nfrom glob import glob\n\n%matplotlib inline \nimport matplotlib\nimport matplotlib.pyplot as plt\n\nimport base64\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-06-30T20:57:11.269614Z","iopub.execute_input":"2023-06-30T20:57:11.269973Z","iopub.status.idle":"2023-06-30T20:57:11.282508Z","shell.execute_reply.started":"2023-06-30T20:57:11.269932Z","shell.execute_reply":"2023-06-30T20:57:11.278814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nimport torch\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\nimport pandas as pd\nimport torchvision.transforms as T\nfrom ultralytics import YOLO\nfrom PIL import Image\nimport numpy as np\nimport zlib\nimport base64\nfrom pycocotools import mask as coco_mask\nimport typing as t","metadata":{"execution":{"iopub.status.busy":"2023-06-30T20:57:11.286933Z","iopub.execute_input":"2023-06-30T20:57:11.287284Z","iopub.status.idle":"2023-06-30T20:57:11.309924Z","shell.execute_reply.started":"2023-06-30T20:57:11.287250Z","shell.execute_reply":"2023-06-30T20:57:11.308972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def checking_mask(mask: np.ndarray) -> np.ndarray:\n    if mask.dtype != np.bool:\n        raise ValueError(\"Expects a binary mask, received dtype == %s\" % mask.dtype)\n    return mask\n\ndef convert_mask(mask: np.ndarray):\n    mask_to_encode = mask.astype(np.uint8)\n    mask_to_encode = np.asfortranarray(mask_to_encode)\n    return mask_to_encode\n\ndef compress_encode(encoded_mask) -> t.Text:\n    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n    base64_str = base64.b64encode(binary_str)\n    return base64_str\n\ndef encode_binary_mask(mask: np.ndarray) -> t.Text:\n    mask = checking_mask(mask)\n    mask_to_encode = convert_mask(mask)\n    encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n    base64_str = compress_encode(encoded_mask)\n    return base64_str\n\ndef get_prediction_string(masks: list) -> str:\n    prediction_string = ''\n    if masks:\n        for outputs in masks:\n            mask = outputs[\"mask\"]\n            mask = np.where(mask > 0.5, 1, 0).astype(np.bool)\n            base64_str = encode_binary_mask(mask)\n            confidence = outputs[\"confidence\"]\n            prediction_string += f\"0 {confidence} {base64_str.decode('utf-8')} \"\n    else:\n        return \"\"\n    return prediction_string","metadata":{"execution":{"iopub.status.busy":"2023-06-30T20:57:11.310974Z","iopub.execute_input":"2023-06-30T20:57:11.313138Z","iopub.status.idle":"2023-06-30T20:57:11.323699Z","shell.execute_reply.started":"2023-06-30T20:57:11.313105Z","shell.execute_reply":"2023-06-30T20:57:11.322862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# self.__get_columns()\nconf = 0.05\nheight = 512\nwidth = 512\n\nmodel_path = \"/kaggle/input/yolo-v8/best.pt\"\nmodel = YOLO(model_path)\n\ndirpath = '/kaggle/input/hubmap-hacking-the-human-vasculature/test'\nfilenames = os.listdir(dirpath)\nfor filename in filenames:\n    \n    identifier = filename.split(\".\")[0]\n    path = os.path.join(dirpath, filename)\n\n    masks = []\n    result = model(path)[0]\n    if result.masks:\n        for i in range(len(result.masks.data)):\n            conf = round(float(result.boxes.conf[i]), 2)\n            mask = np.expand_dims(result.masks.data[i].cpu().numpy(), axis=0).transpose(1,2,0)\n\n            if int(result.boxes.cls[i]) == 0 and conf >= conf:\n                masks.append({\"mask\": mask, \"confidence\": conf})\n                \n    prediction_string = get_prediction_string(masks)\n    \n    submission_dict = {\n                            \"id\": [],\n                            \"height\": [],\n                            \"width\": [],\n                            \"prediction_string\": []\n                        }\n    submission_dict[\"id\"].append(identifier)\n    submission_dict[\"height\"].append(height)\n    submission_dict[\"width\"].append(width)\n    submission_dict[\"prediction_string\"].append(prediction_string)\n    \nsubmission = pd.DataFrame(submission_dict)\nsubmission = submission.set_index('id')\nsubmission.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-06-30T20:57:11.325181Z","iopub.execute_input":"2023-06-30T20:57:11.325540Z","iopub.status.idle":"2023-06-30T20:57:44.560151Z","shell.execute_reply.started":"2023-06-30T20:57:11.325508Z","shell.execute_reply":"2023-06-30T20:57:44.559071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-06-30T20:57:44.561547Z","iopub.execute_input":"2023-06-30T20:57:44.561970Z","iopub.status.idle":"2023-06-30T20:57:44.578484Z","shell.execute_reply.started":"2023-06-30T20:57:44.561936Z","shell.execute_reply":"2023-06-30T20:57:44.577147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}