PINTO0309 / onnx2tf

Self-Created Tools to convert ONNX files (NCHW) to TensorFlow/TFLite/Keras format (NHWC). The purpose of this tool is to solve the massive Transpose extrapolation problem in onnx-tensorflow (onnx-tf). I don't need a Star, but give me a pull request.
MIT License
699 stars 74 forks source link

[YOLOvN] failed tf to tflite convertion #676

Closed R4Ajeti closed 2 months ago

R4Ajeti commented 3 months ago

Issue Type

Others

OS

Linux

onnx2tf version number

1.25.7

onnx version number

1.16.2

onnxruntime version number

1.18.1

onnxsim (onnx_simplifier) version number

1.0.4

tensorflow version number

2.17.0

Download URL for ONNX

pip install onnx2tf Yolov9 custom model onnx

Parameter Replacement JSON

{}

Description

error

ERROR: input_onnx_file_path: best-striped-model-final.onnx ERROR: onnx_op_name: wa/model.3/AveragePool ERROR: Read this and deal with it. https://github.com/PINTO0309/onnx2tf#parameter-replacement ERROR: Alternatively, if the input OP has a dynamic dimension, use the -b or -ois option to rewrite it to a static shape and try again. ERROR: If the input OP of ONNX before conversion is NHWC or an irregular channel arrangement other than NCHW, use the -kt or -kat option. ERROR: Also, for models that include NonMaxSuppression in the post-processing, try the -onwdt option.

github-actions[bot] commented 3 months ago

If there is no activity within the next two days, this issue will be closed automatically.

koush commented 2 months ago

I have same issue on that op. Here is a model with a repro: https://github.com/koush/onnx-models/blob/main/scrypted_yolov9t_320/best.onnx

onnx2f -i best.onnx

PINTO0309 commented 2 months ago

It is a bug in the API implemented by YOLOv5, YOLOv8, YOLOv9 and YOLOv10. It has nothing to do with onnx2tf.

Your ONNX is broken from the start.

onnxsim best.onnx best.onnx 

Simplifying...
Traceback (most recent call last):
  File "/home/xxxx/.local/bin/onnxsim", line 8, in <module>
    sys.exit(main())
  File "/home/xxxx/.local/lib/python3.10/site-packages/onnxsim/onnx_simplifier.py", line 481, in main
    model_opt, check_ok = simplify(
  File "/home/xxxx/.local/lib/python3.10/site-packages/onnxsim/onnx_simplifier.py", line 184, in simplify
    model = remove_initializer_from_input(model)
  File "/home/xxxx/.local/lib/python3.10/site-packages/onnxsim/onnx_simplifier.py", line 66, in remove_initializer_from_input
    onnx.checker.check_model(model)
  File "/home/xxxx/.local/lib/python3.10/site-packages/onnx/checker.py", line 179, in check_model
    C.check_model(
onnx.onnx_cpp2py_export.checker.ValidationError: Your model has duplicate keys in metadata_props.
import onnx
from pprint import pprint

model = onnx.load('best.onnx')
pprint(model.metadata_props)

print('')
model.metadata_props.pop(-1)
pprint(model.metadata_props)

onnx.save(proto=model, f='best_model_bugfix.onnx')
[key: "stride"
value: "32"
, key: "names"
value: "{0: \'person\', 1: \'vehicle\', 2: \'animal\'}"
, key: "names"
value: "{0: \'person\', 1: \'vehicle\', 2: \'animal\'}"
]

[key: "stride"
value: "32"
, key: "names"
value: "{0: \'person\', 1: \'vehicle\', 2: \'animal\'}"
]

image

onnxsim best_model_bugfix.onnx best_model_bugfix.onnx

Simplifying...
Finish! Here is the difference:
┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃             ┃ Original Model ┃ Simplified Model ┃
┡━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━┩
│ Add         │ 52             │ 44               │
│ AveragePool │ 5              │ 5                │
│ Concat      │ 33             │ 33               │
│ Constant    │ 432            │ 387              │
│ Conv        │ 186            │ 186              │
│ Div         │ 9              │ 1                │
│ Gather      │ 8              │ 0                │
│ MaxPool     │ 3              │ 3                │
│ Mul         │ 196            │ 180              │
│ Reshape     │ 5              │ 5                │
│ Resize      │ 2              │ 2                │
│ Shape       │ 8              │ 0                │
│ Sigmoid     │ 180            │ 180              │
│ Slice       │ 16             │ 16               │
│ Softmax     │ 1              │ 1                │
│ Split       │ 2              │ 2                │
│ Sub         │ 2              │ 2                │
│ Transpose   │ 2              │ 2                │
│ Model Size  │ 7.4MiB         │ 7.4MiB           │
└─────────────┴────────────────┴──────────────────┘
onnx2tf -i best_model_bugfix.onnx -cotof

INFO: onnx_output_name: wa/model.22/Concat_3_output_0 tf_output_name: tf.concat_30/concat:0 shape: (1, 67, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Split_output_0 tf_output_name: tf.split/split:0 shape: (1, 64, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Split_output_1 tf_output_name: tf.split/split:1 shape: (1, 3, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/dfl/Reshape_output_0 tf_output_name: tf.reshape_11/Reshape:0 shape: (1, 4, 16, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Sigmoid_output_0 tf_output_name: tf.math.sigmoid_179/Sigmoid:0 shape: (1, 3, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/dfl/Transpose_output_0 tf_output_name: tf.compat.v1.transpose_19/transpose:0 shape: (1, 2100, 4, 16) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/dfl/Softmax_output_0 tf_output_name: tf.nn.softmax/wa/model.22/dfl/Softmax:0 shape: (1, 2100, 4, 16) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/dfl/Transpose_1_output_0 tf_output_name: tf.compat.v1.transpose_20/transpose:0 shape: (1, 16, 4, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/dfl/conv/Conv_output_0 tf_output_name: tf.nn.convolution_185/convolution:0 shape: (1, 1, 4, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/dfl/Reshape_1_output_0 tf_output_name: tf.reshape_14/Reshape:0 shape: (1, 4, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Split_1_output_0 tf_output_name: tf.split_1/split:0 shape: (1, 2, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Split_1_output_1 tf_output_name: tf.split_1/split:1 shape: (1, 2, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Sub_output_0 tf_output_name: tf.math.subtract/Sub:0 shape: (1, 2, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Add_output_0 tf_output_name: tf.math.add_269/Add:0 shape: (1, 2, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Add_1_output_0 tf_output_name: tf.math.add_270/Add:0 shape: (1, 2, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Sub_1_output_0 tf_output_name: tf.math.subtract_1/Sub:0 shape: (1, 2, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Div_output_0 tf_output_name: tf.math.divide/truediv:0 shape: (1, 2, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Concat_4_output_0 tf_output_name: tf.concat_34/concat:0 shape: (1, 4, 2100) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: wa/model.22/Mul_output_0 tf_output_name: tf.math.multiply_812/Mul:0 shape: (1, 4, 2100) dtype: float32 validate_result:  Unmatched  max_abs_error: 0.0008087158203125
INFO: onnx_output_name: output0 tf_output_name: tf.concat_35/concat:0 shape: (1, 7, 2100) dtype: float32 validate_result:  Unmatched  max_abs_error: 0.0008087158203125

image

koush commented 2 months ago

thanks, that was the issue