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
712 stars 73 forks source link

Conversion of YoloV5, conversion of StridedSlice from 5d to 4d #557

Closed alpapstm32 closed 11 months ago

alpapstm32 commented 11 months ago

Issue Type

Others

OS

Windows

onnx2tf version number

1.19.0

onnx version number

1.14.1

onnxruntime version number

1.16.0

onnxsim (onnx_simplifier) version number

0.4.33

tensorflow version number

2.14.0

Download URL for ONNX

https://drive.google.com/file/d/1dimjGPBDiqW6jZtnqrXZHiuxSvoSWmMs/view?usp=sharing

Parameter Replacement JSON

{
  "format_version": 1,
  "operations": [
    {
      "op_name": "wa/model.24/Reshape",
      "param_target": "inputs",
      "param_name": "wa/model.24/Constant_output_0",
      "values": [3,6,80,80]
    },
    {
      "op_name": "wa/model.24/Reshape",
      "param_target": "outputs",
      "param_name": "wa/model.24/Reshape_output_0",
      "pre_process_transpose_perm": [3,6,80,80]
    },
    {
      "op_name": "wa/model.24/Transpose",
      "param_target": "outputs",
      "param_name": "onnx____Sigmoid_345",
      "pre_process_transpose_perm": [3,80,80,6]
    },
    {
      "op_name": "wa/model.24/Transpose",
      "param_target": "inputs",
      "param_name": "wa/model.24/Reshape_output_0",
      "values": [3,6,80,80]
    }  
  ]
}

Description

  1. Research development of a low power system based on stm32 microprocessors. This step is crucial for the implementation of the low-power version of YOLOv5 on STM32 microprocessors. The latest version of TFLite micro library supports StridedSlice with vectors up to 4D.
  2. I have executed the onnx2tf using the command "onnx2tf -i best.onnx -oiqt -prf replace_lstm.json -kat input h_in c_in," and despite altering the sizes of the Transpose layer, the error persists.
  3. I have read the instructions for Parameter replacement.
  4. This step is important to upload the TFLite model to the microprocessor. onnx2tf
PINTO0309 commented 11 months ago

https://github.com/PINTO0309/onnx2tf?tab=readme-ov-file#cli-parameter

  -nodafsc, --number_of_dimensions_after_flexstridedslice_compression
    Number of StridedSlice OP dimensions generated after avoiding FlexStridedSlice generation.
    Default: 5
Temoslog commented 11 months ago

I'm facing a similar issue with the YOLOv5 version. Despite attempting to execute the library with the -nodaftc 4 option, the problem persists. Also, I tried to implement a similar Json file as above replacing the sizes with that from our model but the problem was not solved.

image_2023_12_15T13_07_35_189Z

The ONNX model has an input size of 1x3x128x128 and the link for the onnx model: https://drive.google.com/file/d/10rGaq4Xcu5hVHby3to8FfX0-3dBL0Geb/view?usp=drivesdk

Are there alternative methods to decrease the number of dimensions?

Thanks in advance for helping me and congrats on the really good work in the repo. I appreciate that!

PINTO0309 commented 11 months ago

After repeated version upgrades, I noticed a bug where the --nodafsc option was no longer working properly. I will fix it later.

onnx2tf -i best.onnx -nodafsc 4
PINTO0309 commented 11 months ago
onnx2tf -i best.onnx -nodafsc 4

image

PINTO0309 commented 11 months ago

I am in auto-regression testing with CI. If no problems arise in testing, I will release an updated version in about 2-8 hours that fixes the bug where the -nodafsc option is unconditionally skipped.

I want to ask you, can you handle 5D Reshape correctly? I am not very familiar with the TF micro specs.

PINTO0309 commented 11 months ago

Fixes: https://github.com/PINTO0309/onnx2tf/releases/tag/1.19.1

Temoslog commented 11 months ago

Thank you very much!