{
  "id": 108898,
  "title": "Ideas for merging ensemble's predictions",
  "url": "/competitions/understanding_cloud_organization/discussion/108898",
  "author_name": "",
  "post_date": "2019-09-14T17:56:03.755683100Z",
  "votes": 6,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Dear all,</p>\n\n<p>I am looking for a way to merge the predictions of my different models.</p>\n\n<p>The models are: \n- U-Net with ResNet-34 backbone\n- LinkNet with EfficientNetB3 backbone.</p>\n\n<p>Both those architectures were trained on 5 folds, resulting in 10 models.</p>\n\n<p>For now, I have tried to intersect the ten predicted masks, a mask having the shape (batch_size, image_width, image_height, n_chanels = 4). This results in a tensor of zeros.</p>\n\n<p>Following is the code: </p>\n\n<p><code>\npreds = np.ones((BATCH_SIZE,  *IMG_SHAPE, N_CLASSES)) <br>\nfor modelResUnet, modelEffLinknet in zip(modelsResUnet, modelsEffLinknet):\n      preds *= make_preds(modelResUnet, test_imgs, batch_idx)\n      preds *= make_preds(modelEffLinknet, test_imgs, batch_idx)\npreds = np.around(preds)\n</code></p>\n\n<p>My second attempt being to average the predictions of each model's folds and finally computing the intersection of each model's average prediction.</p>\n\n<p>```\npredsResUnet = []\npredsEffLinknet = []</p>\n\n<p>for modelResUnet, modelEffLinknet in zip(modelsResUnet, modelsEffLinknet):\n     predsResUnet.append(make_preds(modelResUnet, test_imgs, batch_idx))\n     predsEffLinknet.append(make_preds(modelEffLinknet, test_imgs, batch_idx))</p>\n\n<p>predsResUnet = np.stack(predsResUnet)\npredsResUnet = np.mean(predsResUnet, axis = 0)</p>\n\n<p>predsEffLinknet = np.stack(predsEffLinknet)\npredsEffLinknet = np.mean(predsEffLinknet, axis = 0)</p>\n\n<p>preds = predsResUnet * predsEffLinknet\npreds = np.around(preds)`\n```</p>\n\n<p>The LB score of this second attempt is 0.626.</p>\n\n<p>I also wanted to add a classifier or a regressor on top of this 'stage', however I am not sure what would be a good way of doing it. The idea is to feed as input the predicted masks and as labels the true mask (rle decoded data).</p>\n\n<p>Has someone any thoughts about this matter or would be interested in teaming up to try something similar?</p>\n\n<p>P.-S.: I am aware that some things might be unclear so do not hesitate to ask for clarification.</p>\n\n<p>By the way, if someone is intersted, using only the folds from the UNet model gave a LB score of 0.616. And for the LinkNet folds a LB score of 0.602, using a code similar to second code snippet</p>",
  "messages": [
    {
      "id": "626708",
      "postDate": "09/14/2019 17:56:03",
      "content": "<p>Dear all,</p>\n\n<p>I am looking for a way to merge the predictions of my different models.</p>\n\n<p>The models are: \n- U-Net with ResNet-34 backbone\n- LinkNet with EfficientNetB3 backbone.</p>\n\n<p>Both those architectures were trained on 5 folds, resulting in 10 models.</p>\n\n<p>For now, I have tried to intersect the ten predicted masks, a mask having the shape (batch_size, image_width, image_height, n_chanels = 4). This results in a tensor of zeros.</p>\n\n<p>Following is the code: </p>\n\n<p><code>\npreds = np.ones((BATCH_SIZE,  *IMG_SHAPE, N_CLASSES)) <br>\nfor modelResUnet, modelEffLinknet in zip(modelsResUnet, modelsEffLinknet):\n      preds *= make_preds(modelResUnet, test_imgs, batch_idx)\n      preds *= make_preds(modelEffLinknet, test_imgs, batch_idx)\npreds = np.around(preds)\n</code></p>\n\n<p>My second attempt being to average the predictions of each model's folds and finally computing the intersection of each model's average prediction.</p>\n\n<p>```\npredsResUnet = []\npredsEffLinknet = []</p>\n\n<p>for modelResUnet, modelEffLinknet in zip(modelsResUnet, modelsEffLinknet):\n     predsResUnet.append(make_preds(modelResUnet, test_imgs, batch_idx))\n     predsEffLinknet.append(make_preds(modelEffLinknet, test_imgs, batch_idx))</p>\n\n<p>predsResUnet = np.stack(predsResUnet)\npredsResUnet = np.mean(predsResUnet, axis = 0)</p>\n\n<p>predsEffLinknet = np.stack(predsEffLinknet)\npredsEffLinknet = np.mean(predsEffLinknet, axis = 0)</p>\n\n<p>preds = predsResUnet * predsEffLinknet\npreds = np.around(preds)`\n```</p>\n\n<p>The LB score of this second attempt is 0.626.</p>\n\n<p>I also wanted to add a classifier or a regressor on top of this 'stage', however I am not sure what would be a good way of doing it. The idea is to feed as input the predicted masks and as labels the true mask (rle decoded data).</p>\n\n<p>Has someone any thoughts about this matter or would be interested in teaming up to try something similar?</p>\n\n<p>P.-S.: I am aware that some things might be unclear so do not hesitate to ask for clarification.</p>\n\n<p>By the way, if someone is intersted, using only the folds from the UNet model gave a LB score of 0.616. And for the LinkNet folds a LB score of 0.602, using a code similar to second code snippet</p>",
      "rawMarkdown": "Dear all,\n\nI am looking for a way to merge the predictions of my different models.\n\nThe models are: \n- U-Net with ResNet-34 backbone\n- LinkNet with EfficientNetB3 backbone.\n\nBoth those architectures were trained on 5 folds, resulting in 10 models.\n\nFor now, I have tried to intersect the ten predicted masks, a mask having the shape (batch_size, image_width, image_height, n_chanels = 4). This results in a tensor of zeros.\n\nFollowing is the code: \n\n```\npreds = np.ones((BATCH_SIZE,  *IMG_SHAPE, N_CLASSES))       \nfor modelResUnet, modelEffLinknet in zip(modelsResUnet, modelsEffLinknet):\n      preds *= make_preds(modelResUnet, test_imgs, batch_idx)\n      preds *= make_preds(modelEffLinknet, test_imgs, batch_idx)\npreds = np.around(preds)\n```\n\nMy second attempt being to average the predictions of each model's folds and finally computing the intersection of each model's average prediction.\n\n```\npredsResUnet = []\npredsEffLinknet = []\n        \nfor modelResUnet, modelEffLinknet in zip(modelsResUnet, modelsEffLinknet):\n     predsResUnet.append(make_preds(modelResUnet, test_imgs, batch_idx))\n     predsEffLinknet.append(make_preds(modelEffLinknet, test_imgs, batch_idx))\n\npredsResUnet = np.stack(predsResUnet)\npredsResUnet = np.mean(predsResUnet, axis = 0)\n        \npredsEffLinknet = np.stack(predsEffLinknet)\npredsEffLinknet = np.mean(predsEffLinknet, axis = 0)\n        \npreds = predsResUnet * predsEffLinknet\npreds = np.around(preds)`\n```\n\nThe LB score of this second attempt is 0.626.\n\nI also wanted to add a classifier or a regressor on top of this 'stage', however I am not sure what would be a good way of doing it. The idea is to feed as input the predicted masks and as labels the true mask (rle decoded data).\n\nHas someone any thoughts about this matter or would be interested in teaming up to try something similar?\n\nP.-S.: I am aware that some things might be unclear so do not hesitate to ask for clarification.\n\nBy the way, if someone is intersted, using only the folds from the UNet model gave a LB score of 0.616. And for the LinkNet folds a LB score of 0.602, using a code similar to second code snippet",
      "votes": null
    },
    {
      "id": "626758",
      "postDate": "09/14/2019 19:52:41",
      "content": "<p>Oh cool! I too am interested in this topic. All the ensembles I tried are giving me less scores than the individual models. However I didn't do kfold but trained on a 80-20 split, so I have 2 models only.</p>",
      "rawMarkdown": "Oh cool! I too am interested in this topic. All the ensembles I tried are giving me less scores than the individual models. However I didn't do kfold but trained on a 80-20 split, so I have 2 models only.",
      "votes": null
    },
    {
      "id": "626983",
      "postDate": "09/15/2019 08:02:19",
      "content": "<p>Hi <a href=\"/mightyrains\">@mightyrains</a>, interesting, maybe your models are not diverse enough, which would be a reason why it does not improve your score. I remember that with using only one UNet model with the resnet34 backbone gave me a score of 0.602 and as written above with 5 folds 0.616, so maybe if you try using k-folds you will hopefully see an improvement </p>",
      "rawMarkdown": "Hi @mightyrains, interesting, maybe your models are not diverse enough, which would be a reason why it does not improve your score. I remember that with using only one UNet model with the resnet34 backbone gave me a score of 0.602 and as written above with 5 folds 0.616, so maybe if you try using k-folds you will hopefully see an improvement",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 626758,
      "author_name": "mightyrains",
      "author_url": "",
      "post_date": "09/14/2019 19:52:41",
      "content": "<p>Oh cool! I too am interested in this topic. All the ensembles I tried are giving me less scores than the individual models. However I didn't do kfold but trained on a 80-20 split, so I have 2 models only.</p>",
      "votes": null,
      "replies": [
        {
          "id": 626983,
          "author_name": "jonasfreibs",
          "author_url": "",
          "post_date": "09/15/2019 08:02:19",
          "content": "<p>Hi <a href=\"/mightyrains\">@mightyrains</a>, interesting, maybe your models are not diverse enough, which would be a reason why it does not improve your score. I remember that with using only one UNet model with the resnet34 backbone gave me a score of 0.602 and as written above with 5 folds 0.616, so maybe if you try using k-folds you will hopefully see an improvement </p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "626708": "Dear all,\n\nI am looking for a way to merge the predictions of my different models.\n\nThe models are: \n- U-Net with ResNet-34 backbone\n- LinkNet with EfficientNetB3 backbone.\n\nBoth those architectures were trained on 5 folds, resulting in 10 models.\n\nFor now, I have tried to intersect the ten predicted masks, a mask having the shape (batch_size, image_width, image_height, n_chanels = 4). This results in a tensor of zeros.\n\nFollowing is the code: \n\n```\npreds = np.ones((BATCH_SIZE,  *IMG_SHAPE, N_CLASSES))       \nfor modelResUnet, modelEffLinknet in zip(modelsResUnet, modelsEffLinknet):\n      preds *= make_preds(modelResUnet, test_imgs, batch_idx)\n      preds *= make_preds(modelEffLinknet, test_imgs, batch_idx)\npreds = np.around(preds)\n```\n\nMy second attempt being to average the predictions of each model's folds and finally computing the intersection of each model's average prediction.\n\n```\npredsResUnet = []\npredsEffLinknet = []\n        \nfor modelResUnet, modelEffLinknet in zip(modelsResUnet, modelsEffLinknet):\n     predsResUnet.append(make_preds(modelResUnet, test_imgs, batch_idx))\n     predsEffLinknet.append(make_preds(modelEffLinknet, test_imgs, batch_idx))\n\npredsResUnet = np.stack(predsResUnet)\npredsResUnet = np.mean(predsResUnet, axis = 0)\n        \npredsEffLinknet = np.stack(predsEffLinknet)\npredsEffLinknet = np.mean(predsEffLinknet, axis = 0)\n        \npreds = predsResUnet * predsEffLinknet\npreds = np.around(preds)`\n```\n\nThe LB score of this second attempt is 0.626.\n\nI also wanted to add a classifier or a regressor on top of this 'stage', however I am not sure what would be a good way of doing it. The idea is to feed as input the predicted masks and as labels the true mask (rle decoded data).\n\nHas someone any thoughts about this matter or would be interested in teaming up to try something similar?\n\nP.-S.: I am aware that some things might be unclear so do not hesitate to ask for clarification.\n\nBy the way, if someone is intersted, using only the folds from the UNet model gave a LB score of 0.616. And for the LinkNet folds a LB score of 0.602, using a code similar to second code snippet",
    "626758": "Oh cool! I too am interested in this topic. All the ensembles I tried are giving me less scores than the individual models. However I didn't do kfold but trained on a 80-20 split, so I have 2 models only.",
    "626983": "Hi @mightyrains, interesting, maybe your models are not diverse enough, which would be a reason why it does not improve your score. I remember that with using only one UNet model with the resnet34 backbone gave me a score of 0.602 and as written above with 5 folds 0.616, so maybe if you try using k-folds you will hopefully see an improvement"
  },
  "source": "meta"
}