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

[LaMa] Matmul #716

Closed slowlyideal closed 1 month ago

slowlyideal commented 1 month ago

Issue Type

Others

OS

Windows

onnx2tf version number

1.20.0

onnx version number

1.16.1

onnxruntime version number

1.18.1

onnxsim (onnx_simplifier) version number

0.4.33

tensorflow version number

2.17.0

Download URL for ONNX

https://huggingface.co/Carve/LaMa-ONNX/tree/main

lama_fp32.onnx

Parameter Replacement JSON

{
  "format_version": 1,
  "operations": [
    {
      "op_name": "/generator/model/model.5/conv1/ffc/convg2g/fu/rttn/MatMul",
      "param_target": "inputs",
      "param_name":"/generator/model/model.5/conv1/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    },
    {
      "op_name": "/generator/model/model.5/conv1/ffc/convg2g/fu/rttn/MatMul_1",
      "param_target": "inputs",
      "param_name":"/generator/model/model.5/conv1/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    },
    {
      "op_name": "/generator/model/model.5/conv2/ffc/convg2g/fu/rttn/MatMul",
      "param_target": "inputs",
      "param_name":"/generator/model/model.5/conv2/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    },
    {
      "op_name": "/generator/model/model.5/conv2/ffc/convg2g/fu/rttn/MatMul_1",
      "param_target": "inputs",
      "param_name":"/generator/model/model.5/conv2/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    },
    {
      "op_name": "/generator/model/model.6/conv1/ffc/convg2g/fu/rttn/MatMul",
      "param_target": "inputs",
      "param_name":"/generator/model/model.6/conv1/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    },
    {
      "op_name": "/generator/model/model.6/conv1/ffc/convg2g/fu/rttn/MatMul_1",
      "param_target": "inputs",
      "param_name":"/generator/model/model.6/conv1/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    },
    {
      "op_name": "/generator/model/model.6/conv2/ffc/convg2g/fu/rttn/MatMul",
      "param_target": "inputs",
      "param_name":"/generator/model/model.6/conv2/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    },
    {
      "op_name": "/generator/model/model.6/conv2/ffc/convg2g/fu/rttn/MatMul_1",
      "param_target": "inputs",
      "param_name":"/generator/model/model.6/conv2/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    },
    {
      "op_name": "/generator/model/model.7/conv1/ffc/convg2g/fu/rttn/MatMul",
      "param_target": "inputs",
      "param_name":"/generator/model/model.7/conv1/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    },
    {
      "op_name": "/generator/model/model.7/conv1/ffc/convg2g/fu/rttn/MatMul_1",
      "param_target": "inputs",
      "param_name":"/generator/model/model.7/conv1/ffc/convg2g/conv1/conv1.2/Relu_output_0",
      "pre_process_transpose_perm": [0,3,1,2]
    }
  ]
}

Description

1、the model is so big, it has many matmul 2、it will generate an error when meet nchw matmul wa 3、it will stop when meet shape op, if you press (crtl+c) after 10 second it continue

PINTO0309 commented 1 month ago
PINTO0309 commented 1 month ago
https://huggingface.co/Carve/LaMa-ONNX/resolve/main/lama_fp32.onnx?download=true

pip show onnx2tf spo4onnx

Name: onnx2tf
Version: 1.26.2

Name: spo4onnx
Version: 1.0.5

onnxsim lama_fp32.onnx lama_fp32.onnx \
--overwrite-input-shape "image:1,3,512,512" "mask:1,1,512,512"

ONNXSIM_FIXED_POINT_ITERS=10000 spo4onnx -if lama_fp32.onnx

https://zenn.dev/pinto0309/scraps/72f2336a5b9c2f

onnx2tf -i lama_fp32.onnx -cotof

Converted: https://github.com/PINTO0309/onnx2tf/releases/download/1.26.2/models_Needs_Fix.zip

There is an error in the conversion of wa/generator/model/model.5/conv2/ffc/convg2l/Conv_output_0 so please correct it yourself.

INFO: onnx_output_name: wa/generator/model/model.5/conv2/ffc/convg2l/Conv_output_0 tf_output_name: tf.nn.convolution_14/convolution:0 shape: (1, 128, 64, 64) dtype: float32 validate_result:  Unmatched  max_abs_error: 148.25352478027344

image

image