【问题标题】:pytorch.load() dict object has not attribute 'features'pytorch.load() dict 对象没有属性'features'
【发布时间】:2019-02-28 00:51:27
【问题描述】:
    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-learning computer-vision conv-neural-network pytorch


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 2012-01-12
      • 2021-03-08
      • 2017-05-12
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      相关资源
      最近更新 更多