How to extract features from intermediate layers of VGG16?

Hi, I would like to get outputs from multiple layers of a pretrained VGG-16 network. Following is what I have done:

model = torchvision.models.vgg16()

# make new models to extract features
layers = list(model.children())[0][:8]
model_conv22 = nn.Sequential(*layers)  

layers = list(model.children())[0][:15]
model_conv33 = nn.Sequential(*layers)   

layers = list(model.children())[0][:22]
model_conv43 = nn.Sequential(*layers) 

layers = list(model.children())[0][:29]
model_conv53 = nn.Sequential(*layers)  

# get outputs
x1 = model_conv22(input)   
x2 = model_conv33(input)   
x3 = model_conv43(input)   
x4 = model_conv53(input)

I know that this is not an intelligent way. Could you give me some ideas to make it efficient?
Thank you very much.

In your case, you can use torchvision.models._utils.IntermediateLayerGetter.
For example:

import torchvision
from torchvision.models._utils import IntermediateLayerGetter
model = torchvision.models.vgg16()
return_layers = {'8': 'out_layer8', '15': 'out_layer15'}
model_with_multuple_layer = IntermediateLayerGetter(model.features, return_layers=return_layers)
x = torch.rand(5,3,512,512)
y = model_with_multuple_layer(x)

You can see that y contains the output of layer 8 and layer 15 of model.features

1 Like