{"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":"# Install pycocotools package\nimport os\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 -q . --no-index --find-links /kaggle/working/packages/\nos.chdir(\"/kaggle/working\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-27T11:53:18.911334Z","iopub.execute_input":"2023-05-27T11:53:18.911677Z","iopub.status.idle":"2023-05-27T11:53:59.843725Z","shell.execute_reply.started":"2023-05-27T11:53:18.911643Z","shell.execute_reply":"2023-05-27T11:53:59.842492Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:53:59.846854Z","iopub.execute_input":"2023-05-27T11:53:59.847236Z","iopub.status.idle":"2023-05-27T11:53:59.884631Z","shell.execute_reply.started":"2023-05-27T11:53:59.847201Z","shell.execute_reply":"2023-05-27T11:53:59.883824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Add segment anything from kaggle models \n### on the right hand side of the screen","metadata":{}},{"cell_type":"code","source":"from segment_anything import SamAutomaticMaskGenerator, sam_model_registry\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:53:59.885939Z","iopub.execute_input":"2023-05-27T11:53:59.886392Z","iopub.status.idle":"2023-05-27T11:54:01.624806Z","shell.execute_reply.started":"2023-05-27T11:53:59.886359Z","shell.execute_reply":"2023-05-27T11:54:01.623843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sam = sam_model_registry[\"vit_h\"](checkpoint=\"/kaggle/input/segment-anything/pytorch/vit-h/1/model.pth\")\n#predictor = SamPredictor(sam)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:01.627725Z","iopub.execute_input":"2023-05-27T11:54:01.628736Z","iopub.status.idle":"2023-05-27T11:54:10.054692Z","shell.execute_reply.started":"2023-05-27T11:54:01.628698Z","shell.execute_reply":"2023-05-27T11:54:10.053703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_generator = SamAutomaticMaskGenerator(sam)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:10.056343Z","iopub.execute_input":"2023-05-27T11:54:10.056725Z","iopub.status.idle":"2023-05-27T11:54:10.062089Z","shell.execute_reply.started":"2023-05-27T11:54:10.056691Z","shell.execute_reply":"2023-05-27T11:54:10.060811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Change to cuda or will be very slow","metadata":{}},{"cell_type":"code","source":"device = \"cuda\"\nsam.to(device=device)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:10.063907Z","iopub.execute_input":"2023-05-27T11:54:10.064655Z","iopub.status.idle":"2023-05-27T11:54:12.430815Z","shell.execute_reply.started":"2023-05-27T11:54:10.064594Z","shell.execute_reply":"2023-05-27T11:54:12.429927Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"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\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\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:16.739178Z","iopub.execute_input":"2023-05-27T11:54:16.739554Z","iopub.status.idle":"2023-05-27T11:54:16.751932Z","shell.execute_reply.started":"2023-05-27T11:54:16.739523Z","shell.execute_reply":"2023-05-27T11:54:16.750826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Full path to test images","metadata":{}},{"cell_type":"code","source":"test_path = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test/\"\ntest_images = os.listdir(test_path)\ntest_paths = [test_path + i for i in test_images]","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:18.439911Z","iopub.execute_input":"2023-05-27T11:54:18.440283Z","iopub.status.idle":"2023-05-27T11:54:18.446932Z","shell.execute_reply.started":"2023-05-27T11:54:18.440255Z","shell.execute_reply":"2023-05-27T11:54:18.445742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check path is correct","metadata":{}},{"cell_type":"code","source":"test_paths","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:19.919498Z","iopub.execute_input":"2023-05-27T11:54:19.919896Z","iopub.status.idle":"2023-05-27T11:54:19.926533Z","shell.execute_reply.started":"2023-05-27T11:54:19.919864Z","shell.execute_reply":"2023-05-27T11:54:19.925538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Read in the first (and only? test image)","metadata":{}},{"cell_type":"code","source":"tiff_data = cv2.imread(test_paths[0], cv2.IMREAD_UNCHANGED)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:22.002261Z","iopub.execute_input":"2023-05-27T11:54:22.002669Z","iopub.status.idle":"2023-05-27T11:54:22.017569Z","shell.execute_reply.started":"2023-05-27T11:54:22.002637Z","shell.execute_reply":"2023-05-27T11:54:22.016476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(tiff_data)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:22.778061Z","iopub.execute_input":"2023-05-27T11:54:22.778424Z","iopub.status.idle":"2023-05-27T11:54:23.115768Z","shell.execute_reply.started":"2023-05-27T11:54:22.778397Z","shell.execute_reply":"2023-05-27T11:54:23.114953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = mask_generator.generate(tiff_data)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:24.999263Z","iopub.execute_input":"2023-05-27T11:54:24.999624Z","iopub.status.idle":"2023-05-27T11:54:29.910747Z","shell.execute_reply.started":"2023-05-27T11:54:24.999594Z","shell.execute_reply":"2023-05-27T11:54:29.909767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_anns(anns):\n    ## From the segment anything nb \n    if len(anns) == 0:\n        return\n    sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)\n    ax = plt.gca()\n    ax.set_autoscale_on(False)\n    polygons = []\n    color = []\n    for ann in sorted_anns:\n        m = ann['segmentation']\n        img = np.ones((m.shape[0], m.shape[1], 3))\n        color_mask = np.random.random((1, 3)).tolist()[0]\n        for i in range(3):\n            img[:,:,i] = color_mask[i]\n        ax.imshow(np.dstack((img, m*0.35)))","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:34.673518Z","iopub.execute_input":"2023-05-27T11:54:34.673958Z","iopub.status.idle":"2023-05-27T11:54:34.682903Z","shell.execute_reply.started":"2023-05-27T11:54:34.673925Z","shell.execute_reply":"2023-05-27T11:54:34.680246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nplt.imshow(tiff_data)\nshow_anns(masks)\nplt.axis('off')\nplt.show() ","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:35.801580Z","iopub.execute_input":"2023-05-27T11:54:35.801958Z","iopub.status.idle":"2023-05-27T11:54:42.125704Z","shell.execute_reply.started":"2023-05-27T11:54:35.801927Z","shell.execute_reply":"2023-05-27T11:54:42.124814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Take a look at the masks generated \n","metadata":{}},{"cell_type":"code","source":"sorted_anns = sorted(masks, key=(lambda x: x['area']), reverse=True)\npd.DataFrame(sorted_anns).head()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:42.127408Z","iopub.execute_input":"2023-05-27T11:54:42.128414Z","iopub.status.idle":"2023-05-27T11:54:42.833499Z","shell.execute_reply.started":"2023-05-27T11:54:42.128377Z","shell.execute_reply":"2023-05-27T11:54:42.832326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsample_submission = pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv')\n\nids = []\nheights = []\nwidths = []\nprediction_strings = []\nfor img_name in os.listdir(test_path):\n    img = cv2.imread(f\"{test_path}/{img_name}\")\n    h, w, c = img.shape\n    \n    # Generate new mask for each image\n    masks = mask_generator.generate(img)\n    # after seg infer\n    pred_string = \"\"\n    fig, ax = plt.subplots(1,2, figsize = (10,10))\n    ax[0].imshow(img)\n    binmask = np.zeros((512,512), np.bool8)\n\n    for i, item in enumerate(masks):\n        if item['area'] > 100_000 or item['area'] < 500:\n            # Remove mask areas above or below a certain threshold\n            continue\n        temp = item['segmentation']\n        # Combine the masks\n        binmask = np.logical_or(binmask,temp)\n        \n        ax[1].imshow(binmask, cmap='gray', vmin=0, vmax=1)\n        encoded = encode_binary_mask(binmask)\n        # this below needs fixing\n        if i == 0:\n            pred_string += f\"0 1.0 {encoded.decode('utf-8')}\"\n        else:\n            pred_string += f\" 0 1.0 {encoded.decode('utf-8')}\"\n    ids.append(img_name.split('.')[0])\n    heights.append(h)\n    widths.append(w)\n    prediction_strings.append(pred_string)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:44.846476Z","iopub.execute_input":"2023-05-27T11:54:44.846862Z","iopub.status.idle":"2023-05-27T11:54:49.604517Z","shell.execute_reply.started":"2023-05-27T11:54:44.846831Z","shell.execute_reply":"2023-05-27T11:54:49.603646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame()\nsubmission['id'] = ids\nsubmission['height'] = heights\nsubmission['width'] = widths\nsubmission['prediction_string'] = prediction_strings\nsubmission = submission.set_index('id')\nsubmission.to_csv(\"submission.csv\")\nprint(submission)","metadata":{"execution":{"iopub.status.busy":"2023-05-27T11:54:49.606399Z","iopub.execute_input":"2023-05-27T11:54:49.606923Z","iopub.status.idle":"2023-05-27T11:54:49.624171Z","shell.execute_reply.started":"2023-05-27T11:54:49.606891Z","shell.execute_reply":"2023-05-27T11:54:49.623142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm -rf /kaggle/working/packages","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}