Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model upload issue #1087

Closed
GMSL1706 opened this issue Apr 6, 2022 · 2 comments
Closed

Model upload issue #1087

GMSL1706 opened this issue Apr 6, 2022 · 2 comments

Comments

@GMSL1706
Copy link

GMSL1706 commented Apr 6, 2022

While training, I used SaveModelCallback() to save the model with the best valid loss value as suggested here.

Now, I am trying to upload my model for inference but it doesn't work. I used to the same data preprocessing.
Here is my code:

model_type = models.mmdet.faster_rcnn
backbone = model_type.backbones.resnext101_32x4d_fpn_2x
extra_args = {}
model = model_type.model(backbone=backbone(pretrained=True), num_classes=len(parser.class_map), **extra_args) 
state_dict = torch.load("./faster_rcnn__resnext101_32x4d_fpn_2x.pth")
model.load_state_dict(state_dict)

Output:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
[<ipython-input-12-decb596209c8>](https://localhost:8080/#) in <module>()
----> 1 model.load_state_dict(state_dict)

[/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py](https://localhost:8080/#) in load_state_dict(self, state_dict, strict)
   1481         if len(error_msgs) > 0:
   1482             raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
-> 1483                                self.__class__.__name__, "\n\t".join(error_msgs)))
   1484         return _IncompatibleKeys(missing_keys, unexpected_keys)
   1485 

RuntimeError: Error(s) in loading state_dict for FasterRCNN:
	Missing key(s) in state_dict: "backbone.conv1.weight", "backbone.bn1.weight", "backbone.bn1.bias", "backbone.bn1.running_mean", "backbone.bn1.running_var", "backbone.layer1.0.conv1.weight", "backbone.layer1.0.bn1.weight", "backbone.layer1.0.bn1.bias", "backbone.layer1.0.bn1.running_mean", "backbone.layer1.0.bn1.running_var", "backbone.layer1.0.conv2.weight", "backbone.layer1.0.bn2.weight", "backbone.layer1.0.bn2.bias", "backbone.layer1.0.bn2.running_mean", "backbone.layer1.0.bn2.running_var", "backbone.layer1.0.conv3.weight", "backbone.layer1.0.bn3.weight", "backbone.layer1.0.bn3.bias", "backbone.layer1.0.bn3.running_mean", "backbone.layer1.0.bn3.running_var", "backbone.layer1.0.downsample.0.weight", "backbone.layer1.0.downsample.1.weight", "backbone.layer1.0.downsample.1.bias", "backbone.layer1.0.downsample.1.running_mean", "backbone.layer1.0.downsample.1.running_var", "backbone.layer1.1.conv1.weight", "backbone.layer1.1.bn1.weight", "backbone.layer1.1.bn1.bias", "backbone.layer1.1.bn1.running_mean", "backbone.layer1.1.bn1.running_var", "backbone.layer1.1.conv2.weight", "backbone.layer1.1.bn2.weight", "backbone.layer1.1.bn2.bias", "backbone.layer1.1.bn2.running_mean", "backbone.layer1.1.bn2.running_var", "backbone.layer1.1.conv3.weight", "backbone.layer1.1.bn3.weight", "backbone.layer1.1.bn3.bias", "backbone.layer1.1.bn3.running_mean", "backbone.layer1.1.bn3.running_var", "backbone.layer1.2.conv1.weight", "backbone.layer1.2.bn1.weight", "backbone.layer1.2.bn1.bias", "bac...
	Unexpected key(s) in state_dict: "model.backbone.conv1.weight", "model.backbone.bn1.weight", "model.backbone.bn1.bias", "model.backbone.bn1.running_mean", "model.backbone.bn1.running_var", "model.backbone.bn1.num_batches_tracked", "model.backbone.layer1.0.conv1.weight", "model.backbone.layer1.0.bn1.weight", "model.backbone.layer1.0.bn1.bias", "model.backbone.layer1.0.bn1.running_mean", "model.backbone.layer1.0.bn1.running_var", "model.backbone.layer1.0.bn1.num_batches_tracked", "model.backbone.layer1.0.conv2.weight", "model.backbone.layer1.0.bn2.weight", "model.backbone.layer1.0.bn2.bias", "model.backbone.layer1.0.bn2.running_mean", "model.backbone.layer1.0.bn2.running_var", "model.backbone.layer1.0.bn2.num_batches_tracked", "model.backbone.layer1.0.conv3.weight", "model.backbone.layer1.0.bn3.weight", "model.backbone.layer1.0.bn3.bias", "model.backbone.layer1.0.bn3.running_mean", "model.backbone.layer1.0.bn3.running_var", "model.backbone.layer1.0.bn3.num_batches_tracked", "model.backbone.layer1.0.downsample.0.weight", "model.backbone.layer1.0.downsample.1.weight", "model.backbone.layer1.0.downsample.1.bias", "model.backbone.layer1.0.downsample.1.running_mean", "model.backbone.layer1.0.downsample.1.running_var", "model.backbone.layer1.0.downsample.1.num_batches_tracked", "model.backbone.layer1.1.conv1.weight", "model.backbone.layer1.1.bn1.weight", "model.backbone.layer1.1.bn1.bias", "model.backbone.layer1.1.bn1.running_mean", "model.backbone.layer1.1.bn1.running_var", "mod...

Can someone help me ?

@FraPochetti
Copy link
Contributor

FraPochetti commented Apr 6, 2022

As you can see torch is looking for backbone.conv1.weight but it is finding model.backbone.conv1.weight.
Basically all the keys in the pth dictionary have model. appended because of how the weights were saved.

Just open the dictionary and manually fix the keys.
Something like that

state_dict = torch.load("./faster_rcnn__resnext101_32x4d_fpn_2x.pth")
for k in state_dict.keys():
    state_dict[k.replace("model.", "")] = state_dict.pop(k)

@GMSL1706
Copy link
Author

GMSL1706 commented Apr 6, 2022

Thank you very much!

Here is the code that I used and it works according to this issue :

state_dict = torch.load("./faster_rcnn__resnext101_32x4d_fpn_2x.pth")
my_dic_keys = list(state_dict.keys())
for key in my_dic_keys:
    state_dict[key.replace("model.", "")] = state_dict[key]
    del state_dict[key]

model.load_state_dict(state_dict)

@GMSL1706 GMSL1706 closed this as completed Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants