{"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":"### create tfrecord & Training Code is available\n\nhttps://www.kaggle.com/code/kaerunantoka/hubmap-tf-instance-segm-create-tfrecord-training/notebook?scriptVersionId=133516969","metadata":{}},{"cell_type":"code","source":"import glob\nfrom PIL import Image\n\nimport pandas as pd\nimport tifffile as tiff\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\n\nimport tensorflow as tf","metadata":{},"execution_count":null,"outputs":[]},{"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 -q\n!pip install . --no-index --find-links /kaggle/working/packages/ -q\nos.chdir(\"/kaggle/working\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Helper function","metadata":{}},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib\n\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\ndef draw_mask_on_image(mask, x1, y1, x2, y2):\n    image = np.zeros((512, 512), dtype=np.uint8)\n\n    xmin = max(x1, 0)\n    ymin = max(y1, 0)\n    xmax = min(x2, image.shape[1]-1)\n    ymax = min(y2, image.shape[0]-1)\n\n    resized_mask = np.array(Image.fromarray(mask).resize((xmax-xmin+1, ymax-ymin+1), resample=Image.NEAREST))\n\n    image[ymin:ymax+1, xmin:xmax+1] = resized_mask\n\n    return image","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### load model","metadata":{}},{"cell_type":"code","source":"export_dir = '/kaggle/input/hubmap-tf-instance-segm-create-tfrecord-training/exported_model/'\n\nimported = tf.saved_model.load(export_dir)\nmodel_fn = imported.signatures['serving_default']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_imgs = glob.glob('/kaggle/input/hubmap-hacking-the-human-vasculature/test/*.tif')\n\nids = []\nheights = []\nwidths = []\nprediction_strings = []\n\n\nsample = None\nfor img_path in all_imgs:\n    idx = img_path.split('/')[-1].replace('.tif', '')\n    array = tiff.imread(img_path)\n    img_example = Image.fromarray(array).convert(\"RGB\")\n    img = np.array(img_example)\n    image = tf.expand_dims(img, axis=0)\n    image = tf.cast(image, dtype = tf.uint8)\n    image_np = image[0].numpy()\n    pred = model_fn(image)\n    detection_scores = pred['detection_scores'][0].numpy()\n    detection_masks = pred['detection_masks'][0].numpy()\n    detection_boxes = pred['detection_boxes'][0].numpy()\n\n    if sample is None: sample=pred\n    pred_string = ''\n    for i, (det_mask, det_bbox, det_score) in enumerate(zip(detection_masks, detection_boxes, detection_scores)):\n        if det_score == 0:\n            continue\n        seg_mask = det_mask>0.5\n        y1, x1, y2, x2 = map(int, det_bbox)\n        seg_mask = draw_mask_on_image(seg_mask, x1, y1, x2, y2) \n        seg_mask = np.where(seg_mask>0.5, 1, 0).astype(bool)\n\n        encoded = encode_binary_mask(seg_mask)\n        if i==0:\n            pred_string += f\"0 {det_score} {encoded.decode('utf-8')}\"\n        else:\n            pred_string += f\" 0 {det_score} {encoded.decode('utf-8')}\"\n            \n    h, w, c = img.shape\n    ids.append(idx)\n    heights.append(h)\n    widths.append(w)\n    prediction_strings.append(pred_string)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = 0\nfor i, (det_mask, det_bbox, det_score) in enumerate(zip(detection_masks, detection_boxes, detection_scores)):\n    if det_score < 0.5:\n        continue\n    seg_mask = det_mask>0.5\n    y1, x1, y2, x2 = map(int, det_bbox)\n    seg_mask = draw_mask_on_image(seg_mask, x1, y1, x2, y2) \n    seg_mask = np.where(seg_mask>0.5, 1, 0).astype(bool)\n    im += seg_mask\nim = np.where(im>0.5, 1, 0).astype(bool)\nplt.imshow(im)\nplt.show()","metadata":{"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\")\nsubmission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_strings[0].split(' ')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(prediction_strings[0].split(' '))//3","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}