How to delete layer in pretrained model?

You can replace any layer with an nn.Identity module, but especially layers “in the middle” of the model might need additional changes if the original layer was changing the shape of the input.
E.g. imagine you want to replace an nn.Conv2d(64, 128, 5) which will change the channel dimension as well as the spatial size of the activation due to the lack of padding. Replacing this layer with an nn.Identity could easily break the model since the following layer would receive an invalid activation shape.