{
  "id": 109253,
  "title": "Regular image segmentation approach",
  "url": "/competitions/understanding_cloud_organization/discussion/109253",
  "author_name": "",
  "post_date": "2019-09-18T00:50:10.037734Z",
  "votes": 3,
  "comment_count": 5,
  "views": 0,
  "content": "<p>Hi everyone, this is my first experience with image segmentation. While I was browsing for some examples I saw that in most cases people have 2 sets of images, one for the images and another for the masks, here we have the images and the masks are string given on the dataset. I took some time and managed to create those two sets of images, I resize the images and create masks images from the data strings (the \"EncodedPixels\" column), this way I can try new things faster because now I have data similar to the regular approach.</p>\n\n<p>So my point is, is there anyone else doing this? and do you also think this may be a good idea?</p>\n\n<p>If more people are interested I can create a kernel and show how I do this process.</p>\n\n<p>ps: this way I can use regular Keras generators as well.</p>",
  "messages": [
    {
      "id": "628821",
      "postDate": "09/18/2019 00:50:10",
      "content": "<p>Hi everyone, this is my first experience with image segmentation. While I was browsing for some examples I saw that in most cases people have 2 sets of images, one for the images and another for the masks, here we have the images and the masks are string given on the dataset. I took some time and managed to create those two sets of images, I resize the images and create masks images from the data strings (the \"EncodedPixels\" column), this way I can try new things faster because now I have data similar to the regular approach.</p>\n\n<p>So my point is, is there anyone else doing this? and do you also think this may be a good idea?</p>\n\n<p>If more people are interested I can create a kernel and show how I do this process.</p>\n\n<p>ps: this way I can use regular Keras generators as well.</p>",
      "rawMarkdown": "Hi everyone, this is my first experience with image segmentation. While I was browsing for some examples I saw that in most cases people have 2 sets of images, one for the images and another for the masks, here we have the images and the masks are string given on the dataset. I took some time and managed to create those two sets of images, I resize the images and create masks images from the data strings (the \"EncodedPixels\" column), this way I can try new things faster because now I have data similar to the regular approach.\n\nSo my point is, is there anyone else doing this? and do you also think this may be a good idea?\n\nIf more people are interested I can create a kernel and show how I do this process.\n\nps: this way I can use regular Keras generators as well.",
      "votes": null
    },
    {
      "id": "628934",
      "postDate": "09/18/2019 06:13:09",
      "content": "<p>Dimitre <a href=\"/dimitreoliveira\">@dimitreoliveira</a> ,\nI'm not a subject-matter expert, but I believe everything speeding up our experiments is a good idea. </p>\n\n<p>In our particular case, given the dataset size, it seems we can speed up masks processing even further by pre-loading masks in memory. \nFYI, I'm using the following function for this purpose:\n<code>\ndef generate_masks(imgs_list):\n    masks_np = np.empty((len(imgs_list), image_height, image_width, len(model_class_names)), np.float32)\n    for class_i, class_name in enumerate(tqdm(model_class_names, desc='Generating masks..')):\n        for img_i, img_name in enumerate(imgs_list):\n            mask = make_mask(train_df_orig, img_name + '_' + class_name)\n            masks_np[img_i, :, :, class_i] = mask\n    return masks_np\n</code></p>\n\n<p>And then in the data-generator constructor I simply do\n<code>\nself.masks = generate_masks(self.images_list)\n</code>\nBased on <a href=\"https://www.quora.com/Is-the-speed-of-SSD-and-RAM-the-same/answer/Michael-Krech\">this recent post</a>,</p>\n\n<blockquote>\n  <p>we are really comparing a 3GB/sec max SSD (or 0.05GB/sec at random 4kb chunks) with an 36GB/sec main memory.</p>\n</blockquote>\n\n<p>So, even though modern SSDs are very impressive, in-memory still should give a tangible performance boost.</p>",
      "rawMarkdown": "Dimitre @dimitreoliveira ,\nI'm not a subject-matter expert, but I believe everything speeding up our experiments is a good idea. \n\nIn our particular case, given the dataset size, it seems we can speed up masks processing even further by pre-loading masks in memory. \nFYI, I'm using the following function for this purpose:\n```\ndef generate_masks(imgs_list):\n    masks_np = np.empty((len(imgs_list), image_height, image_width, len(model_class_names)), np.float32)\n    for class_i, class_name in enumerate(tqdm(model_class_names, desc='Generating masks..')):\n        for img_i, img_name in enumerate(imgs_list):\n            mask = make_mask(train_df_orig, img_name + '_' + class_name)\n            masks_np[img_i, :, :, class_i] = mask\n    return masks_np\n```\n\nAnd then in the data-generator constructor I simply do\n```\nself.masks = generate_masks(self.images_list)\n```\nBased on [this recent post](https://www.quora.com/Is-the-speed-of-SSD-and-RAM-the-same/answer/Michael-Krech),\n &gt; we are really comparing a 3GB/sec max SSD (or 0.05GB/sec at random 4kb chunks) with an 36GB/sec main memory.\n\nSo, even though modern SSDs are very impressive, in-memory still should give a tangible performance boost.",
      "votes": null
    },
    {
      "id": "628988",
      "postDate": "09/18/2019 07:54:26",
      "content": "<p><a href=\"/samusram\">@samusram</a> Raman, I  have a newbie question, to me <code>mask_np</code> is quite difficult to index, is it still efficient to use <code>mask_dict</code> and use <code>img_name</code> as key?</p>",
      "rawMarkdown": "samusram Raman, I  have a newbie question, to me `mask_np` is quite difficult to index, is it still efficient to use `mask_dict` and use `img_name` as key?",
      "votes": null
    },
    {
      "id": "629003",
      "postDate": "09/18/2019 08:12:32",
      "content": "<p><a href=\"/ratthachat\">@ratthachat</a> Jung, compared to reading from disk, I believe Yes, python dictionary in-memory would bring an improvement. I'm sure you know these things better than me, so just thinking out loud: dictionary is basically a hashmap, it cannot be more efficient compared to direct indexing ('cause it has hashing and possible clashes as extra steps), but still it has constant indexing time asymptotically.</p>\n\n<p>Actually, thx for your point about weird indexing.. I believe reordering dimensions might help:\n<code>\nmasks_np = np.empty((len(imgs_list), len(model_class_names), image_height, image_width), np.float32)\n</code>\nthen indexing should be\n<code>\nmasks_np[img_i][class_i]\n</code></p>",
      "rawMarkdown": "ratthachat Jung, compared to reading from disk, I believe Yes, python dictionary in-memory would bring an improvement. I'm sure you know these things better than me, so just thinking out loud: dictionary is basically a hashmap, it cannot be more efficient compared to direct indexing ('cause it has hashing and possible clashes as extra steps), but still it has constant indexing time asymptotically.\n\nActually, thx for your point about weird indexing.. I believe reordering dimensions might help:\n```\nmasks_np = np.empty((len(imgs_list), len(model_class_names), image_height, image_width), np.float32)\n```\nthen indexing should be\n```\nmasks_np[img_i][class_i]\n```",
      "votes": null
    },
    {
      "id": "629062",
      "postDate": "09/18/2019 09:27:30",
      "content": "<p>Raman, your information is always solid than mine 😁 </p>",
      "rawMarkdown": "Raman, your information is always solid than mine 😁",
      "votes": null
    },
    {
      "id": "629408",
      "postDate": "09/18/2019 18:30:06",
      "content": "<p><a href=\"/samusram\">@samusram</a> loading the masks on memory seems to be very clever, my problem with the current way people are doing this is because we load images and transform them while training, so resizing images and masks previously seems to be more efficient. But maybe it could be even fasters to preprocess images before training and loading masks from memory as you suggested. I will try it and report how it goes.</p>",
      "rawMarkdown": "samusram loading the masks on memory seems to be very clever, my problem with the current way people are doing this is because we load images and transform them while training, so resizing images and masks previously seems to be more efficient. But maybe it could be even fasters to preprocess images before training and loading masks from memory as you suggested. I will try it and report how it goes.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 628934,
      "author_name": "samusram",
      "author_url": "",
      "post_date": "09/18/2019 06:13:09",
      "content": "<p>Dimitre <a href=\"/dimitreoliveira\">@dimitreoliveira</a> ,\nI'm not a subject-matter expert, but I believe everything speeding up our experiments is a good idea. </p>\n\n<p>In our particular case, given the dataset size, it seems we can speed up masks processing even further by pre-loading masks in memory. \nFYI, I'm using the following function for this purpose:\n<code>\ndef generate_masks(imgs_list):\n    masks_np = np.empty((len(imgs_list), image_height, image_width, len(model_class_names)), np.float32)\n    for class_i, class_name in enumerate(tqdm(model_class_names, desc='Generating masks..')):\n        for img_i, img_name in enumerate(imgs_list):\n            mask = make_mask(train_df_orig, img_name + '_' + class_name)\n            masks_np[img_i, :, :, class_i] = mask\n    return masks_np\n</code></p>\n\n<p>And then in the data-generator constructor I simply do\n<code>\nself.masks = generate_masks(self.images_list)\n</code>\nBased on <a href=\"https://www.quora.com/Is-the-speed-of-SSD-and-RAM-the-same/answer/Michael-Krech\">this recent post</a>,</p>\n\n<blockquote>\n  <p>we are really comparing a 3GB/sec max SSD (or 0.05GB/sec at random 4kb chunks) with an 36GB/sec main memory.</p>\n</blockquote>\n\n<p>So, even though modern SSDs are very impressive, in-memory still should give a tangible performance boost.</p>",
      "votes": null,
      "replies": [
        {
          "id": 628988,
          "author_name": "ratthachat",
          "author_url": "",
          "post_date": "09/18/2019 07:54:26",
          "content": "<p><a href=\"/samusram\">@samusram</a> Raman, I  have a newbie question, to me <code>mask_np</code> is quite difficult to index, is it still efficient to use <code>mask_dict</code> and use <code>img_name</code> as key?</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 629003,
          "author_name": "samusram",
          "author_url": "",
          "post_date": "09/18/2019 08:12:32",
          "content": "<p><a href=\"/ratthachat\">@ratthachat</a> Jung, compared to reading from disk, I believe Yes, python dictionary in-memory would bring an improvement. I'm sure you know these things better than me, so just thinking out loud: dictionary is basically a hashmap, it cannot be more efficient compared to direct indexing ('cause it has hashing and possible clashes as extra steps), but still it has constant indexing time asymptotically.</p>\n\n<p>Actually, thx for your point about weird indexing.. I believe reordering dimensions might help:\n<code>\nmasks_np = np.empty((len(imgs_list), len(model_class_names), image_height, image_width), np.float32)\n</code>\nthen indexing should be\n<code>\nmasks_np[img_i][class_i]\n</code></p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 629062,
          "author_name": "ratthachat",
          "author_url": "",
          "post_date": "09/18/2019 09:27:30",
          "content": "<p>Raman, your information is always solid than mine 😁 </p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 629408,
          "author_name": "dimitreoliveira",
          "author_url": "",
          "post_date": "09/18/2019 18:30:06",
          "content": "<p><a href=\"/samusram\">@samusram</a> loading the masks on memory seems to be very clever, my problem with the current way people are doing this is because we load images and transform them while training, so resizing images and masks previously seems to be more efficient. But maybe it could be even fasters to preprocess images before training and loading masks from memory as you suggested. I will try it and report how it goes.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "628821": "Hi everyone, this is my first experience with image segmentation. While I was browsing for some examples I saw that in most cases people have 2 sets of images, one for the images and another for the masks, here we have the images and the masks are string given on the dataset. I took some time and managed to create those two sets of images, I resize the images and create masks images from the data strings (the \"EncodedPixels\" column), this way I can try new things faster because now I have data similar to the regular approach.\n\nSo my point is, is there anyone else doing this? and do you also think this may be a good idea?\n\nIf more people are interested I can create a kernel and show how I do this process.\n\nps: this way I can use regular Keras generators as well.",
    "628934": "Dimitre @dimitreoliveira ,\nI'm not a subject-matter expert, but I believe everything speeding up our experiments is a good idea. \n\nIn our particular case, given the dataset size, it seems we can speed up masks processing even further by pre-loading masks in memory. \nFYI, I'm using the following function for this purpose:\n```\ndef generate_masks(imgs_list):\n    masks_np = np.empty((len(imgs_list), image_height, image_width, len(model_class_names)), np.float32)\n    for class_i, class_name in enumerate(tqdm(model_class_names, desc='Generating masks..')):\n        for img_i, img_name in enumerate(imgs_list):\n            mask = make_mask(train_df_orig, img_name + '_' + class_name)\n            masks_np[img_i, :, :, class_i] = mask\n    return masks_np\n```\n\nAnd then in the data-generator constructor I simply do\n```\nself.masks = generate_masks(self.images_list)\n```\nBased on [this recent post](https://www.quora.com/Is-the-speed-of-SSD-and-RAM-the-same/answer/Michael-Krech),\n &gt; we are really comparing a 3GB/sec max SSD (or 0.05GB/sec at random 4kb chunks) with an 36GB/sec main memory.\n\nSo, even though modern SSDs are very impressive, in-memory still should give a tangible performance boost.",
    "628988": "samusram Raman, I  have a newbie question, to me `mask_np` is quite difficult to index, is it still efficient to use `mask_dict` and use `img_name` as key?",
    "629003": "ratthachat Jung, compared to reading from disk, I believe Yes, python dictionary in-memory would bring an improvement. I'm sure you know these things better than me, so just thinking out loud: dictionary is basically a hashmap, it cannot be more efficient compared to direct indexing ('cause it has hashing and possible clashes as extra steps), but still it has constant indexing time asymptotically.\n\nActually, thx for your point about weird indexing.. I believe reordering dimensions might help:\n```\nmasks_np = np.empty((len(imgs_list), len(model_class_names), image_height, image_width), np.float32)\n```\nthen indexing should be\n```\nmasks_np[img_i][class_i]\n```",
    "629062": "Raman, your information is always solid than mine 😁",
    "629408": "samusram loading the masks on memory seems to be very clever, my problem with the current way people are doing this is because we load images and transform them while training, so resizing images and masks previously seems to be more efficient. But maybe it could be even fasters to preprocess images before training and loading masks from memory as you suggested. I will try it and report how it goes."
  },
  "source": "meta"
}