{
  "id": 232357,
  "title": "Fast Code to Convert Mask to RLE",
  "url": "/competitions/hubmap-kidney-segmentation/discussion/232357",
  "author_name": "",
  "post_date": "2021-04-13T11:52:47.556419900Z",
  "votes": 28,
  "comment_count": 6,
  "views": 0,
  "content": "<p>Below is fast code from <a href=\"https://www.kaggle.com/xhlulu\" target=\"_blank\">@xhlulu</a> to convert <code>mask</code> to <code>rle</code> from his notebook <a href=\"https://www.kaggle.com/xhlulu/efficient-mask2rle\" target=\"_blank\">here</a> </p>\n<p>(The original code comes from <a href=\"https://www.kaggle.com/paulorzp\" target=\"_blank\">@paulorzp</a> )</p>\n<pre><code>def mask2rle(img):\n    '''\n    Efficient implementation of mask2rle, from @paulorzp\n    --\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    Source: https://www.kaggle.com/xhlulu/efficient-mask2rle\n    '''\n    pixels = img.T.flatten()\n    pixels = np.pad(pixels, ((1, 1), ))\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n</code></pre>\n<p>Example usage is below</p>\n<pre><code>mask = np.array([[1,0,1,0],\n                 [1,1,0,1],\n                 [1,1,0,0]])\nrle = mask2rle(x)\nprint( rle )\n</code></pre>\n<p>this outputs the string</p>\n<pre><code>'1 3 5 3 11 1'\n</code></pre>",
  "messages": [
    {
      "id": "1272306",
      "postDate": "04/13/2021 11:52:47",
      "content": "<p>Below is fast code from <a href=\"https://www.kaggle.com/xhlulu\" target=\"_blank\">@xhlulu</a> to convert <code>mask</code> to <code>rle</code> from his notebook <a href=\"https://www.kaggle.com/xhlulu/efficient-mask2rle\" target=\"_blank\">here</a> </p>\n<p>(The original code comes from <a href=\"https://www.kaggle.com/paulorzp\" target=\"_blank\">@paulorzp</a> )</p>\n<pre><code>def mask2rle(img):\n    '''\n    Efficient implementation of mask2rle, from @paulorzp\n    --\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    Source: https://www.kaggle.com/xhlulu/efficient-mask2rle\n    '''\n    pixels = img.T.flatten()\n    pixels = np.pad(pixels, ((1, 1), ))\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n</code></pre>\n<p>Example usage is below</p>\n<pre><code>mask = np.array([[1,0,1,0],\n                 [1,1,0,1],\n                 [1,1,0,0]])\nrle = mask2rle(x)\nprint( rle )\n</code></pre>\n<p>this outputs the string</p>\n<pre><code>'1 3 5 3 11 1'\n</code></pre>",
      "rawMarkdown": "Below is fast code from @xhlulu to convert `mask` to `rle` from his notebook [here][1] \n\n(The original code comes from @paulorzp )\n\n    def mask2rle(img):\n        '''\n        Efficient implementation of mask2rle, from @paulorzp\n        --\n        img: numpy array, 1 - mask, 0 - background\n        Returns run length as string formated\n        Source: https://www.kaggle.com/xhlulu/efficient-mask2rle\n        '''\n        pixels = img.T.flatten()\n        pixels = np.pad(pixels, ((1, 1), ))\n        runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n        runs[1::2] -= runs[::2]\n        return ' '.join(str(x) for x in runs)\n\nExample usage is below\n\n    mask = np.array([[1,0,1,0],\n                     [1,1,0,1],\n                     [1,1,0,0]])\n    rle = mask2rle(x)\n    print( rle )\n\nthis outputs the string\n\n    '1 3 5 3 11 1'\n\n[1]: https://www.kaggle.com/xhlulu/efficient-mask2rle",
      "votes": null
    },
    {
      "id": "1272470",
      "postDate": "04/13/2021 13:56:32",
      "content": "<p>very nice! thx!</p>",
      "rawMarkdown": "very nice! thx!",
      "votes": null
    },
    {
      "id": "1276346",
      "postDate": "04/17/2021 12:36:33",
      "content": "<p>From the <code>mask2rle</code> docstring: </p>\n<blockquote>\n  <p>Efficient implementation of mask2rle, from <a href=\"https://www.kaggle.com/paulorzp\" target=\"_blank\">@paulorzp</a> </p>\n</blockquote>\n<p>The lineage of authors is complete I guess now. 😄</p>",
      "rawMarkdown": "From the `mask2rle` docstring: \n\n> Efficient implementation of mask2rle, from @paulorzp \n\nThe lineage of authors is complete I guess now. 😄",
      "votes": null
    },
    {
      "id": "1277557",
      "postDate": "04/18/2021 22:57:08",
      "content": "<p>Thanks, i updated post to indicate original author</p>",
      "rawMarkdown": "Thanks, i updated post to indicate original author",
      "votes": null
    },
    {
      "id": "1279341",
      "postDate": "04/20/2021 19:41:01",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/cdeotte\" target=\"_blank\">@cdeotte</a>, any idea why <code>+1</code> in <code>np.where(pixels[1:] != pixels[:-1])[0] + 1</code>?</p>",
      "rawMarkdown": "Hi @cdeotte, any idea why `+1` in `np.where(pixels[1:] != pixels[:-1])[0] + 1`?",
      "votes": null
    },
    {
      "id": "1279429",
      "postDate": "04/20/2021 22:41:18",
      "content": "<p>Yes. The first pixel in the top left corner is pixel number 1. It does not start at 0. For example the array</p>\n<pre><code>mask = np.array([[1,0,0,0],\n                 [1,0,0,0],\n                 [0,0,0,0]])\nrle = mask2rle(x)\nprint( rle )\n</code></pre>\n<p>should print the rle string <code>\"1 2\"</code> not <code>\"0 2\"</code>.</p>",
      "rawMarkdown": "Yes. The first pixel in the top left corner is pixel number 1. It does not start at 0. For example the array\n\n    mask = np.array([[1,0,0,0],\n                     [1,0,0,0],\n                     [0,0,0,0]])\n    rle = mask2rle(x)\n    print( rle )\n\nshould print the rle string `\"1 2\"` not `\"0 2\"`.",
      "votes": null
    },
    {
      "id": "1282033",
      "postDate": "04/23/2021 14:24:29",
      "content": "<p>thank you for your sharing</p>",
      "rawMarkdown": "thank you for your sharing",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1272470,
      "author_name": "gwanghan",
      "author_url": "",
      "post_date": "04/13/2021 13:56:32",
      "content": "<p>very nice! thx!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1276346,
      "author_name": "yassinealouini",
      "author_url": "",
      "post_date": "04/17/2021 12:36:33",
      "content": "<p>From the <code>mask2rle</code> docstring: </p>\n<blockquote>\n  <p>Efficient implementation of mask2rle, from <a href=\"https://www.kaggle.com/paulorzp\" target=\"_blank\">@paulorzp</a> </p>\n</blockquote>\n<p>The lineage of authors is complete I guess now. 😄</p>",
      "votes": null,
      "replies": [
        {
          "id": 1277557,
          "author_name": "cdeotte",
          "author_url": "",
          "post_date": "04/18/2021 22:57:08",
          "content": "<p>Thanks, i updated post to indicate original author</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1279341,
      "author_name": "awsaf49",
      "author_url": "",
      "post_date": "04/20/2021 19:41:01",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/cdeotte\" target=\"_blank\">@cdeotte</a>, any idea why <code>+1</code> in <code>np.where(pixels[1:] != pixels[:-1])[0] + 1</code>?</p>",
      "votes": null,
      "replies": [
        {
          "id": 1279429,
          "author_name": "cdeotte",
          "author_url": "",
          "post_date": "04/20/2021 22:41:18",
          "content": "<p>Yes. The first pixel in the top left corner is pixel number 1. It does not start at 0. For example the array</p>\n<pre><code>mask = np.array([[1,0,0,0],\n                 [1,0,0,0],\n                 [0,0,0,0]])\nrle = mask2rle(x)\nprint( rle )\n</code></pre>\n<p>should print the rle string <code>\"1 2\"</code> not <code>\"0 2\"</code>.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1282033,
      "author_name": "yeahbrian",
      "author_url": "",
      "post_date": "04/23/2021 14:24:29",
      "content": "<p>thank you for your sharing</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1272306": "Below is fast code from @xhlulu to convert `mask` to `rle` from his notebook [here][1] \n\n(The original code comes from @paulorzp )\n\n    def mask2rle(img):\n        '''\n        Efficient implementation of mask2rle, from @paulorzp\n        --\n        img: numpy array, 1 - mask, 0 - background\n        Returns run length as string formated\n        Source: https://www.kaggle.com/xhlulu/efficient-mask2rle\n        '''\n        pixels = img.T.flatten()\n        pixels = np.pad(pixels, ((1, 1), ))\n        runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n        runs[1::2] -= runs[::2]\n        return ' '.join(str(x) for x in runs)\n\nExample usage is below\n\n    mask = np.array([[1,0,1,0],\n                     [1,1,0,1],\n                     [1,1,0,0]])\n    rle = mask2rle(x)\n    print( rle )\n\nthis outputs the string\n\n    '1 3 5 3 11 1'\n\n[1]: https://www.kaggle.com/xhlulu/efficient-mask2rle",
    "1272470": "very nice! thx!",
    "1276346": "From the `mask2rle` docstring: \n\n> Efficient implementation of mask2rle, from @paulorzp \n\nThe lineage of authors is complete I guess now. 😄",
    "1277557": "Thanks, i updated post to indicate original author",
    "1279341": "Hi @cdeotte, any idea why `+1` in `np.where(pixels[1:] != pixels[:-1])[0] + 1`?",
    "1279429": "Yes. The first pixel in the top left corner is pixel number 1. It does not start at 0. For example the array\n\n    mask = np.array([[1,0,0,0],\n                     [1,0,0,0],\n                     [0,0,0,0]])\n    rle = mask2rle(x)\n    print( rle )\n\nshould print the rle string `\"1 2\"` not `\"0 2\"`.",
    "1282033": "thank you for your sharing"
  },
  "source": "meta"
}