Clone a model from pytorch existing model, only till certain layers

Hello All,

I am looking to define a model, which is a subset of existing models such as ResNet18, ResNet34 or ResNet101. In this context i know that pytorch already provides these (pretrained) models through import torchvision.models.

In my case I was looking for a specifc scenario, where supposing I select ResNet34 as my base model. Now I am looking to sub-divide this base model in to two models which is Base and Head respectively, where Base consist of starting few layers of ResNet34 and Head the remaining layers.

I was wondering how could I use the existing ResNet34(for example) model provided by Pytorch, to get what I want.

Thank you.

You could write a custom model, derive from Resnet34 as its base class, and assign the wanted modules to your new Base and Head submodules.
To do so, check the torchvision implementation and make sure e.g. all original functional API calls are used in your new forward method as well (assuming you don’t want to change the execution of the model’s layers).

1 Like