EfficientNet-PyTorch icon indicating copy to clipboard operation
EfficientNet-PyTorch copied to clipboard

How to get intermediate output not only the last features

Open liutianling opened this issue 4 years ago • 2 comments

#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 ...

liutianling avatar Nov 19 '20 09:11 liutianling

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

Emmunaf avatar Dec 14 '20 15:12 Emmunaf

Use EfficientNet.extract_endpoints(). It returns the intermediate values of the neural network before each reduction in image size (224 × 224 → 112 × 112, etc.).

veeara282 avatar Dec 24 '20 07:12 veeara282