{
  "id": 472117,
  "title": "Error in RLE processing",
  "url": "/competitions/blood-vessel-segmentation/discussion/472117",
  "author_name": "Pritam Sinha",
  "post_date": "2024-01-30T21:19:24.399000",
  "votes": 0,
  "comment_count": 10,
  "views": 0,
  "content": "<p>I am getting an operand shape mismatch error when I am converting the mask to RLE using the function:</p>\n<pre><code> ():\n    pixel = mask.flatten()\n    pixel = np.concatenate([[], pixel, []])\n    run = np.where(pixel[:] != pixel[:-])[] + \n    run[::] -= run[::]\n    rle = .join((r)  r  run)\n     rle == :\n        rle = \n     rle\n</code></pre>\n<p>My model output <code>512x512</code> and after redimension it, when running the RLE code, it says <code>operands could not be broadcast together with shapes (1324,) (1325,) (1324,)</code> in <code>run[1::2] -= run[::2]</code> line.<br>\nThis error is coming not for every cases. For example, when I am trying <code>kidney_1_dense_0301.tif</code> it is coming.</p>\n<p>Anyone encountered this type of issue? How to solve it?</p>",
  "messages": [
    {
      "id": 2628446,
      "postDate": "2024-01-31T10:10:27.467Z",
      "content": "<p>It only happens when 512 size comes inn. In that case pixel[1:] and pixel[:-1] turns odd and breaks.</p>\n<p>EDIT: It happens when a even dimension comes inn with real values. So <a href=\"https://www.kaggle.com/pritamsinha23\" target=\"_blank\">@pritamsinha23</a> be sure your mask is actually a mask.</p>",
      "rawMarkdown": "It only happens when 512 size comes inn. In that case pixel[1:] and pixel[:-1] turns odd and breaks.\n\nEDIT: It happens when a even dimension comes inn with real values. So @pritamsinha23 be sure your mask is actually a mask.",
      "votes": 1
    },
    {
      "id": 2628292,
      "postDate": "2024-01-31T08:00:29.947Z",
      "content": "<p>Hi! Tell me pls, what size of your segmented image before rle encoding (after resize)? <br>\nHowever, it's a bit strange. If product of height and width is even this algorithm works correct.<br>\nExample:<br>\nImage size = 3 × 2 = 6 px (flatten)<br>\npixel[::2] == pixel[[0, 2, 4]]<br>\npixel[1::2] == pixel[[1, 3, 5]]<br>\nIf <strong>both height and width are odd</strong> (else product will be even as in example above), algorithm <strong>doesn't work</strong><br>\nExample:<br>\nImage size = 3 × 3 = 9 px (flatten)<br>\npixel[::2] == pixel[[0, 2, 4, 6, 8]]<br>\npixel[1::2] == pixel[[1, 3, 5, 7]]</p>\n<p>Try to check image size. It'll be 1303×912 px (product is even)</p>",
      "rawMarkdown": "Hi! Tell me pls, what size of your segmented image before rle encoding (after resize)? \nHowever, it's a bit strange. If product of height and width is even this algorithm works correct.\nExample:\nImage size = 3 × 2 = 6 px (flatten)\npixel[::2] == pixel[[0, 2, 4]]\npixel[1::2] == pixel[[1, 3, 5]]\nIf **both height and width are odd** (else product will be even as in example above), algorithm **doesn't work**\nExample:\nImage size = 3 × 3 = 9 px (flatten)\npixel[::2] == pixel[[0, 2, 4, 6, 8]]\npixel[1::2] == pixel[[1, 3, 5, 7]]\n\nTry to check image size. It'll be 1303×912 px (product is even)",
      "replies": [
        {
          "id": 2628372,
          "postDate": "2024-01-31T08:57:40.923Z",
          "content": "<p>I've been playng with random arrays and I reproduced the error. I'm tryng to undersrand the cause now. For some reason run[1::2] are 1 unit shorter than run[::2]. Is it supposed to mask have a certain values format? Integers may be?</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F8722753%2F22123d432681898afc9483de1d247795%2Frle_error.jpg?generation=1706691668235522&amp;alt=media\"></p>",
          "rawMarkdown": "I've been playng with random arrays and I reproduced the error. I'm tryng to undersrand the cause now. For some reason run[1::2] are 1 unit shorter than run[::2]. Is it supposed to mask have a certain values format? Integers may be?\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F8722753%2F22123d432681898afc9483de1d247795%2Frle_error.jpg?generation=1706691668235522&alt=media)",
          "votes": 1,
          "replies": [
            {
              "id": 2628380,
              "postDate": "2024-01-31T09:06:19.550Z",
              "content": "<p>It's true if both width and height are odd. Then their product (length of flatten array) will be odd and length of slices differ by one element as shown above. If either width or height is even flatten array has even length so slices will have same lengths: the first one contains odd indexes only, the second one - even indexes. Good luck ).</p>\n<p>UPD. It's really strange. As a variant you can use rle_encode function from Carvana Image Masking competition notebooks with small modifications if this function doesn't work</p>",
              "rawMarkdown": "It's true if both width and height are odd. Then their product (length of flatten array) will be odd and length of slices differ by one element as shown above. If either width or height is even flatten array has even length so slices will have same lengths: the first one contains odd indexes only, the second one - even indexes. Good luck ).\n\nUPD. It's really strange. As a variant you can use rle_encode function from Carvana Image Masking competition notebooks with small modifications if this function doesn't work",
              "votes": 1
            },
            {
              "id": 2628411,
              "postDate": "2024-01-31T09:33:19.343Z",
              "rawMarkdown": "",
              "isDeleted": true
            },
            {
              "id": 2628426,
              "postDate": "2024-01-31T09:43:47.607Z",
              "content": "<p>I added depth cause I suspect is tryng to encode batches not single images. But in the example 24x512x512 are all even…<br>\nI've just changed mask to be numpy array and it worked. It seems that the problem is on np.where() on a torch tensor or the slices in a torch tensor.<br>\nNevermind, sometimes happens, sometimes not. So it's about the values.<br>\nIt only happens with 512 on H or W. Apparently always when both.<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F8722753%2F937901ebf14159d37cf075df88ca45bf%2Frle_error.jpg?generation=1706694622647748&amp;alt=media\"><br>\nOdd is working too.</p>",
              "rawMarkdown": "I added depth cause I suspect is tryng to encode batches not single images. But in the example 24x512x512 are all even...\nI've just changed mask to be numpy array and it worked. It seems that the problem is on np.where() on a torch tensor or the slices in a torch tensor.\nNevermind, sometimes happens, sometimes not. So it's about the values.\nIt only happens with 512 on H or W. Apparently always when both.\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F8722753%2F937901ebf14159d37cf075df88ca45bf%2Frle_error.jpg?generation=1706694622647748&alt=media)\nOdd is working too.",
              "votes": 1
            }
          ]
        }
      ]
    },
    {
      "id": 2628268,
      "postDate": "2024-01-31T07:43:45.110Z",
      "content": "<p>Whats the shape of the mask you are feeding rle function?</p>\n<p>EDIT: Oh. If you guys are using cv2.resize() check carefully documentation since there is a particular HxW order.</p>",
      "rawMarkdown": "Whats the shape of the mask you are feeding rle function?\n\nEDIT: Oh. If you guys are using cv2.resize() check carefully documentation since there is a particular HxW order.",
      "replies": [
        {
          "id": 2628280,
          "postDate": "2024-01-31T07:52:55.670Z",
          "content": "<p>model outputs <code>512x512</code> and redimension it to <code>(1303, 912)</code> using <code>interpolate()</code> then feeding it to <code>rle_decode()</code>. <br>\nAnd I am not using <code>cv2.resize()</code>.</p>",
          "rawMarkdown": "model outputs `512x512` and redimension it to `(1303, 912)` using `interpolate()` then feeding it to `rle_decode()`. \nAnd I am not using `cv2.resize()`.",
          "replies": [
            {
              "id": 2628306,
              "postDate": "2024-01-31T08:07:18.650Z",
              "content": "<p>The one right before rle function, the one that is flatten.</p>",
              "rawMarkdown": "The one right before rle function, the one that is flatten."
            }
          ]
        }
      ]
    },
    {
      "id": 2627747,
      "postDate": "2024-01-30T21:19:24.400Z",
      "content": "<p>I am getting an operand shape mismatch error when I am converting the mask to RLE using the function:</p>\n<pre><code> ():\n    pixel = mask.flatten()\n    pixel = np.concatenate([[], pixel, []])\n    run = np.where(pixel[:] != pixel[:-])[] + \n    run[::] -= run[::]\n    rle = .join((r)  r  run)\n     rle == :\n        rle = \n     rle\n</code></pre>\n<p>My model output <code>512x512</code> and after redimension it, when running the RLE code, it says <code>operands could not be broadcast together with shapes (1324,) (1325,) (1324,)</code> in <code>run[1::2] -= run[::2]</code> line.<br>\nThis error is coming not for every cases. For example, when I am trying <code>kidney_1_dense_0301.tif</code> it is coming.</p>\n<p>Anyone encountered this type of issue? How to solve it?</p>",
      "rawMarkdown": "I am getting an operand shape mismatch error when I am converting the mask to RLE using the function:\n\n```python\ndef rle_encode(mask):\n    pixel = mask.flatten()\n    pixel = np.concatenate([[0], pixel, [0]])\n    run = np.where(pixel[1:] != pixel[:-1])[0] + 1\n    run[1::2] -= run[::2]\n    rle = ' '.join(str(r) for r in run)\n    if rle == '':\n        rle = '1 0'\n    return rle\n```\nMy model output `512x512` and after redimension it, when running the RLE code, it says `operands could not be broadcast together with shapes (1324,) (1325,) (1324,) ` in `run[1::2] -= run[::2]` line.\nThis error is coming not for every cases. For example, when I am trying `kidney_1_dense_0301.tif` it is coming.\n\nAnyone encountered this type of issue? How to solve it?"
    },
    {
      "id": 2627771,
      "postDate": "2024-01-30T21:46:23.560Z",
      "rawMarkdown": "",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 2628446,
      "author_name": "Ángel Jacinto Sánchez Ruiz",
      "author_url": "",
      "post_date": "2024-01-31T10:10:27.467000",
      "content": "<p>It only happens when 512 size comes inn. In that case pixel[1:] and pixel[:-1] turns odd and breaks.</p>\n<p>EDIT: It happens when a even dimension comes inn with real values. So <a href=\"https://www.kaggle.com/pritamsinha23\" target=\"_blank\">@pritamsinha23</a> be sure your mask is actually a mask.</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 2628292,
      "author_name": "Stanislav Baratov",
      "author_url": "",
      "post_date": "2024-01-31T08:00:29.947000",
      "content": "<p>Hi! Tell me pls, what size of your segmented image before rle encoding (after resize)? <br>\nHowever, it's a bit strange. If product of height and width is even this algorithm works correct.<br>\nExample:<br>\nImage size = 3 × 2 = 6 px (flatten)<br>\npixel[::2] == pixel[[0, 2, 4]]<br>\npixel[1::2] == pixel[[1, 3, 5]]<br>\nIf <strong>both height and width are odd</strong> (else product will be even as in example above), algorithm <strong>doesn't work</strong><br>\nExample:<br>\nImage size = 3 × 3 = 9 px (flatten)<br>\npixel[::2] == pixel[[0, 2, 4, 6, 8]]<br>\npixel[1::2] == pixel[[1, 3, 5, 7]]</p>\n<p>Try to check image size. It'll be 1303×912 px (product is even)</p>",
      "votes": 0,
      "replies": [
        {
          "id": 2628372,
          "author_name": "Ángel Jacinto Sánchez Ruiz",
          "author_url": "",
          "post_date": "2024-01-31T08:57:40.923000",
          "content": "<p>I've been playng with random arrays and I reproduced the error. I'm tryng to undersrand the cause now. For some reason run[1::2] are 1 unit shorter than run[::2]. Is it supposed to mask have a certain values format? Integers may be?</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F8722753%2F22123d432681898afc9483de1d247795%2Frle_error.jpg?generation=1706691668235522&amp;alt=media\"></p>",
          "votes": 1,
          "replies": [
            {
              "id": 2628380,
              "author_name": "Stanislav Baratov",
              "author_url": "",
              "post_date": "2024-01-31T09:06:19.550000",
              "content": "<p>It's true if both width and height are odd. Then their product (length of flatten array) will be odd and length of slices differ by one element as shown above. If either width or height is even flatten array has even length so slices will have same lengths: the first one contains odd indexes only, the second one - even indexes. Good luck ).</p>\n<p>UPD. It's really strange. As a variant you can use rle_encode function from Carvana Image Masking competition notebooks with small modifications if this function doesn't work</p>",
              "votes": 1,
              "replies": []
            },
            {
              "id": 2628411,
              "author_name": "",
              "author_url": "",
              "post_date": "2024-01-31T09:33:19.343000",
              "content": "",
              "votes": 0,
              "replies": []
            },
            {
              "id": 2628426,
              "author_name": "Ángel Jacinto Sánchez Ruiz",
              "author_url": "",
              "post_date": "2024-01-31T09:43:47.607000",
              "content": "<p>I added depth cause I suspect is tryng to encode batches not single images. But in the example 24x512x512 are all even…<br>\nI've just changed mask to be numpy array and it worked. It seems that the problem is on np.where() on a torch tensor or the slices in a torch tensor.<br>\nNevermind, sometimes happens, sometimes not. So it's about the values.<br>\nIt only happens with 512 on H or W. Apparently always when both.<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F8722753%2F937901ebf14159d37cf075df88ca45bf%2Frle_error.jpg?generation=1706694622647748&amp;alt=media\"><br>\nOdd is working too.</p>",
              "votes": 1,
              "replies": []
            }
          ]
        }
      ]
    },
    {
      "id": 2628268,
      "author_name": "Ángel Jacinto Sánchez Ruiz",
      "author_url": "",
      "post_date": "2024-01-31T07:43:45.110000",
      "content": "<p>Whats the shape of the mask you are feeding rle function?</p>\n<p>EDIT: Oh. If you guys are using cv2.resize() check carefully documentation since there is a particular HxW order.</p>",
      "votes": 0,
      "replies": [
        {
          "id": 2628280,
          "author_name": "Pritam Sinha",
          "author_url": "",
          "post_date": "2024-01-31T07:52:55.670000",
          "content": "<p>model outputs <code>512x512</code> and redimension it to <code>(1303, 912)</code> using <code>interpolate()</code> then feeding it to <code>rle_decode()</code>. <br>\nAnd I am not using <code>cv2.resize()</code>.</p>",
          "votes": 0,
          "replies": [
            {
              "id": 2628306,
              "author_name": "Ángel Jacinto Sánchez Ruiz",
              "author_url": "",
              "post_date": "2024-01-31T08:07:18.650000",
              "content": "<p>The one right before rle function, the one that is flatten.</p>",
              "votes": 0,
              "replies": []
            }
          ]
        }
      ]
    },
    {
      "id": 2627771,
      "author_name": "",
      "author_url": "",
      "post_date": "2024-01-30T21:46:23.560000",
      "content": "",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2628446": "It only happens when 512 size comes inn. In that case pixel[1:] and pixel[:-1] turns odd and breaks.\n\nEDIT: It happens when a even dimension comes inn with real values. So @pritamsinha23 be sure your mask is actually a mask.",
    "2628292": "Hi! Tell me pls, what size of your segmented image before rle encoding (after resize)? \nHowever, it's a bit strange. If product of height and width is even this algorithm works correct.\nExample:\nImage size = 3 × 2 = 6 px (flatten)\npixel[::2] == pixel[[0, 2, 4]]\npixel[1::2] == pixel[[1, 3, 5]]\nIf **both height and width are odd** (else product will be even as in example above), algorithm **doesn't work**\nExample:\nImage size = 3 × 3 = 9 px (flatten)\npixel[::2] == pixel[[0, 2, 4, 6, 8]]\npixel[1::2] == pixel[[1, 3, 5, 7]]\n\nTry to check image size. It'll be 1303×912 px (product is even)",
    "2628268": "Whats the shape of the mask you are feeding rle function?\n\nEDIT: Oh. If you guys are using cv2.resize() check carefully documentation since there is a particular HxW order.",
    "2627747": "I am getting an operand shape mismatch error when I am converting the mask to RLE using the function:\n\n```python\ndef rle_encode(mask):\n    pixel = mask.flatten()\n    pixel = np.concatenate([[0], pixel, [0]])\n    run = np.where(pixel[1:] != pixel[:-1])[0] + 1\n    run[1::2] -= run[::2]\n    rle = ' '.join(str(r) for r in run)\n    if rle == '':\n        rle = '1 0'\n    return rle\n```\nMy model output `512x512` and after redimension it, when running the RLE code, it says `operands could not be broadcast together with shapes (1324,) (1325,) (1324,) ` in `run[1::2] -= run[::2]` line.\nThis error is coming not for every cases. For example, when I am trying `kidney_1_dense_0301.tif` it is coming.\n\nAnyone encountered this type of issue? How to solve it?",
    "2627771": ""
  }
}