Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get intermediate output not only the last features #248

Open
liutianling opened this issue Nov 19, 2020 · 2 comments
Open

How to get intermediate output not only the last features #248

liutianling opened this issue Nov 19, 2020 · 2 comments

Comments

@liutianling
Copy link

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

@Emmunaf
Copy link

Emmunaf commented Dec 14, 2020

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

@veeara282
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants