提取vgg19 pytorch特征时,“顺序”对象没有属性“特征”

时间:2019-12-28 16:53:41

标签: deep-learning pytorch


我正在尝试使用 VGG19 网络提取图像的特征(输出应为每帧昏暗的[1,7,7,512]
这是我使用的代码:

    deep_net = models.vgg19(pretrained=True).cuda()
    deep_net = nn.Sequential(*list(deep_net.children())[:-2])
    deep_net.eval()
    save_file_sample_path = '/media/data1/out.npy'
    input_image = torch.zeros(1, 3, 224, 224)
    output_feat = np.zeros(shape=[1, 49, 512])
    with torch.no_grad():
      im = default_loader('/media/data1/images/frame612.jpg')
      im = transform(im)
      input_image[0, :, :] = im
      input_image = input_image.cuda()
      output_feat = deep_net(input_image)
      output_feat = output_feat.features[:-2].view(1, 512, 49).transpose(1, 2)

但是出现以下错误:

  

AttributeError:“顺序”对象没有属性“功能”

在线:

          output_feat = output_feat.features[:-2].view(1, 512, 49).transpose(1, 2)

有人知道为什么这不再起作用了吗?以及如何解决?
谢谢!

1 个答案:

答案 0 :(得分:0)

这是因为您正在用deep_net重建nn.Sequential,所以它失去了属性features

deep_net = models.vgg19(pretrained=True)
deep_net.features
Sequential(
  (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (1): ReLU(inplace=True)
  ...
  (36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
deep_net = nn.Sequential(*list(deep_net.children())[:-2])
deep_net.features
AttributeError: 'Sequential' object has no attribute 'features'

您现在想要的等效项是:

list(deep_net.children())[0][:-2]