Is there any problem with using "sub models"?

I wish to create a model which uses a resnet as part of the architecture

It will eventually have other layers, but I’ve put together a quick prototype:

class CombinedModel(torch.nn.Module):
    def __init__(self):
        super(CombinedModel, self).__init__()

        self.resnet = torch.hub.load('pytorch/vision:v0.10.0', 'resnet101', pretrained=True)

    def forward(self, x):
        x = self.resnet(x)

        return x

This seems to learn fine with my (very standard) training code. Are there any things I need to be aware of though? Do I need to forward, for example, .eval and .train calls to the sub-model?

Will depend if the resnet is already trained with weights that are “good” for your data For the evaluation part you will definitely have to call to sucessfully run it.

No, all properly registered submodules will automatically receive the .train(), .eval(), and .to() calls and you can double check it:

model = CombinedModel()
print(model.resnet.training)
# True

model.eval()
print(model.training)
# False
print(model.resnet.training)
# False
2 Likes