pytorch.load()dict对象没有属性“功能”

时间:2019-02-27 14:50:29

标签: deep-learning computer-vision conv-neural-network pytorch

    import torch
    import torch.nn as nn
    from torch.autograd import Variable
    import torchvision.models as models

    class AlexSal(nn.Module):
        def __init__(self):
            super(AlexSal, self).__init__()
            self.features = nn.Sequential(*list(torch.load('alexnet_places365.pth.tar').features.children())[:-2])
            self.relu = nn.ReLU()
            self.sigmoid = nn.Sigmoid()
            self.conv6 = nn.Conv2d(256, 1, kernel_size=(1, 1), stride=(1, 1))

        def forward(self, x):
            x = self.relu(self.features(x))
            x = self.sigmoid(self.conv6(x))
            x = x.squeeze(1)
            return x
    model = AlexSal().cuda()

Traceback (most recent call last):
  File "main.py", line 23, in <module>
    model = AlexSal().cuda()
  File "main.py", line 13, in __init__
    self.features = nn.Sequential(*list(torch.load('alexnet_places365.pth.tar').features.children())[:-2])
AttributeError: 'dict' object has no attribute 'features'

我从互联网上获得了这段代码,我下载了alexnet_places365.pth.tar,当我运行它时,它显示了以上错误

1 个答案:

答案 0 :(得分:0)

看起来torch.load('alexnet_places365.pth.tar')不包含带有成员features的对象,而是像here所述的状态字典。

我建议您打印出torch.load('alexnet_places365.pth.tar')的结果,然后寻找条目features