{"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":"## Look at my other notebook if you don't know how to install pycocotools\n!pip install --no-index --no-deps /kaggle/input/pycocotools-206/wheels/*.whl","metadata":{"execution":{"iopub.status.busy":"2023-06-12T10:10:08.126481Z","iopub.execute_input":"2023-06-12T10:10:08.127688Z","iopub.status.idle":"2023-06-12T10:10:36.965478Z","shell.execute_reply.started":"2023-06-12T10:10:08.127608Z","shell.execute_reply":"2023-06-12T10:10:36.963730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport pandas as pd\n\n\n## Function from https://www.kaggle.com/competitions/hubmap-hacking-the-human-vasculature/overview/evaluation\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-12T10:10:36.968069Z","iopub.execute_input":"2023-06-12T10:10:36.968478Z","iopub.status.idle":"2023-06-12T10:10:37.016013Z","shell.execute_reply.started":"2023-06-12T10:10:36.968443Z","shell.execute_reply":"2023-06-12T10:10:37.014766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission should be in format: [id, height, width, pred string], where pred_string is \"0 {confidence} {encodedmask}\"","metadata":{}},{"cell_type":"markdown","source":"# 1. Dummy shapes","metadata":{}},{"cell_type":"markdown","source":"## First let's create two dummy shapes to make their masks that we will use in pred string","metadata":{}},{"cell_type":"code","source":"dummy_shape_1 = cv2.line(np.zeros((512, 512), np.uint8), (100, 350), (100, 200), 255, thickness=5)\ndummy_shape_2 = cv2.circle(np.zeros((512, 512), np.uint8), (320,250), 50, 255, cv2.FILLED)\n\nobjects = [dummy_shape_1, dummy_shape_2]\nplt.imshow(dummy_shape_1, cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-06-12T10:10:37.017419Z","iopub.execute_input":"2023-06-12T10:10:37.017754Z","iopub.status.idle":"2023-06-12T10:10:37.376982Z","shell.execute_reply.started":"2023-06-12T10:10:37.017725Z","shell.execute_reply":"2023-06-12T10:10:37.375706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(dummy_shape_2, cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-06-12T10:10:37.378423Z","iopub.execute_input":"2023-06-12T10:10:37.378783Z","iopub.status.idle":"2023-06-12T10:10:37.700573Z","shell.execute_reply.started":"2023-06-12T10:10:37.378753Z","shell.execute_reply":"2023-06-12T10:10:37.699444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Pred string\n## Use encode_binary_mask function to encode each of the dummy objects into a string, then add them","metadata":{}},{"cell_type":"code","source":"def get_pred_string(objs):\n\n    string = \"\"\n    for i, item in enumerate(objs):\n        mask = np.zeros((512,512), np.bool8)\n        mask[item>0] = 1\n        encoded_mask = encode_binary_mask(mask).decode(\"utf-8\")\n\n        if i == 0:\n            string += f\"0 1.0 {encoded_mask}\"\n        else:\n            string += f\" 0 1.0 {encoded_mask}\"\n        \n    return string","metadata":{"execution":{"iopub.status.busy":"2023-06-12T10:10:37.703243Z","iopub.execute_input":"2023-06-12T10:10:37.703586Z","iopub.status.idle":"2023-06-12T10:10:37.711301Z","shell.execute_reply.started":"2023-06-12T10:10:37.703557Z","shell.execute_reply":"2023-06-12T10:10:37.709966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Full submission\n## Get ID, height, width and prediction_string for each of the imgs in the test set","metadata":{}},{"cell_type":"code","source":"test_path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test/\"\nsubmission = pd.DataFrame()\n\nids = []\nh = []\nw = []\npred_strings = []\n    \nfor img_id in os.listdir(test_path):\n    curr_img = cv2.imread(test_path + img_id)\n\n    ## Get id, height, width\n    height, width, channels = curr_img.shape\n    ids.append(img_id.split(\".\")[0])\n    h.append(height)\n    w.append(width)\n    \n    ## Get prediction_string\n    pred_strings.append(get_pred_string(objects))\n\nsubmission[\"id\"] = ids\nsubmission[\"height\"] = h\nsubmission[\"width\"] = w\nsubmission[\"prediction_string\"] = pred_strings\nsubmission.set_index(\"id\", inplace=True)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-06-12T10:10:37.712521Z","iopub.execute_input":"2023-06-12T10:10:37.712925Z","iopub.status.idle":"2023-06-12T10:10:37.810232Z","shell.execute_reply.started":"2023-06-12T10:10:37.712885Z","shell.execute_reply":"2023-06-12T10:10:37.809027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-06-12T10:10:37.811604Z","iopub.execute_input":"2023-06-12T10:10:37.811941Z","iopub.status.idle":"2023-06-12T10:10:37.824972Z","shell.execute_reply.started":"2023-06-12T10:10:37.811912Z","shell.execute_reply":"2023-06-12T10:10:37.823889Z"},"trusted":true},"execution_count":null,"outputs":[]}]}