{
  "id": 344064,
  "title": "model.train() vs. model.eval()",
  "url": "/competitions/hubmap-organ-segmentation/discussion/344064",
  "author_name": "",
  "post_date": "2022-08-13T19:46:05.528263300Z",
  "votes": 2,
  "comment_count": 4,
  "views": 0,
  "content": "<p>Hello everyone, I just stumbled upon something unexpected: setting <code>model.train()</code> instead of <code>model.eval()</code> results in a higher leaderboard score:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3778743%2Fd9f332e4ac2a74310f165487df7203c1%2FBildschirmfoto%202022-08-13%20um%2021.16.12.png?generation=1660419572592580&amp;alt=media\" alt=\"\"></p>\n<p>The only difference in these notebooks is the line <code>model.train()</code> vs. <code>model.eval()</code> right before inference. Both notebooks use a UNet with efficientnet encoder from the  <a href=\"https://github.com/qubvel/segmentation_models.pytorch\" target=\"_blank\">segmentation-models-pytorch</a> library, the same weights and a (inference) batch size of 1. </p>\n<p>Is this common behaviour?</p>\n<p>Maybe it's worth trying out to get some improvements :)</p>",
  "messages": [
    {
      "id": "1897502",
      "postDate": "08/13/2022 19:46:05",
      "content": "<p>Hello everyone, I just stumbled upon something unexpected: setting <code>model.train()</code> instead of <code>model.eval()</code> results in a higher leaderboard score:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3778743%2Fd9f332e4ac2a74310f165487df7203c1%2FBildschirmfoto%202022-08-13%20um%2021.16.12.png?generation=1660419572592580&amp;alt=media\" alt=\"\"></p>\n<p>The only difference in these notebooks is the line <code>model.train()</code> vs. <code>model.eval()</code> right before inference. Both notebooks use a UNet with efficientnet encoder from the  <a href=\"https://github.com/qubvel/segmentation_models.pytorch\" target=\"_blank\">segmentation-models-pytorch</a> library, the same weights and a (inference) batch size of 1. </p>\n<p>Is this common behaviour?</p>\n<p>Maybe it's worth trying out to get some improvements :)</p>",
      "rawMarkdown": "Hello everyone, I just stumbled upon something unexpected: setting `model.train()` instead of `model.eval()` results in a higher leaderboard score:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3778743%2Fd9f332e4ac2a74310f165487df7203c1%2FBildschirmfoto%202022-08-13%20um%2021.16.12.png?generation=1660419572592580&alt=media)\n\nThe only difference in these notebooks is the line `model.train()` vs. `model.eval()` right before inference. Both notebooks use a UNet with efficientnet encoder from the  [segmentation-models-pytorch](https://github.com/qubvel/segmentation_models.pytorch) library, the same weights and a (inference) batch size of 1. \n\nIs this common behaviour?\n\nMaybe it's worth trying out to get some improvements :)",
      "votes": null
    },
    {
      "id": "1897943",
      "postDate": "08/14/2022 06:57:05",
      "content": "<p>it's werid..  model.eval() turns off batch norm or dropout when applied during the training phase. model.eval() is correct in the case of inference.</p>",
      "rawMarkdown": "it's werid..  model.eval() turns off batch norm or dropout when applied during the training phase. model.eval() is correct in the case of inference.",
      "votes": null
    },
    {
      "id": "1898110",
      "postDate": "08/14/2022 10:00:07",
      "content": "<p>Didn't work for me though 🤒</p>",
      "rawMarkdown": "Didn't work for me though 🤒",
      "votes": null
    },
    {
      "id": "1900192",
      "postDate": "08/15/2022 19:06:17",
      "content": "<p>Thats so interesting! I think perhaps similar to <a href=\"https://www.kaggle.com/deepkim\" target=\"_blank\">@deepkim</a> said, using model.train() uses batch norm or dropout, so perhaps the model learned how to use those to its advantage somehow? </p>",
      "rawMarkdown": "Thats so interesting! I think perhaps similar to @deepkim said, using model.train() uses batch norm or dropout, so perhaps the model learned how to use those to its advantage somehow?",
      "votes": null
    },
    {
      "id": "1901713",
      "postDate": "08/16/2022 20:38:05",
      "content": "<p>This isn't expected behavior. I could imagine if your model was overfit it might actually benefit from having dropout enabled at inference time, definitely hard to say though</p>",
      "rawMarkdown": "This isn't expected behavior. I could imagine if your model was overfit it might actually benefit from having dropout enabled at inference time, definitely hard to say though",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1897943,
      "author_name": "deepkim",
      "author_url": "",
      "post_date": "08/14/2022 06:57:05",
      "content": "<p>it's werid..  model.eval() turns off batch norm or dropout when applied during the training phase. model.eval() is correct in the case of inference.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1898110,
      "author_name": "kfk42kfk",
      "author_url": "",
      "post_date": "08/14/2022 10:00:07",
      "content": "<p>Didn't work for me though 🤒</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1900192,
      "author_name": "aadityaagnihotri",
      "author_url": "",
      "post_date": "08/15/2022 19:06:17",
      "content": "<p>Thats so interesting! I think perhaps similar to <a href=\"https://www.kaggle.com/deepkim\" target=\"_blank\">@deepkim</a> said, using model.train() uses batch norm or dropout, so perhaps the model learned how to use those to its advantage somehow? </p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1901713,
      "author_name": "maxvandijck",
      "author_url": "",
      "post_date": "08/16/2022 20:38:05",
      "content": "<p>This isn't expected behavior. I could imagine if your model was overfit it might actually benefit from having dropout enabled at inference time, definitely hard to say though</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1897502": "Hello everyone, I just stumbled upon something unexpected: setting `model.train()` instead of `model.eval()` results in a higher leaderboard score:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F3778743%2Fd9f332e4ac2a74310f165487df7203c1%2FBildschirmfoto%202022-08-13%20um%2021.16.12.png?generation=1660419572592580&alt=media)\n\nThe only difference in these notebooks is the line `model.train()` vs. `model.eval()` right before inference. Both notebooks use a UNet with efficientnet encoder from the  [segmentation-models-pytorch](https://github.com/qubvel/segmentation_models.pytorch) library, the same weights and a (inference) batch size of 1. \n\nIs this common behaviour?\n\nMaybe it's worth trying out to get some improvements :)",
    "1897943": "it's werid..  model.eval() turns off batch norm or dropout when applied during the training phase. model.eval() is correct in the case of inference.",
    "1898110": "Didn't work for me though 🤒",
    "1900192": "Thats so interesting! I think perhaps similar to @deepkim said, using model.train() uses batch norm or dropout, so perhaps the model learned how to use those to its advantage somehow?",
    "1901713": "This isn't expected behavior. I could imagine if your model was overfit it might actually benefit from having dropout enabled at inference time, definitely hard to say though"
  },
  "source": "meta"
}