{
  "id": 399312,
  "title": "RLE with Patches",
  "url": "/competitions/vesuvius-challenge-ink-detection/discussion/399312",
  "author_name": "",
  "post_date": "2023-04-03T14:26:52.030109200Z",
  "votes": 5,
  "comment_count": 2,
  "views": 0,
  "content": "<p>I followed this notebook to get patches to train on <a href=\"https://www.kaggle.com/code/fchollet/a-simple-high-performance-tf-data-pipeline\" target=\"_blank\">https://www.kaggle.com/code/fchollet/a-simple-high-performance-tf-data-pipeline</a></p>\n<p>but I am struggling to generate a submission file with its sort of data.</p>\n<p>At the end you have patches of 128x128 - do you have to restitch them together to run them through RLE or is there a better way?</p>",
  "messages": [
    {
      "id": "2207628",
      "postDate": "04/03/2023 14:26:52",
      "content": "<p>I followed this notebook to get patches to train on <a href=\"https://www.kaggle.com/code/fchollet/a-simple-high-performance-tf-data-pipeline\" target=\"_blank\">https://www.kaggle.com/code/fchollet/a-simple-high-performance-tf-data-pipeline</a></p>\n<p>but I am struggling to generate a submission file with its sort of data.</p>\n<p>At the end you have patches of 128x128 - do you have to restitch them together to run them through RLE or is there a better way?</p>",
      "rawMarkdown": "I followed this notebook to get patches to train on https://www.kaggle.com/code/fchollet/a-simple-high-performance-tf-data-pipeline\n\nbut I am struggling to generate a submission file with its sort of data.\n\nAt the end you have patches of 128x128 - do you have to restitch them together to run them through RLE or is there a better way?",
      "votes": null
    },
    {
      "id": "2208195",
      "postDate": "04/03/2023 21:40:57",
      "content": "<p>If you have trained a model using patches *, then you will need to stitch them together to form a full-sized image before encoding it using run-length encoding (RLE).</p>\n<p>To do this, you can use the same approach as used during inference to break the full-sized image into patches. You will need to keep track of the position of each patch in the original image so that you can correctly reassemble them.</p>\n<p>Once you have stitched together the patches to form the full-sized image, you can encode it using RLE to generate the submission file.</p>\n<p>Here's some example code that you can use to stitch the patches together:</p>\n<p>python</p>\n<p>def stitch_patches(patches, patch_size, image_size):<br>\n    \"\"\"Stitch together patches into a full-sized image.\"\"\"<br>\n    image = np.zeros(image_size, dtype=np.uint8)<br>\n    rows, cols = image_size<br>\n    patch_rows, patch_cols = patch_size<br>\n    num_rows = rows // patch_rows<br>\n    num_cols = cols // patch_cols</p>\n<pre><code>for i in range(num_rows):\n    for j in range(num_cols):\n        patch = patches[i*num_cols + j]\n        row_start = i * patch_rows\n        row_end = row_start + patch_rows\n        col_start = j * patch_cols\n        col_end = col_start + patch_cols\n        image[row_start:row_end, col_start:col_end] = patch\n\nreturn image\n</code></pre>\n<p>You can use this function to stitch together the patches that you have generated during inference, and then encode the resulting image using RLE to generate the submission file.</p>",
      "rawMarkdown": "If you have trained a model using patches *, then you will need to stitch them together to form a full-sized image before encoding it using run-length encoding (RLE).\n\nTo do this, you can use the same approach as used during inference to break the full-sized image into patches. You will need to keep track of the position of each patch in the original image so that you can correctly reassemble them.\n\nOnce you have stitched together the patches to form the full-sized image, you can encode it using RLE to generate the submission file.\n\nHere's some example code that you can use to stitch the patches together:\n\npython\n\ndef stitch_patches(patches, patch_size, image_size):\n    \"\"\"Stitch together patches into a full-sized image.\"\"\"\n    image = np.zeros(image_size, dtype=np.uint8)\n    rows, cols = image_size\n    patch_rows, patch_cols = patch_size\n    num_rows = rows // patch_rows\n    num_cols = cols // patch_cols\n    \n    for i in range(num_rows):\n        for j in range(num_cols):\n            patch = patches[i*num_cols + j]\n            row_start = i * patch_rows\n            row_end = row_start + patch_rows\n            col_start = j * patch_cols\n            col_end = col_start + patch_cols\n            image[row_start:row_end, col_start:col_end] = patch\n    \n    return image\n\nYou can use this function to stitch together the patches that you have generated during inference, and then encode the resulting image using RLE to generate the submission file.",
      "votes": null
    },
    {
      "id": "2214558",
      "postDate": "04/08/2023 15:15:51",
      "content": "<p>Thanks I ended up using something a little different:</p>\n<pre><code>image = np.zeros(test_mask.shape, dtype=np.float32)\n loc_batch, patch_batch  tqdm((locations_ds, test_ds)):\n    ans = model1.predict(patch_batch)\n    index = \n     batchElement  ans:\n        x, y = loc_batch[index]\n        (batchElement.shape)\n        after = batchElement.reshape((,))\n        (after.shape)\n        ()\n        image[x - PATCH_HALFSIZE :x + PATCH_HALFSIZE, y - PATCH_HALFSIZE :y + PATCH_HALFSIZE] = after\n        (after)\n        ()\n        (image)\n        index=index+\ngc.collect()\n()\n</code></pre>",
      "rawMarkdown": "Thanks I ended up using something a little different:\n\n```python\nimage = np.zeros(test_mask.shape, dtype=np.float32)\nfor loc_batch, patch_batch in tqdm(zip(locations_ds, test_ds)):\n    ans = model1.predict(patch_batch)\n    index = 0\n    for batchElement in ans:\n        x, y = loc_batch[index]\n        print(batchElement.shape)\n        after = batchElement.reshape((128,128))\n        print(after.shape)\n        print(f\"Patch patch_batch[{index}] is for location ({x}, {y})\")\n        image[x - PATCH_HALFSIZE :x + PATCH_HALFSIZE, y - PATCH_HALFSIZE :y + PATCH_HALFSIZE] = after\n        print(after)\n        print('...')\n        print(image)\n        index=index+1\ngc.collect()\nprint(\"Loading complete.\")\n\n```",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2208195,
      "author_name": "thedataperson",
      "author_url": "",
      "post_date": "04/03/2023 21:40:57",
      "content": "<p>If you have trained a model using patches *, then you will need to stitch them together to form a full-sized image before encoding it using run-length encoding (RLE).</p>\n<p>To do this, you can use the same approach as used during inference to break the full-sized image into patches. You will need to keep track of the position of each patch in the original image so that you can correctly reassemble them.</p>\n<p>Once you have stitched together the patches to form the full-sized image, you can encode it using RLE to generate the submission file.</p>\n<p>Here's some example code that you can use to stitch the patches together:</p>\n<p>python</p>\n<p>def stitch_patches(patches, patch_size, image_size):<br>\n    \"\"\"Stitch together patches into a full-sized image.\"\"\"<br>\n    image = np.zeros(image_size, dtype=np.uint8)<br>\n    rows, cols = image_size<br>\n    patch_rows, patch_cols = patch_size<br>\n    num_rows = rows // patch_rows<br>\n    num_cols = cols // patch_cols</p>\n<pre><code>for i in range(num_rows):\n    for j in range(num_cols):\n        patch = patches[i*num_cols + j]\n        row_start = i * patch_rows\n        row_end = row_start + patch_rows\n        col_start = j * patch_cols\n        col_end = col_start + patch_cols\n        image[row_start:row_end, col_start:col_end] = patch\n\nreturn image\n</code></pre>\n<p>You can use this function to stitch together the patches that you have generated during inference, and then encode the resulting image using RLE to generate the submission file.</p>",
      "votes": null,
      "replies": [
        {
          "id": 2214558,
          "author_name": "sethkitchen",
          "author_url": "",
          "post_date": "04/08/2023 15:15:51",
          "content": "<p>Thanks I ended up using something a little different:</p>\n<pre><code>image = np.zeros(test_mask.shape, dtype=np.float32)\n loc_batch, patch_batch  tqdm((locations_ds, test_ds)):\n    ans = model1.predict(patch_batch)\n    index = \n     batchElement  ans:\n        x, y = loc_batch[index]\n        (batchElement.shape)\n        after = batchElement.reshape((,))\n        (after.shape)\n        ()\n        image[x - PATCH_HALFSIZE :x + PATCH_HALFSIZE, y - PATCH_HALFSIZE :y + PATCH_HALFSIZE] = after\n        (after)\n        ()\n        (image)\n        index=index+\ngc.collect()\n()\n</code></pre>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2207628": "I followed this notebook to get patches to train on https://www.kaggle.com/code/fchollet/a-simple-high-performance-tf-data-pipeline\n\nbut I am struggling to generate a submission file with its sort of data.\n\nAt the end you have patches of 128x128 - do you have to restitch them together to run them through RLE or is there a better way?",
    "2208195": "If you have trained a model using patches *, then you will need to stitch them together to form a full-sized image before encoding it using run-length encoding (RLE).\n\nTo do this, you can use the same approach as used during inference to break the full-sized image into patches. You will need to keep track of the position of each patch in the original image so that you can correctly reassemble them.\n\nOnce you have stitched together the patches to form the full-sized image, you can encode it using RLE to generate the submission file.\n\nHere's some example code that you can use to stitch the patches together:\n\npython\n\ndef stitch_patches(patches, patch_size, image_size):\n    \"\"\"Stitch together patches into a full-sized image.\"\"\"\n    image = np.zeros(image_size, dtype=np.uint8)\n    rows, cols = image_size\n    patch_rows, patch_cols = patch_size\n    num_rows = rows // patch_rows\n    num_cols = cols // patch_cols\n    \n    for i in range(num_rows):\n        for j in range(num_cols):\n            patch = patches[i*num_cols + j]\n            row_start = i * patch_rows\n            row_end = row_start + patch_rows\n            col_start = j * patch_cols\n            col_end = col_start + patch_cols\n            image[row_start:row_end, col_start:col_end] = patch\n    \n    return image\n\nYou can use this function to stitch together the patches that you have generated during inference, and then encode the resulting image using RLE to generate the submission file.",
    "2214558": "Thanks I ended up using something a little different:\n\n```python\nimage = np.zeros(test_mask.shape, dtype=np.float32)\nfor loc_batch, patch_batch in tqdm(zip(locations_ds, test_ds)):\n    ans = model1.predict(patch_batch)\n    index = 0\n    for batchElement in ans:\n        x, y = loc_batch[index]\n        print(batchElement.shape)\n        after = batchElement.reshape((128,128))\n        print(after.shape)\n        print(f\"Patch patch_batch[{index}] is for location ({x}, {y})\")\n        image[x - PATCH_HALFSIZE :x + PATCH_HALFSIZE, y - PATCH_HALFSIZE :y + PATCH_HALFSIZE] = after\n        print(after)\n        print('...')\n        print(image)\n        index=index+1\ngc.collect()\nprint(\"Loading complete.\")\n\n```"
  },
  "source": "meta"
}