RuntimeError: Error(s) in loading state_dict for BNInception:size mismatch for

花了半天终于解决这个问题了,
Traceback (most recent call last):
  File "test_video.py", line 105, in <module>
    img_feature_dim=args.img_feature_dim, print_spec=False)
  File "/data2/zcq1/TRN-pytorch/models.py", line 43, in __init__
    self._prepare_base_model(base_model)
  File "/data2/zcq1/TRN-pytorch/models.py", line 108, in _prepare_base_model
    self.base_model = getattr(model_zoo, base_model)()
  File "/data2/zcq1/TRN-pytorch/model_zoo/bninception/pytorch_load.py", line 38, in __init__
    self.load_state_dict(torch.utils.model_zoo.load_url(weight_url, model_dir='/data2/zcq1/TRN-pytorch/model_zoo'))
  File "/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py", line 847, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for BNInception:
	size mismatch for conv1_7x7_s2_bn.weight: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for conv1_7x7_s2_bn.bias: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for conv1_7x7_s2_bn.running_mean: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for conv1_7x7_s2_bn.running_var: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for conv2_3x3_reduce_bn.weight: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for conv2_3x3_reduce_bn.bias: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for conv2_3x3_reduce_bn.running_mean: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for conv2_3x3_reduce_bn.running_var: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for conv2_3x3_bn.weight: copying a param with shape torch.Size([1, 192]) from checkpoint, the shape in current model is torch.Size([192]).
	size mismatch for conv2_3x3_bn.bias: copying a param with shape torch.Size([1, 192]) from checkpoint, the shape in current model is torch.Size([192]).
	size mismatch for conv2_3x3_bn.running_mean: copying a param with shape torch.Size([1, 192]) from checkpoint, the shape in current model is torch.Size([192]).
	size mismatch for conv2_3x3_bn.running_var: copying a param with shape torch.Size([1, 192]) from checkpoint, the shape in current model is torch.Size([192]).
	size mismatch for inception_3a_1x1_bn.weight: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_1x1_bn.bias: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_1x1_bn.running_mean: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_1x1_bn.running_var: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_3x3_reduce_bn.weight: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_3x3_reduce_bn.bias: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_3x3_reduce_bn.running_mean: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_3x3_reduce_bn.running_var: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_3x3_bn.weight: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_3x3_bn.bias: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_3x3_bn.running_mean: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_3x3_bn.running_var: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_double_3x3_reduce_bn.weight: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).
	size mismatch for inception_3a_double_3x3_reduce_bn.bias: copying a param with shape torch.Size([1, 64]) from checkpoint, the shape in current model is torch.Size([64]).


解决前的代码是这样的:
self.load_state_dict(torch.utils.model_zoo.load_url(weight_url, model_dir='./TRN-pytorch-master/model_zoo'))

解决后:
model_BNInception = torch.utils.model_zoo.load_url(weight_url, model_dir='./TRN-pytorch-master/model_zoo')
        for name, weights in model_BNInception.items():
            # print(name, weights.size())  可以查看模型中的模型名字和权重维度
            if len(weights.size()) == 2: #判断需要修改维度的条件
                model_BNInception[name] = weights.squeeze(0)  #去掉维度0,把(1,128)转为(128)
        self.load_state_dict(model_BNInception)


终于解决了,希望别人不要像我一样再浪费时间慢慢解决这个问题,特此共享一下解决方法






 

  • 16
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值