栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Attempted to set the storage of a tensor on device “cpu“ to a storage on different device “cuda:0“.

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Attempted to set the storage of a tensor on device “cpu“ to a storage on different device “cuda:0“.

在Resnet等网络加载权重时出现权重位置与网络位置不匹配导致报错,与系统、pytorch版本有关,报错如图

可以尝试更改运行环境也可以如下将加载权重位置直接改到cpu,再将加载完的模型放进gpu

# 原始加载权重代码
model_weight_path = "./resnet34-pre.pth"
assert os.path.exists(model_weight_path), "file {} does not exist.".format(model_weight_path)
net = resnet34(num_classes=5)
pre_weights = torch.load(model_weight_path, map_location=device)

# 删除map_location即可
model_weight_path = "./resnet34-pre.pth"
assert os.path.exists(model_weight_path), "file {} does not exist.".format(model_weight_path)
net = resnet34(num_classes=5)
pre_weights = torch.load(model_weight_path)
net.to(device)

修改完可以正常运行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012400.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号