How to get intermediate features from pre-trained model

trained_model have
    self.conv1 = ~~~
    self.conv2 = ~~~
    self.conv3 = ~~~
    self.conv4 = ~~~
    ...

then,

trained_model = torch.load('trained_net')
half_model = nn.Sequential(trained_model.conv1, trained_model.conv2)

Hi all,
I’ve tried to get intermediate features from pre-trained model.
Is it correct way to get intermediate features ?

If you need conv2 output, yes it would work.

Another way is to override the forward function of the model class in a subclass and run the input through the layers (conv1, conv2) only.

You can use register_forward_hook for whichever intermediate layer you want. Check out my previous post below on extracting features.

Hi! I made a library for this, it’s called Pytorch-Intermediate-Layer-Getter (I know, cool name), check it out.