How to load net structure of the model and its parameters from the pretrained *.pth with Pytrch?

I trained the model with matconvnet code, and convert the model to PyTorch from MatConvNet model with the tool, and the converted models can be download from

I hope to load the net structure and modify parts of layers, and use the parameters of other layers in the converted model. How can I load the net structure of the model and its parameters with Pytorch from the pretrained pedestrian-alginment.pth?

I write the follow code, but it get the errors: I think the error arises from the wrong defined model variable? I should define a empty net object or how to correct it?

import torchvision.models as models
import torch
import torch.nn as nn

model = models.resnet50(pretrained)
checkpoint = torch.load(modelpath)
Traceback (most recent call last):
File "/home/chengzi/workspace/demo/", line 13, in
File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/", line 490, in load_state_dict
KeyError: 'unexpected key "conv1.bias" in state_dict'


To load parameters from a pre-trained model to another, two models must have the same structure. And the conv1 layer in officially implemented resnet does not have bias parameters.

@SKYHOWIE25 Your method is not suitable for my question. The net structure is undefined in Pytorch, and I want to directly load it from the converted model
Because my model it is converted from a trained matconvnet model, and Can I do not the net structure but directly load the net structure from the converted model?

The .pth file very probably only contains the trained parameter values. That is the recommended way of saving a model.

So you need to create the network structure in your code (or borrow their code) and then load the weights.

Here is how their code to load their saved model

After create the network structure with the above code, the .pth pretrained model can be load, but get the follow warning. I fix it according to fix unbroadcastable UserWarning in, but it is failed.
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/ UserWarning: src is not broadcastable to dst, but they have the same number of elements. Falling back to deprecated pointwise behavior.

I think you can ignore the warning.

The model was probably saved in a previous version of pytorch and there has probably been of a slight change in behaviour in some part of pytorch.

The warning occurs for res2a_branch2a.weight, which is of shape (64, 64, 1, 1), but got saved shape (64, 64). It looks to me like they are compatible and that a pointwise copy would work equivalently to the suggested fix.

I wondered why only one instance of a Conv2d caused such a warning when the model contains many, and interestingly enough, there is only one Conv2d in the entire model with in_channels==out_channels and kernel_size=[1, 1] and stride=(1, 1). Maybe the shape of the weight array in this specific case has been changed in a recent update to pytorch.

I load the converted Pytorch model with PyTORCH0.3.0_post4 and Python3.5.4, and the model is also converted with the same version of Pytorch.

$ pip3 show torch
Metadata-Version: 2.0
Name: torch
Version: 0.3.0.post4
Summary: Tensors and Dynamic neural networks in Python with strong GPU acceleration
Home-page: UNKNOWN
Author-email: UNKNOWN
Installer: pip
License: UNKNOWN
Location: /usr/local/lib/python3.5/dist-packages
Requires: numpy, pyyaml

Is this what you are looking for?