Error(s) pytorch 加载checkpoint state_dict出错:Missing key(s) && Unexpected key(s) in state_dict

简介: Error(s) pytorch 加载checkpoint state_dict出错:Missing key(s) && Unexpected key(s) in state_dict

ERROR:

1. Traceback (most recent call last):
2. File "test_0.py", line 130, in
3. model = load_model()
4. File "test_0.py", line 104, in load_model
5. model.load_state_dict(checkpoint['state_dict'])
6. File "/home/cosmo/anaconda3/envs/tf8/lib/python3.6/site-packages/torch/nn/modules/module.py", line 719, in load_state_dict
7. self.class.name, "\n\t".join(error_msgs)))
8. RuntimeError: Error(s) in loading state_dict for DataParallel:
9. Missing key(s) in state_dict: "module.features.0.weight", "module.features.0.bias", "module.features.3.weight", "module.features.3.bias", "module.features.6.weight", "module.features.6.bias", "module.features.8.weight", "module.features.8.bias", "module.features.10.weight", "module.features.10.bias", "module.classifier.weight", "module.classifier.bias".
10. Unexpected key(s) in state_dict: "features.module.0.weight", "features.module.0.bias", "features.module.3.weight", "features.module.3.bias", "features.module.6.weight", "features.module.6.bias", "features.module.8.weight", "features.module.8.bias", "features.module.10.weight", "features.module.10.bias", "classifier.weight", "classifier.bias".

REASON:

The problem is the module is load with dataparallel activated and you are trying to load it without data parallel. That's why there's an extra module at the beginning of each key!

错误原因就是net.load_state_dict的时候,net的状态不是处在gpu并行状态,而存储的net模型checkpoint是在gpu并行状态下的!

SOLVE:

在net.load_state_dict前将net的状态设置成gpu并行模式就好了。


AIEarth是一个由众多领域内专家博主共同打造的学术平台,旨在建设一个拥抱智慧未来的学术殿堂!【平台地址:https://devpress.csdn.net/aiearth】 很高兴认识你!加入我们共同进步!

目录
打赏
0
0
0
0
4
分享
相关文章
人工智能平台PAI产品使用合集之Alink是否加载预训练好的pytorch模型
阿里云人工智能平台PAI是一个功能强大、易于使用的AI开发平台,旨在降低AI开发门槛,加速创新,助力企业和开发者高效构建、部署和管理人工智能应用。其中包含了一系列相互协同的产品与服务,共同构成一个完整的人工智能开发与应用生态系统。以下是对PAI产品使用合集的概述,涵盖数据处理、模型开发、训练加速、模型部署及管理等多个环节。
239 10
PyTorch进阶:模型保存与加载,以及断点续训技巧
【4月更文挑战第17天】本文介绍了PyTorch中模型的保存与加载,以及断点续训技巧。使用`torch.save`和`torch.load`可保存和加载模型权重和状态字典。保存模型时,可选择仅保存轻量级的状态字典或整个模型对象。加载时,需确保模型结构与保存时一致。断点续训需保存训练状态,包括epoch、batch index、optimizer和scheduler状态。中断后,加载这些状态以恢复训练,节省时间和资源。
1274 0

热门文章

最新文章

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问