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'
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?
@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)
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.