**
Error (s) in loading state_dict for GeneratorResNet
**
cause of the problem: check whether we use dataparallel for multi GPU during training. The model generated by this method will automatically add key: module
observe the error message:
you can find that the key values in the model are more modules
Solution:
1. Delete the module
gentmps=torch.load("./saved_models/generator_%d.pth" % opt.epoch)
distmps = torch.load("./saved_models/discriminator_%d.pth" % opt.epoch)
from collections import OrderedDict
new_gens = OrderedDict()
new_diss = OrderedDict()
for k, v in gentmps.items():
name = k.replace('module.','') # remove 'module.'
new_gens[name] = v #The value corresponding to the key value of the new dictionary is a one-to-one value.
for k, v in distmps.items():
name = k.replace('module.','') # remove 'module.'
new_diss[name] = v #The value corresponding to the key value of the new dictionary is a one-to-one value.
generator.load_state_dict(new_gens)
discriminator.load_state_dict(new_diss)
Read More:
- Pytorch Loading model error: RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict
- pytorch RuntimeError: Error(s) in loading state_ Dict for dataparall… Import model error solution
- [Solved] AttributeError: ‘_IncompatibleKeys’ object has no attribute
- urllib.error.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>
- [Solved] AttributeError: ‘DataParallel‘ object has no attribute ‘save‘
- [Solved] AttributeError: ‘_IncompatibleKeys‘ object has no attribute ‘parameters‘
- [Solved] RuntimeError: Error(s) in loading state_dict for BertForTokenClassification
- Python errors: valueerror: if using all scalar values, you must pass an index (four solutions)
- How to Fix keyerror in Python dictionary lookup
- pytorch model.load_state_dict Error [How to Solve]
- [Solved] YOLO v5 Error: AttributeError: Can‘t get attribute SPPF on module models
- [Solved] HTTPError: 404 Client Error: Not Found for url: https://huggingface.co/saved_model
- Pytorch ValueError: Expected more than 1 value per channel when training, got input size [1, 768
- How to Solve keras load_model() Error
- [Solved] RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the
- [Solved] KeyError: ‘Transformer/encoderblock_0/MultiHeadDotProductAttention_1/query\\kernel is
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- [Solved] RuntimeError: Error(s) in loading state_dict for Net:
- “EncoderDecoder: ‘mit_b1 is not in the backbone registry‘“ [How to Solve]
- [Solved] torchsummary Error: RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.F