{
  "id": 286051,
  "title": "Python Masks",
  "url": "/competitions/sartorius-cell-instance-segmentation/discussion/286051",
  "author_name": "",
  "post_date": "2021-11-07T17:53:53.068428700Z",
  "votes": 1,
  "comment_count": 1,
  "views": 0,
  "content": "<p>I'd like to comprehend the following function <em>plot_masks_all</em> (found in <a href=\"https://www.kaggle.com/ishandutta/sartorius-indepth-eda-explanation-model):\" target=\"_blank\">https://www.kaggle.com/ishandutta/sartorius-indepth-eda-explanation-model):</a></p>\n<blockquote>\n  <p>def plot_masks_all(image_ids, colors=True):<br>\n      fig, ax = plt.subplots(len(image_ids),3,figsize=(16, 21))<br>\n      for idx,image_id in enumerate(image_ids):<br>\n          labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()<br>\n          cell_type = df_train[df_train[\"id\"] == image_id][\"cell_type\"].tolist()<br>\n          cmap = {\"shsy5y\":(0,0,255),\"astro\":(0,255,0),\"cort\":(255,0,0)}</p>\n</blockquote>\n<pre><code>    if colors:\n        mask = np.zeros((520, 704, 3))\n        for label,cell_t in zip(labels,cell_type):\n            c = cmap[cell_t]\n            mask += rle_decode(label, shape=(520, 704, 3), color=c)\n    else:\n        mask = np.zeros((520, 704, 1))\n        for label in labels:\n            mask += rle_decode(label, shape=(520, 704, 1))\n    mask = mask.clip(0, 1)\n</code></pre>\n<p>What does it do, and does   mask = np.zeros((520, 704, 1)) mean, mask += rle_decode(label, shape=(520, 704, 1)) and mask = mask.clip(0, 1)?</p>",
  "messages": [
    {
      "id": "1574612",
      "postDate": "11/07/2021 17:53:53",
      "content": "<p>I'd like to comprehend the following function <em>plot_masks_all</em> (found in <a href=\"https://www.kaggle.com/ishandutta/sartorius-indepth-eda-explanation-model):\" target=\"_blank\">https://www.kaggle.com/ishandutta/sartorius-indepth-eda-explanation-model):</a></p>\n<blockquote>\n  <p>def plot_masks_all(image_ids, colors=True):<br>\n      fig, ax = plt.subplots(len(image_ids),3,figsize=(16, 21))<br>\n      for idx,image_id in enumerate(image_ids):<br>\n          labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()<br>\n          cell_type = df_train[df_train[\"id\"] == image_id][\"cell_type\"].tolist()<br>\n          cmap = {\"shsy5y\":(0,0,255),\"astro\":(0,255,0),\"cort\":(255,0,0)}</p>\n</blockquote>\n<pre><code>    if colors:\n        mask = np.zeros((520, 704, 3))\n        for label,cell_t in zip(labels,cell_type):\n            c = cmap[cell_t]\n            mask += rle_decode(label, shape=(520, 704, 3), color=c)\n    else:\n        mask = np.zeros((520, 704, 1))\n        for label in labels:\n            mask += rle_decode(label, shape=(520, 704, 1))\n    mask = mask.clip(0, 1)\n</code></pre>\n<p>What does it do, and does   mask = np.zeros((520, 704, 1)) mean, mask += rle_decode(label, shape=(520, 704, 1)) and mask = mask.clip(0, 1)?</p>",
      "rawMarkdown": "I'd like to comprehend the following function *plot_masks_all* (found in https://www.kaggle.com/ishandutta/sartorius-indepth-eda-explanation-model):\n\n> def plot_masks_all(image_ids, colors=True):\n    fig, ax = plt.subplots(len(image_ids),3,figsize=(16, 21))\n    for idx,image_id in enumerate(image_ids):\n        labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()\n        cell_type = df_train[df_train[\"id\"] == image_id][\"cell_type\"].tolist()\n        cmap = {\"shsy5y\":(0,0,255),\"astro\":(0,255,0),\"cort\":(255,0,0)}\n\n        if colors:\n            mask = np.zeros((520, 704, 3))\n            for label,cell_t in zip(labels,cell_type):\n                c = cmap[cell_t]\n                mask += rle_decode(label, shape=(520, 704, 3), color=c)\n        else:\n            mask = np.zeros((520, 704, 1))\n            for label in labels:\n                mask += rle_decode(label, shape=(520, 704, 1))\n        mask = mask.clip(0, 1)\n\nWhat does it do, and does   mask = np.zeros((520, 704, 1)) mean, mask += rle_decode(label, shape=(520, 704, 1)) and mask = mask.clip(0, 1)?",
      "votes": null
    },
    {
      "id": "1574781",
      "postDate": "11/07/2021 21:36:23",
      "content": "<p>usually a mask is just a binary image with 0 and 1, so you only really need the first layer.<br>\nyou can construct a rgb mask by having 3 identical layers for the mask matrix and multiply the 1s per 255. not sure it helps.</p>",
      "rawMarkdown": "usually a mask is just a binary image with 0 and 1, so you only really need the first layer.\nyou can construct a rgb mask by having 3 identical layers for the mask matrix and multiply the 1s per 255. not sure it helps.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1574781,
      "author_name": "chabir",
      "author_url": "",
      "post_date": "11/07/2021 21:36:23",
      "content": "<p>usually a mask is just a binary image with 0 and 1, so you only really need the first layer.<br>\nyou can construct a rgb mask by having 3 identical layers for the mask matrix and multiply the 1s per 255. not sure it helps.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1574612": "I'd like to comprehend the following function *plot_masks_all* (found in https://www.kaggle.com/ishandutta/sartorius-indepth-eda-explanation-model):\n\n> def plot_masks_all(image_ids, colors=True):\n    fig, ax = plt.subplots(len(image_ids),3,figsize=(16, 21))\n    for idx,image_id in enumerate(image_ids):\n        labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()\n        cell_type = df_train[df_train[\"id\"] == image_id][\"cell_type\"].tolist()\n        cmap = {\"shsy5y\":(0,0,255),\"astro\":(0,255,0),\"cort\":(255,0,0)}\n\n        if colors:\n            mask = np.zeros((520, 704, 3))\n            for label,cell_t in zip(labels,cell_type):\n                c = cmap[cell_t]\n                mask += rle_decode(label, shape=(520, 704, 3), color=c)\n        else:\n            mask = np.zeros((520, 704, 1))\n            for label in labels:\n                mask += rle_decode(label, shape=(520, 704, 1))\n        mask = mask.clip(0, 1)\n\nWhat does it do, and does   mask = np.zeros((520, 704, 1)) mean, mask += rle_decode(label, shape=(520, 704, 1)) and mask = mask.clip(0, 1)?",
    "1574781": "usually a mask is just a binary image with 0 and 1, so you only really need the first layer.\nyou can construct a rgb mask by having 3 identical layers for the mask matrix and multiply the 1s per 255. not sure it helps."
  },
  "source": "meta"
}