Most of us find that it is very difficult to add additional layers and generate connections between the model and additional layers . But , here I am going to make it simple . So that , everyone can get benfit out of it . Just read this out once and we will be good to go .

So , here I am going to use the architechture of two small models (EfficientNet_b0 & ResNet18) as our example to understand the topic .

EfficientNet_b0 →

Image for post

First of all we will install the pre-trained model

!pip install efficientnet_pytorch

then if we look in the GitHub of efficientNet of Pytorch we will find import for this

from efficientnet_pytorch import EfficientNet

finally we will define our own class

class EfficientNet_b0(nn.Module):

After that we define the constructor for our class

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

##  where this line super(EfficientNet_b0, self).__init__() is used to inherit nn.Module used above.

After that we will load the Pre-trained EfficientNet Model .

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

and finally I dediced to add extra-layers of **a dense layer **, then a batch Normalisation layer then a dropout layer and finally two dense layers .

self.classifier_layer = nn.Sequential(
            nn.Linear(1280 , 512),
            nn.BatchNorm1d(512),
            nn.Dropout(0.2),
            nn.Linear(512 , 256),
            nn.Linear(256 , no._of_outputs_classes_for_your_dataset)
        )

#computer-vision #using-pretrained-model #data-science #deep-learning #transfer-learning

How to add additional layers in a pre-trained model using Pytorch
13.45 GEEK