ultralytics / yolov3

YOLOv3 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
10.22k stars 3.45k forks source link

Missing key(s) in state_dict: "total_ops", "total_params", "module_list.total_ops" #1471

Closed daixiangzi closed 4 years ago

daixiangzi commented 4 years ago

Before submitting a bug report, please be aware that your issue must be reproducible with all of the following, otherwise it is non-actionable, and we can not help you:

🐛 Bug

A clear and concise description of what the bug is. I find total_ops is registed model,when use thop in file utils/torch_utils.py .so i load weight files ,model exist key total_ops ,but weight not exist,so come up error.

To Reproduce (REQUIRED)

Cmd:

python3 test.py --cfg yolov3-spp.cfg --weights weights/yolov3-spp-ultralytics.pt
Namespace(augment=False, batch_size=16, cfg='./cfg/yolov3-spp.cfg', conf_thres=0.001, data='data/coco2014.data', device='', img_size=512, iou_thres=0.6, save_json=True, single_cls=False, task='test', weights='weights/yolov3-spp-ultralytics.pt')
Model Summary: 225 layers, 6.29987e+07 parameters, 6.29987e+07 gradients, 118.0 GFLOPS

Traceback (most recent call last): File "test.py", line 263, in opt.augment) File "test.py", line 40, in test model.load_state_dict(torch.load(weights, map_location=device)['model']) File "/anaconda3/envs/pytorch-gpu/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1045, in load_state_dict self.class.name, "\n\t".join(error_msgs))) RuntimeError: Error(s) in loading state_dict for Darknet: Missing key(s) in state_dict: "total_ops", "total_params", "module_list.total_ops", "module_list.total_params", "module_list.0.total_ops"



## Expected behavior
A clear and concise description of what you expected to happen.

## Environment
If applicable, add screenshots to help explain your problem.

 - OS: Ubuntu20
 - GPU 2080 Ti
python3.7

## Additional context
Add any other context about the problem here.
daixiangzi commented 4 years ago

solved method: 1 model.load_state_dict(state_dice,strick=False) 2 pip3 uninstall thop or conda uninstall thop

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ZhangYuef commented 3 years ago

Another approach for this issue: to delete unmatched total_ops and total_params in state_dict's keys

    checkpoint = torch.load(cpt_path)
    # delete unmatched total_ops total_params
    state_dict = []
    for n, p in checkpoint['model'].items():
        if "total_ops" not in n and "total_params" not in n:
            state_dict.append((n, p))
    state_dict = dict(state_dict)

    model.load_state_dict(state_dict)
Jingnan-Jia commented 1 year ago

solved method: 1 model.load_state_dict(state_dice,strick=False) 2 pip3 uninstall thop or conda uninstall thop

a typo: strick=False should be strict=False

pvti commented 1 year ago

In my case, it related to thop.

pvti commented 1 year ago

@ZhangYuef your solution woked in my case