[solved] KeyError: 'unexpected key "module.encoder.embedding.weight" in state_dict'

@weblucas

What is args in this case? Is it project-specific?

This code that I am using saves the model using torch.save(model)… in this case the model is load using args.pretrained = torch.load(args.pretrained)
when it is a single gpu. model is one of my models MyModelNet(nn.Module), but in the multi gpu case it is nn.DataParallel(MyModelNet(nn.Module))

Ok so that wouldn’t really fix the loading problem but will help saving the correct state_dict() depending on whether your model is parallelized or not.

A more graceful solution is:

name = k.replace(".module", “”) # removing ‘.moldule’ from key

As for me using the k[7:] wasn’t properly removing the ‘module’.

@fmassa

I used your code to remove unexpected keys, however I can not get out from this error.
I tried all other tricks also.

Please give some hints to solve it. It will be very appreciated.

state_dict = torch.load("/media/Data/jcl-vb/output_dir/model_0000050.pth")
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[9:] # remove module.
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
‘’

However, it shows the error like:

RuntimeError: Error(s) in loading state_dict for GeneralizedRCNN:
Missing key(s) in state_dict: “backbone.body.stem.conv1.weight”, “backbone.body.stem.bn1.weight”, “backbone.body.stem.bn1.bias”, “backbone.body.stem.bn1.running_mean”, “backbone.body.stem.bn1.running_var”, “backbone.body.layer1.0.downsample.0.weight”, “backbone.body.layer1.0.downsample.1.weight”, “backbone.body.layer1.0.downsample.1.bias”, “backbone.body.layer1.0.downsample.1.running_mean”, “backbone.body.layer1.0.downsample.1.running_var”, “backbone.body.layer1.0.conv1.weight”, “backbone.body.layer1.0.bn1.weight”, “backbone.body.layer1.0.bn1.bias”, “backbone.body.layer1.0.bn1.running_mean”, “backbone.body.layer1.0.bn1.running_var”, “backbone.body.layer1.0.conv2.weight”, “backbone.body.layer1.0.bn2.weight”, “backbone.body.layer1.0.bn2.bias”, “backbone.body.layer1.0.bn2.running_mean”, “backbone.body.layer1.0.bn2.running_var”, “backbone.body.layer1.0.conv3.weight”, “backbone.body.layer1.0.bn3.weight”, “backbone.body.layer1.0.bn3.bias”, “backbone.body.layer1.0.bn3.running_mean”, “backbone.body.layer1.0.bn3.running_var”, “backbone.body.layer1.1.conv1.weight”, “backbone.body.layer1.1.bn1.weight”, “backbone.body.layer1.1.bn1.bias”, “backbone.body.layer1.1.bn1.running_mean”, “backbone.body.layer1.1.bn1.running_var”, “backbone.body.layer1.1.conv2.weight”, “backbone.body.layer1.1.bn2.weight”, “backbone.body.layer1.1.bn2.bias”, “backbone.body.layer1.1.bn2.running_mean”, “backbone.body.layer1.1.bn2.running_var”, “backbone.body.layer1.1.conv3.weight”, “backbone.body.layer1.1.bn3.weight”, “backbone.body.layer1.1.bn3.bias”, “backbone.body.layer1.1.bn3.running_mean”, “backbone.body.layer1.1.bn3.running_var”, “backbone.body.layer1.2.conv1.weight”, “backbone.body.layer1.2.bn1.weight”, “backbone.body.layer1.2.bn1.bias”, “backbone.body.layer1.2.bn1.running_mean”, “backbone.body.layer1.2.bn1.running_var”, “backbone.body.layer1.2.conv2.weight”, “backbone.body.layer1.2.bn2.weight”, “backbone.body.layer1.2.bn2.bias”, “backbone.body.layer1.2.bn2.running_mean”, “backbone.body.layer1.2.bn2.running_var”, “backbone.body.layer1.2.conv3.weight”, “backbone.body.layer1.2.bn3.weight”, “backbone.body.layer1.2.bn3.bias”, “backbone.body.layer1.2.bn3.running_mean”, “backbone.body.layer1.2.bn3.running_var”, “backbone.body.layer2.0.downsample.0.weight”, “backbone.body.layer2.0.downsample.1.weight”, “backbone.body.layer2.0.downsample.1.bias”, “backbone.body.layer2.0.downsample.1.running_mean”, “backbone.body.layer2.0.downsample.1.running_var”, “backbone.body.layer2.0.conv1.weight”, “backbone.body.layer2.0.bn1.weight”, “backbone.body.layer2.0.bn1.bias”, “backbone.body.layer2.0.bn1.running_mean”, “backbone.body.layer2.0.bn1.running_var”, “backbone.body.layer2.0.conv2.weight”, “backbone.body.layer2.0.bn2.weight”, “backbone.body.layer2.0.bn2.bias”, “backbone.body.layer2.0.bn2.running_mean”, “backbone.body.layer2.0.bn2.running_var”, “backbone.body.layer2.0.conv3.weight”, “backbone.body.layer2.0.bn3.weight”, “backbone.body.layer2.0.bn3.bias”, “backbone.body.layer2.0.bn3.running_mean”, “backbone.body.layer2.0.bn3.running_var”, “backbone.body.layer2.1.conv1.weight”, “backbone.body.layer2.1.bn1.weight”, “backbone.body.layer2.1.bn1.bias”, “backbone.body.layer2.1.bn1.running_mean”, “backbone.body.layer2.1.bn1.running_var”, “backbone.body.layer2.1.conv2.weight”, “backbone.body.layer2.1.bn2.weight”, “backbone.body.layer2.1.bn2.bias”, “backbone.body.layer2.1.bn2.running_mean”, “backbone.body.layer2.1.bn2.running_var”, “backbone.body.layer2.1.conv3.weight”, “backbone.body.layer2.1.bn3.weight”, “backbone.body.layer2.1.bn3.bias”, “backbone.body.layer2.1.bn3.running_mean”, “backbone.body.layer2.1.bn3.running_var”, “backbone.body.layer2.2.conv1.weight”, “backbone.body.layer2.2.bn1.weight”, “backbone.body.layer2.2.bn1.bias”, “backbone.body.layer2.2.bn1.running_mean”, “backbone.body.layer2.2.bn1.running_var”, “backbone.body.layer2.2.conv2.weight”, “backbone.body.layer2.2.bn2.weight”, “backbone.body.layer2.2.bn2.bias”, “backbone.body.layer2.2.bn2.running_mean”, “backbone.body.layer2.2.bn2.running_var”, “backbone.body.layer2.2.conv3.weight”, “backbone.body.layer2.2.bn3.weight”, “backbone.body.layer2.2.bn3.bias”, “backbone.body.layer2.2.bn3.running_mean”, “backbone.body.layer2.2.bn3.running_var”, “backbone.body.layer2.3.conv1.weight”, “backbone.body.layer2.3.bn1.weight”, “backbone.body.layer2.3.bn1.bias”, “backbone.body.layer2.3.bn1.running_mean”, “backbone.body.layer2.3.bn1.running_var”, “backbone.body.layer2.3.conv2.weight”, “backbone.body.layer2.3.bn2.weight”, “backbone.body.layer2.3.bn2.bias”, “backbone.body.layer2.3.bn2.running_mean”, “backbone.body.layer2.3.bn2.running_var”, “backbone.body.layer2.3.conv3.weight”, “backbone.body.layer2.3.bn3.weight”, “backbone.body.layer2.3.bn3.bias”, “backbone.body.layer2.3.bn3.running_mean”, “backbone.body.layer2.3.bn3.running_var”, “backbone.body.layer3.0.downsample.0.weight”, “backbone.body.layer3.0.downsample.1.weight”, “backbone.body.layer3.0.downsample.1.bias”, “backbone.body.layer3.0.downsample.1.running_mean”, “backbone.body.layer3.0.downsample.1.running_var”, “backbone.body.layer3.0.conv1.weight”, “backbone.body.layer3.0.bn1.weight”, “backbone.body.layer3.0.bn1.bias”, “backbone.body.layer3.0.bn1.running_mean”, “backbone.body.layer3.0.bn1.running_var”, “backbone.body.layer3.0.conv2.weight”, “backbone.body.layer3.0.bn2.weight”, “backbone.body.layer3.0.bn2.bias”, “backbone.body.layer3.0.bn2.running_mean”, “backbone.body.layer3.0.bn2.running_var”, “backbone.body.layer3.0.conv3.weight”, “backbone.body.layer3.0.bn3.weight”, “backbone.body.layer3.0.bn3.bias”, “backbone.body.layer3.0.bn3.running_mean”, “backbone.body.layer3.0.bn3.running_var”, “backbone.body.layer3.1.conv1.weight”, “backbone.body.layer3.1.bn1.weight”, “backbone.body.layer3.1.bn1.bias”, “backbone.body.layer3.1.bn1.running_mean”, “backbone.body.layer3.1.bn1.running_var”, “backbone.body.layer3.1.conv2.weight”, “backbone.body.layer3.1.bn2.weight”, “backbone.body.layer3.1.bn2.bias”, “backbone.body.layer3.1.bn2.running_mean”, “backbone.body.layer3.1.bn2.running_var”, “backbone.body.layer3.1.conv3.weight”, “backbone.body.layer3.1.bn3.weight”, “backbone.body.layer3.1.bn3.bias”, “backbone.body.layer3.1.bn3.running_mean”, “backbone.body.layer3.1.bn3.running_var”, “backbone.body.layer3.2.conv1.weight”, “backbone.body.layer3.2.bn1.weight”, “backbone.body.layer3.2.bn1.bias”, “backbone.body.layer3.2.bn1.running_mean”, “backbone.body.layer3.2.bn1.running_var”, “backbone.body.layer3.2.conv2.weight”, “backbone.body.layer3.2.bn2.weight”, “backbone.body.layer3.2.bn2.bias”, “backbone.body.layer3.2.bn2.running_mean”, “backbone.body.layer3.2.bn2.running_var”, “backbone.body.layer3.2.conv3.weight”, “backbone.body.layer3.2.bn3.weight”, “backbone.body.layer3.2.bn3.bias”, “backbone.body.layer3.2.bn3.running_mean”, “backbone.body.layer3.2.bn3.running_var”, “backbone.body.layer3.3.conv1.weight”, “backbone.body.layer3.3.bn1.weight”, “backbone.body.layer3.3.bn1.bias”, “backbone.body.layer3.3.bn1.running_mean”, “backbone.body.layer3.3.bn1.running_var”, “backbone.body.layer3.3.conv2.weight”, “backbone.body.layer3.3.bn2.weight”, “backbone.body.layer3.3.bn2.bias”, “backbone.body.layer3.3.bn2.running_mean”, “backbone.body.layer3.3.bn2.running_var”, “backbone.body.layer3.3.conv3.weight”, “backbone.body.layer3.3.bn3.weight”, “backbone.body.layer3.3.bn3.bias”, “backbone.body.layer3.3.bn3.running_mean”, “backbone.body.layer3.3.bn3.running_var”, “backbone.body.layer3.4.conv1.weight”, “backbone.body.layer3.4.bn1.weight”, “backbone.body.layer3.4.bn1.bias”, “backbone.body.layer3.4.bn1.running_mean”, “backbone.body.layer3.4.bn1.running_var”, “backbone.body.layer3.4.conv2.weight”, “backbone.body.layer3.4.bn2.weight”, “backbone.body.layer3.4.bn2.bias”, “backbone.body.layer3.4.bn2.running_mean”, “backbone.body.layer3.4.bn2.running_var”, “backbone.body.layer3.4.conv3.weight”, “backbone.body.layer3.4.bn3.weight”, “backbone.body.layer3.4.bn3.bias”, “backbone.body.layer3.4.bn3.running_mean”, “backbone.body.layer3.4.bn3.running_var”, “backbone.body.layer3.5.conv1.weight”, “backbone.body.layer3.5.bn1.weight”, “backbone.body.layer3.5.bn1.bias”, “backbone.body.layer3.5.bn1.running_mean”, “backbone.body.layer3.5.bn1.running_var”, “backbone.body.layer3.5.conv2.weight”, “backbone.body.layer3.5.bn2.weight”, “backbone.body.layer3.5.bn2.bias”, “backbone.body.layer3.5.bn2.running_mean”, “backbone.body.layer3.5.bn2.running_var”, “backbone.body.layer3.5.conv3.weight”, “backbone.body.layer3.5.bn3.weight”, “backbone.body.layer3.5.bn3.bias”, “backbone.body.layer3.5.bn3.running_mean”, “backbone.body.layer3.5.bn3.running_var”, “backbone.body.layer4.0.downsample.0.weight”, “backbone.body.layer4.0.downsample.1.weight”, “backbone.body.layer4.0.downsample.1.bias”, “backbone.body.layer4.0.downsample.1.running_mean”, “backbone.body.layer4.0.downsample.1.running_var”, “backbone.body.layer4.0.conv1.weight”, “backbone.body.layer4.0.bn1.weight”, “backbone.body.layer4.0.bn1.bias”, “backbone.body.layer4.0.bn1.running_mean”, “backbone.body.layer4.0.bn1.running_var”, “backbone.body.layer4.0.conv2.weight”, “backbone.body.layer4.0.bn2.weight”, “backbone.body.layer4.0.bn2.bias”, “backbone.body.layer4.0.bn2.running_mean”, “backbone.body.layer4.0.bn2.running_var”, “backbone.body.layer4.0.conv3.weight”, “backbone.body.layer4.0.bn3.weight”, “backbone.body.layer4.0.bn3.bias”, “backbone.body.layer4.0.bn3.running_mean”, “backbone.body.layer4.0.bn3.running_var”, “backbone.body.layer4.1.conv1.weight”, “backbone.body.layer4.1.bn1.weight”, “backbone.body.layer4.1.bn1.bias”, “backbone.body.layer4.1.bn1.running_mean”, “backbone.body.layer4.1.bn1.running_var”, “backbone.body.layer4.1.conv2.weight”, “backbone.body.layer4.1.bn2.weight”, “backbone.body.layer4.1.bn2.bias”, “backbone.body.layer4.1.bn2.running_mean”, “backbone.body.layer4.1.bn2.running_var”, “backbone.body.layer4.1.conv3.weight”, “backbone.body.layer4.1.bn3.weight”, “backbone.body.layer4.1.bn3.bias”, “backbone.body.layer4.1.bn3.running_mean”, “backbone.body.layer4.1.bn3.running_var”, “backbone.body.layer4.2.conv1.weight”, “backbone.body.layer4.2.bn1.weight”, “backbone.body.layer4.2.bn1.bias”, “backbone.body.layer4.2.bn1.running_mean”, “backbone.body.layer4.2.bn1.running_var”, “backbone.body.layer4.2.conv2.weight”, “backbone.body.layer4.2.bn2.weight”, “backbone.body.layer4.2.bn2.bias”, “backbone.body.layer4.2.bn2.running_mean”, “backbone.body.layer4.2.bn2.running_var”, “backbone.body.layer4.2.conv3.weight”, “backbone.body.layer4.2.bn3.weight”, “backbone.body.layer4.2.bn3.bias”, “backbone.body.layer4.2.bn3.running_mean”, “backbone.body.layer4.2.bn3.running_var”, “backbone.fpn.fpn_inner1.weight”, “backbone.fpn.fpn_inner1.bias”, “backbone.fpn.fpn_layer1.weight”, “backbone.fpn.fpn_layer1.bias”, “backbone.fpn.fpn_inner2.weight”, “backbone.fpn.fpn_inner2.bias”, “backbone.fpn.fpn_layer2.weight”, “backbone.fpn.fpn_layer2.bias”, “backbone.fpn.fpn_inner3.weight”, “backbone.fpn.fpn_inner3.bias”, “backbone.fpn.fpn_layer3.weight”, “backbone.fpn.fpn_layer3.bias”, “backbone.fpn.fpn_inner4.weight”, “backbone.fpn.fpn_inner4.bias”, “backbone.fpn.fpn_layer4.weight”, “backbone.fpn.fpn_layer4.bias”, “rpn.anchor_generator.cell_anchors.0”, “rpn.anchor_generator.cell_anchors.1”, “rpn.anchor_generator.cell_anchors.2”, “rpn.anchor_generator.cell_anchors.3”, “rpn.anchor_generator.cell_anchors.4”, “rpn.head.conv.weight”, “rpn.head.conv.bias”, “rpn.head.cls_logits.weight”, “rpn.head.cls_logits.bias”, “rpn.head.bbox_pred.weight”, “rpn.head.bbox_pred.bias”, “roi_heads.box.feature_extractor.fc6.weight”, “roi_heads.box.feature_extractor.fc6.bias”, “roi_heads.box.feature_extractor.fc7.weight”, “roi_heads.box.feature_extractor.fc7.bias”, “roi_heads.box.predictor.cls_score.weight”, “roi_heads.box.predictor.cls_score.bias”, “roi_heads.box.predictor.bbox_pred.weight”, “roi_heads.box.predictor.bbox_pred.bias”.
Unexpected key(s) in state_dict: “”.

1 Like