{
  "id": 104981,
  "title": "Ensembling Tips",
  "url": "/competitions/aptos2019-blindness-detection/discussion/104981",
  "author_name": "Bibek",
  "post_date": "2019-08-20T12:50:43.049000",
  "votes": 41,
  "comment_count": 10,
  "views": 0,
  "content": "<p>I believe we r in a stage where everybody has started ensembling their models. Here are some ways of ensembling that may help u achieve a better score.</p>\n\n<h3>1. Model Ensemble</h3>\n\n<p>Let's say u have two models: one for <code>classification</code> and another for <code>regression</code>\nThe simplest way to ensemble them would be to average their predictions</p>\n\n<p><code>python\nmodel_preds = []\nfor model in [regression_model, classification_model]:\n         pred = model.predict(test_image)\n         model_preds.append(pred)\nfinal_pred = take_average(model_preds)\n</code></p>\n\n<h3>2. Augmentation Ensemble(TTA: Test Time Augmentation)</h3>\n\n<p>Another popular way would be combine predictions of different version(augmentations) of the same image; popularly known as <code>TTA</code> \n<code>python\nmodel = regression or classification \naug_preds = []\nfor augmentations in [augmentations_1, augmentations_2]:\n         augment_image = do_augment(test_image, augmentations)\n         pred = model.predict(augment_image)\n         aug_preds.append(pred)\nfinal_pred = take_average(aug_preds)\n</code></p>\n\n<h3>3. Scale(Image Size) Ensemble</h3>\n\n<p>This is something that I recently learnt: make predictions on different scales of the same image\n<code>python\nmodel = regression or classification \nscale_preds = []\nfor img_size in [img_size_1, img_size_2]:\n         resized_image = do_scale(test_image, img_size)\n         pred = model.predict(resized_image)\n         scale_preds.append(pred)\nfinal_pred = take_average(scale_preds)\n</code></p>\n\n<h3>4. Be Creative(Combine everything?)</h3>\n\n<p>You can be creative with ur approach of ensembling by combing different ensembling methods.\n<code>python\ncombine_preds = []\nfor model in [model_1, model_2]:\n   for augmentation in [augmentations_1, augmentations_2]:\n       for img_size in [img_size_1, img_size_2]:\n            sc_aug_image = scale_n_augment(test_image, img_size, augmentation)\n             pred = model.predict(sc_aug_image)\n             combine_preds.append(pred)\nfinal_pred = take_average(combine_preds)\n</code>\nThese are some of the ensembling approaches that I can think of. Please share if u have some other interesting ways to ensemble.</p>",
  "messages": [
    {
      "id": 603561,
      "postDate": "2019-08-20T12:50:43.050Z",
      "content": "<p>I believe we r in a stage where everybody has started ensembling their models. Here are some ways of ensembling that may help u achieve a better score.</p>\n\n<h3>1. Model Ensemble</h3>\n\n<p>Let's say u have two models: one for <code>classification</code> and another for <code>regression</code>\nThe simplest way to ensemble them would be to average their predictions</p>\n\n<p><code>python\nmodel_preds = []\nfor model in [regression_model, classification_model]:\n         pred = model.predict(test_image)\n         model_preds.append(pred)\nfinal_pred = take_average(model_preds)\n</code></p>\n\n<h3>2. Augmentation Ensemble(TTA: Test Time Augmentation)</h3>\n\n<p>Another popular way would be combine predictions of different version(augmentations) of the same image; popularly known as <code>TTA</code> \n<code>python\nmodel = regression or classification \naug_preds = []\nfor augmentations in [augmentations_1, augmentations_2]:\n         augment_image = do_augment(test_image, augmentations)\n         pred = model.predict(augment_image)\n         aug_preds.append(pred)\nfinal_pred = take_average(aug_preds)\n</code></p>\n\n<h3>3. Scale(Image Size) Ensemble</h3>\n\n<p>This is something that I recently learnt: make predictions on different scales of the same image\n<code>python\nmodel = regression or classification \nscale_preds = []\nfor img_size in [img_size_1, img_size_2]:\n         resized_image = do_scale(test_image, img_size)\n         pred = model.predict(resized_image)\n         scale_preds.append(pred)\nfinal_pred = take_average(scale_preds)\n</code></p>\n\n<h3>4. Be Creative(Combine everything?)</h3>\n\n<p>You can be creative with ur approach of ensembling by combing different ensembling methods.\n<code>python\ncombine_preds = []\nfor model in [model_1, model_2]:\n   for augmentation in [augmentations_1, augmentations_2]:\n       for img_size in [img_size_1, img_size_2]:\n            sc_aug_image = scale_n_augment(test_image, img_size, augmentation)\n             pred = model.predict(sc_aug_image)\n             combine_preds.append(pred)\nfinal_pred = take_average(combine_preds)\n</code>\nThese are some of the ensembling approaches that I can think of. Please share if u have some other interesting ways to ensemble.</p>",
      "rawMarkdown": "I believe we r in a stage where everybody has started ensembling their models. Here are some ways of ensembling that may help u achieve a better score.\n### 1. Model Ensemble\nLet's say u have two models: one for `classification` and another for `regression`\nThe simplest way to ensemble them would be to average their predictions\n \n```python\nmodel_preds = []\nfor model in [regression_model, classification_model]:\n         pred = model.predict(test_image)\n         model_preds.append(pred)\nfinal_pred = take_average(model_preds)\n```\n### 2. Augmentation Ensemble(TTA: Test Time Augmentation)\nAnother popular way would be combine predictions of different version(augmentations) of the same image; popularly known as `TTA` \n```python\nmodel = regression or classification \naug_preds = []\nfor augmentations in [augmentations_1, augmentations_2]:\n         augment_image = do_augment(test_image, augmentations)\n         pred = model.predict(augment_image)\n         aug_preds.append(pred)\nfinal_pred = take_average(aug_preds)\n```\n### 3. Scale(Image Size) Ensemble\nThis is something that I recently learnt: make predictions on different scales of the same image\n```python\nmodel = regression or classification \nscale_preds = []\nfor img_size in [img_size_1, img_size_2]:\n         resized_image = do_scale(test_image, img_size)\n         pred = model.predict(resized_image)\n         scale_preds.append(pred)\nfinal_pred = take_average(scale_preds)\n```\n### 4. Be Creative(Combine everything?)\nYou can be creative with ur approach of ensembling by combing different ensembling methods.\n```python\ncombine_preds = []\nfor model in [model_1, model_2]:\n   for augmentation in [augmentations_1, augmentations_2]:\n       for img_size in [img_size_1, img_size_2]:\n            sc_aug_image = scale_n_augment(test_image, img_size, augmentation)\n             pred = model.predict(sc_aug_image)\n             combine_preds.append(pred)\nfinal_pred = take_average(combine_preds)\n```\nThese are some of the ensembling approaches that I can think of. Please share if u have some other interesting ways to ensemble.\n\n ",
      "votes": 40
    },
    {
      "id": 605737,
      "postDate": "2019-08-22T17:10:52.070Z",
      "content": "<p>One thing I don't understand with ensembling is if you are doing a classification task and you are using model ensembling , if one model predicts a image belongs to class 3 and another as class 4 if we take average of their predictions won't it be wrong??? can anyone provide more insight on this. </p>",
      "rawMarkdown": "One thing I don't understand with ensembling is if you are doing a classification task and you are using model ensembling , if one model predicts a image belongs to class 3 and another as class 4 if we take average of their predictions won't it be wrong??? can anyone provide more insight on this. ",
      "replies": [
        {
          "id": 605844,
          "postDate": "2019-08-22T20:08:05.063Z",
          "content": "<p>Then take an odd number, and if all the predictions are unique you can take the mean or the prediction of your best single model.</p>",
          "rawMarkdown": "Then take an odd number, and if all the predictions are unique you can take the mean or the prediction of your best single model."
        },
        {
          "id": 615644,
          "postDate": "2019-09-02T07:49:00.190Z",
          "content": "<p><a href=\"/seif95\">@seif95</a> say I have 2 regression models I want to ensemble.. I don't understand how averaging would work here. Say one model predicts 1 and the other 4. Ensembling gives 2.5? Even if I average the numbers before thresholding, 1.2 versus 3.8 gives 2.5? How would that work?</p>",
          "rawMarkdown": "@seif95 say I have 2 regression models I want to ensemble.. I don't understand how averaging would work here. Say one model predicts 1 and the other 4. Ensembling gives 2.5? Even if I average the numbers before thresholding, 1.2 versus 3.8 gives 2.5? How would that work?"
        }
      ]
    },
    {
      "id": 603803,
      "postDate": "2019-08-20T17:18:21.050Z",
      "content": "<p>Thanks for sharing..!! Very helpful...!!!</p>",
      "rawMarkdown": "Thanks for sharing..!! Very helpful...!!!"
    },
    {
      "id": 603626,
      "postDate": "2019-08-20T13:59:58.543Z",
      "content": "<p>Thanks ! Will other mechanisms like majority_vote/mode of models work other than average? What is the preferred one usually?</p>",
      "rawMarkdown": "Thanks ! Will other mechanisms like majority_vote/mode of models work other than average? What is the preferred one usually?"
    },
    {
      "id": 621675,
      "postDate": "2019-09-08T20:28:48.460Z",
      "rawMarkdown": "",
      "isDeleted": true
    },
    {
      "id": 604344,
      "postDate": "2019-08-21T09:34:43.533Z",
      "rawMarkdown": "",
      "isDeleted": true
    },
    {
      "id": 603562,
      "postDate": "2019-08-20T12:51:48.507Z",
      "content": "<p>thanks a  lot very informative</p>",
      "rawMarkdown": "thanks a  lot very informative",
      "votes": 1
    },
    {
      "id": 603591,
      "postDate": "2019-08-20T13:25:35.487Z",
      "content": "<p>thanks <a href=\"/bibek777\">@bibek777</a> \nhelps lot to me..</p>",
      "rawMarkdown": "thanks @bibek777 \nhelps lot to me.."
    },
    {
      "id": 606331,
      "postDate": "2019-08-23T12:54:00.683Z",
      "content": "<p>Thanks a lot!</p>",
      "rawMarkdown": "Thanks a lot!",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 605737,
      "author_name": "Shivu",
      "author_url": "",
      "post_date": "2019-08-22T17:10:52.070000",
      "content": "<p>One thing I don't understand with ensembling is if you are doing a classification task and you are using model ensembling , if one model predicts a image belongs to class 3 and another as class 4 if we take average of their predictions won't it be wrong??? can anyone provide more insight on this. </p>",
      "votes": 0,
      "replies": [
        {
          "id": 605844,
          "author_name": "Seifeddine Fezzani",
          "author_url": "",
          "post_date": "2019-08-22T20:08:05.063000",
          "content": "<p>Then take an odd number, and if all the predictions are unique you can take the mean or the prediction of your best single model.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 615644,
          "author_name": "Yousef Rabi",
          "author_url": "",
          "post_date": "2019-09-02T07:49:00.190000",
          "content": "<p><a href=\"/seif95\">@seif95</a> say I have 2 regression models I want to ensemble.. I don't understand how averaging would work here. Say one model predicts 1 and the other 4. Ensembling gives 2.5? Even if I average the numbers before thresholding, 1.2 versus 3.8 gives 2.5? How would that work?</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 603803,
      "author_name": "Saikat Biswas",
      "author_url": "",
      "post_date": "2019-08-20T17:18:21.050000",
      "content": "<p>Thanks for sharing..!! Very helpful...!!!</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 603626,
      "author_name": "Kaushik Ramachandran",
      "author_url": "",
      "post_date": "2019-08-20T13:59:58.543000",
      "content": "<p>Thanks ! Will other mechanisms like majority_vote/mode of models work other than average? What is the preferred one usually?</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 621675,
      "author_name": "",
      "author_url": "",
      "post_date": "2019-09-08T20:28:48.460000",
      "content": "",
      "votes": 0,
      "replies": []
    },
    {
      "id": 604344,
      "author_name": "",
      "author_url": "",
      "post_date": "2019-08-21T09:34:43.533000",
      "content": "",
      "votes": 0,
      "replies": []
    },
    {
      "id": 603562,
      "author_name": "leixiang@AInnovation",
      "author_url": "",
      "post_date": "2019-08-20T12:51:48.507000",
      "content": "<p>thanks a  lot very informative</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 603591,
      "author_name": "Nitesh Yadav",
      "author_url": "",
      "post_date": "2019-08-20T13:25:35.487000",
      "content": "<p>thanks <a href=\"/bibek777\">@bibek777</a> \nhelps lot to me..</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 606331,
      "author_name": "",
      "author_url": "",
      "post_date": "2019-08-23T12:54:00.683000",
      "content": "<p>Thanks a lot!</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "603561": "I believe we r in a stage where everybody has started ensembling their models. Here are some ways of ensembling that may help u achieve a better score.\n### 1. Model Ensemble\nLet's say u have two models: one for `classification` and another for `regression`\nThe simplest way to ensemble them would be to average their predictions\n \n```python\nmodel_preds = []\nfor model in [regression_model, classification_model]:\n         pred = model.predict(test_image)\n         model_preds.append(pred)\nfinal_pred = take_average(model_preds)\n```\n### 2. Augmentation Ensemble(TTA: Test Time Augmentation)\nAnother popular way would be combine predictions of different version(augmentations) of the same image; popularly known as `TTA` \n```python\nmodel = regression or classification \naug_preds = []\nfor augmentations in [augmentations_1, augmentations_2]:\n         augment_image = do_augment(test_image, augmentations)\n         pred = model.predict(augment_image)\n         aug_preds.append(pred)\nfinal_pred = take_average(aug_preds)\n```\n### 3. Scale(Image Size) Ensemble\nThis is something that I recently learnt: make predictions on different scales of the same image\n```python\nmodel = regression or classification \nscale_preds = []\nfor img_size in [img_size_1, img_size_2]:\n         resized_image = do_scale(test_image, img_size)\n         pred = model.predict(resized_image)\n         scale_preds.append(pred)\nfinal_pred = take_average(scale_preds)\n```\n### 4. Be Creative(Combine everything?)\nYou can be creative with ur approach of ensembling by combing different ensembling methods.\n```python\ncombine_preds = []\nfor model in [model_1, model_2]:\n   for augmentation in [augmentations_1, augmentations_2]:\n       for img_size in [img_size_1, img_size_2]:\n            sc_aug_image = scale_n_augment(test_image, img_size, augmentation)\n             pred = model.predict(sc_aug_image)\n             combine_preds.append(pred)\nfinal_pred = take_average(combine_preds)\n```\nThese are some of the ensembling approaches that I can think of. Please share if u have some other interesting ways to ensemble.\n\n ",
    "605737": "One thing I don't understand with ensembling is if you are doing a classification task and you are using model ensembling , if one model predicts a image belongs to class 3 and another as class 4 if we take average of their predictions won't it be wrong??? can anyone provide more insight on this. ",
    "603803": "Thanks for sharing..!! Very helpful...!!!",
    "603626": "Thanks ! Will other mechanisms like majority_vote/mode of models work other than average? What is the preferred one usually?",
    "621675": "",
    "604344": "",
    "603562": "thanks a  lot very informative",
    "603591": "thanks @bibek777 \nhelps lot to me..",
    "606331": "Thanks a lot!"
  }
}