RuntimeError: Error(s) in loading state_dict for ..:Missing key(s) in state_dict: …Unexpected key...

这篇具有很好参考价值的文章主要介绍了RuntimeError: Error(s) in loading state_dict for ..:Missing key(s) in state_dict: …Unexpected key...。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

原因:预训练权重层数的键值与新构建的模型中的权重层数名称不吻合,Checkpoint里面的模型是在双卡上训练的,保存的key前面都多一个module.

解决:model = torch.nn.DataParallel(model, device_ids=[0, 1]).cuda()

torch.nn.DataParallel是一种能够将数据分散到多张显卡上从而加快模型训练的方法。它的原理是首先在指定的每张显卡上拷贝一份模型,然后将输入的数据分散到各张显卡上,计算梯度,回传到第一张显卡上,然后再对模型进行参数优化。

注意:不能直接在load_state_dict里面加strict = False来解决此问题,加入strict = False,预训练权重层数的键值与新构建的模型中的权重层数名称不用完全吻合,容易出错。

torch.load_state_dict()函数就是用于将预训练的参数权重加载到新的模型之中,load_state_dict(fsd,strict=False) ,当strict=True,要求预训练练权重层数的键值与新构建的模型中的权重层数名称完全吻合。

pytorch中state_dict()和load_state_dict()函数配合使用可以实现状态的获取与重载,load()和save()函数配合使用可以实现参数的存储与读取。其中最重要的部分是“字典”的概念,因为参数对象的存储是需要“名称”——“值”对应(即键值对),读取时也是通过键值对读取的。

载入muti-GPU模型:

pretrain_model = torch.load('muti_gpu_model.pth') # 网络+权重
# 载入为single-GPU模型
gpu_model = pretrained_model.module
# 载入为CPU模型
model = ModelArch()
pretained_dict = pretained_model.module.state_dict()
model.load_satte_dict(pretained_dict)

载入muti-GPU权重:

model = ModelArch().cuda() 
model = torch.nn.DataParallel(model, device_ids=[0]) # 将model转为muti-GPU模式
checkpoint = torch.load(model_path, map_location=lambda storage, loc:storage) 
model.load_state_dict(checkpoint)
# 载入为single-GPU模型
gpu_model = model.module
# 载入为CPU模型
model = ModelArch()
model.load_state_dict(gpu_model.state_dict())
torch.save(cpu_model.state_dict(), 'cpu_model.pth')

载入CPU权重:

# 载入为CPU模型
model = ModelArch()
checkpoint = torch.load(model_path, map_location=lambda storage, loc:storage) 

# 载入为single-GPU模型
model = ModelArch().cuda() 
checkpoint = torch.load(model_path, map_location=lambda storage, loc:storage.cuda(0)) 
model.load_state_dict(checkpoint)

# 载入为muti-GPU模型
model = ModelArch().cuda() 
model = torch.nn.DataParallel(model, device_ids=[0, 1]) 
checkpoint = torch.load(model_path, map_location=lambda storage, loc:storage.cuda(0)) 
model.module.load_state_dict(checkpoint)

其他报错:

RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cuda:1

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! 

解决:把所有tensor都要放在GPU上去文章来源地址https://www.toymoban.com/news/detail-812301.html

到了这里,关于RuntimeError: Error(s) in loading state_dict for ..:Missing key(s) in state_dict: …Unexpected key...的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [GIN-debug] [ERROR] listen tcp: address 8080: missing port in address

    学习Golang_gin框架的第一天 遇到一下报错 :  [GIN-debug] [ERROR] listen tcp: address 8080: missing port in address   错误代码 :  报错原因 : 粗心 改错 : 在8080前面加上 : 符号 代码变为 :  希望大家写代码要细心!!!

    2024年02月13日
    浏览(32)
  • 【git push ERROR: commit id: missing Change-Id in message footer】

    使用 gerrit 后,提交代码会出现如下截图问题: 临时解决: step1: 把上面红色的那条gitidir复制下来执行下: step2:执行下面的命令会添加change_id step3: 然后推送代码到服务器上 上面这个情况 主要针对本地刚下载的仓库 , 第一次提交会出现这个情况 ,只需要执行一次,以后再

    2024年04月22日
    浏览(30)
  • RuntimeError: Error compiling objects for extension手把手带你解决(超详细)

    我在执行python setup.py build develop的时候,编译出错,显示 RuntimeError: Error compiling objects for extension 我在网上找了一些帖子,有让换pytorch版本的,也有其他方法的。我都试过,换pytorch版本确实能起效果,但是这个方法太笨了,我还是推荐使用另外一种方法。步骤如下: 1、进入

    2024年02月15日
    浏览(65)
  • Accurate 3D Prediction of Missing Teeth in Diverse Patterns for Precise Dental Implant Planning

    近年来,由于种植体的高成功率和美观优势,对种植体的需求激增。 然而,由于牙齿结构的复杂性和牙齿脱落模式的可变性,准确预测缺失牙齿的精确数字种植计划仍然是一个挑战。本研究提出了一种新的框架,用于准确预测不同模式的缺牙,促进数字化种植计划。提出的框

    2024年01月23日
    浏览(33)
  • 解决Git提交代码报错: ERROR: commit xxxxx: missing Change-Id in message footer

    在近期的工作中完成代码修改提交代码时Git报错并提示提交不成功,具体错误如下:  原因是Change-Id缺失。至于解决方法,Git在报错时已经提示了,如下图黄框所示: 首先,复制命令  gitdir=$(git rev-parse --git-dir); scp -p -P 29418 lijunlin@192.168.64.47:hooks/commit-msg ${gitdir}/hooks/ 并执行

    2024年02月05日
    浏览(37)
  • LLM - Model Load_in_8bit For LLaMA

      LLM 量化是将大语言模型进行压缩和优化以减少其计算和存储需求的过程。 博主在使用 LLaMA-33B 时尝试使用量化加载模型,用传统 API 参数控制量化失败,改用其他依赖尝试成功。这里先铺下结论: ◆ Load_in_8bit ✔️ ◆ Load_in_4bit ❌ 直接 load_in_8bit=True 报错: 下载 Accelerate 继续

    2024年02月04日
    浏览(28)
  • RuntimeError: CUDA error: no kernel image is available for execution on the device

    导致的原因一般都是显卡算力和cuda或者torch版本不匹配 比如在conda中安装的pytorch=1.5.0 cuda=10.2 错误:RuntimeError: CUDA error: no kernel image is available for execution on the device 参考pytorch 报错 RuntimeError: CUDA error: no kernel image is available for execution on the device_可豌豆的博客-CSDN博客 则应该安装

    2024年02月15日
    浏览(43)
  • 报错解决:RuntimeError: Error compiling objects for extension和nvcc fatal: Unsupported gpu architecture

    博主的软硬件环境(供参考): Linux NVIDIA GeForce RTX 3090 CUDA Driver version 515.76 CUDA 10.2 gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 Pytorch 1.10.0+cu102 博主在配置mmdetection3d环境时,运行 pip install -v -e . 会有如下报错: 可能的原因和解决方法如下: 查看GPU的型号: 首先查看cuda版本 输出如下,

    2024年02月13日
    浏览(26)
  • Vue报错解决[Vue warn]: Error in render: “TypeError: Cannot read property ‘state‘ of undefined“

    Vue2项目中报错: [Vue warn]: Error in render: “TypeError: Cannot read property ‘state’ of undefined” [Vue warn]: Error in mounted hook: “TypeError: Cannot read property ‘dispatch’ of undefined” 这个错误提示通常出现在 Vue 组件中,它尝试读取 Vuex store 的 state 对象,但是该对象没有被定义。这可能是由以

    2024年02月10日
    浏览(44)
  • 解决RuntimeError: CUDA error: no kernel image is available for execution on the deviceCUDA

    解决RuntimeError: CUDA error: no kernel image is available for execution on the deviceCUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect. 在服务器复现代码的时候,遇到了上述错误,解决办法如下。 .bashrc文件在服务器上初始页面的配置文件的地方 参考:

    2024年02月16日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包