size mismatch for segmentation_head.0.weight: copying a param with shape torch.Size([2, 16, 3, 3]) from checkpoint, the shape in current model is torch.Size([9, 16, 3, 3]).
size mismatch for segmentation_head.0.bias: copying a param with shape torch.Size([2]) from checkpoint, the shape in current model is torch.Size([9]).
解决方法:
报错:加载的权重维度是[2,16,3,3],但是在当前model中要求的维度是[9,16,3,3]。
我仔细想了想,原来是num_classes
的值设置的不对。我在train.py中设置的num_classes=2,但是在test.py中,num_classes的值我却写成9了。
另附:如果权重值在加载时会报唯独不匹配,请尝试使用这条语句:
model.load_state_dict(state_dict, strict=False)
这条语句会忽略在加载权重时,维度和当前网络不匹配的值
解决该问题的高质量博客:
https://blog.csdn.net/weixin_44966641/article/details/120083303