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 https://github.com/albanie/pytorch-mcn, and the converted models can be download from http://www.robots.ox.ac.uk/~albanie/pytorch-models.html#pedestrian-alginment
https://github.com/albanie/pytorch-mcn/issues/1

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

pretrained=False
model = models.resnet50(pretrained)
modelpath="/home/chengzi/Downloads/netvlad_v103/pre-trained-models/pedestrian_alignment.pth"
checkpoint = torch.load(modelpath)
net=model.load_state_dict(checkpoint)
print(net)
Traceback (most recent call last):
File "/home/chengzi/workspace/demo/model_s.py", line 13, in
net=model.load_state_dict(checkpoint)
File "/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py", line 490, in load_state_dict
.format(name))
KeyError: 'unexpected key "conv1.bias" in state_dict'

Hi

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 http://www.robots.ox.ac.uk/~albanie/pytorch-models.html#pedestrian-alginment
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 http://www.robots.ox.ac.uk/~albanie/models/pytorch-mcn/ped_align.py

@jpeg729
After create the network structure with the above code ped_align.py, the .pth pretrained model can be load, but get the follow warning. I fix it according to fix unbroadcastable UserWarning in inception.py, but it is failed.
/usr/local/lib/python3.5/dist-packages/torch/nn/modules/module.py:482: UserWarning: src is not broadcastable to dst, but they have the same number of elements. Falling back to deprecated pointwise behavior.
own_state[name].copy_(param)

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: UNKNOWN
Author-email: UNKNOWN
Installer: pip
License: UNKNOWN
Location: /usr/local/lib/python3.5/dist-packages
Requires: numpy, pyyaml
Classifiers:

Is this what you are looking for?

https://pytorch.org/docs/stable/notes/serialization.html#recommend-saving-models