{
  "id": 198343,
  "title": "Memory efficient mask2rle function",
  "url": "/competitions/hubmap-kidney-segmentation/discussion/198343",
  "author_name": "xhlulu",
  "post_date": "2020-11-20T18:56:17.516000",
  "votes": 25,
  "comment_count": 3,
  "views": 0,
  "content": "<p>In previous competitions I've used <a href=\"https://www.kaggle.com/paulorzp\" target=\"_blank\">@paulorzp</a> 's amazing <a href=\"https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\" target=\"_blank\">notebook here</a> for encoding and decoding RLE strings. </p>\n<p>Unfortunately, it seems that due to the size of the masks in this competition, I ran into memory problems (it kept crashing).</p>\n<p>So I made a more efficient implementation of his mask2rle function below:</p>\n<pre><code>def mask2rle(img):\n    '''\n    Efficient implementation of mask2rle, from @paulorzp\n    https://www.kaggle.com/xhlulu/efficient-mask2rle\n    https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n    --\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\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>You can find my <a href=\"https://www.kaggle.com/xhlulu/efficient-mask2rle\" target=\"_blank\">notebook</a> here if you want to test it.</p>",
  "messages": [
    {
      "id": 1085214,
      "postDate": "2020-11-20T18:56:17.517Z",
      "content": "<p>In previous competitions I've used <a href=\"https://www.kaggle.com/paulorzp\" target=\"_blank\">@paulorzp</a> 's amazing <a href=\"https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\" target=\"_blank\">notebook here</a> for encoding and decoding RLE strings. </p>\n<p>Unfortunately, it seems that due to the size of the masks in this competition, I ran into memory problems (it kept crashing).</p>\n<p>So I made a more efficient implementation of his mask2rle function below:</p>\n<pre><code>def mask2rle(img):\n    '''\n    Efficient implementation of mask2rle, from @paulorzp\n    https://www.kaggle.com/xhlulu/efficient-mask2rle\n    https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n    --\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\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>You can find my <a href=\"https://www.kaggle.com/xhlulu/efficient-mask2rle\" target=\"_blank\">notebook</a> here if you want to test it.</p>",
      "rawMarkdown": "In previous competitions I've used @paulorzp 's amazing [notebook here](https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode) for encoding and decoding RLE strings. \n\nUnfortunately, it seems that due to the size of the masks in this competition, I ran into memory problems (it kept crashing).\n\nSo I made a more efficient implementation of his mask2rle function below:\n```python\ndef mask2rle(img):\n    '''\n    Efficient implementation of mask2rle, from @paulorzp\n    https://www.kaggle.com/xhlulu/efficient-mask2rle\n    https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n    --\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\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```\n\nYou can find my [notebook](https://www.kaggle.com/xhlulu/efficient-mask2rle) here if you want to test it.",
      "votes": 25
    },
    {
      "id": 1086254,
      "postDate": "2020-11-21T14:18:51.780Z",
      "content": "<p>I think the difference is that np.concatenate is changed to np.pad? Am I missing something?</p>\n<p>I took another path in <a href=\"https://www.kaggle.com/bguberfain/memory-aware-rle-encoding\" target=\"_blank\">this notebook</a>: this operation is only to encode first and last pixel. If you can ignore these pixels, you don't need to pad the input with zeros.</p>",
      "rawMarkdown": "I think the difference is that np.concatenate is changed to np.pad? Am I missing something?\n\nI took another path in [this notebook](https://www.kaggle.com/bguberfain/memory-aware-rle-encoding): this operation is only to encode first and last pixel. If you can ignore these pixels, you don't need to pad the input with zeros."
    },
    {
      "id": 1085990,
      "postDate": "2020-11-21T10:12:29.107Z",
      "rawMarkdown": "",
      "isDeleted": true
    },
    {
      "id": 1085329,
      "postDate": "2020-11-20T20:50:34.400Z",
      "content": "<p>Awesome, thanks for sharing</p>",
      "rawMarkdown": "Awesome, thanks for sharing"
    }
  ],
  "comments": [
    {
      "id": 1086254,
      "author_name": "Bruno G. do Amaral",
      "author_url": "",
      "post_date": "2020-11-21T14:18:51.780000",
      "content": "<p>I think the difference is that np.concatenate is changed to np.pad? Am I missing something?</p>\n<p>I took another path in <a href=\"https://www.kaggle.com/bguberfain/memory-aware-rle-encoding\" target=\"_blank\">this notebook</a>: this operation is only to encode first and last pixel. If you can ignore these pixels, you don't need to pad the input with zeros.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 1085990,
      "author_name": "",
      "author_url": "",
      "post_date": "2020-11-21T10:12:29.107000",
      "content": "",
      "votes": 0,
      "replies": []
    },
    {
      "id": 1085329,
      "author_name": "Matt",
      "author_url": "",
      "post_date": "2020-11-20T20:50:34.400000",
      "content": "<p>Awesome, thanks for sharing</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1085214": "In previous competitions I've used @paulorzp 's amazing [notebook here](https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode) for encoding and decoding RLE strings. \n\nUnfortunately, it seems that due to the size of the masks in this competition, I ran into memory problems (it kept crashing).\n\nSo I made a more efficient implementation of his mask2rle function below:\n```python\ndef mask2rle(img):\n    '''\n    Efficient implementation of mask2rle, from @paulorzp\n    https://www.kaggle.com/xhlulu/efficient-mask2rle\n    https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n    --\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\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```\n\nYou can find my [notebook](https://www.kaggle.com/xhlulu/efficient-mask2rle) here if you want to test it.",
    "1086254": "I think the difference is that np.concatenate is changed to np.pad? Am I missing something?\n\nI took another path in [this notebook](https://www.kaggle.com/bguberfain/memory-aware-rle-encoding): this operation is only to encode first and last pixel. If you can ignore these pixels, you don't need to pad the input with zeros.",
    "1085990": "",
    "1085329": "Awesome, thanks for sharing"
  }
}