Pytorch错误集锦

RuntimeError: Expected one of cpu, cuda, mkldnn, opengl, opencl, ideep, hip, msnpu device type at start of device string: 0

在python\lib\site-packages\torch\cuda_init_.py文件在这里插入代码片中的set_device方法修改

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  yaml_cfg = edict(yaml.load(f))

方法一: 在config.py文件中修改cfg_from_file方法(见错误日志),
为yaml_cfg = edict(yaml.load(f), Loader=yaml.FullLoader)
方法二: 更新yaml
pip install pyyaml==3.12
3.

assert语句:
断言:assert语句用于检测表达式是否为真,如果为假,引发AssertionError错误。
语法:assert expression [,args]
expression:表达式
 size mismatch for encoder.weight: copying a param with shape torch.Size

查了半天结果是用了gpu,这是因为在多gpu上训练的模型在保存时候在参数名前多加了一个“module.”前缀,加载的时候把这个前缀去掉就行了:

device = torch.device('cpu')
# device = torch.device('cuda:0')
state_dict = torch.load('xxx.pth', map_location=device)
 
from collections import OrderedDict
state_dict_new = OrderedDict()
for k, v in state_dict.items():
    name = k[7:]  # 去掉 `module.`
    state_dict_new[name] = v
 
model.load_state_dict(state_dict_new)
 
model.eval()

你可能感兴趣的:(daily,深度学习)