Adding Layers to Transfer Learnig

Hello!
I’m currently wondering if I can add a layer before and after the transfer learning model.

In many cases

In many cases, for example,
TRANSFER LEARNING FOR COMPUTER VISION TUTORIAL,

only one layer is replaced with a new layer.

https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

model_ft = models.resnet18(pretrained=True)
num_ftrs = model_ft.fc.in_features
# Here the size of each output sample is set to 2.
# Alternatively, it can be generalized to nn.Linear(num_ftrs, len(class_names)).
model_ft.fc = nn.Linear(num_ftrs, 2)

What want to do

I would like to add layers to the transferred model, for example, VGG 16.

I can add layers to the transferred model. (I’m not sure this is working😢.)

net2 = models.vgg16(pretrained=use_pretrained)

net2.classifier[6] = nn.Linear(in_features=4096, out_features=2)

# add layers after
net2.classifier.add_module('addition', nn.Linear(2, 2))

However, I cannot know how to insert layers before the transferred model.
And, I am not sure method of adding layers to the transferred model after, the above code.

How do we insert layers to transferred model?

Thank you very much!

1 Like

Hi,
you could create a new class for your model, which contains your layers before the model + the transfer model with a changed last layer (how you did int line 2 of your example) + all your additional layers at the end. In your forward method you now call your ‘before layers’, then your transferred model and last your ‘after layers’.

class MyModel(nn.Model):
  def __init__(self, out_features, use_pretrained):
    self.pre_layers = ...
    self.backbone = models.vgg16(pretrained=use_pretrained)
    self.backbone[6] = nn.Linear(in_features=4096, out_features=out_features)
    self.after_layers = ...

  def forward(self, x):
    x = self.pre_layers(x)
    x = self.backbone(x)
    x = self.after_layers(x)
    return x
2 Likes

Thank you! @Caruso

class MyModel(nn.Module):
    
    def __init__(self):
        super(MyModel, self).__init__()
        self.backbone = models.vgg16(pretrained=use_pretrained)
        self.backbone.classifier[6] = nn.Linear(in_features=4096, out_features=2)
        self.fv1 = nn.Linear(in_features=2, out_features=2)
        self.fv2 = nn.Linear(in_features=2, out_features=2)
    
    def forward(self, x):
        x = self.backbone(x)
        x = self.fv1(x)
        x = self.fv2(x)
        return x

This code goes well! Thank you very much :slight_smile:

1 Like