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