问题重现:
RuntimeError: Error(s) in loading state_dict for xxxNet:
size mismatch for bn1.weight: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([512]).
size mismatch for bn1.bias: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([512]).
size mismatch for bn1.running_mean: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([512]).
size mismatch for bn1.running_var: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([512]).
问题分析:
在使用load_state_dict(model_dict, False)
时会报错,原因是你现有的模型和你权重文件的保存的模型结构不一样,但是我现有的模型结构和原来是同一个,为什么还会报错呢?
原因是:你现有的模型内有一些已定义,但是没有使用的层,比如在我上面的报错中,bn1
层在我们模型的__init__
里面定义了,但是没有在forward
中使用,但是load_state_dict()
仍然会把权重文件.pth里面的bn
层赋值给bn1
层,导致层的对应关系错误,所以要把bn1
去掉就可以了。