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

Unexpected key(s) in state_dict: "heads.sample_x_indexs", "heads.prior_feat_ys", "heads.prior_ys", "heads.criterion.weight". #3

Open
sahamitul opened this issue Aug 23, 2022 · 10 comments · May be fixed by #10

Comments

@sahamitul
Copy link

sahamitul commented Aug 23, 2022

Hi,

I run:
python torch2onnx.py <config_file> --load_from <pth_file>
and get crash:-

pretrained model: https://download.pytorch.org/models/resnet101-5d3b4d8f.pth
Traceback (most recent call last):
File "torch2onnx.py", line 49, in
main()
File "torch2onnx.py", line 28, in main
net.load_state_dict(new_state_dict)
File "/home/msaha/anaconda3/envs/clrnet/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1223, in load_state_dict
raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for Detector:
Unexpected key(s) in state_dict: "heads.sample_x_indexs", "heads.prior_feat_ys", "heads.prior_ys", "heads.criterion.weight".

Any ideas? @xuanandsix
Thanks!

@xuanandsix
Copy link
Owner

You did not successfully replace the clr_head.py file.
cp clr_head.py in this code to CLRNet/clrnet/models/heads/ in official code.

@sahamitul
Copy link
Author

Thanks @xuanandsix.

It seems to be the clr_head.py from this repo is being indeed loaded.
I am trying to debug now - then why I still get this crash, as you say I should not.
Meanwhile if you have more thoughts, let us know here.

@xuanandsix
Copy link
Owner

xuanandsix commented Aug 25, 2022

1、By 'pip list' check if the path to the 'clrnet' package is correct.
2、Or choose to replace the clr_head.py file first before running package install (i.e. 'python setup.py build develop').
If you need training source code, it is recommended to maintain training and deployment with two environments.

A log has been upload here https://github.com/xuanandsix/CLRNet-onnxruntime-and-tensorrt-demo/blob/main/my_log/test_onnx.log
My deployment process from scratch. You can view each step by search: ***@ai02

@sahamitul
Copy link
Author

Hi @xuanandsix, I notice:

In your example "python torch2onnx.py configs/clrnet/clr_resnet18_tusimple.py --load_from tusimple_r18.pth",
when I print the keys in "state_dict" from "https://github.com/xuanandsix/CLRNet-onnxruntime-and-tensorrt-demo/blob/main/torch2onnx.py#L22",
I do not see these:-
module.heads.sample_x_indexs
module.heads.prior_feat_ys
module.heads.prior_ys
module.heads.criterion.weight

But I have these when I load my trained model.

So maybe this is why you are not getting my crash that I list in this issue. What do you think?

@Yutong-gannis
Copy link

Yutong-gannis commented Oct 14, 2022

@sahamitul Have you solved this problem?

@mengxia1994
Copy link

same issues,any advices?

@Yutong-gannis
Copy link

@mengxia1994 maybe you can try other backbone. I met this problem when use resnet-18, but it worked when I use dla 34.

@LukeAI
Copy link

LukeAI commented Nov 5, 2022

I found that exporting seems to work ok if I use the pretraining weights provided by https://github.com/Turoad/clrnet but if I train my own network, I get this error, with both dla34-culane and resnet18-tusimple. Not sure what's different

@LukeAI
Copy link

LukeAI commented Nov 5, 2022

OK I got this working literally just by deleting the offending dict keys:

      del new_state_dict["heads.sample_x_indexs"]                                  
      del new_state_dict["heads.prior_feat_ys"]                                    
      del new_state_dict["heads.prior_ys"]                                         
      del new_state_dict["heads.criterion.weight

inserted just before

net.load_state_dict(new_state_dict)

Edit. probably better to do as @AshwinAKannan suggests with strict=False

    net.load_state_dict(new_state_dict, strict=False)

@ashwin-999
Copy link

I passed 'strict=False'

model = load_state_dict(path, strict=False)

LukeAI added a commit to LukeAI/CLRNet-onnxruntime-and-tensorrt-demo that referenced this issue Nov 23, 2022
@LukeAI LukeAI linked a pull request Nov 23, 2022 that will close this issue
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

Successfully merging a pull request may close this issue.

6 participants