deep-learning - 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 ,当我运行它时,它显示了上述错误
解决方案
它看起来torch.load('alexnet_places365.pth.tar')
不包含一个带有成员的对象features
,而是一个状态字典,如此处所述。
我建议您打印出结果torch.load('alexnet_places365.pth.tar')
然后查找条目features
。
推荐阅读
- jquery - 如何从jquery中已经打开的窗口打开一个新窗口?
- javascript - 如何让 promise.response 返回 API 的响应?
- html - 在 .py 文件中编写 html 代码的格式是什么?
- javascript - 在 vue.js 中,我如何为需要 js 库的组件提供服务?
- c# - 如何在 STA 线程中运行 NUnit 测试?
- javascript - Ajax 请求问题,3 次返回未定义的 2 次
- java - 如果键可以通过基于最大匹配的检索开始支持“*”,我们如何压缩表中的条目数
- sql - 如何通过 PostgreSQL 更新特定列的特定行以反映另一个表中的值?
- docker - 无法准备上下文:无法评估 Dockerfile 路径中的符号链接:lstat /var/lib/snapd/void/Dockerfile:没有这样的文件或目录
- kubernetes - 将 gitlab 连接到 Rancher 中托管的 kubernetes 集群