When pytorch loads the model, if some judgment is used in the model, the judgment is used as the selection execution condition, but it is also saved in the model. However, when calling, the network in the judgment condition is not selected and load_state_Dict is used will report an error. Some operators cannot find the name. For example:
if backbone == "mobilenet":
self.backbone = mobilenet()
flat_shape = 1024
elif backbone == "inception_resnetv1":
self.backbone = inception_resnet()
else:
raise ValueError('Unsupported backbone - `{}`, Use mobilenet, inception_resnetv1.'.format(backbone))
self.avg = nn.AdaptiveAvgPool2d((1,1))
self.Bottleneck = nn.Linear(flat_shape, embedding_size,bias=False)
self.last_bn = nn.BatchNorm1d(embedding_size, eps=0.001, momentum=0.1, affine=True)
if mode == "train": # Judgment condition, test without loading full connection
self.classifier = nn.Linear(embedding_size, num_classes)
The strict = false option can be added to avoid operators not called in the network:
model2.load_state_dict(state_dict2, strict=False)
Read More:
- Pytorch Loading model error: RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict
- [Solved] PyTorch Load Model Error: Missing key(s) RuntimeError: Error(s) in loading state_dict for
- pytorch RuntimeError: Error(s) in loading state_ Dict for dataparall… Import model error solution
- [Solved] Pytorch Error: RuntimeError: Error(s) in loading state_dict for Network: size mismatch
- [Solved] RuntimeError: Error(s) in loading state_dict for BertForTokenClassification
- [Solved] RuntimeError: Error(s) in loading state_dict for Net:
- [Solved] Error(s) in loading state_dict for GeneratorResNet
- [Solved] RuntimeError: Error(s) in loading state dict for YOLOX:
- How to Solve keras load_model() Error
- [Solved] D2lzh_Pytorch Import error: importerror: DLL load failed while importing
- `Model.XXX` is not supported when the `Model` instance was constructed with eager mode enabled
- [Solved] Python 3.6 Error: ‘dict’ object has no attribute ‘has_key’
- Pytorch ValueError: Expected more than 1 value per channel when training, got input size [1, 768
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- [Solved] TUM associate.py Scripte Error: AttributeError: ‘dict_keys‘ object has no attribute ‘remove‘
- [ONNXRuntimeError] : 10 : INVALID_Graph loading model error
- To solve the problem that the loss of verification set of resnet50 pre-training model remains unchanged
- How to Solve Pytorch eval Stuck Error
- How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’
- Pytorch: error message with chunks of 0 [How to Solve]