Cannot copy param 0 weights from layer 'conv1'; shape mismatch.

本文记录了一次使用Caffe框架进行图像测试的经历,在遇到输入参数配置问题后,通过调整deploy.prototxt文件中的Input_param设置解决了问题。文章分享了从1通道调整到3通道的具体过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我使用训练好的caffemodel准备测试一下图片的时候,我定义了deploy.prototxt文件,然后利用bat脚本进行测试,发现报错,错误如下:


错误解决:打开我的deploy.prototxt文件,把Input_param中的数据改掉,把1通道改为3通道,shape参数的意思:第一个参数表示batch_size,第二个参数表示通道(彩色为3通道),剩下两个表示图像大小,我用的是黑白图像,所以改成了1通道,但是不行,所以我就又改了回去,再次运行bat脚本,就发现可以使用了。

input_param修改如下:

至于deploy.prototxt文件如何定义,这时建立在网络文件上的,修改方法详见此文章:https://blog.youkuaiyun.com/kele_imon/article/details/79106942

祝大家在caffe的路上一路顺利。

C:\Users\Administrator\Desktop\1\.venv\Scripts\python.exe C:\Users\Administrator\Desktop\1\1.py Using device: cuda === 使用自定义训练模型进行分类 === Using device: cuda C:\Users\Administrator\Desktop\1\1.py:30: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature. self.model.load_state_dict(torch.load(model_path, map_location=self.device)) Traceback (most recent call last): File "C:\Users\Administrator\Desktop\1\1.py", line 202, in <module> cifar_classifier = ResNetImageClassifier( ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\Desktop\1\1.py", line 30, in __init__ self.model.load_state_dict(torch.load(model_path, map_location=self.device)) File "C:\Users\Administrator\Desktop\1\.venv\Lib\site-packages\torch\nn\modules\module.py", line 2584, in load_state_dict raise RuntimeError( RuntimeError: Error(s) in loading state_dict for ResNet: Missing key(s) in state_dict: "layer2.0.downsample.0.weight", "layer2.0.downsample.1.weight", "layer2.0.downsample.1.bias", "layer2.0.downsample.1.running_mean", "layer2.0.downsample.1.running_var", "layer3.0.downsample.0.weight", "layer3.0.downsample.1.weight", "layer3.0.downsample.1.bias", "layer3.0.downsample.1.running_mean", "layer3.0.downsample.1.running_var", "layer4.0.downsample.0.weight", "layer4.0.downsample.1.weight", "layer4.0.downsample.1.bias", "layer4.0.downsample.1.running_mean", "layer4.0.downsample.1.running_var", "fc.weight", "fc.bias". Unexpected key(s) in state_dict: "linear.weight", "linear.bias", "layer2.0.shortcut.0.weight", "layer2.0.shortcut.1.weight", "layer2.0.shortcut.1.bias", "layer2.0.shortcut.1.running_mean", "layer2.0.shortcut.1.running_var", "layer2.0.shortcut.1.num_batches_tracked", "layer3.0.shortcut.0.weight", "layer3.0.shortcut.1.weight", "layer3.0.shortcut.1.bias", "layer3.0.shortcut.1.running_mean", "layer3.0.shortcut.1.running_var", "layer3.0.shortcut.1.num_batches_tracked", "layer4.0.shortcut.0.weight", "layer4.0.shortcut.1.weight", "layer4.0.shortcut.1.bias", "layer4.0.shortcut.1.running_mean", "layer4.0.shortcut.1.running_var", "layer4.0.shortcut.1.num_batches_tracked". size mismatch for conv1.weight: copying a param with shape torch.Size([64, 3, 3, 3]) from checkpoint, the shape in current model is torch.Size([64, 3, 7, 7]).
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值