问题描述

我想在我自己的项目更换其他的模型,下载的预训练模型出现了FC层不匹配的问题,找了好多人都写了这个点,今天总结一下:
首先我们遇到的问题如下:
他的意思是resnet50的fc层是1000分类,而我的是62分类,所以不匹配,那怎么处理呢?
在这里插入图片描述

RuntimeError: Error(s) in loading state_dict for ResNet50:
	size mismatch for fc.weight: copying a param with shape torch.Size([1000, 2048]) from checkpoint, the shape in current model is torch.Size([62, 2048]).
	size mismatch for fc.bias: copying a param with shape torch.Size([1000]) from checkpoint, the shape in current model is torch.Size([62]).


网传不可靠方法:

在这里插入图片描述
好多人都这么写
在这里插入图片描述

应用了pop,p掉全连接的参数,
这样呢,我试了,
在这里插入图片描述

 File "/mnt/ImageClassification_test/tools/resnet50.py", line 126, in choose_model50
    model.pop("fc.bias")
  File "/root/miniconda3/lib/python3.9/site-packages/torch/nn/modules/module.py", line 778, in __getattr__
    raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
torch.nn.modules.module.

**ModuleAttributeError: 'ResNet50' object has no attribute 'pop'**

根本就不行,别着急,我还有牛逼的方法:


解决方案:

从报错信息可以看出,是fc层的权重参数不匹配,那我们只要不load 这一层的参数就可以了。:

def choose_model50(name, num_classes, device, weights):
    if name == 'ResNet50':
    	# 选择模型
        model = ResNet50(num_classes=num_classes).to(device)
        # 加载pth预训练模型
        pretrained_dict = torch.load(weights, map_location=device)
        model_dict = model.state_dict()
        # 重新制作预训练的权重,主要是减去参数不匹配的层,楼主这边层名为“fc”
        pretrained_dict = {k: v for k, v in pretrained_dict.items() if (k in model_dict and 'fc' not in k)}
        # 更新权重
        model_dict.update(pretrained_dict)
        model.load_state_dict(model_dict)
       
        return model

这样不论你的模型文件和预训练文件放在那,都可以传入
对应的信息改一下,这回保证没问题
跑起来了,非常nice
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐