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
662 stars 65 forks source link

Unable to replace flatten #583

Closed jslok closed 6 months ago

jslok commented 6 months ago

Issue Type

Others

OS

Windows

onnx2tf version number

1.19.11

onnx version number

1.15.0

onnxruntime version number

1.16.3

onnxsim (onnx_simplifier) version number

0.4.33

tensorflow version number

2.15.0

Download URL for ONNX

end2end.onnx.zip

Parameter Replacement JSON

{
  "format_version": 1,
  "operations": [
    {
      "op_name": "wa/Flatten_3",
      "param_target": "attributes",
      "param_name": "axis",
      "values": 2
    },
    {
      "op_name": "wa/Flatten_3",
      "param_target": "inputs",
      "param_name": "wa/Split_output_3",
      "pre_process_transpose_perm": [0, 8]
    },
    {
      "op_name": "wa/Flatten_3",
      "param_target": "outputs",
      "param_name": "wa/Flatten_3_output_0",
      "post_process_transpose_perm": [0, 8]
    }
  ]
}

Description

Exploring how to convert and quantize my model to tflite uint8. onnx2tf -i end2end_320.onnx -oiqt -qt per-tensor -onwdt -ois input:1,3,320,320 -prf parameter_replacement.json

Tried some different values for param replacement json but the error always remains the same. I am clearly not understanding some things. Any help would be much appreciated!

INFO: onnx_op_type: Flatten onnx_op_name: wa/Flatten_3
INFO:  input_name.1: wa/Split_output_3 shape: None dtype: float32
INFO:  output_name.1: wa/Flatten_3_output_0 shape: None dtype: float32
ERROR: The trace log is below.
Traceback (most recent call last):
  File "c:\users\jslok\onnx2tf\onnx2tf\utils\common_functions.py", line 310, in print_wrapper_func
    result = func(*args, **kwargs)
  File "c:\users\jslok\onnx2tf\onnx2tf\utils\common_functions.py", line 383, in inverted_operation_enable_disable_wrapper_func
    result = func(*args, **kwargs)
  File "c:\users\jslok\onnx2tf\onnx2tf\utils\common_functions.py", line 53, in get_replacement_parameter_wrapper_func
    func(*args, **kwargs)
  File "c:\users\jslok\onnx2tf\onnx2tf\ops\Flatten.py", line 104, in make_node
    tf.reduce_prod(input_tensor_shape[0:axis]),
  File "C:\Users\jslok\.conda\envs\onnx2tf\lib\site-packages\tensorflow\python\ops\weak_tensor_ops.py", line 88, in wrapper
    return op(*args, **kwargs)
  File "C:\Users\jslok\.conda\envs\onnx2tf\lib\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\jslok\.conda\envs\onnx2tf\lib\site-packages\tensorflow\python\framework\constant_op.py", line 357, in _tensor_shape_tensor_conversion_function
    raise ValueError(
ValueError: Cannot convert a partially known TensorShape (None, 8) to a Tensor.

ERROR: input_onnx_file_path: end2end_320.onnx
ERROR: onnx_op_name: wa/Flatten_3
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.
jslok commented 6 months ago

The model is a custom trained rtmdet-ins from mmdetection/pytorch that I converted to onnx.

PINTO0309 commented 6 months ago

Remove this garbage-like post-processing. mmdetection/pytorch

image

github-actions[bot] commented 6 months ago

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

jslok commented 5 months ago

@PINTO0309 Can you explain more please? I only enabled NMS but if I remove it, the output will be unusable. Maybe I misunderstand the situation.

Edit: Found the section about NMS in your docs. This is what you mean? https://github.com/PINTO0309/onnx2tf?tab=readme-ov-file#10-fixing-the-output-of-nonmaxsuppression-nms

kennethmugo commented 2 months ago

@PINTO0309 Can you explain more please? I only enabled NMS but if I remove it, the output will be unusable. Maybe I misunderstand the situation.

Edit: Found the section about NMS in your docs. This is what you mean? https://github.com/PINTO0309/onnx2tf?tab=readme-ov-file#10-fixing-the-output-of-nonmaxsuppression-nms

Were you able to get it to convert? Am facing the same issue.