size mismatch for fc.weight: copying a param with shape torch.Size([1000, 2048]) from checkpoint, th
问题描述
我想在我自己的项目更换其他的模型
,下载的预训练模型出现了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_model50model.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
size mismatch for fc.weight: copying a param with shape torch.Size([1000, 2048]) from checkpoint, th相关推荐
- size mismatch for roi_heads.box_predictor.cls_score.weight: copying a param with shape torch.Size([9
1. 报错 RuntimeError: Error(s) in loading state_dict for FasterRCNN: size mismatch for roi_heads.box_p ...
- size mismatch for yolo_head2.1.bias: copying a param with shape torch.Size(【75】) from checkpoint...
凯哥英语视频 今天一个朋友用YOLO4预测图片报错:size mismatch for yolo_head2.1.bias: copying a param with shape torch.Size ...
- 【Python】解决CNN中训练权重参数不匹配size mismatch for fc.weight,size mismatch for fc.bias
目录 1.问题描述 2.问题原因 3.问题解决 3.1思路1--忽视最后一层权重 额外说明:假如载入权重不写strict=False, 直接是model.load_state_dict(pre_wei ...
- strict=False 但还是size mismatch for []: copying a param with shape [] from checkpoint,the shape in cur
strict=False 但还是size mismatch for []: copying a param with shape [] from checkpoint,the shape in cur ...
- size mismatch for xx.weight错误的解决方法
问题重现: RuntimeError: Error(s) in loading state_dict for xxxNet:size mismatch for bn1.weight: copying ...
- 解决size mismatch for embedding.embed_dict.userid.weight
文章目录 一.问题描述 二.解决方法 三.其他问题 Reference 一.问题描述 导入之前训练好的模型权重后使用模型预测时如题报错size mismatch for embedding.embed ...
- 2021-05-31 size mismatch for transformers copying a param
size mismatch for transformers copying a param with shape torch.Size from checkpoint, the shape in c ...
- pytorch神经网络,解决输入图像大小不匹配问题 size mismatch
问题如下:RuntimeError: size mismatch, m1: [4 x 512], m2: [64 x 128]-- RuntimeError: size mismatch, m1: [ ...
- 【error】RuntimeError: size mismatch与全连接fc层
今天跑代码的时候遇到了这个错误: RuntimeError: size mismatch, m1:[1152 x 1] ,m2:[576 x 192] ,at /opt/conda/conda-bld ...
最新文章
- Linux字符设备驱动程序的框架(新写法)
- docker一:mac入门安装
- R语言入门学习笔记 - 对R软件的认识
- Borland C++Builder 6下实现RichEdit滚屏效果[原创]
- linux vnc用户,给CentOS服务器添加用户并开启vncserver的方法
- HTTP 错误 500.19 - Internal Server Error
- 切换Debug/Release编译模式和Archive的作用
- 幸福秘诀 男女必须要看哦
- RAID阵列基础知识
- 静态代理,动态代理,Cglib代理详解
- 5. DICOM图像层级分类-DCMTK-压缩图像PixelData读取
- 推导多元最小二乘法的计算方法
- HTML5高级之拖拽drag
- 如何甄别应聘者简历的包装程度?
- apache评分表的意义_APACHE评分系统及评分表 -
- Android 跳转外部浏览器坑
- 中科大计算机学院吴雨霏,刘敏 女 博导 计算技术研究所
- pc使用qq for android,腾讯QQ for Pad Android版
- C# GDAL 数字图像处理Part6 大气辐射矫正
- Pytorch随记(3)
热门文章
- formData实现批量上传图片
- Android应用开发技巧之更方便的使用Sqlite
- jmeter怎么在服务器上运行,Jmeter必知必会(十四)-在服务器上运行脚本
- 自定义python框架_python 简单日志框架 自定义logger
- ofbiz mysql_OFBIZ 10.04 开发环境搭建(ofbiz+mysql+eclipse)
- Spring注解(包含SpringBoot)
- offsetWidth、clientWidth、scrollWidth如何计算
- ZM-SMT-SL-500婚姻登记智能自助服务终端技术规格说明书
- 二十五个顶级PHP模板引擎
- Android高仿微信/支付宝 扫一扫(弱光检测扫一扫自动放大功能)