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 remove the last layer? #227

Closed
Zacchaeus00 opened this issue Aug 28, 2020 · 7 comments
Closed

How to remove the last layer? #227

Zacchaeus00 opened this issue Aug 28, 2020 · 7 comments

Comments

@Zacchaeus00
Copy link

Hi,
Great repo!
I'm doing an image retrieval task. I've already trained the model on my dataset and would like to use the backbone for feature extraction. In other words, I would like the output to be a feature vector. Should I simply remove the last _fc layer?
Thank you in advance!
Best,
Zach

@lukemelas
Copy link
Owner

Like this example from the README?

model = EfficientNet.from_pretrained('efficientnet-b0')
features = model.extract_features(img)

@Zacchaeus00
Copy link
Author

@lukemelas Thank you!

@Zacchaeus00
Copy link
Author

@lukemelas Hi Lukemelas, I'm still a little confused. For b0, I expect a feature vector with torch.Size([1, 1280]), however I got torch.Size([1, 1280, 7, 7]). For ResNet I hook output of the last average pooling layer. How can I implement this in efficientnet?

@lukemelas
Copy link
Owner

Does this work?

features = model.extract_features(img)
pooled_features = torch.nn.functional.adaptive_avg_pool2d(features, 1)

@Zacchaeus00
Copy link
Author

@lukemelas Got it! curious if setting model._fc = torch.nn.Identity() would work?

@lukemelas
Copy link
Owner

Yup, that should work too!

@Zacchaeus00
Copy link
Author

@lukemelas Now I'm clear. Thank you!

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

2 participants