Nioolek / PPYOLOE_pytorch

An unofficial implementation of Pytorch version PP-YOLOE,based on Megvii YOLOX training code.
Apache License 2.0
177 stars 20 forks source link

loading state_dict for PPYOLOE error #27

Open jrsebastian opened 3 months ago

jrsebastian commented 3 months ago

Hi, i convert the model from paddle2torch.py, using the commond below:

python paddle2torch.py -f 0 -i ../ppyoloe_plus_crn_s_80e_coco.pdparams -o ppyoloe_s.pth

The ppyoloe_plus_crn_s_80e_coco.pdparams is from paddledet.

But when i eval the model using COCO dataset, it met an error when loading state_dict for PPYOLOE:

$ python -m yolox.tools.eval -f  exps/ppyoloe/default/ppyoloe_s.py -c ppyoloe_s.pth -b 64 --conf 0.001 --legacy --ppyoloe --device 0
2024-03-19 17:27:22 | INFO     | yolox.data.datasets.coco:64 - loading annotations into memory...
2024-03-19 17:27:22 | INFO     | yolox.data.datasets.coco:64 - Done (t=0.54s)
2024-03-19 17:27:22 | INFO     | pycocotools.coco:86 - creating index...
2024-03-19 17:27:22 | INFO     | pycocotools.coco:86 - index created!
2024-03-19 17:27:24 | INFO     | __main__:165 - loading checkpoint from ppyoloe_s.pth
2024-03-19 17:27:24 | ERROR    | yolox.core.launch:98 - An error has been caught in function 'launch', process 'MainProcess' (100736), thread 'MainThread' (139940105688192):
Traceback (most recent call last):

  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
           │         │     └ {'__name__': '__main__', '__doc__': None, '__package__': 'yolox.tools', '__loader__': <_frozen_importlib_external.SourceFileL...
           │         └ <code object <module> at 0x7f44f7c24870, file "/data/shijiarong/workspace/ppyoloe/PPYOLOE_pytorch/tools/eval.py", line 5>
           └ <function _run_code at 0x7f4657c3ca60>
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
         │     └ {'__name__': '__main__', '__doc__': None, '__package__': 'yolox.tools', '__loader__': <_frozen_importlib_external.SourceFileL...
         └ <code object <module> at 0x7f44f7c24870, file "/data/shijiarong/workspace/ppyoloe/PPYOLOE_pytorch/tools/eval.py", line 5>

  File "/data/shijiarong/workspace/ppyoloe/PPYOLOE_pytorch/tools/eval.py", line 211, in <module>
    launch(
    └ <function launch at 0x7f44f7c391f0>

> File "/data/shijiarong/workspace/ppyoloe/PPYOLOE_pytorch/yolox/core/launch.py", line 98, in launch
    main_func(*args)
    │          └ (╒═══════════════════╤═══════════════════════════════════════════════════════════════════════════════════════════════════════...
    └ <function main at 0x7f4417400dc0>

  File "/data/shijiarong/workspace/ppyoloe/PPYOLOE_pytorch/tools/eval.py", line 168, in main
    model.load_state_dict(ckpt["model"])
    │     │               └ OrderedDict([('model', OrderedDict([('backbone.stem.0.conv.weight', tensor([[[[ 4.4871e-02, -4.5447e-02, -2.9153e-02],
    │     │                       ...
    │     └ <function Module.load_state_dict at 0x7f45a7d38430>
    └ PPYOLOE(
        (backbone): CSPResNet(
          (stem): Sequential(
            (0): ConvBNLayer(
              (conv): Conv2d(3, 16, kernel_size=(...

  File "/data/shijiarong/.local/lib/python3.9/site-packages/torch/nn/modules/module.py", line 2153, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(

RuntimeError: Error(s) in loading state_dict for PPYOLOE:
        Missing key(s) in state_dict: "head.proj".
        Unexpected key(s) in state_dict: "backbone.stages.0.blocks.0.conv2.alpha", "backbone.stages.1.blocks.0.conv2.alpha", "backbone.stages.1.blocks.1.conv2.alpha", "backbone.stages.2.blocks.0.conv2.alpha", "backbone.stages.2.blocks.1.conv2.alpha", "backbone.stages.3.blocks.0.conv2.alpha".

Have any ideas of what wrong with me? Could you give me some help? Thanks in advance.

Nioolek commented 3 months ago

From the error, it is that key does not correspond to the weights of paddle and torch. This code is two years ago. I think paddledet is updated, but this code is not updated with paddledet. The solution is: print out paddledets model state_dict key and pytorchmodel state_dict key, see what doesn't match, and manually change the convert script to make them match