Tag Archives: Deep learning notes

[Solved] RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict

Error message:

Using the network weights trained by FCN, UNET and deeplab, an error is reported when loading the model:

RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict....

Training environment:

CPU:Intel E5
GPU: 3090*2
Pytorch1. ten

Solution:

Solve the mismatch problem
method 1 (invalid): the dictionary cannot match strictly. When the model is loaded, use the following code to add false to solve the mismatch problem

net.load_state_dict(t.load(ckpt_path),False)

Note: this method unlocks the strict matching and can ignore the error report. However, the model loaded by this method has problems in the actual segmentation effect due to the mismatch of parameters. Use it with caution!

Method 2: the network is the same, the only difference is that NN is used in the training process The dataparallel() method calls two graphics cards for training, so before loading the model weight, the model is also suitable for dataparallel packaging, which can solve the above error reporting problem.

net = nn.DataParallel(net)
net = net.to(device)