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 add additional layer( inside the features module) in pre-trained EfficientNetB0 model? #343

Open
sashika-himali opened this issue Aug 6, 2023 · 2 comments

Comments

@sashika-himali
Copy link

Could you please guide me on how to add a multi-head attention layer after model. features[5:] . I want to add this layer inside the features .
import torch
import torchvision
weights = torchvision. models.EfficientNet_B0_Weights.DEFAULT
model = torchvision.models.efficientnet_b0(weights=weights).to(device)

@sashika-himali sashika-himali changed the title How to add additional layer in pre-trained EfficientNetB0 model? How to add additional layer( inside the features module) in pre-trained EfficientNetB0 model? Aug 6, 2023
@cthugha10
Copy link

cthugha10 commented Dec 10, 2023

Try just create another nn.mudule which includes the pre-trained model and your layer?

@adhilcodes
Copy link

class EfficientNet_bo(nn.Module):
    def __init__(self, num_classes = 2):
        super().__init__()
        self.model = EfficientNet.from_pretrained('efficientnet-b0')

        for param in self.model.parameters():
            param.requires_grad = False

        # Extract number of features from last layer
        in_features = self.model._fc.in_features

        self.model._fc = nn.Sequential(
            nn.Linear(in_features, 512),
            nn.ReLU(inplace=True),
            nn.Linear(512, 256),
            nn.ReLU(inplace=True),
            nn.Linear(256, num_classes),
            nn.Sigmoid() 
        )

    def forward(self, x):
        return  self.model(x)
    




    Here is a sample. Customise in the way you want it

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