首页 > 解决方案 > pytorch.load() dict 对象没有属性'features'

问题描述

    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 ,当我运行它时,它显示了上述错误

标签: deep-learningcomputer-visionconv-neural-networkpytorch

解决方案


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

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


推荐阅读