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

花了半天终于解决这个问题了,
Traceback (most recent call last):
  File "test_video.py", line 105, in 
    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)


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






 

你可能感兴趣的:(深度学习纠错)