Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors in loading state_dict (size mismatch) #71

Open
dubdu opened this issue Jul 5, 2022 · 3 comments
Open

Errors in loading state_dict (size mismatch) #71

dubdu opened this issue Jul 5, 2022 · 3 comments

Comments

@dubdu
Copy link

dubdu commented Jul 5, 2022

Hey, can you please help me?

I was trying to train the model on custom dataset following instructions from this issue (deleted LPRDataLoader.getitem and modified CHARS, though did not change T_length).

I am running the code from Google Colaboratory and here is the issue:

Successful to build network!

RuntimeError Traceback (most recent call last)
in ()
154
155 if name == "main":
--> 156 test()

1 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)
1496 if len(error_msgs) > 0:
1497 raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
-> 1498 self.class.name, "\n\t".join(error_msgs)))
1499 return _IncompatibleKeys(missing_keys, unexpected_keys)
1500

RuntimeError: Error(s) in loading state_dict for LPRNet:
size mismatch for backbone.20.weight: copying a param with shape torch.Size([68, 256, 13, 1]) from checkpoint, the shape in current model is torch.Size([37, 256, 13, 1]).
size mismatch for backbone.20.bias: copying a param with shape torch.Size([68]) from checkpoint, the shape in current model is torch.Size([37]).
size mismatch for backbone.21.weight: copying a param with shape torch.Size([68]) from checkpoint, the shape in current model is torch.Size([37]).
size mismatch for backbone.21.bias: copying a param with shape torch.Size([68]) from checkpoint, the shape in current model is torch.Size([37]).
size mismatch for backbone.21.running_mean: copying a param with shape torch.Size([68]) from checkpoint, the shape in current model is torch.Size([37]).
size mismatch for backbone.21.running_var: copying a param with shape torch.Size([68]) from checkpoint, the shape in current model is torch.Size([37]).
size mismatch for container.0.weight: copying a param with shape torch.Size([68, 516, 1, 1]) from checkpoint, the shape in current model is torch.Size([37, 485, 1, 1]).
size mismatch for container.0.bias: copying a param with shape torch.Size([68]) from checkpoint, the shape in current model is torch.Size([37]).

Code itself:

def test():
args = get_parser()
lprnet = build_lprnet(lpr_max_len=args.lpr_max_len, phase=args.phase_train, class_num=len(CHARS), dropout_rate=args.dropout_rate)
device = torch.device("cuda:0" if args.cuda else "cpu")
lprnet.to(device)
print("Successful to build network!")

# load pretrained model
if args.pretrained_model:
    lprnet.load_state_dict(torch.load(args.pretrained_model)) _##underlined_
    print("load pretrained model successful!")
else:
    print("[Error] Can't found pretrained mode, please check!")
    return False
test_img_dirs = os.path.expanduser(args.test_img_dirs)
test_dataset = LPRDataLoader(test_img_dirs.split(','), args.img_size, args.lpr_max_len)
try:
    Greedy_Decode_Eval(lprnet, test_dataset, args)
finally:
    cv2.destroyAllWindows()

What should I do?..

@Vergissmeinnic
Copy link

I'm the same with you.

@MyraBaba
Copy link

MyraBaba commented Aug 9, 2022

same error:

Successful to build network! Traceback (most recent call last): File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py", line 1483, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/tulpar/Project/LPRNet_Pytorch/test_LPRNet.py", line 205, in <module> test() File "/Users/tulpar/Project/LPRNet_Pytorch/test_LPRNet.py", line 73, in test lprnet.load_state_dict(torch.load(args.pretrained_model, map_location=torch.device(device))) File "/Users/tulpar/Projects/Deepfake-Detection2/venv/InsightFace-REST-Nov2021/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1497, in load_state_dict raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format( RuntimeError: Error(s) in loading state_dict for LPRNet: Missing key(s) in state_dict: "stage1.0.weight", "stage1.0.bias", "stage1.1.weight", "stage1.1.bias", "stage1.1.running_mean", "stage1.1.running_var", "stage1.3.block.0.weight", "stage1.3.block.0.bias", "stage1.3.block.1.weight", "stage1.3.block.1.bias", "stage1.3.block.1.running_mean", "stage1.3.block.1.running_var", "stage1.3.block.3.weight", "stage1.3.block.3.bias", "stage1.3.block.4.weight", "stage1.3.block.4.bias", "stage1.3.block.4.running_mean", "stage1.3.block.4.running_var", "stage1.4.downsample.block.0.weight", "stage1.4.downsample.block.0.bias", "stage1.4.block.0.weight", "stage1.4.block.0.bias", "stage1.4.block.1.weight", "stage1.4.block.1.bias", "stage1.4.block.1.running_mean", "stage1.4.block.1.running_var", "stage1.4.block.3.weight", "stage1.4.block.3.bias", "stage1.4.block.4.weight", "stage1.4.block.4.bias", "stage1.4.block.4.running_mean", "stage1.4.block.4.running_var", "stage1.5.downsample.block.0.weight", "stage1.5.downsample.block.0.bias", "stage1.5.block.0.weight", "stage1.5.block.0.bias", "stage1.5.block.1.weight", "stage1.5.block.1.bias", "stage1.5.block.1.running_mean", "stage1.5.block.1.running_var", "stage1.5.block.3.weight", "stage1.5.block.3.bias", "stage1.5.block.4.weight", "stage1.5.block.4.bias", "stage1.5.block.4.running_mean", "stage1.5.block.4.running_var", "stage1.6.downsample.block.0.weight", "stage1.6.downsample.block.0.bias", "stage1.6.block.0.weight", "stage1.6.block.0.bias", "stage1.6.block.1.weight", "stage1.6.block.1.bias", "stage1.6.block.1.running_mean", "stage1.6.block.1.running_var", "stage1.6.block.3.weight", "stage1.6.block.3.bias", "stage1.6.block.4.weight", "stage1.6.block.4.bias", "stage1.6.block.4.running_mean", "stage1.6.block.4.running_var", "downsample1.0.weight", "downsample1.0.bias", "downsample1.1.weight", "downsample1.1.bias", "downsample1.1.running_mean", "downsample1.1.running_var", "downsample1.3.weight", "downsample1.3.bias", "downsample1.4.weight", "downsample1.4.bias", "downsample1.4.running_mean", "downsample1.4.running_var", "downsample1.6.weight", "downsample1.6.bias", "downsample1.7.weight", "downsample1.7.bias", "downsample1.7.running_mean", "downsample1.7.running_var", "stage2.0.downsample.block.0.weight", "stage2.0.downsample.block.0.bias", "stage2.0.block.0.weight", "stage2.0.block.0.bias", "stage2.0.block.1.weight", "stage2.0.block.1.bias", "stage2.0.block.1.running_mean", "stage2.0.block.1.running_var", "stage2.0.block.3.weight", "stage2.0.block.3.bias", "stage2.0.block.4.weight", "stage2.0.block.4.bias", "stage2.0.block.4.running_mean", "stage2.0.block.4.running_var", "stage2.1.block.0.weight", "stage2.1.block.0.bias", "stage2.1.block.1.weight", "stage2.1.block.1.bias", "stage2.1.block.1.running_mean", "stage2.1.block.1.running_var", "stage2.1.block.3.weight", "stage2.1.block.3.bias", "stage2.1.block.4.weight", "stage2.1.block.4.bias", "stage2.1.block.4.running_mean", "stage2.1.block.4.running_var", "downsample2.0.weight", "downsample2.0.bias", "downsample2.1.weight", "downsample2.1.bias", "downsample2.1.running_mean", "downsample2.1.running_var", "downsample2.3.weight", "downsample2.3.bias", "downsample2.4.weight", "downsample2.4.bias", "downsample2.4.running_mean", "downsample2.4.running_var", "stage3.0.downsample.block.0.weight", "stage3.0.downsample.block.0.bias", "stage3.0.block.0.weight", "stage3.0.block.0.bias", "stage3.0.block.1.weight", "stage3.0.block.1.bias", "stage3.0.block.1.running_mean", "stage3.0.block.1.running_var", "stage3.0.block.3.weight", "stage3.0.block.3.bias", "stage3.0.block.4.weight", "stage3.0.block.4.bias", "stage3.0.block.4.running_mean", "stage3.0.block.4.running_var", "stage3.1.downsample.block.0.weight", "stage3.1.downsample.block.0.bias", "stage3.1.block.0.weight", "stage3.1.block.0.bias", "stage3.1.block.1.weight", "stage3.1.block.1.bias", "stage3.1.block.1.running_mean", "stage3.1.block.1.running_var", "stage3.1.block.3.weight", "stage3.1.block.3.bias", "stage3.1.block.4.weight", "stage3.1.block.4.bias", "stage3.1.block.4.running_mean", "stage3.1.block.4.running_var", "stage4.0.weight", "stage4.0.bias", "stage4.1.weight", "stage4.1.bias", "stage4.1.running_mean", "stage4.1.running_var", "stage4.3.weight", "stage4.3.bias", "stage4.4.weight", "stage4.4.bias", "stage4.4.running_mean", "stage4.4.running_var", "bn.0.weight", "bn.0.bias", "bn.0.running_mean", "bn.0.running_var", "bn4.0.weight", "bn4.0.bias", "bn4.0.running_mean", "bn4.0.running_var". Unexpected key(s) in state_dict: "backbone.0.weight", "backbone.0.bias", "backbone.1.weight", "backbone.1.bias", "backbone.1.running_mean", "backbone.1.running_var", "backbone.1.num_batches_tracked", "backbone.4.block.0.weight", "backbone.4.block.0.bias", "backbone.4.block.2.weight", "backbone.4.block.2.bias", "backbone.4.block.4.weight", "backbone.4.block.4.bias", "backbone.4.block.6.weight", "backbone.4.block.6.bias", "backbone.5.weight", "backbone.5.bias", "backbone.5.running_mean", "backbone.5.running_var", "backbone.5.num_batches_tracked", "backbone.8.block.0.weight", "backbone.8.block.0.bias", "backbone.8.block.2.weight", "backbone.8.block.2.bias", "backbone.8.block.4.weight", "backbone.8.block.4.bias", "backbone.8.block.6.weight", "backbone.8.block.6.bias", "backbone.9.weight", "backbone.9.bias", "backbone.9.running_mean", "backbone.9.running_var", "backbone.9.num_batches_tracked", "backbone.11.block.0.weight", "backbone.11.block.0.bias", "backbone.11.block.2.weight", "backbone.11.block.2.bias", "backbone.11.block.4.weight", "backbone.11.block.4.bias", "backbone.11.block.6.weight", "backbone.11.block.6.bias", "backbone.12.weight", "backbone.12.bias", "backbone.12.running_mean", "backbone.12.running_var", "backbone.12.num_batches_tracked", "backbone.16.weight", "backbone.16.bias", "backbone.17.weight", "backbone.17.bias", "backbone.17.running_mean", "backbone.17.running_var", "backbone.17.num_batches_tracked", "backbone.20.weight", "backbone.20.bias", "backbone.21.weight", "backbone.21.bias", "backbone.21.running_mean", "backbone.21.running_var", "backbone.21.num_batches_tracked". size mismatch for container.0.weight: copying a param with shape torch.Size([68, 516, 1, 1]) from checkpoint, the shape in current model is torch.Size([11, 779, 1, 1]). size mismatch for container.0.bias: copying a param with shape torch.Size([68]) from checkpoint, the shape in current model is torch.Size([11]). python-BaseException

@mesakarghm
Copy link

This is probably because of the change in class_num parameter. If you changed the characters the model is going to train on, you cannot use the pretrained model. Better go with xavier initialization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants