open-mmlab / mmyolo

OpenMMLab YOLO series toolbox and benchmark. Implemented RTMDet, RTMDet-Rotated,YOLOv5, YOLOv6, YOLOv7, YOLOv8,YOLOX, PPYOLOE, etc.
https://mmyolo.readthedocs.io/zh_CN/dev/
GNU General Public License v3.0
2.97k stars 537 forks source link

yolov7_to_mmyolo.py error #875

Open chuong98 opened 1 year ago

chuong98 commented 1 year ago

Prerequisite

🐞 Describe the bug

I tested the command: tools/model_converters/yolov7_to_mmyolo.py on Yolov7-w6. It ran successfully up to layer 117, then throw the following error:

Convert model.117.conv.weight to bbox_head.head_module.main_convs_pred.3.0.conv.weight
Convert model.117.bn.weight to bbox_head.head_module.main_convs_pred.3.0.bn.weight
Convert model.117.bn.bias to bbox_head.head_module.main_convs_pred.3.0.bn.bias
Convert model.117.bn.running_mean to bbox_head.head_module.main_convs_pred.3.0.bn.running_mean
Convert model.117.bn.running_var to bbox_head.head_module.main_convs_pred.3.0.bn.running_var
Convert model.117.bn.num_batches_tracked to bbox_head.head_module.main_convs_pred.3.0.bn.num_batches_tracked
Traceback (most recent call last):
  File "/root/workspace/yolov7/yolov7_to_mmyolo.py", line 1093, in <module>
    main()
  File "/root/workspace/yolov7/yolov7_to_mmyolo.py", line 1085, in main
    convert(args.src, args.dst)
  File "/root/workspace/yolov7/yolov7_to_mmyolo.py", line 1051, in convert
    convert_dict['.'.join(strs_key)])
KeyError: 'model.118.conv.weight'

Environment

sys.platform: linux Python: 3.10.12 (main, Jul 5 2023, 18:54:27) [GCC 11.2.0] CUDA available: True numpy_random_seed: 2147483648 GPU 0,1,2,3: Quadro RTX 8000 CUDA_HOME: /usr/local/cuda-11.8/ GCC: gcc (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0 PyTorch: 2.0.1+cu117 PyTorch compiling details: PyTorch built with:

TorchVision: 0.15.1a0+60a3e72 OpenCV: 4.6.0 MMEngine: 0.8.4 MMCV: 2.0.0 MMDetection: 3.1.0 MMYOLO: 0.6.0+84932d7

Additional information

I printed the state dict keys from the official Yolov7, and found there are layers from 118-->122 that are not included in the mmyolo converter code. In addition, layer 122 is the box_head instead of layer 118. I think the convert_dict_w is not correct for yolov7-w6.

SikaAntler commented 1 month ago

Have you solve this problem? I have encountered similar error when converted yolov5l.pt to mmyolo format. The error is: KeyError: 'model.9.cv3'. I checked the key 'model.9.cv3' was not in the convert_dict.

chuong98 commented 1 month ago

I don't remember exactly, but here are the step that I would do:

SikaAntler commented 1 month ago

I don't remember exactly, but here are the step that I would do:

  • Modify the scrip 'yolov7_to_mmyolo.py' to create a Mapping Dict: map_dict={Yolov7_key:mmyolo_key}.
  • Loop over the key of yolov7 statedict to find unmatched keys:
umatch_yolov7=[k for k in yolov7.state_dict().keys() if k not in list(map_dict.keys())]
  • Loop over the key of mmyolo statedict to find unmatched keys:
umatch_mmyolov=[k for k in mmyolo.state_dict().keys() if k not in list(map_dict.values())]
  • Manually match the umatch_yolov7 with umatch_mmyolov, then append the result to map_dict

Thank you for your reply. I have found the problem was that the given weights was trained by the early version of yolov5 released, which contained module like 'Focus'. I succesfully converted the weights download from official repository.