How to return the features of vgg fc1 layer?


( Dex Xu) #1

Hi!

I am new to pytorch and doing style transfer. I want to have the features of vgg fc1 layer (1x1x4096 weights). I can get features of convolution layer easily by recreating the model structure (from pytorch tutorial):

 class VGGNet(nn.Module):
     def __init__(self):
         """Select conv1_1 ~ conv5_1 activation maps."""
         super(VGGNet, self).__init__()
         self.select = ['0', '5', '10', '19', '28']
         self.vgg = models.vgg19(pretrained=True).features
 
     def forward(self, x):
         """Extract 5 conv activation maps from an input image.
 
         Args:
             x: 4D tensor of shape (1, 3, height, width).
 
         Returns:
             features: a list containing 5 conv activation maps.
         """
         features = []
         for name, layer in self.vgg._modules.items():
             print(name, layer)
             x = layer(x)
             if name in self.select:
                 features.append(x)
         return features

I found the fc layers are only in classifier. I can do something like new_classifer=nn.Sequential(*list(model.classifier.children())[:-6]) according to this post

But how do I get the features of vgg fc1 layer? Thank you in advance!


(Moskomule) #2

Hi, the code of vgg is here.

So for example, to get first linear layer,

liner_layer = list(model.classifier.children())[0]