CLRNet-onnxruntime-and-tensorrt-demo icon indicating copy to clipboard operation
CLRNet-onnxruntime-and-tensorrt-demo copied to clipboard

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

Open sahamitul opened this issue 2 years ago • 10 comments

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!

sahamitul avatar Aug 23 '22 16:08 sahamitul

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.

xuanandsix avatar Aug 24 '22 02:08 xuanandsix

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.

sahamitul avatar Aug 24 '22 21:08 sahamitul

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

xuanandsix avatar Aug 25 '22 02:08 xuanandsix

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?

sahamitul avatar Sep 08 '22 21:09 sahamitul

@sahamitul Have you solved this problem?

Yutong-gannis avatar Oct 14 '22 16:10 Yutong-gannis

same issues,any advices?

mengxia1994 avatar Nov 01 '22 06:11 mengxia1994

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

Yutong-gannis avatar Nov 01 '22 14:11 Yutong-gannis

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 avatar Nov 05 '22 18:11 LukeAI

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 https://github.com/xuanandsix/CLRNet-onnxruntime-and-tensorrt-demo/blob/07a47e666d4bfca9a4c60a777c62c23828fca663/torch2onnx.py#L28

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

    net.load_state_dict(new_state_dict, strict=False)

LukeAI avatar Nov 05 '22 20:11 LukeAI

I passed 'strict=False'

model = load_state_dict(path, strict=False)

ashwin-999 avatar Nov 06 '22 20:11 ashwin-999