EfficientNet-PyTorch
EfficientNet-PyTorch copied to clipboard
How to get intermediate output not only the last features
#coding:utf8 import torch from efficientnet_pytorch import EfficientNet import torch.nn as nn
class myModel(): def init(self): base_model = EfficientNet.from_pretrained(model_name="efficientnet-b5", weights_path="efficientnet-b5-b6417697.pth") #self.model = nn.Sequential(*list(base_model.children())[:2], *list(base_model.children())[2][:38]) self.model = nn.Sequential(*list(base_model.children())[:6]) self.model.eval() #print(self.model)
def forword(self, x):
x = self.model.forward(x)
print(x.shape)
if name=="main": model = myModel() x = torch.randn(1,3,32,32) model.forword(x)
I get the error: TypeError: forward() takes 1 positional argument but 2 were given
when I set the self.model = nn.Sequential(*list(base_model.children())[:2], *list(base_model.children())[2][:38]) It works ! Why and how to fix it... Thank you very much ...
If you need to prune just the last layers to use it as features extractor this seems a duplicate of this issue that was solved with this PR by nwschurink
If you need a more customised model look at:
- This SO answer of mine for a short case explanation
- This (basically explains how you can override some layers with identity and so how to "delete" some layers)
- This if you want to extend the model
Use EfficientNet.extract_endpoints()
. It returns the intermediate values of the neural network before each reduction in image size (224 × 224 → 112 × 112, etc.).