{
  "id": 62512,
  "title": "Submission Error: Index out of Bounds",
  "url": "/competitions/airbus-ship-detection/discussion/62512",
  "author_name": "maximilian",
  "post_date": "2018-08-02T13:56:07.086000",
  "votes": 3,
  "comment_count": 0,
  "views": 0,
  "content": "<p>When I upload my submission I get an exception: \"Index was outside the bounds of the array.\" Why? I use the following code for run length encoding:</p>\n\n<pre><code># ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef rle_encode(img):\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.concatenate([[0], pixels, [0]])\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\n<p>The indexing of the encoding starts with 1 for the first pixel and ends with size^2. This is also, what the function given <a href=\"https://www.kaggle.com/inversion/run-length-decoding-quick-start/notebook\">here</a> expects. Could it be, that the bounds check assumes size^2-1 as last allowed value? The images I encode are of course 768x768 in size and have the same IDs as the sample submission. Or did I possibly make a mistake somewhere else?</p>\n\n<p><strong>ANSWER:</strong> The error was very probably caused by an error in the submission file. For a given image id I had detected some ships, but sometimes added an empty line as well. If you give ships and no ships at the same time, this will produce the error.</p>",
  "messages": [
    {
      "id": 365395,
      "postDate": "2018-08-02T13:56:07.087Z",
      "content": "<p>When I upload my submission I get an exception: \"Index was outside the bounds of the array.\" Why? I use the following code for run length encoding:</p>\n\n<pre><code># ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\ndef rle_encode(img):\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.concatenate([[0], pixels, [0]])\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\n<p>The indexing of the encoding starts with 1 for the first pixel and ends with size^2. This is also, what the function given <a href=\"https://www.kaggle.com/inversion/run-length-decoding-quick-start/notebook\">here</a> expects. Could it be, that the bounds check assumes size^2-1 as last allowed value? The images I encode are of course 768x768 in size and have the same IDs as the sample submission. Or did I possibly make a mistake somewhere else?</p>\n\n<p><strong>ANSWER:</strong> The error was very probably caused by an error in the submission file. For a given image id I had detected some ships, but sometimes added an empty line as well. If you give ships and no ships at the same time, this will produce the error.</p>",
      "rawMarkdown": "When I upload my submission I get an exception: \"Index was outside the bounds of the array.\" Why? I use the following code for run length encoding:\n\n    # ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\n    def rle_encode(img):\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.concatenate([[0], pixels, [0]])\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\nThe indexing of the encoding starts with 1 for the first pixel and ends with size^2. This is also, what the function given [here][1] expects. Could it be, that the bounds check assumes size^2-1 as last allowed value? The images I encode are of course 768x768 in size and have the same IDs as the sample submission. Or did I possibly make a mistake somewhere else?\n\n**ANSWER:** The error was very probably caused by an error in the submission file. For a given image id I had detected some ships, but sometimes added an empty line as well. If you give ships and no ships at the same time, this will produce the error.\n\n  [1]: https://www.kaggle.com/inversion/run-length-decoding-quick-start/notebook",
      "votes": 3
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "365395": "When I upload my submission I get an exception: \"Index was outside the bounds of the array.\" Why? I use the following code for run length encoding:\n\n    # ref: https://www.kaggle.com/paulorzp/run-length-encode-and-decode\n    def rle_encode(img):\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.concatenate([[0], pixels, [0]])\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\nThe indexing of the encoding starts with 1 for the first pixel and ends with size^2. This is also, what the function given [here][1] expects. Could it be, that the bounds check assumes size^2-1 as last allowed value? The images I encode are of course 768x768 in size and have the same IDs as the sample submission. Or did I possibly make a mistake somewhere else?\n\n**ANSWER:** The error was very probably caused by an error in the submission file. For a given image id I had detected some ships, but sometimes added an empty line as well. If you give ships and no ships at the same time, this will produce the error.\n\n  [1]: https://www.kaggle.com/inversion/run-length-decoding-quick-start/notebook"
  }
}