{
  "id": 346952,
  "title": "Notebook Threw Exception when using SegFormer for inference",
  "url": "/competitions/hubmap-organ-segmentation/discussion/346952",
  "author_name": "",
  "post_date": "2022-08-22T08:02:01.454263900Z",
  "votes": 12,
  "comment_count": 5,
  "views": 0,
  "content": "<p>I was experimenting with SegFormer recently and got a decent score. It would probably score 0.8 on lb but I couldn't figure out why my notebook is throwing exception during the inference. It works perfectly fine while committing.</p>\n<pre><code>class SegFormerModel(nn.Module):\n\n    def __init__(self, model_class, model_args, upsample_args):\n\n        super(SegFormerModel, self).__init__()\n\n        self.model = getattr(transformers, model_class).from_pretrained(**model_args)\n        self.upsample = nn.Upsample(\n            size=upsample_args['upsample_size'],\n            mode=upsample_args['upsample_mode'],\n            align_corners=upsample_args['upsample_align_corners']\n        )\n\n    def forward(self, x):\n\n        return self.upsample(self.model(x)[0])\n</code></pre>\n<p>This is the model wrapper I use. Anyone else encountered similar problems while using SegFormer?</p>",
  "messages": [
    {
      "id": "1909008",
      "postDate": "08/22/2022 08:02:01",
      "content": "<p>I was experimenting with SegFormer recently and got a decent score. It would probably score 0.8 on lb but I couldn't figure out why my notebook is throwing exception during the inference. It works perfectly fine while committing.</p>\n<pre><code>class SegFormerModel(nn.Module):\n\n    def __init__(self, model_class, model_args, upsample_args):\n\n        super(SegFormerModel, self).__init__()\n\n        self.model = getattr(transformers, model_class).from_pretrained(**model_args)\n        self.upsample = nn.Upsample(\n            size=upsample_args['upsample_size'],\n            mode=upsample_args['upsample_mode'],\n            align_corners=upsample_args['upsample_align_corners']\n        )\n\n    def forward(self, x):\n\n        return self.upsample(self.model(x)[0])\n</code></pre>\n<p>This is the model wrapper I use. Anyone else encountered similar problems while using SegFormer?</p>",
      "rawMarkdown": "I was experimenting with SegFormer recently and got a decent score. It would probably score 0.8 on lb but I couldn't figure out why my notebook is throwing exception during the inference. It works perfectly fine while committing.\n\n```\nclass SegFormerModel(nn.Module):\n\n    def __init__(self, model_class, model_args, upsample_args):\n\n        super(SegFormerModel, self).__init__()\n\n        self.model = getattr(transformers, model_class).from_pretrained(**model_args)\n        self.upsample = nn.Upsample(\n            size=upsample_args['upsample_size'],\n            mode=upsample_args['upsample_mode'],\n            align_corners=upsample_args['upsample_align_corners']\n        )\n\n    def forward(self, x):\n\n        return self.upsample(self.model(x)[0])\n```\n\nThis is the model wrapper I use. Anyone else encountered similar problems while using SegFormer?",
      "votes": null
    },
    {
      "id": "1909024",
      "postDate": "08/22/2022 08:23:23",
      "content": "<p>\"The HuBMAP images range in size from 4500x4500 down to 160x160 pixels\"</p>\n<p>try all sizes in the range above in your local debugging (e.g. choose one image for each organ and create various size for each).<br>\nthis also helps you to visualize your segmentation results and its sensitivity to size</p>",
      "rawMarkdown": "\"The HuBMAP images range in size from 4500x4500 down to 160x160 pixels\"\n\ntry all sizes in the range above in your local debugging (e.g. choose one image for each organ and create various size for each).\nthis also helps you to visualize your segmentation results and its sensitivity to size",
      "votes": null
    },
    {
      "id": "1909050",
      "postDate": "08/22/2022 09:05:44",
      "content": "<p>Images are resized to 768 and I apply 3 ttas. I concatenate them as a single batch of 4 and pass them to segformer mit2. I don't think my problem is related to input size.</p>",
      "rawMarkdown": "Images are resized to 768 and I apply 3 ttas. I concatenate them as a single batch of 4 and pass them to segformer mit2. I don't think my problem is related to input size.",
      "votes": null
    },
    {
      "id": "1910062",
      "postDate": "08/23/2022 06:33:33",
      "content": "<p>i try many times, but only get 0.78(Hubmap0.55+hpa0.22) LB score.</p>",
      "rawMarkdown": "i try many times, but only get 0.78(Hubmap0.55+hpa0.22) LB score.",
      "votes": null
    },
    {
      "id": "1910493",
      "postDate": "08/23/2022 13:23:17",
      "content": "<p>I had the same issue as you  with my models ensemble on constant images size. In my case the problem was due to cuda out of memory error. You can check if you have the same when you run your inference code on several duplicated images from public test dataset, not on one image only.</p>",
      "rawMarkdown": "I had the same issue as you  with my models ensemble on constant images size. In my case the problem was due to cuda out of memory error. You can check if you have the same when you run your inference code on several duplicated images from public test dataset, not on one image only.",
      "votes": null
    },
    {
      "id": "1927752",
      "postDate": "09/06/2022 00:02:33",
      "content": "<p>This solved my problem! My postprocessing threw errors when the image size was as small as 160x160.</p>",
      "rawMarkdown": "This solved my problem! My postprocessing threw errors when the image size was as small as 160x160.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1909024,
      "author_name": "hengck23",
      "author_url": "",
      "post_date": "08/22/2022 08:23:23",
      "content": "<p>\"The HuBMAP images range in size from 4500x4500 down to 160x160 pixels\"</p>\n<p>try all sizes in the range above in your local debugging (e.g. choose one image for each organ and create various size for each).<br>\nthis also helps you to visualize your segmentation results and its sensitivity to size</p>",
      "votes": null,
      "replies": [
        {
          "id": 1909050,
          "author_name": "gunesevitan",
          "author_url": "",
          "post_date": "08/22/2022 09:05:44",
          "content": "<p>Images are resized to 768 and I apply 3 ttas. I concatenate them as a single batch of 4 and pass them to segformer mit2. I don't think my problem is related to input size.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 1927752,
          "author_name": "shionhonda",
          "author_url": "",
          "post_date": "09/06/2022 00:02:33",
          "content": "<p>This solved my problem! My postprocessing threw errors when the image size was as small as 160x160.</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 1910062,
      "author_name": "pkyangno1",
      "author_url": "",
      "post_date": "08/23/2022 06:33:33",
      "content": "<p>i try many times, but only get 0.78(Hubmap0.55+hpa0.22) LB score.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1910493,
      "author_name": "aksell7",
      "author_url": "",
      "post_date": "08/23/2022 13:23:17",
      "content": "<p>I had the same issue as you  with my models ensemble on constant images size. In my case the problem was due to cuda out of memory error. You can check if you have the same when you run your inference code on several duplicated images from public test dataset, not on one image only.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1909008": "I was experimenting with SegFormer recently and got a decent score. It would probably score 0.8 on lb but I couldn't figure out why my notebook is throwing exception during the inference. It works perfectly fine while committing.\n\n```\nclass SegFormerModel(nn.Module):\n\n    def __init__(self, model_class, model_args, upsample_args):\n\n        super(SegFormerModel, self).__init__()\n\n        self.model = getattr(transformers, model_class).from_pretrained(**model_args)\n        self.upsample = nn.Upsample(\n            size=upsample_args['upsample_size'],\n            mode=upsample_args['upsample_mode'],\n            align_corners=upsample_args['upsample_align_corners']\n        )\n\n    def forward(self, x):\n\n        return self.upsample(self.model(x)[0])\n```\n\nThis is the model wrapper I use. Anyone else encountered similar problems while using SegFormer?",
    "1909024": "\"The HuBMAP images range in size from 4500x4500 down to 160x160 pixels\"\n\ntry all sizes in the range above in your local debugging (e.g. choose one image for each organ and create various size for each).\nthis also helps you to visualize your segmentation results and its sensitivity to size",
    "1909050": "Images are resized to 768 and I apply 3 ttas. I concatenate them as a single batch of 4 and pass them to segformer mit2. I don't think my problem is related to input size.",
    "1910062": "i try many times, but only get 0.78(Hubmap0.55+hpa0.22) LB score.",
    "1910493": "I had the same issue as you  with my models ensemble on constant images size. In my case the problem was due to cuda out of memory error. You can check if you have the same when you run your inference code on several duplicated images from public test dataset, not on one image only.",
    "1927752": "This solved my problem! My postprocessing threw errors when the image size was as small as 160x160."
  },
  "source": "meta"
}