raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}‘.format( RuntimeError: Error(s)..报错

简介: 即load_state_dict(fsd,strict=False) 属性strict;当strict=True,要求预训练练权重层数的键值与新构建的模型中的权重层数名称完全吻合;

今天在跑基于VGG16的迁移学习的时候,遇到错误。


 raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for VGG16_SE:

6379328e0a9b4a678ac6434a45fdaae3.png


原因


加载预训练权重的过程中因为没使用VGG的其他的一些层的预训练权重,bias。产生了一个错误。


解决方案


我们只需要加一个strict=False这个参数


load_state_dict(fsd,strict=False) 属性strict;当strict=True,要求预训练练权重层数的键值与新构建的模型中的权重层数名称完全吻合;


如果我们采用strict=False就能够完美的解决这个问题。也就说与训练权重中与新构建网络中匹配层的键值就进行使用,没有的就默认初始化。


如果你是用的是 model.load_state_dict(torch.load(’ '))

 model.load_state_dict(torch.load('./xxx.pt'))


改为


 model.load_state_dict(torch.load('./xxx.pt'),strict=False)


如果你是用的是 model.load_state_dict(torch.load(’ '))


torch.load('xxx.pt')


改为


torch.load('xx.pth'),False


拓展


很多教程说名字不匹配直接添加False参数即可,但是这里需要注意一个大坑。


如果模型的键值和网络的键值完全不匹配,那么模型就没有加载预训练参数,虽然不再报错。


该False参数作用在于 非严格匹配加载模型,可以下面几种情况进行分析。


1. 模型包含网络的部分参数


比如说模型是resnet101模型,你现在的网络是resnet50。再假设resnet50的参数名包含在resnet101的参数中,那么直接使用False会为你的网络resnet50加载键值相同的参数。这样就避免了对resnet101的每个键对值进行循环匹配,看是否是resnet50需要的。


2. 模型完全不包含网络的参数


情况如1,模型有100个参数,都包含’module.’ ,网络也有100个参数,都没有’module.’ 。这种情况下如果参数设置为False,会发现没有任何键值能匹配上,因此网络就不会加载任何参数。


3. 再介绍一个False使用场景


比如蒸馏网络PISR中,教师网络包含Encoder和Decoder两部分,学生网络由其中的Decoder部分组成,所以在训练学生网络时,如果要加载教师网络保存的预训练模型,设置False会自动识别Decoder部分键值相同,然后加载。


综上,设置False参数后依旧是按照键值查询加载参数的,有多少键值匹配,就加载多少模型的参数。

目录
相关文章
|
12月前
加载模型出现-RuntimeError: Error(s) in loading state_dict for Net:unexpected key(s) in state_dict: XXX
加载模型出现-RuntimeError: Error(s) in loading state_dict for Net:unexpected key(s) in state_dict: XXX
384 0
|
10月前
error C2449: found ‘{‘ at file scope (missing function header?)和error C2059: syntax error : ‘}‘
error C2449: found ‘{‘ at file scope (missing function header?)和error C2059: syntax error : ‘}‘
72 0
npm ERR! typeerror Error: Missing required argument #1
npm ERR! typeerror Error: Missing required argument #1
610 0
|
10月前
|
Linux
error: ‘TASK_INTERRUPTIBLE’ undeclared (first use in this function)
error: ‘TASK_INTERRUPTIBLE’ undeclared (first use in this function)
77 0
|
12月前
|
并行计算 PyTorch 算法框架/工具
RuntimeError: CUDA error (10): invalid device ordinal
造成这个错误的原因主要是本地只有一个 GPU (GPU:0),而程序中使用 GPUs:1。
295 0
|
12月前
|
计算机视觉
【在虚拟环境下完美解决】1698: error: (-215:Assertion failed) empty() in function cv::CascadeClassifier
首先我们要找到那个分类器所在的路径,我们可以在Cmd中直接输入【若是虚拟环境的话,则需要切换到对于虚拟环境中,再去查找对应的opencv版本与位置信息】
229 0
|
TensorFlow 算法框架/工具
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
230 0
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
|
计算机视觉
opencv出错:error: (-213:The function/feature is not implemented) Unknown/unsupported array type
opencv出错:error: (-213:The function/feature is not implemented) Unknown/unsupported array type
404 0
报错:pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 7, saw 2
报错:pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 7, saw 2
报错:pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 7, saw 2
成功解决TypeError: __init__() got an unexpected keyword argument 'serialized_options'
成功解决TypeError: __init__() got an unexpected keyword argument 'serialized_options'