How to remove some layers in Resnet18?

I am using the pre-trained model of resnet18 through torchvision. Now I only want to use its backbone to do feature extraction, which means I don’t need the last few layers as the image shows:


How should I remove it?
I try to use: layers = list(enc.children())[:-2]
but it only can remove avgpool and fc layers. I don’t know how to remove the last bn layer.

You can use the hook to pull the Conv1 output and ignore the rest.

Better Solution in this post