size mismatch for fc.weight: copying a param with shape torch.Size([1000, 2048]) from checkpoint, th

2 篇文章 0 订阅
1 篇文章 0 订阅

问题描述

我想在我自己的项目更换其他的模型,下载的预训练模型出现了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
在这里插入图片描述

  • 27
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值