Loading pre-trained BNInception in Pytorch 0.4

mujtabaasif: <p>I am not able to load checkpoint from Pytorch 0.3 to Pytorch 0.4. The architecture is BNInception and I get the following error.<br>
Traceback (most recent call last):<br>
File “bninception.py”, line 513, in <br>
model = bninception()<br>
File “bninception.py”, line 504, in bninception<br>
model.load_state_dict(model_zoo.load_url(settings[‘url’]))<br>
File “/anaconda/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 719, in load_state_dict<br>
self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
RuntimeError: Error(s) in loading state_dict for BNInception:<br>
size mismatch for conv1_7x7_s2_bn.weight: copying a param of torch.Size([64]) from checkpoint, where the shape is torch.Size([1, 64]) in current model.<br>
size mismatch for conv1_7x7_s2_bn.bias: copying a param of torch.Size([64]) from checkpoint, where the shape is torch.Size([1, 64]) in current model.<br>
size mismatch for conv1_7x7_s2_bn.running_mean: copying a param of torch.Size([64]) from checkpoint, where the shape is torch.Size([1, 64]) in current model.<br>
size mismatch for conv1_7x7_s2_bn.running_var: copying a param of torch.Size([64]) from checkpoint, where the shape is torch.Size([1, 64]) in current model.<br>
size mismatch for conv2_3x3_reduce_bn.weight: copying a param of torch.Size([64]) from checkpoint, where the shape is torch.Size([1, 64]) in current model.<br>
size mismatch for conv2_3x3_reduce_bn.bias: copying a param of torch.Size([64]) from checkpoint, where the shape is torch.Size([1, 64]) in current model.</p>

alwynmathew: <p>Loading model is backward compatible but not forward compatible i.e., you can load a model from 0.4 in 0.3 but not 0.3 in 0.4 <a href="https://github.com/pytorch/pytorch/issues/6801" rel="nofollow noopener">issue 6801</a></p>

Jie: <p>I have similar problem,but the error is a  little different,my pytorch edition is 1.1.0<br>
the error is as follows:<br>
Traceback (most recent call last):<br>
Traceback (most recent call last):<br>
File “D:/Git/demo/tsn-pytorch/main.py”, line 301, in <br>
main()<br>
File “D:/Git/demo/tsn-pytorch/main.py”, line 35, in main<br>
consensus_type=args.consensus_type, dropout=args.dropout, partial_bn=not args.no_partialbn)<br>
File “D:\Git\demo\tsn-pytorch\models.py”, line 39, in <strong>init</strong><br>
self._prepare_base_model(base_model)<br>
File “D:\Git\demo\tsn-pytorch\models.py”, line 96, in _prepare_base_model<br>
self.base_model = getattr(tf_model_zoo, base_model)()<br>
File “D:\Git\demo\tsn-pytorch\tf_model_zoo\bninception\pytorch_load.py”, line 35, in <strong>init</strong><br>
self.load_state_dict(torch.utils.model_zoo.load_url(weight_url))<br>
File “D:\Anaconda\envs\pytorch\lib\site-packages\torch\nn\modules\module.py”, line 777, in load_state_dict<br>
self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
RuntimeError: Error(s) in loading state_dict for BNInception:<br>
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]).<br>
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]).<br>
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]).<br>
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]).<br>
can you help me?Many thanks!</p>

anti1990: <p>I have a similar problem. Can you tell me how you solved it? Thank you</p>

mujtabaasif: <p>This worked for me</p>
<pre><code class="lang-auto">def bninception(num_classes=1000, pretrained=None, path='/workspace/model/bn_inception.pth'):
    model = BNInception(num_classes=num_classes)
    if pretrained is not None:
        # First load the model weights into a dictionary
        state_dict = torch.load(path)
        # Update the weight shapes for layers by iterating the dictionary
        for name, weights in state_dict.items():
            if 'conv1_7x7_s2_bn' in name or 'conv2_3x3' in name or 'inception' in name:
                if len(weights.size()) == 2:
                    state_dict[name] = weights.view(-1)

        # Load the updated weights into a model
        model.load_state_dict(state_dict)
</code></pre>