{
  "id": 112290,
  "title": "How to create custom head in pytorch?",
  "url": "/competitions/rsna-intracranial-hemorrhage-detection/discussion/112290",
  "author_name": "Tim Yee",
  "post_date": "2019-10-11T23:42:30.835000",
  "votes": 3,
  "comment_count": 2,
  "views": 0,
  "content": "<p><a href=\"/drhabib\">@drhabib</a> I'm trying to add adaptiveavgpool2d, then flatten, then linear as highlighted here: <a href=\"https://www.kaggle.com/c/aptos2019-blindness-detection/discussion/102812#593578\">https://www.kaggle.com/c/aptos2019-blindness-detection/discussion/102812#593578</a></p>\n\n<p>Currently I am trying something like this. I don't know if I'm on the right track.</p>\n\n<p>&gt; <code>model = EfficientNet.from_pretrained('efficientnet-b0')</code>\n<code>model = nn.Sequential(*list(model.children())[:-1],</code>\n<code>nn.AdaptiveAvgPool2d(1),</code>\n<code>nn.Flatten(),</code>\n<code>nn.Linear(in_features=1280, out_features=6,bias=True))</code></p>\n\n<p>Anyone know how to add a custom head using pytorch?</p>",
  "messages": [
    {
      "id": 647086,
      "postDate": "2019-10-12T03:43:29.843Z",
      "content": "<p>```\nclass Efficient(nn.Module):\n    def <strong>init</strong>(self, num_classes, encoder='efficientnet-b0'):\n        super().<strong>init</strong>()\n        n_channels_dict = {'efficientnet-b0': 1280, 'efficientnet-b1': 1280, 'efficientnet-b2': 1408,\n                           'efficientnet-b3': 1536, 'efficientnet-b4': 1792, 'efficientnet-b5': 2048,\n                           'efficientnet-b6': 2304, 'efficientnet-b7': 2560}\n        self.net = EfficientNet.from_pretrained(encoder)\n        self.avg_pool = nn.AdaptiveAvgPool2d(1)\n        self.classifier = nn.Sequential(\n            nn.Flatten(),\n            nn.Linear(in_features=n_channels_dict[encoder], out_features=6, bias=True)\n        )</p>\n\n<pre><code>def forward(self, x):\n    x = self.net.extract_features(x)\n    x = self.avg_pool(x)\n    out = self.classifier(x)\n\n    return out\n</code></pre>\n\n<p>```\nI think it works well.</p>",
      "rawMarkdown": "```\nclass Efficient(nn.Module):\n    def __init__(self, num_classes, encoder='efficientnet-b0'):\n        super().__init__()\n        n_channels_dict = {'efficientnet-b0': 1280, 'efficientnet-b1': 1280, 'efficientnet-b2': 1408,\n                           'efficientnet-b3': 1536, 'efficientnet-b4': 1792, 'efficientnet-b5': 2048,\n                           'efficientnet-b6': 2304, 'efficientnet-b7': 2560}\n        self.net = EfficientNet.from_pretrained(encoder)\n        self.avg_pool = nn.AdaptiveAvgPool2d(1)\n        self.classifier = nn.Sequential(\n            nn.Flatten(),\n            nn.Linear(in_features=n_channels_dict[encoder], out_features=6, bias=True)\n        )\n\n    def forward(self, x):\n        x = self.net.extract_features(x)\n        x = self.avg_pool(x)\n        out = self.classifier(x)\n\n        return out\n```\nI think it works well.",
      "votes": 7
    },
    {
      "id": 646979,
      "postDate": "2019-10-11T23:42:30.837Z",
      "content": "<p><a href=\"/drhabib\">@drhabib</a> I'm trying to add adaptiveavgpool2d, then flatten, then linear as highlighted here: <a href=\"https://www.kaggle.com/c/aptos2019-blindness-detection/discussion/102812#593578\">https://www.kaggle.com/c/aptos2019-blindness-detection/discussion/102812#593578</a></p>\n\n<p>Currently I am trying something like this. I don't know if I'm on the right track.</p>\n\n<p>&gt; <code>model = EfficientNet.from_pretrained('efficientnet-b0')</code>\n<code>model = nn.Sequential(*list(model.children())[:-1],</code>\n<code>nn.AdaptiveAvgPool2d(1),</code>\n<code>nn.Flatten(),</code>\n<code>nn.Linear(in_features=1280, out_features=6,bias=True))</code></p>\n\n<p>Anyone know how to add a custom head using pytorch?</p>",
      "rawMarkdown": "@drhabib I'm trying to add adaptiveavgpool2d, then flatten, then linear as highlighted here: https://www.kaggle.com/c/aptos2019-blindness-detection/discussion/102812#593578\n\nCurrently I am trying something like this. I don't know if I'm on the right track.\n\n&gt; `model = EfficientNet.from_pretrained('efficientnet-b0')`\n`model = nn.Sequential(*list(model.children())[:-1], `\n`                      nn.AdaptiveAvgPool2d(1), `\n`                      nn.Flatten(), `\n`                      nn.Linear(in_features=1280, out_features=6,bias=True))`\n\n\nAnyone know how to add a custom head using pytorch?",
      "votes": 3
    },
    {
      "id": 647070,
      "postDate": "2019-10-12T03:11:11.247Z",
      "content": "<p>I'm not using exact reference here, but I'm sure you can pass num_classes to a constructing function for EfficientNet. Else, you should look up the source code, find definition of fully connected (usually self.fc or self._fc, and redefine it.</p>",
      "rawMarkdown": "I'm not using exact reference here, but I'm sure you can pass num_classes to a constructing function for EfficientNet. Else, you should look up the source code, find definition of fully connected (usually self.fc or self._fc, and redefine it."
    }
  ],
  "comments": [
    {
      "id": 647086,
      "author_name": "takuoko",
      "author_url": "",
      "post_date": "2019-10-12T03:43:29.843000",
      "content": "<p>```\nclass Efficient(nn.Module):\n    def <strong>init</strong>(self, num_classes, encoder='efficientnet-b0'):\n        super().<strong>init</strong>()\n        n_channels_dict = {'efficientnet-b0': 1280, 'efficientnet-b1': 1280, 'efficientnet-b2': 1408,\n                           'efficientnet-b3': 1536, 'efficientnet-b4': 1792, 'efficientnet-b5': 2048,\n                           'efficientnet-b6': 2304, 'efficientnet-b7': 2560}\n        self.net = EfficientNet.from_pretrained(encoder)\n        self.avg_pool = nn.AdaptiveAvgPool2d(1)\n        self.classifier = nn.Sequential(\n            nn.Flatten(),\n            nn.Linear(in_features=n_channels_dict[encoder], out_features=6, bias=True)\n        )</p>\n\n<pre><code>def forward(self, x):\n    x = self.net.extract_features(x)\n    x = self.avg_pool(x)\n    out = self.classifier(x)\n\n    return out\n</code></pre>\n\n<p>```\nI think it works well.</p>",
      "votes": 7,
      "replies": []
    },
    {
      "id": 647070,
      "author_name": "Nicholas Lyu",
      "author_url": "",
      "post_date": "2019-10-12T03:11:11.247000",
      "content": "<p>I'm not using exact reference here, but I'm sure you can pass num_classes to a constructing function for EfficientNet. Else, you should look up the source code, find definition of fully connected (usually self.fc or self._fc, and redefine it.</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "647086": "```\nclass Efficient(nn.Module):\n    def __init__(self, num_classes, encoder='efficientnet-b0'):\n        super().__init__()\n        n_channels_dict = {'efficientnet-b0': 1280, 'efficientnet-b1': 1280, 'efficientnet-b2': 1408,\n                           'efficientnet-b3': 1536, 'efficientnet-b4': 1792, 'efficientnet-b5': 2048,\n                           'efficientnet-b6': 2304, 'efficientnet-b7': 2560}\n        self.net = EfficientNet.from_pretrained(encoder)\n        self.avg_pool = nn.AdaptiveAvgPool2d(1)\n        self.classifier = nn.Sequential(\n            nn.Flatten(),\n            nn.Linear(in_features=n_channels_dict[encoder], out_features=6, bias=True)\n        )\n\n    def forward(self, x):\n        x = self.net.extract_features(x)\n        x = self.avg_pool(x)\n        out = self.classifier(x)\n\n        return out\n```\nI think it works well.",
    "646979": "@drhabib I'm trying to add adaptiveavgpool2d, then flatten, then linear as highlighted here: https://www.kaggle.com/c/aptos2019-blindness-detection/discussion/102812#593578\n\nCurrently I am trying something like this. I don't know if I'm on the right track.\n\n&gt; `model = EfficientNet.from_pretrained('efficientnet-b0')`\n`model = nn.Sequential(*list(model.children())[:-1], `\n`                      nn.AdaptiveAvgPool2d(1), `\n`                      nn.Flatten(), `\n`                      nn.Linear(in_features=1280, out_features=6,bias=True))`\n\n\nAnyone know how to add a custom head using pytorch?",
    "647070": "I'm not using exact reference here, but I'm sure you can pass num_classes to a constructing function for EfficientNet. Else, you should look up the source code, find definition of fully connected (usually self.fc or self._fc, and redefine it."
  }
}