How to delete layer in pretrained model?

Yes, I would not recommend to delete the modules directly, as this would break the model as seen here:

model = models.resnet18()
del model.fc

out = model(torch.randn(1, 3, 224, 224))
> ModuleAttributeError: 'ResNet' object has no attribute 'fc'

While the .fc module was removed, it’s still used in the forward method, which will raise this error.
In the linked example, the submodules of the resnet are called manually in a sequential manner, so you would have to be careful, as you won’t be able to call the model directly anymore.

3 Likes