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

How to add additional layer( inside the features module) in pre-trained EfficientNetB0 model?

Open sashika-himali opened this issue 2 years ago • 2 comments

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 avatar Aug 06 '23 12:08 sashika-himali

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

cthugha10 avatar Dec 10 '23 08:12 cthugha10

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

adhilcodes avatar Feb 16 '24 05:02 adhilcodes