{
  "id": 413151,
  "title": "My predicition output is just one 512*512 mask in float32, submission help needed!",
  "url": "/competitions/hubmap-hacking-the-human-vasculature/discussion/413151",
  "author_name": "",
  "post_date": "2023-05-27T07:55:45.024552200Z",
  "votes": 3,
  "comment_count": 3,
  "views": 0,
  "content": "<p>I'm new in this competition series. I'm using <code>model.predict()</code> from <a href=\"https://github.com/qubvel/segmentation_models.pytorch\" target=\"_blank\">segmentation_models.pytorch</a>. How can I separate prediction strings multiple instance masks for the same image with a space on request?</p>",
  "messages": [
    {
      "id": "2276788",
      "postDate": "05/27/2023 07:55:45",
      "content": "<p>I'm new in this competition series. I'm using <code>model.predict()</code> from <a href=\"https://github.com/qubvel/segmentation_models.pytorch\" target=\"_blank\">segmentation_models.pytorch</a>. How can I separate prediction strings multiple instance masks for the same image with a space on request?</p>",
      "rawMarkdown": "I'm new in this competition series. I'm using `model.predict()` from [segmentation_models.pytorch](https://github.com/qubvel/segmentation_models.pytorch). How can I separate prediction strings multiple instance masks for the same image with a space on request?",
      "votes": null
    },
    {
      "id": "2276839",
      "postDate": "05/27/2023 08:39:58",
      "content": "<pre><code>from skimage.measure import label, regionprops, regionprops_table\nfrom skimage import morphology\n\n### mask : H x W (single predicted mask)\ndef get_vessels(mask):\n    mask = image.astype(bool)\n    label_img = label(mask)\n    regions = regionprops(label_img)\n\n    # get items/vessels\n    label_items = []\n    for region in regions:\n        minr, minc, maxr, maxc = region.bbox\n        zero = np.zeros(mask.shape)\n        zero[minr:maxr, minc:maxc] = 1\n        label_item = (mask*zero).astype(bool)\n        label_items.append(label_item)\n    return  label_items  \n\n###### now to the real predictions\n# preds = {'ids': ...., 'preds': ....} # all `model.predict()` and the corresponding ids\n\nids = preds['s_id']\n\nheights = []\nwidths = []\nprediction_strings = []\n\nfor k in range(len(ids)):\n    mask = preds['preds'][k].cpu().numpy().astype('bool')\n    h, w = mask.shape\n\n    seg_instances = get_vessels(mask) #preds['preds'][k].cpu().numpy())\n    # after seg infer\n    pred_string = \"\"\n    for i, binmask in enumerate(seg_instances):\n        encoded = encode_binary_mask(binmask)\n\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\n    heights.append(h)\n    widths.append(w)\n\n    prediction_strings.append(pred_string)\n\nsubmission = pd.DataFrame()\nsubmission['id'] = ids\nsubmission['height'] = heights\nsubmission['width'] = widths\nsubmission['prediction_string'] = prediction_strings\nsubmission['prediction_string'] = prediction_strings\nsubmission = submission.set_index('id')\nsubmission.to_csv(\"submission.csv\")\n\nprint(submission)\n</code></pre>",
      "rawMarkdown": "```\nfrom skimage.measure import label, regionprops, regionprops_table\nfrom skimage import morphology\n\n### mask : H x W (single predicted mask)\ndef get_vessels(mask):\n    mask = image.astype(bool)\n    label_img = label(mask)\n    regions = regionprops(label_img)\n    \n    # get items/vessels\n    label_items = []\n    for region in regions:\n        minr, minc, maxr, maxc = region.bbox\n        zero = np.zeros(mask.shape)\n        zero[minr:maxr, minc:maxc] = 1\n        label_item = (mask*zero).astype(bool)\n        label_items.append(label_item)\n    return  label_items  \n\n###### now to the real predictions\n# preds = {'ids': ...., 'preds': ....} # all `model.predict()` and the corresponding ids\n\nids = preds['s_id']\n\nheights = []\nwidths = []\nprediction_strings = []\n\nfor k in range(len(ids)):\n    mask = preds['preds'][k].cpu().numpy().astype('bool')\n    h, w = mask.shape\n    \n    seg_instances = get_vessels(mask) #preds['preds'][k].cpu().numpy())\n    # after seg infer\n    pred_string = \"\"\n    for i, binmask in enumerate(seg_instances):\n        encoded = encode_binary_mask(binmask)\n\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    \n    heights.append(h)\n    widths.append(w)\n    \n    prediction_strings.append(pred_string)\n\nsubmission = pd.DataFrame()\nsubmission['id'] = ids\nsubmission['height'] = heights\nsubmission['width'] = widths\nsubmission['prediction_string'] = prediction_strings\nsubmission['prediction_string'] = prediction_strings\nsubmission = submission.set_index('id')\nsubmission.to_csv(\"submission.csv\")\n\nprint(submission)\n```",
      "votes": null
    },
    {
      "id": "2276840",
      "postDate": "05/27/2023 08:42:07",
      "content": "<p>you can use the above code snippet to structure your submission from <code>model.predict()</code> output</p>",
      "rawMarkdown": "you can use the above code snippet to structure your submission from `model.predict()` output",
      "votes": null
    },
    {
      "id": "2276844",
      "postDate": "05/27/2023 08:50:09",
      "content": "<p>Many thanks, I'll try it!</p>",
      "rawMarkdown": "Many thanks, I'll try it!",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2276839,
      "author_name": "alabibojesomo",
      "author_url": "",
      "post_date": "05/27/2023 08:39:58",
      "content": "<pre><code>from skimage.measure import label, regionprops, regionprops_table\nfrom skimage import morphology\n\n### mask : H x W (single predicted mask)\ndef get_vessels(mask):\n    mask = image.astype(bool)\n    label_img = label(mask)\n    regions = regionprops(label_img)\n\n    # get items/vessels\n    label_items = []\n    for region in regions:\n        minr, minc, maxr, maxc = region.bbox\n        zero = np.zeros(mask.shape)\n        zero[minr:maxr, minc:maxc] = 1\n        label_item = (mask*zero).astype(bool)\n        label_items.append(label_item)\n    return  label_items  \n\n###### now to the real predictions\n# preds = {'ids': ...., 'preds': ....} # all `model.predict()` and the corresponding ids\n\nids = preds['s_id']\n\nheights = []\nwidths = []\nprediction_strings = []\n\nfor k in range(len(ids)):\n    mask = preds['preds'][k].cpu().numpy().astype('bool')\n    h, w = mask.shape\n\n    seg_instances = get_vessels(mask) #preds['preds'][k].cpu().numpy())\n    # after seg infer\n    pred_string = \"\"\n    for i, binmask in enumerate(seg_instances):\n        encoded = encode_binary_mask(binmask)\n\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\n    heights.append(h)\n    widths.append(w)\n\n    prediction_strings.append(pred_string)\n\nsubmission = pd.DataFrame()\nsubmission['id'] = ids\nsubmission['height'] = heights\nsubmission['width'] = widths\nsubmission['prediction_string'] = prediction_strings\nsubmission['prediction_string'] = prediction_strings\nsubmission = submission.set_index('id')\nsubmission.to_csv(\"submission.csv\")\n\nprint(submission)\n</code></pre>",
      "votes": null,
      "replies": [
        {
          "id": 2276840,
          "author_name": "alabibojesomo",
          "author_url": "",
          "post_date": "05/27/2023 08:42:07",
          "content": "<p>you can use the above code snippet to structure your submission from <code>model.predict()</code> output</p>",
          "votes": null,
          "replies": [
            {
              "id": 2276844,
              "author_name": "wqx20000115",
              "author_url": "",
              "post_date": "05/27/2023 08:50:09",
              "content": "<p>Many thanks, I'll try it!</p>",
              "votes": null,
              "replies": []
            }
          ]
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2276788": "I'm new in this competition series. I'm using `model.predict()` from [segmentation_models.pytorch](https://github.com/qubvel/segmentation_models.pytorch). How can I separate prediction strings multiple instance masks for the same image with a space on request?",
    "2276839": "```\nfrom skimage.measure import label, regionprops, regionprops_table\nfrom skimage import morphology\n\n### mask : H x W (single predicted mask)\ndef get_vessels(mask):\n    mask = image.astype(bool)\n    label_img = label(mask)\n    regions = regionprops(label_img)\n    \n    # get items/vessels\n    label_items = []\n    for region in regions:\n        minr, minc, maxr, maxc = region.bbox\n        zero = np.zeros(mask.shape)\n        zero[minr:maxr, minc:maxc] = 1\n        label_item = (mask*zero).astype(bool)\n        label_items.append(label_item)\n    return  label_items  \n\n###### now to the real predictions\n# preds = {'ids': ...., 'preds': ....} # all `model.predict()` and the corresponding ids\n\nids = preds['s_id']\n\nheights = []\nwidths = []\nprediction_strings = []\n\nfor k in range(len(ids)):\n    mask = preds['preds'][k].cpu().numpy().astype('bool')\n    h, w = mask.shape\n    \n    seg_instances = get_vessels(mask) #preds['preds'][k].cpu().numpy())\n    # after seg infer\n    pred_string = \"\"\n    for i, binmask in enumerate(seg_instances):\n        encoded = encode_binary_mask(binmask)\n\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    \n    heights.append(h)\n    widths.append(w)\n    \n    prediction_strings.append(pred_string)\n\nsubmission = pd.DataFrame()\nsubmission['id'] = ids\nsubmission['height'] = heights\nsubmission['width'] = widths\nsubmission['prediction_string'] = prediction_strings\nsubmission['prediction_string'] = prediction_strings\nsubmission = submission.set_index('id')\nsubmission.to_csv(\"submission.csv\")\n\nprint(submission)\n```",
    "2276840": "you can use the above code snippet to structure your submission from `model.predict()` output",
    "2276844": "Many thanks, I'll try it!"
  },
  "source": "meta"
}