{
  "id": 333712,
  "title": "Plot Images with Masks",
  "url": "/competitions/hubmap-organ-segmentation/discussion/333712",
  "author_name": "",
  "post_date": "2022-06-27T20:42:02.223439Z",
  "votes": 13,
  "comment_count": 3,
  "views": 0,
  "content": "<p>A short code snippet to plot the competition's training images with the associated masks. Notice   that this isn't the most optimized way to do it since we transform PIL images into tensors before retransforming into PIL images for plotting but it could be useful if you have tensors (when you do the inference for example). I will post a follow-up with a more optimized version later.</p>\n<p>The code comes mostly from this great <a href=\"http://pytorch.org/vision/stable/index.html\" target=\"_blank\">torchvision</a> <a href=\"https://pytorch.org/vision/master/auto_examples/plot_visualization_utils.html\" target=\"_blank\">tutorial</a>:</p>\n<pre><code>from torchvision.utils import draw_segmentation_masks\nfrom torchvision.transforms import ToTensor\nimport torch\nimport matplotlib.pyplot as plt\nimport torchvision.transforms.functional as F\nimport numpy as np\nfrom PIL import Image\n\n\nplt.rcParams[\"savefig.bbox\"] = 'tight'\n\n\n\n# Adapted from https://www.kaggle.com/code/xhlulu/efficient-mask2rle/notebook\ndef rle2mask(mask_rle, shape=(1600,256)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    Source: https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T\n\n\ndef show(imgs):\n    if not isinstance(imgs, list):\n        imgs = [imgs]\n    fig, axs = plt.subplots(ncols=len(imgs), squeeze=False)\n    for i, img in enumerate(imgs):\n        img = img.detach()\n        img = F.to_pil_image(img)\n        axs[0, i].imshow(np.asarray(img))\n        axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])\n\n\ntrain_df = pd.read_csv(\"../input/hubmap-organ-segmentation/train.csv\")\n\n# You can add more ids here\n\n\nrow_ids = [1, 10]\n\ngrid = []\n\n\nfor row_id in row_ids:\n    rle = train_df.loc[row_id, \"rle\"]\n    img_id = train_df.loc[row_id, \"id\"]\n    img_size = (int(train_df.loc[row_id, \"img_width\"]), \n                int(train_df.loc[row_id, \"img_height\"]))\n\n    mask = rle2mask(rle, shape=img_size)\n    img = Image.open(f\"../input/hubmap-organ-segmentation/train_images/{img_id}.tiff\")\n\n\n    torch_img = (255 * ToTensor()(img)).to(torch.uint8)\n    torch_mask = ToTensor()(mask).to(torch.bool)\n    img_with_mask = draw_segmentation_masks(torch_img, masks=torch_mask, alpha=0.7)\n    grid.append(img_with_mask)\n\n\n\nshow(grid)\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2F9399016a58c98461f346416120dab235%2Fimages_with_masks.jpg?generation=1656362680285139&amp;alt=media\" alt=\"images_with_masks\"></p>",
  "messages": [
    {
      "id": "1835552",
      "postDate": "06/27/2022 20:42:02",
      "content": "<p>A short code snippet to plot the competition's training images with the associated masks. Notice   that this isn't the most optimized way to do it since we transform PIL images into tensors before retransforming into PIL images for plotting but it could be useful if you have tensors (when you do the inference for example). I will post a follow-up with a more optimized version later.</p>\n<p>The code comes mostly from this great <a href=\"http://pytorch.org/vision/stable/index.html\" target=\"_blank\">torchvision</a> <a href=\"https://pytorch.org/vision/master/auto_examples/plot_visualization_utils.html\" target=\"_blank\">tutorial</a>:</p>\n<pre><code>from torchvision.utils import draw_segmentation_masks\nfrom torchvision.transforms import ToTensor\nimport torch\nimport matplotlib.pyplot as plt\nimport torchvision.transforms.functional as F\nimport numpy as np\nfrom PIL import Image\n\n\nplt.rcParams[\"savefig.bbox\"] = 'tight'\n\n\n\n# Adapted from https://www.kaggle.com/code/xhlulu/efficient-mask2rle/notebook\ndef rle2mask(mask_rle, shape=(1600,256)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    Source: https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T\n\n\ndef show(imgs):\n    if not isinstance(imgs, list):\n        imgs = [imgs]\n    fig, axs = plt.subplots(ncols=len(imgs), squeeze=False)\n    for i, img in enumerate(imgs):\n        img = img.detach()\n        img = F.to_pil_image(img)\n        axs[0, i].imshow(np.asarray(img))\n        axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])\n\n\ntrain_df = pd.read_csv(\"../input/hubmap-organ-segmentation/train.csv\")\n\n# You can add more ids here\n\n\nrow_ids = [1, 10]\n\ngrid = []\n\n\nfor row_id in row_ids:\n    rle = train_df.loc[row_id, \"rle\"]\n    img_id = train_df.loc[row_id, \"id\"]\n    img_size = (int(train_df.loc[row_id, \"img_width\"]), \n                int(train_df.loc[row_id, \"img_height\"]))\n\n    mask = rle2mask(rle, shape=img_size)\n    img = Image.open(f\"../input/hubmap-organ-segmentation/train_images/{img_id}.tiff\")\n\n\n    torch_img = (255 * ToTensor()(img)).to(torch.uint8)\n    torch_mask = ToTensor()(mask).to(torch.bool)\n    img_with_mask = draw_segmentation_masks(torch_img, masks=torch_mask, alpha=0.7)\n    grid.append(img_with_mask)\n\n\n\nshow(grid)\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2F9399016a58c98461f346416120dab235%2Fimages_with_masks.jpg?generation=1656362680285139&amp;alt=media\" alt=\"images_with_masks\"></p>",
      "rawMarkdown": "A short code snippet to plot the competition's training images with the associated masks. Notice   that this isn't the most optimized way to do it since we transform PIL images into tensors before retransforming into PIL images for plotting but it could be useful if you have tensors (when you do the inference for example). I will post a follow-up with a more optimized version later.\n\nThe code comes mostly from this great [torchvision](http://pytorch.org/vision/stable/index.html) [tutorial](https://pytorch.org/vision/master/auto_examples/plot_visualization_utils.html):\n\n\n```\nfrom torchvision.utils import draw_segmentation_masks\nfrom torchvision.transforms import ToTensor\nimport torch\nimport matplotlib.pyplot as plt\nimport torchvision.transforms.functional as F\nimport numpy as np\nfrom PIL import Image\n\n\nplt.rcParams[\"savefig.bbox\"] = 'tight'\n\n\n\n# Adapted from https://www.kaggle.com/code/xhlulu/efficient-mask2rle/notebook\ndef rle2mask(mask_rle, shape=(1600,256)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    Source: https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T\n\n\ndef show(imgs):\n    if not isinstance(imgs, list):\n        imgs = [imgs]\n    fig, axs = plt.subplots(ncols=len(imgs), squeeze=False)\n    for i, img in enumerate(imgs):\n        img = img.detach()\n        img = F.to_pil_image(img)\n        axs[0, i].imshow(np.asarray(img))\n        axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])\n\n\ntrain_df = pd.read_csv(\"../input/hubmap-organ-segmentation/train.csv\")\n\n# You can add more ids here\n\n\nrow_ids = [1, 10]\n\ngrid = []\n\n\nfor row_id in row_ids:\n    rle = train_df.loc[row_id, \"rle\"]\n    img_id = train_df.loc[row_id, \"id\"]\n    img_size = (int(train_df.loc[row_id, \"img_width\"]), \n                int(train_df.loc[row_id, \"img_height\"]))\n\n    mask = rle2mask(rle, shape=img_size)\n    img = Image.open(f\"../input/hubmap-organ-segmentation/train_images/{img_id}.tiff\")\n\n\n    torch_img = (255 * ToTensor()(img)).to(torch.uint8)\n    torch_mask = ToTensor()(mask).to(torch.bool)\n    img_with_mask = draw_segmentation_masks(torch_img, masks=torch_mask, alpha=0.7)\n    grid.append(img_with_mask)\n    \n\n\nshow(grid)\n\n```\n\n![images_with_masks](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2F9399016a58c98461f346416120dab235%2Fimages_with_masks.jpg?generation=1656362680285139&alt=media)",
      "votes": null
    },
    {
      "id": "1837734",
      "postDate": "06/29/2022 21:01:18",
      "content": "<p>This import is missing, so I am adding it:</p>\n<pre><code>from PIL import Image\n</code></pre>",
      "rawMarkdown": "This import is missing, so I am adding it:\n\n```\nfrom PIL import Image\n\n```",
      "votes": null
    },
    {
      "id": "1838204",
      "postDate": "06/30/2022 10:16:13",
      "content": "<p>have a dataset with generated masks as images: <a href=\"https://www.kaggle.com/datasets/jirkaborovec/hacking-the-human-body-annotation-masks\" target=\"_blank\">https://www.kaggle.com/datasets/jirkaborovec/hacking-the-human-body-annotation-masks</a><br>\nplus a notebook on how to tile the large images: <a href=\"https://www.kaggle.com/code/jirkaborovec/ftus-segm-decompose-large-images-to-tiles\" target=\"_blank\">https://www.kaggle.com/code/jirkaborovec/ftus-segm-decompose-large-images-to-tiles</a></p>",
      "rawMarkdown": "have a dataset with generated masks as images: https://www.kaggle.com/datasets/jirkaborovec/hacking-the-human-body-annotation-masks\nplus a notebook on how to tile the large images: https://www.kaggle.com/code/jirkaborovec/ftus-segm-decompose-large-images-to-tiles",
      "votes": null
    },
    {
      "id": "1838316",
      "postDate": "06/30/2022 12:03:19",
      "content": "<p>That's great <a href=\"https://www.kaggle.com/jirkaborovec\" target=\"_blank\">@jirkaborovec</a>, thanks for making these and sharing. 🔥</p>",
      "rawMarkdown": "That's great @jirkaborovec, thanks for making these and sharing. 🔥",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1837734,
      "author_name": "yassinealouini",
      "author_url": "",
      "post_date": "06/29/2022 21:01:18",
      "content": "<p>This import is missing, so I am adding it:</p>\n<pre><code>from PIL import Image\n</code></pre>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1838204,
      "author_name": "jirkaborovec",
      "author_url": "",
      "post_date": "06/30/2022 10:16:13",
      "content": "<p>have a dataset with generated masks as images: <a href=\"https://www.kaggle.com/datasets/jirkaborovec/hacking-the-human-body-annotation-masks\" target=\"_blank\">https://www.kaggle.com/datasets/jirkaborovec/hacking-the-human-body-annotation-masks</a><br>\nplus a notebook on how to tile the large images: <a href=\"https://www.kaggle.com/code/jirkaborovec/ftus-segm-decompose-large-images-to-tiles\" target=\"_blank\">https://www.kaggle.com/code/jirkaborovec/ftus-segm-decompose-large-images-to-tiles</a></p>",
      "votes": null,
      "replies": [
        {
          "id": 1838316,
          "author_name": "yassinealouini",
          "author_url": "",
          "post_date": "06/30/2022 12:03:19",
          "content": "<p>That's great <a href=\"https://www.kaggle.com/jirkaborovec\" target=\"_blank\">@jirkaborovec</a>, thanks for making these and sharing. 🔥</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1835552": "A short code snippet to plot the competition's training images with the associated masks. Notice   that this isn't the most optimized way to do it since we transform PIL images into tensors before retransforming into PIL images for plotting but it could be useful if you have tensors (when you do the inference for example). I will post a follow-up with a more optimized version later.\n\nThe code comes mostly from this great [torchvision](http://pytorch.org/vision/stable/index.html) [tutorial](https://pytorch.org/vision/master/auto_examples/plot_visualization_utils.html):\n\n\n```\nfrom torchvision.utils import draw_segmentation_masks\nfrom torchvision.transforms import ToTensor\nimport torch\nimport matplotlib.pyplot as plt\nimport torchvision.transforms.functional as F\nimport numpy as np\nfrom PIL import Image\n\n\nplt.rcParams[\"savefig.bbox\"] = 'tight'\n\n\n\n# Adapted from https://www.kaggle.com/code/xhlulu/efficient-mask2rle/notebook\ndef rle2mask(mask_rle, shape=(1600,256)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n    Source: https://www.kaggle.com/paulorzp/rle-functions-run-lenght-encode-decode\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T\n\n\ndef show(imgs):\n    if not isinstance(imgs, list):\n        imgs = [imgs]\n    fig, axs = plt.subplots(ncols=len(imgs), squeeze=False)\n    for i, img in enumerate(imgs):\n        img = img.detach()\n        img = F.to_pil_image(img)\n        axs[0, i].imshow(np.asarray(img))\n        axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])\n\n\ntrain_df = pd.read_csv(\"../input/hubmap-organ-segmentation/train.csv\")\n\n# You can add more ids here\n\n\nrow_ids = [1, 10]\n\ngrid = []\n\n\nfor row_id in row_ids:\n    rle = train_df.loc[row_id, \"rle\"]\n    img_id = train_df.loc[row_id, \"id\"]\n    img_size = (int(train_df.loc[row_id, \"img_width\"]), \n                int(train_df.loc[row_id, \"img_height\"]))\n\n    mask = rle2mask(rle, shape=img_size)\n    img = Image.open(f\"../input/hubmap-organ-segmentation/train_images/{img_id}.tiff\")\n\n\n    torch_img = (255 * ToTensor()(img)).to(torch.uint8)\n    torch_mask = ToTensor()(mask).to(torch.bool)\n    img_with_mask = draw_segmentation_masks(torch_img, masks=torch_mask, alpha=0.7)\n    grid.append(img_with_mask)\n    \n\n\nshow(grid)\n\n```\n\n![images_with_masks](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2F9399016a58c98461f346416120dab235%2Fimages_with_masks.jpg?generation=1656362680285139&alt=media)",
    "1837734": "This import is missing, so I am adding it:\n\n```\nfrom PIL import Image\n\n```",
    "1838204": "have a dataset with generated masks as images: https://www.kaggle.com/datasets/jirkaborovec/hacking-the-human-body-annotation-masks\nplus a notebook on how to tile the large images: https://www.kaggle.com/code/jirkaborovec/ftus-segm-decompose-large-images-to-tiles",
    "1838316": "That's great @jirkaborovec, thanks for making these and sharing. 🔥"
  },
  "source": "meta"
}