{
  "id": 229536,
  "title": "enc2mask function not working",
  "url": "/competitions/hubmap-kidney-segmentation/discussion/229536",
  "author_name": "Victor Sharkeev",
  "post_date": "2021-03-30T16:16:23.271000",
  "votes": 0,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Hello Kagglers!</p>\n<p>When I was training my model in the Google Colab environment, I noticed that the proposed enc2mask function suddenly stopped working (does not return the mask).</p>\n<pre><code>def enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for m,enc in enumerate(encs):\n        if isinstance(enc,np.float) and np.isnan(enc): continue\n        s = enc.split()\n        for i in range(len(s)//2):\n            start = int(s[2*i]) - 1\n            length = int(s[2*i+1])\n            img[start:start+length] = 1 + m\n    return img.reshape(shape).T\n</code></pre>\n<p>Therefore, based on the structure of the <a href=\"https://www.kaggle.com/c/hubmap-kidney-segmentation/overview/supervised-ml-evaluation\" target=\"_blank\">submission file</a>, I propose a new version of the enc2mask function, which is simpler and faster:</p>\n<pre><code>def enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n\n    tmp = iter(encs.split(' '))\n    encs_lst = [(int(item), int(next(tmp))) for item in tmp]\n\n    for enc in encs_lst:\n        img[enc[0]-1:enc[0]-1+enc[1]] = 1\n\n    return img.reshape(shape).T\n</code></pre>\n<p>What do you think about it?</p>\n<p>Regards,<br>\nVictor</p>",
  "messages": [
    {
      "id": 1257221,
      "postDate": "2021-03-30T16:16:23.270Z",
      "content": "<p>Hello Kagglers!</p>\n<p>When I was training my model in the Google Colab environment, I noticed that the proposed enc2mask function suddenly stopped working (does not return the mask).</p>\n<pre><code>def enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for m,enc in enumerate(encs):\n        if isinstance(enc,np.float) and np.isnan(enc): continue\n        s = enc.split()\n        for i in range(len(s)//2):\n            start = int(s[2*i]) - 1\n            length = int(s[2*i+1])\n            img[start:start+length] = 1 + m\n    return img.reshape(shape).T\n</code></pre>\n<p>Therefore, based on the structure of the <a href=\"https://www.kaggle.com/c/hubmap-kidney-segmentation/overview/supervised-ml-evaluation\" target=\"_blank\">submission file</a>, I propose a new version of the enc2mask function, which is simpler and faster:</p>\n<pre><code>def enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n\n    tmp = iter(encs.split(' '))\n    encs_lst = [(int(item), int(next(tmp))) for item in tmp]\n\n    for enc in encs_lst:\n        img[enc[0]-1:enc[0]-1+enc[1]] = 1\n\n    return img.reshape(shape).T\n</code></pre>\n<p>What do you think about it?</p>\n<p>Regards,<br>\nVictor</p>",
      "rawMarkdown": "Hello Kagglers!\n\nWhen I was training my model in the Google Colab environment, I noticed that the proposed enc2mask function suddenly stopped working (does not return the mask).\n```\ndef enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for m,enc in enumerate(encs):\n        if isinstance(enc,np.float) and np.isnan(enc): continue\n        s = enc.split()\n        for i in range(len(s)//2):\n            start = int(s[2*i]) - 1\n            length = int(s[2*i+1])\n            img[start:start+length] = 1 + m\n    return img.reshape(shape).T\n```\nTherefore, based on the structure of the [submission file](https://www.kaggle.com/c/hubmap-kidney-segmentation/overview/supervised-ml-evaluation), I propose a new version of the enc2mask function, which is simpler and faster:\n```\ndef enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    \n    tmp = iter(encs.split(' '))\n    encs_lst = [(int(item), int(next(tmp))) for item in tmp]\n\n    for enc in encs_lst:\n        img[enc[0]-1:enc[0]-1+enc[1]] = 1\n    \n    return img.reshape(shape).T\n```\nWhat do you think about it?\n\nRegards,\nVictor"
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1257221": "Hello Kagglers!\n\nWhen I was training my model in the Google Colab environment, I noticed that the proposed enc2mask function suddenly stopped working (does not return the mask).\n```\ndef enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for m,enc in enumerate(encs):\n        if isinstance(enc,np.float) and np.isnan(enc): continue\n        s = enc.split()\n        for i in range(len(s)//2):\n            start = int(s[2*i]) - 1\n            length = int(s[2*i+1])\n            img[start:start+length] = 1 + m\n    return img.reshape(shape).T\n```\nTherefore, based on the structure of the [submission file](https://www.kaggle.com/c/hubmap-kidney-segmentation/overview/supervised-ml-evaluation), I propose a new version of the enc2mask function, which is simpler and faster:\n```\ndef enc2mask(encs, shape):\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    \n    tmp = iter(encs.split(' '))\n    encs_lst = [(int(item), int(next(tmp))) for item in tmp]\n\n    for enc in encs_lst:\n        img[enc[0]-1:enc[0]-1+enc[1]] = 1\n    \n    return img.reshape(shape).T\n```\nWhat do you think about it?\n\nRegards,\nVictor"
  }
}