{
  "id": 279561,
  "title": "Discussion of Functions in Python",
  "url": "/competitions/sartorius-cell-instance-segmentation/discussion/279561",
  "author_name": "",
  "post_date": "2021-10-18T16:45:04.661577300Z",
  "votes": 2,
  "comment_count": 2,
  "views": 0,
  "content": "<p>I've seen the following in a notebook (Sartorius - Starter Baseline Torch U-net 793849):</p>\n<blockquote>\n  <p>def rle_decode(mask_rle, shape, color=1):</p>\n</blockquote>\n<p>However I want to know exactly what it does in the context of the whole code. I would like also more explanations on the following:</p>\n<p><code>starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]</code></p>\n<p>What exactly is (s[0:][::2], s[1:][::2])?</p>",
  "messages": [
    {
      "id": "1548947",
      "postDate": "10/18/2021 16:45:04",
      "content": "<p>I've seen the following in a notebook (Sartorius - Starter Baseline Torch U-net 793849):</p>\n<blockquote>\n  <p>def rle_decode(mask_rle, shape, color=1):</p>\n</blockquote>\n<p>However I want to know exactly what it does in the context of the whole code. I would like also more explanations on the following:</p>\n<p><code>starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]</code></p>\n<p>What exactly is (s[0:][::2], s[1:][::2])?</p>",
      "rawMarkdown": "I've seen the following in a notebook (Sartorius - Starter Baseline Torch U-net 793849):\n\n> def rle_decode(mask_rle, shape, color=1):\n\nHowever I want to know exactly what it does in the context of the whole code. I would like also more explanations on the following:\n\n` starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]`\n\nWhat exactly is (s[0:][::2], s[1:][::2])?",
      "votes": null
    },
    {
      "id": "1549005",
      "postDate": "10/18/2021 17:49:24",
      "content": "<p>The following link might help you with your 1st query: <a href=\"https://gist.github.com/gabrielgarza/7b7dfd906d424a1714e5cc2e4c24c059\" target=\"_blank\">https://gist.github.com/gabrielgarza/7b7dfd906d424a1714e5cc2e4c24c059</a>. <br>\nThis function is essentially to convert a binary mask into the rle format. (Atleast that's what I think.)</p>\n<p>As far as the 2nd one is concerned, RLE is a format for storing binary masks. RLE first divides a vector (or vectorized image) into a series of piecewise constant regions and then for each piece simply stores the length of that piece. For example, given M=[0 0 1 1 1 0 1] the RLE counts would be [2 3 1 1], or for M=[1 1 1 1 1 1 0] the counts would be [0 6 1] (note that the odd counts are always the numbers of zeros). <br>\nThus, what is essentially happening there is calculation of lengths of different regions (which have been taken from the vector) given a particular start position. <br>\nHope this helps!</p>",
      "rawMarkdown": "The following link might help you with your 1st query: https://gist.github.com/gabrielgarza/7b7dfd906d424a1714e5cc2e4c24c059. \nThis function is essentially to convert a binary mask into the rle format. (Atleast that's what I think.)\n\nAs far as the 2nd one is concerned, RLE is a format for storing binary masks. RLE first divides a vector (or vectorized image) into a series of piecewise constant regions and then for each piece simply stores the length of that piece. For example, given M=[0 0 1 1 1 0 1] the RLE counts would be [2 3 1 1], or for M=[1 1 1 1 1 1 0] the counts would be [0 6 1] (note that the odd counts are always the numbers of zeros). \nThus, what is essentially happening there is calculation of lengths of different regions (which have been taken from the vector) given a particular start position. \nHope this helps!",
      "votes": null
    },
    {
      "id": "1549012",
      "postDate": "10/18/2021 17:57:36",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/joaovictormelo\" target=\"_blank\">@joaovictormelo</a> </p>\n<p>Regarding code snippet you posted:</p>\n<p>s[0:][::2] --&gt; Iterate the list with a step of 2, starting from the first element (zero indexed)<br>\ns[1:][::2] --&gt;  Iterate the list with a step of 2, starting from the second element</p>\n<p>This is a way of separating the elements in the even positions and in the odd positions, this is: the starts and the lengths of the RLE.</p>\n<p>The rle_encoding and decoding is translating from RLE to 2d masks.</p>\n<p>There are various notebooks in this competition covering that topic. See <a href=\"https://www.kaggle.com/susnato/run-length-encoding-and-decoding-with-examples\" target=\"_blank\">this one</a> for example.<br>\nI have written one myself in a previous competition too (<a href=\"https://www.kaggle.com/julian3833/2-understanding-and-plotting-rle-bounding-boxes\" target=\"_blank\">here</a>).</p>",
      "rawMarkdown": "Hi @joaovictormelo \n\nRegarding code snippet you posted:\n\ns[0:][::2] --> Iterate the list with a step of 2, starting from the first element (zero indexed)\ns[1:][::2] -->  Iterate the list with a step of 2, starting from the second element\n\nThis is a way of separating the elements in the even positions and in the odd positions, this is: the starts and the lengths of the RLE.\n\nThe rle_encoding and decoding is translating from RLE to 2d masks.\n\nThere are various notebooks in this competition covering that topic. See [this one](https://www.kaggle.com/susnato/run-length-encoding-and-decoding-with-examples) for example.\nI have written one myself in a previous competition too ([here](https://www.kaggle.com/julian3833/2-understanding-and-plotting-rle-bounding-boxes)).",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1549005,
      "author_name": "itrustonlykent",
      "author_url": "",
      "post_date": "10/18/2021 17:49:24",
      "content": "<p>The following link might help you with your 1st query: <a href=\"https://gist.github.com/gabrielgarza/7b7dfd906d424a1714e5cc2e4c24c059\" target=\"_blank\">https://gist.github.com/gabrielgarza/7b7dfd906d424a1714e5cc2e4c24c059</a>. <br>\nThis function is essentially to convert a binary mask into the rle format. (Atleast that's what I think.)</p>\n<p>As far as the 2nd one is concerned, RLE is a format for storing binary masks. RLE first divides a vector (or vectorized image) into a series of piecewise constant regions and then for each piece simply stores the length of that piece. For example, given M=[0 0 1 1 1 0 1] the RLE counts would be [2 3 1 1], or for M=[1 1 1 1 1 1 0] the counts would be [0 6 1] (note that the odd counts are always the numbers of zeros). <br>\nThus, what is essentially happening there is calculation of lengths of different regions (which have been taken from the vector) given a particular start position. <br>\nHope this helps!</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1549012,
      "author_name": "julian3833",
      "author_url": "",
      "post_date": "10/18/2021 17:57:36",
      "content": "<p>Hi <a href=\"https://www.kaggle.com/joaovictormelo\" target=\"_blank\">@joaovictormelo</a> </p>\n<p>Regarding code snippet you posted:</p>\n<p>s[0:][::2] --&gt; Iterate the list with a step of 2, starting from the first element (zero indexed)<br>\ns[1:][::2] --&gt;  Iterate the list with a step of 2, starting from the second element</p>\n<p>This is a way of separating the elements in the even positions and in the odd positions, this is: the starts and the lengths of the RLE.</p>\n<p>The rle_encoding and decoding is translating from RLE to 2d masks.</p>\n<p>There are various notebooks in this competition covering that topic. See <a href=\"https://www.kaggle.com/susnato/run-length-encoding-and-decoding-with-examples\" target=\"_blank\">this one</a> for example.<br>\nI have written one myself in a previous competition too (<a href=\"https://www.kaggle.com/julian3833/2-understanding-and-plotting-rle-bounding-boxes\" target=\"_blank\">here</a>).</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1548947": "I've seen the following in a notebook (Sartorius - Starter Baseline Torch U-net 793849):\n\n> def rle_decode(mask_rle, shape, color=1):\n\nHowever I want to know exactly what it does in the context of the whole code. I would like also more explanations on the following:\n\n` starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]`\n\nWhat exactly is (s[0:][::2], s[1:][::2])?",
    "1549005": "The following link might help you with your 1st query: https://gist.github.com/gabrielgarza/7b7dfd906d424a1714e5cc2e4c24c059. \nThis function is essentially to convert a binary mask into the rle format. (Atleast that's what I think.)\n\nAs far as the 2nd one is concerned, RLE is a format for storing binary masks. RLE first divides a vector (or vectorized image) into a series of piecewise constant regions and then for each piece simply stores the length of that piece. For example, given M=[0 0 1 1 1 0 1] the RLE counts would be [2 3 1 1], or for M=[1 1 1 1 1 1 0] the counts would be [0 6 1] (note that the odd counts are always the numbers of zeros). \nThus, what is essentially happening there is calculation of lengths of different regions (which have been taken from the vector) given a particular start position. \nHope this helps!",
    "1549012": "Hi @joaovictormelo \n\nRegarding code snippet you posted:\n\ns[0:][::2] --> Iterate the list with a step of 2, starting from the first element (zero indexed)\ns[1:][::2] -->  Iterate the list with a step of 2, starting from the second element\n\nThis is a way of separating the elements in the even positions and in the odd positions, this is: the starts and the lengths of the RLE.\n\nThe rle_encoding and decoding is translating from RLE to 2d masks.\n\nThere are various notebooks in this competition covering that topic. See [this one](https://www.kaggle.com/susnato/run-length-encoding-and-decoding-with-examples) for example.\nI have written one myself in a previous competition too ([here](https://www.kaggle.com/julian3833/2-understanding-and-plotting-rle-bounding-boxes))."
  },
  "source": "meta"
}