Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant outputs removed from ONNX during conversion #627

Closed
amitk-OE opened this issue May 13, 2024 · 4 comments
Closed

Constant outputs removed from ONNX during conversion #627

amitk-OE opened this issue May 13, 2024 · 4 comments
Labels
feature request feature request

Comments

@amitk-OE
Copy link

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!

@amitk-OE
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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'>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request feature request
Projects
None yet
Development

No branches or pull requests

2 participants