Extract image convolution features using VGG11 & Pytorch

在Pytorch中,vgg11模型没有提供现成的提取特征的方法,可以通过继承VGG并重载forward函数实现。


class MyVGG(vgg.VGG):
    # Do NOT change variable name "features", because pretrained weights link to "features"
    def __init__(self, features):
        super(MyVGG, self).__init__(features, num_classes=1000)
        self.features = features
        # collect all feature images
        self.feature_images = []

    def forward(self, x):
        current_feature_images = []
        # Split VGG's sequential up to alone layers so that we can identify conv layers
        for layer in self.features:
            x = layer(x)
            if isinstance(layer, torch.nn.modules.conv.Conv2d):
                # get features
                print(x.data.shape)
                current_feature_images.append(x.data)
        self.feature_images.append(current_feature_images)
        #x = x.view(x.size(0), -1)
        #x = self.classifier(x)
        return x

    def get_features(self):
        return self.feature_images

my_model = MyVGG(vgg.make_layers(vgg.cfg['A'])).cuda()
my_model.load_state_dict(model_zoo.load_url(vgg.model_urls['vgg11']))

dataloader = DataLoader(Raw_Dataset(transformer), batch_size=1)

for i, (data) in enumerate(dataloader):
    data = Variable(data).cuda()
    output = my_model(data)

features = my_model.get_features()
print(len(features))
print(features[2][3])

获得输出:

( 0 , 0 ,.,.) = 
   0.0000   0.0000   0.0000  ...    0.0000   0.0000   0.0000
   2.4214   0.1004   1.2602  ...    5.9556  12.8149   0.0000
   0.0000   0.0000   0.0000  ...    0.0000   3.5820   0.0000
            ...               ⋱              ...            
   0.0000   0.0000   0.0000  ...    0.0000   1.3177   0.0000
   0.0000   0.0000   0.0000  ...    0.2860   5.2069   0.0000
   1.1075   0.0000   0.0000  ...    4.8140   9.5646   0.0000

( 0 , 1 ,.,.) = 
   0.0000   1.6450   0.9396  ...    0.0000   0.1403   0.0000
   2.4927   1.7271   0.0000  ...    0.0000   0.8866   2.2275
   0.4791   0.0000   0.0000  ...    0.0000   1.8545   0.0000
            ...               ⋱              ...            
   0.6682   0.0000   0.0000  ...    0.0000   0.0000   0.0000
   0.4618   0.2356   0.0000  ...    0.0000   0.0000   0.0000
   0.0000   1.9331   0.0000  ...    0.0000   2.6586   1.7822

( 0 , 2 ,.,.) = 
   0.0000   0.0000   0.0000  ...    0.3853   0.0000   0.0000
   0.0000   0.0000   0.0000  ...    0.0000   0.0000   0.0000
   0.0000   0.0000   0.0000  ...    0.2904   0.0000   0.0000
            ...               ⋱              ...            
   0.0000   0.0000   0.0000  ...    0.8636   0.0000   0.0000
   0.0000   0.0000   0.0000  ...    0.0000   0.0000   0.0000
   0.0000   0.0000   0.0000  ...    0.0000   0.0000   0.0000
    ... 

( 0 ,253,.,.) = 
   0.0000   0.0000   0.0118  ...    0.0000   0.0000   0.1653
   0.0000   0.0000   0.0000  ...    0.0000   0.0000   0.0000
   0.0000   0.0000   0.0000  ...    0.0000   0.0000   0.0000
            ...               ⋱              ...            
   0.0000   0.0000   0.0000  ...    0.0000   0.0000   0.0000
   0.0000   0.0000   0.0000  ...    0.0000   0.0000   0.0000
   0.0000   0.0000   0.0000  ...    1.4344   0.0000   0.0000

( 0 ,254,.,.) = 
   0.0000   0.0000   0.0000  ...    5.1606   0.0000   0.0000
   0.0000   0.0000   0.0000  ...    0.8255   0.0000   0.0000
   0.0000   0.0000   0.0000  ...    0.8174   0.0000   0.0000
            ...               ⋱              ...            
   0.0000   0.0000   0.0000  ...    0.0000   0.0000   1.6579
   0.0000   0.0000   0.0000  ...    0.4888   0.0000   0.0000
   0.2749   0.0000   0.0000  ...    2.6977   0.4342   0.5718

( 0 ,255,.,.) = 
   0.0000   0.4074   0.3179  ...    2.6135   2.9879   1.2361
   0.0000   0.0000   0.6924  ...    1.0088   4.0900   0.5106
   0.0000   0.8456   0.6279  ...    0.0000   2.4190   0.7714
            ...               ⋱              ...            
   0.0000   0.6281   0.3263  ...    0.0000   0.0000   0.0000
   0.0000   0.2666   1.1943  ...    0.0000   0.0000   0.0000
   0.8492   2.1939   1.4926  ...    0.0000   0.0000   0.0000
[torch.cuda.FloatTensor of size 1x256x56x56 (GPU 0)]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值