{
  "id": 50335,
  "title": "Run length encoding error,Pytorch ",
  "url": "/competitions/data-science-bowl-2018/discussion/50335",
  "author_name": "DecentMakeover",
  "post_date": "2018-02-22T16:53:18.548000",
  "votes": 0,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Hi \nMy predictions look like this-(image attached)\nA single image prediction array looks like this,</p>\n\n<pre><code> predict[0]\narray([[ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   ..., \n   [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32)\n</code></pre>\n\n<p>This is how i do run length encoding,</p>\n\n<pre><code>    from skimage.morphology import label\ndef rle_encoding(x):\n    dots = np.where(x.T.flatten() == 1)[0]\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if (b&amp;gt;prev+1): run_lengths.extend((b + 1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return run_lengths\n\ndef prob_to_rles(x, cutoff=0.5):\n    lab_img = label(x &amp;gt; cutoff)\n    for i in range(1, lab_img.max() + 1):\n        yield rle_encoding(lab_img == i)\n\nnew_test_ids = []\nrles = []\nfor n, id_ in enumerate(test_ids):\n    rle = list(prob_to_rles(predict[n]))\n    rles.extend(rle)\n    new_test_ids.extend([id_] * len(rle))\n\n\nsub = pd.DataFrame()\nsub['ImageId'] = new_test_ids\nsub['EncodedPixels'] = pd.Series(rles).apply(lambda x: ' '.join(str(y) for y in x))\nsub.to_csv('sub-dsbowl2018-1.csv', index=False)\n</code></pre>\n\n<p>instead of getting the 4152 rows i get 3654 by which i get my score as 0,Im not sure what the problem is ,Any suggestion would be helpful.</p>\n\n<p>Do let me know if you need more Info!</p>\n\n<p>Thanks </p>",
  "messages": [
    {
      "id": 286691,
      "postDate": "2018-02-22T16:53:18.547Z",
      "content": "<p>Hi \nMy predictions look like this-(image attached)\nA single image prediction array looks like this,</p>\n\n<pre><code> predict[0]\narray([[ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   ..., \n   [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n   [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32)\n</code></pre>\n\n<p>This is how i do run length encoding,</p>\n\n<pre><code>    from skimage.morphology import label\ndef rle_encoding(x):\n    dots = np.where(x.T.flatten() == 1)[0]\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if (b&amp;gt;prev+1): run_lengths.extend((b + 1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return run_lengths\n\ndef prob_to_rles(x, cutoff=0.5):\n    lab_img = label(x &amp;gt; cutoff)\n    for i in range(1, lab_img.max() + 1):\n        yield rle_encoding(lab_img == i)\n\nnew_test_ids = []\nrles = []\nfor n, id_ in enumerate(test_ids):\n    rle = list(prob_to_rles(predict[n]))\n    rles.extend(rle)\n    new_test_ids.extend([id_] * len(rle))\n\n\nsub = pd.DataFrame()\nsub['ImageId'] = new_test_ids\nsub['EncodedPixels'] = pd.Series(rles).apply(lambda x: ' '.join(str(y) for y in x))\nsub.to_csv('sub-dsbowl2018-1.csv', index=False)\n</code></pre>\n\n<p>instead of getting the 4152 rows i get 3654 by which i get my score as 0,Im not sure what the problem is ,Any suggestion would be helpful.</p>\n\n<p>Do let me know if you need more Info!</p>\n\n<p>Thanks </p>",
      "rawMarkdown": "Hi \nMy predictions look like this-(image attached)\nA single image prediction array looks like this,\n\n     predict[0]\n    array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       ..., \n       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32)\n\nThis is how i do run length encoding,\n\n        from skimage.morphology import label\n    def rle_encoding(x):\n        dots = np.where(x.T.flatten() == 1)[0]\n        run_lengths = []\n        prev = -2\n        for b in dots:\n            if (b&gt;prev+1): run_lengths.extend((b + 1, 0))\n            run_lengths[-1] += 1\n            prev = b\n        return run_lengths\n\n    def prob_to_rles(x, cutoff=0.5):\n        lab_img = label(x &gt; cutoff)\n        for i in range(1, lab_img.max() + 1):\n            yield rle_encoding(lab_img == i)\n\n    new_test_ids = []\n    rles = []\n    for n, id_ in enumerate(test_ids):\n        rle = list(prob_to_rles(predict[n]))\n        rles.extend(rle)\n        new_test_ids.extend([id_] * len(rle))\n\n\n    sub = pd.DataFrame()\n    sub['ImageId'] = new_test_ids\n    sub['EncodedPixels'] = pd.Series(rles).apply(lambda x: ' '.join(str(y) for y in x))\n    sub.to_csv('sub-dsbowl2018-1.csv', index=False)\n\ninstead of getting the 4152 rows i get 3654 by which i get my score as 0,Im not sure what the problem is ,Any suggestion would be helpful.\n\nDo let me know if you need more Info!\n\nThanks "
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "286691": "Hi \nMy predictions look like this-(image attached)\nA single image prediction array looks like this,\n\n     predict[0]\n    array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       ..., \n       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n       [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32)\n\nThis is how i do run length encoding,\n\n        from skimage.morphology import label\n    def rle_encoding(x):\n        dots = np.where(x.T.flatten() == 1)[0]\n        run_lengths = []\n        prev = -2\n        for b in dots:\n            if (b&gt;prev+1): run_lengths.extend((b + 1, 0))\n            run_lengths[-1] += 1\n            prev = b\n        return run_lengths\n\n    def prob_to_rles(x, cutoff=0.5):\n        lab_img = label(x &gt; cutoff)\n        for i in range(1, lab_img.max() + 1):\n            yield rle_encoding(lab_img == i)\n\n    new_test_ids = []\n    rles = []\n    for n, id_ in enumerate(test_ids):\n        rle = list(prob_to_rles(predict[n]))\n        rles.extend(rle)\n        new_test_ids.extend([id_] * len(rle))\n\n\n    sub = pd.DataFrame()\n    sub['ImageId'] = new_test_ids\n    sub['EncodedPixels'] = pd.Series(rles).apply(lambda x: ' '.join(str(y) for y in x))\n    sub.to_csv('sub-dsbowl2018-1.csv', index=False)\n\ninstead of getting the 4152 rows i get 3654 by which i get my score as 0,Im not sure what the problem is ,Any suggestion would be helpful.\n\nDo let me know if you need more Info!\n\nThanks "
  }
}