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 in pre-trained model? #207

Open
talhaanwarch opened this issue Jul 14, 2020 · 3 comments
Open

How to add additional layer in pre-trained model? #207

talhaanwarch opened this issue Jul 14, 2020 · 3 comments

Comments

@talhaanwarch
Copy link

Can you please guide me how to add some extra fully connected layer on top of a pre-trained model

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b0')

I am confused, how to access the last layer and connect with another layer

@lukemelas
Copy link
Owner

lukemelas commented Jul 14, 2020

You can do something like

model._fc = nn.Sequential(nn.Linear(self.network._fc.in_features, 512), 
                                           nn.ReLU(),  
                                           nn.Dropout(0.25),
                                           nn.Linear(512, 128), 
                                           nn.ReLU(),  
                                           nn.Dropout(0.50), 
                                           nn.Linear(128,classes))

or if you want to make bigger changes:

class MyEfficientNet(nn.Module):

    def __init__(self):
        super().__init__()

        # EfficientNet
        self.network = EfficientNet.from_pretrained("efficientnet-b0")
        
        # Replace last layer
        self.network._fc = nn.Sequential(nn.Linear(self.network._fc.in_features, 512), 
                                         nn.ReLU(),  
                                         nn.Dropout(0.25),
                                         nn.Linear(512, 128), 
                                         nn.ReLU(),  
                                         nn.Dropout(0.50), 
                                         nn.Linear(128,classes))
    
    def forward(self, x):
        out = self.network(x)
        return out

model = MyEfficientNet()

Look good?

@sachinruk
Copy link

just wondering if the last layer will still have a swish activation? When I print out the model, that seems to be the case. If so how do you remove that last layer?

Last few lines of output of print(model).

(_bn1): BatchNorm2d(1280, eps=0.001, momentum=0.010000000000000009, affine=True, track_running_stats=True)
    (_avg_pooling): AdaptiveAvgPool2d(output_size=1)
    (_dropout): Dropout(p=0.2, inplace=False)
    (_fc): Sequential(
      (0): Linear(in_features=1280, out_features=512, bias=True)
      (1): ReLU()
      (2): Dropout(p=0.25, inplace=False)
      (3): Linear(in_features=512, out_features=128, bias=True)
      (4): ReLU()
      (5): Dropout(p=0.25, inplace=False)
      (6): Linear(in_features=128, out_features=1, bias=True)
    )
    (_swish): MemoryEfficientSwish()
  )
)

@sachinruk
Copy link

I've expanded on the question above on my SO question.

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