RuntimeError: Error(s) in loading state_dict for Sequential:
This error is usually related to the use of nn.DataParallel for training
It means that the string in the model’s parameter key does not match the string in the key fetched by torch.load
Therefore, we just need to modify the dict obtained by torch.load to make it match.
Example
When I torch.save, the string in the parameter key is automatically prepended with ‘module.’
Therefore, after torch.load, we need to remove ‘module.
The method is as follows.
model = Model() model_para_dict_temp = torch.load('xxx.pth') model_para_dict = {} for key_i in model_para_dict_temp.keys(): model_para_dict[key_i[7:]] = model_para_dict_temp[key_i] # Delete 'module.' del model_para_dict_temp model.load_state_dict(model_para_dict)
Read More:
- [Solved] RuntimeError: Error(s) in loading state_dict for FasterRCNN: Missing key(s) in state_dict:……
- [Solved] RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict
- [Solved] Pytorch loading model specified GPU card number error or failed to specify
- [Solved] Pytorch Error: RuntimeError: Trying to backward through the graph a second time
- [Solved] RuntimeError: 1only batches of spatial targets supported (non-empty 3D tensors) but got targets of s
- [Solved] pytorch CrossEntropyLoss Error: RuntimeError: 1D target tensor expected, multi-target not supported
- [Solved] Pytorch3d Error: RuntimeError: Not compiled with GPU support.
- [Solved] NCCL error in: /pytorch/torch/lib/c10d/ProcessGroupNCCL ,unhandled cuda error, NCCLversion 2.7.8
- [Solved] RuntimeError: each element in list of batch should be of equal size
- [Solved] bin/hive Startup Error: Operation category READ is not supported in state standby
- [Solved] RuntimeError: CUDA error: out of memory
- [Solved] Websocket Error: WebSocket is already in CLOSING or CLOSED state
- Pytorch failed to specify GPU resolution
- RuntimeError: CUDNN_STATUS_EXECUTION_FAILED [How to Solve]
- K8s cluster initialization error: Port 6443 is in use [Solved]
- Error in loading latex project compilation [How to Solve]
- Pytorch error: `module ‘torch‘ has no attribute ‘__version___‘`
- [Solved] torch Do Targer Detection Error: RuntimeError: CUDA error: device-side assert triggered
- RuntimeError: Integer division of tensors using div or / is no longer supported, and in a future rel
- [Solved] Error: The slice reducer for key “auth“ returned undefined during initialization. If the state pas