yjh0410 / RT-ODLab

YOLO Tutorial
MIT License
136 stars 29 forks source link

Mismatch error when generate yolox_n onnx file #8

Closed huihui308 closed 10 months ago

huihui308 commented 10 months ago

I generate yolox_n onnx file by the following command.

$ python3 export_onnx.py --dynamic --model=yolox_n --weight=./../weights/plate/yolox_n/yolox_n_best.pth

But htere is errors as following. how to fix it?Thank you very much.

$ python3 export_onnx.py --dynamic --model=yolox_n --weight=./../weights/plate/yolox_n/yolox_n_best.pth
2023-10-13 10:19:53.980 | INFO     | __main__:main:74 - args value: Namespace(batch_size=1, conf_thresh=0.1, dynamic=True, exp_file=None, experiment_name=None, fuse_conv_bn=False, img_size=640, input='images', model='yolox_n', nms_class_agnostic=False, nms_thresh=0.5, no_onnxsim=False, num_classes=80, opset=11, opts=[], output='output', save_dir='../weights/onnx/', topk=100, weight='./../weights/plate/yolox_n/yolox_n_best.pth')
==============================
Model: YOLOX_N ...
==============================
Build YOLOX_N ...
==============================
Model Configuration: 
 {'backbone': 'cspdarknet', 'pretrained': True, 'bk_act': 'silu', 'bk_norm': 'BN', 'bk_dpw': False, 'width': 0.25, 'depth': 0.34, 'stride': [8, 16, 32], 'max_stride': 32, 'fpn': 'yolox_pafpn', 'fpn_reduce_layer': 'conv', 'fpn_downsample_layer': 'conv', 'fpn_core_block': 'cspblock', 'fpn_act': 'silu', 'fpn_norm': 'BN', 'fpn_depthwise': False, 'head': 'decoupled_head', 'head_act': 'silu', 'head_norm': 'BN', 'num_cls_head': 2, 'num_reg_head': 2, 'head_depthwise': False, 'multi_scale': [0.7, 1.25], 'trans_type': 'yolox_nano', 'matcher': {'center_sampling_radius': 2.5, 'topk_candicate': 10}, 'loss_obj_weight': 1.0, 'loss_cls_weight': 1.0, 'loss_box_weight': 5.0, 'trainer_type': 'yolox'}
==============================
Head: Decoupled Head
==============================
Head: Decoupled Head
==============================
Head: Decoupled Head
--------------------------------------
Best model infor:
Epoch: 299
mAP: 83.4
--------------------------------------
2023-10-13 10:19:54.068 | ERROR    | __main__:<module>:135 - An error has been caught in function '<module>', process 'MainProcess' (153225), thread 'MainThread' (139837421885248):
Traceback (most recent call last):

> File "export_onnx.py", line 135, in <module>
    main()
    └ <function main at 0x7f2d970bfdc0>

  File "export_onnx.py", line 87, in main
    model = load_weight(model, args.weight, args.fuse_conv_bn)
            │           │      │    │       │    └ False
            │           │      │    │       └ Namespace(batch_size=1, conf_thresh=0.1, dynamic=True, exp_file=None, experiment_name=None, fuse_conv_bn=False, img_size=640,...
            │           │      │    └ './../weights/plate/yolox_n/yolox_n_best.pth'
            │           │      └ Namespace(batch_size=1, conf_thresh=0.1, dynamic=True, exp_file=None, experiment_name=None, fuse_conv_bn=False, img_size=640,...
            │           └ YOLOX(
            │               (backbone): CSPDarkNet(
            │                 (layer_1): Conv(
            │                   (convs): Sequential(
            │                     (0): Conv2d(3, 16, kernel_size=(6, 6...
            └ <function load_weight at 0x7f2daec275e0>

  File "/home/david/code/paper/PyTorch_YOLO_Tutorial/tools/../utils/misc.py", line 187, in load_weight
    model.load_state_dict(checkpoint_state_dict)
    │     │               └ OrderedDict([('backbone.layer_1.convs.0.weight', tensor([[[[ 8.1130e-02,  8.9618e-02,  1.5810e-01,  1.5324e-01,  9.4843e-02,
    │     │                 ...
    │     └ <function Module.load_state_dict at 0x7f2dbb6d9ee0>
    └ YOLOX(
        (backbone): CSPDarkNet(
          (layer_1): Conv(
            (convs): Sequential(
              (0): Conv2d(3, 16, kernel_size=(6, 6...

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

RuntimeError: Error(s) in loading state_dict for YOLOX:
        size mismatch for non_shared_heads.0.cls_feats.0.convs.0.weight: copying a param with shape torch.Size([64, 64, 3, 3]) from checkpoint, the shape in current model is torch.Size([80, 64, 3, 3]).
        size mismatch for non_shared_heads.0.cls_feats.0.convs.1.weight: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.0.cls_feats.0.convs.1.bias: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.0.cls_feats.0.convs.1.running_mean: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.0.cls_feats.0.convs.1.running_var: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.0.cls_feats.1.convs.0.weight: copying a param with shape torch.Size([64, 64, 3, 3]) from checkpoint, the shape in current model is torch.Size([80, 80, 3, 3]).
        size mismatch for non_shared_heads.0.cls_feats.1.convs.1.weight: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.0.cls_feats.1.convs.1.bias: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.0.cls_feats.1.convs.1.running_mean: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.0.cls_feats.1.convs.1.running_var: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.1.cls_feats.0.convs.0.weight: copying a param with shape torch.Size([64, 64, 3, 3]) from checkpoint, the shape in current model is torch.Size([80, 64, 3, 3]).
        size mismatch for non_shared_heads.1.cls_feats.0.convs.1.weight: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.1.cls_feats.0.convs.1.bias: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.1.cls_feats.0.convs.1.running_mean: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.1.cls_feats.0.convs.1.running_var: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.1.cls_feats.1.convs.0.weight: copying a param with shape torch.Size([64, 64, 3, 3]) from checkpoint, the shape in current model is torch.Size([80, 80, 3, 3]).
        size mismatch for non_shared_heads.1.cls_feats.1.convs.1.weight: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.1.cls_feats.1.convs.1.bias: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.1.cls_feats.1.convs.1.running_mean: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.1.cls_feats.1.convs.1.running_var: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.2.cls_feats.0.convs.0.weight: copying a param with shape torch.Size([64, 64, 3, 3]) from checkpoint, the shape in current model is torch.Size([80, 64, 3, 3]).
        size mismatch for non_shared_heads.2.cls_feats.0.convs.1.weight: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.2.cls_feats.0.convs.1.bias: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.2.cls_feats.0.convs.1.running_mean: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.2.cls_feats.0.convs.1.running_var: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.2.cls_feats.1.convs.0.weight: copying a param with shape torch.Size([64, 64, 3, 3]) from checkpoint, the shape in current model is torch.Size([80, 80, 3, 3]).
        size mismatch for non_shared_heads.2.cls_feats.1.convs.1.weight: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.2.cls_feats.1.convs.1.bias: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.2.cls_feats.1.convs.1.running_mean: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for non_shared_heads.2.cls_feats.1.convs.1.running_var: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for cls_preds.0.weight: copying a param with shape torch.Size([2, 64, 1, 1]) from checkpoint, the shape in current model is torch.Size([80, 80, 1, 1]).
        size mismatch for cls_preds.0.bias: copying a param with shape torch.Size([2]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for cls_preds.1.weight: copying a param with shape torch.Size([2, 64, 1, 1]) from checkpoint, the shape in current model is torch.Size([80, 80, 1, 1]).
        size mismatch for cls_preds.1.bias: copying a param with shape torch.Size([2]) from checkpoint, the shape in current model is torch.Size([80]).
        size mismatch for cls_preds.2.weight: copying a param with shape torch.Size([2, 64, 1, 1]) from checkpoint, the shape in current model is torch.Size([80, 80, 1, 1]).
        size mismatch for cls_preds.2.bias: copying a param with shape torch.Size([2]) from checkpoint, the shape in current model is torch.Size([80]).
huihui308 commented 10 months ago

Thanks, I have solved this issue because I did not add --num_classes parameter.