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

Constant outputs removed from ONNX during conversion #627

Closed amitk-OE closed 6 months ago

amitk-OE commented 6 months ago

Issue Type

Others

OS

Linux

onnx2tf version number

1.1.30

onnx version number

1.12.0

onnxruntime version number

1.17.30

onnxsim (onnx_simplifier) version number

0.4.8

tensorflow version number

2.10.0

Download URL for ONNX

toy_with_constant.onnx.zip toy_with_constant_after_conversion.onnx.zip

Parameter Replacement JSON

{}

Description

Thanks you for this great contribution!

My model, in addition to its regular output, has a constant output. I'll explain - when the model get an image as input, it returns - (1) a set of features (2) a constant array. Though it may sound as a strange practice, I find it useful to deploy the model with all the constants required for the post-process.

When running - "onnx2tf -i toy_with_constant.onnx -osd", the ONNX is changed so the constant output is removed, and the tflite output also does not contain this output.

It would be very helpful if there is way to convert the ONNX to tflite in a way that would preserve the constant output.

Thank you!

PINTO0309 commented 6 months ago

Feature improvement: https://github.com/PINTO0309/onnx2tf/releases/tag/1.21.1

README: https://github.com/PINTO0309/onnx2tf/tree/main?tab=readme-ov-file#16-add-constant-outputs-to-the-model-that-are-not-connected-to-the-model-body

amitk-OE commented 6 months ago

Thanks you very much. I see that this feature was added in vesrion 1.21.1, and as far as I understand from the documentation it does not support the Tensorflow version I use (2.12.0). Is there a solution for applying this feature on an older version?

PINTO0309 commented 6 months ago

Since TensorFlow v2.15.0, the API specification has changed significantly with destructive changes to Keras, so backwarding the current onnx2tf code base to TensorFlow v2.14.0 and earlier API and maintaining multiple onnx2tf versions It is very costly and impractical to maintain multiple versions of onnx2tf.

Therefore, the following measures can be taken.

  1. I understand that this is not practical in your environment, but upgrade to TensorFlow v2.16.1.
  2. Add your own manual modifications to the codebase of onnx2tf v1.17.5, with the modifications you made in onnx2tf v1.21.1. The only changes are in the following areas. The caveat to this approach is that it would backward the code base of onnx2tf significantly, so bug fixes for several hundred commits would not be reflected. https://github.com/PINTO0309/onnx2tf/pull/628/files If you want to install onnx2tf from code using the modified code, see below.
    pip uninstall onnx2tf
    pip install -e .
amitk-OE commented 6 months ago

Thank you very much. I tried to follow your instruction from section 2 but I encounter an issue that prevents me from moving forward. The following issue happens in v.1.17.5 (using the supplied docker), before I apply any changes to the code .

A different ONNX model that includes a resize layer is attached - toy_with_constant_and_resize.onnx.zip

Following the comment in https://github.com/PINTO0309/onnx2tf/pull/628/files I added the flag "-nuo" to my command, but when I try to run "onnx2tf -i toy_with_constant_and_resize.onnx -osd -nuo" I get the following error - TypeError: Could not build a TypeSpec for KerasTensor(type_spec=TensorSpec(shape=(1, None, None, 64), dtype=tf.float32, name=None), name='tf.compat.v1.image.resize_nearest_neighbor/ResizeNearestNeighbor:0', description="created by layer 'tf.compat.v1.image.resize_nearest_neighbor'") of unsupported type <class 'keras.src.engine.keras_tensor.KerasTensor'>.