tensorflow-onnx icon indicating copy to clipboard operation
tensorflow-onnx copied to clipboard

tf2onnx.convert.from_keras does not work anymore with tensorflow >= 2.16

Open Zyrin opened this issue 1 year ago • 8 comments

Describe the bug After updating to tensorflow 2.17 (from 2.15) we noticed that tf2onnx.convert.from_keras does not wort anymore. See examples down below.

Urgency Not particularly urgent for us (but maybe for others?).

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 18.04*): 22.04
  • TensorFlow Version: >=2.16
  • Python version:3.10.12

To Reproduce Here is a simple code snippet with keras.Sequential to reproduce the problem:

import keras
import tf2onnx

model = keras.Sequential([keras.layers.Input((80, 80, 3)), keras.layers.Conv2D(8, 1, 1)])

onnx_model, _ = tf2onnx.convert.from_keras(model, opset = 18, output_path = "test.onnx")

It fails with the following output:

Traceback (most recent call last):
  File ".../test.py", line 6, in <module>
    onnx_model, _ = tf2onnx.convert.from_keras(model, opset = 18, output_path = "test.onnx")
  File ".../.venv/lib/python3.10/site-packages/tf2onnx/convert.py", line 442, in from_keras
    old_out_names = _rename_duplicate_keras_model_names(model)
  File ".../.venv/lib/python3.10/site-packages/tf2onnx/convert.py", line 331, in _rename_duplicate_keras_model_names
    if model.output_names and len(set(model.output_names)) != len(model.output_names):
AttributeError: 'Sequential' object has no attribute 'output_names'. Did you mean: 'output_shape'?

Functional style models like the following:

import keras
import tf2onnx

input = keras.layers.Input((80, 80, 3))
x = keras.layers.Conv2D(8, 1, 1)(input)
model = keras.Model(inputs = input, outputs = x)

onnx_model, _ = tf2onnx.convert.from_keras(model, opset = 18, output_path = "test.onnx")

also fail and produce a different output:

Traceback (most recent call last):
  File ".../test.py", line 8, in <module>
    onnx_model, _ = tf2onnx.convert.from_keras(model, opset = 18, output_path = "test.onnx")
  File ".../.venv/lib/python3.10/site-packages/tf2onnx/convert.py", line 446, in from_keras
    function = _saving_utils.trace_model_call(model, input_signature)
  File ".../.venv/lib/python3.10/site-packages/tensorflow/python/keras/saving/saving_utils.py", line 115, in trace_model_call
    input_signature = model_input_signature(model)
  File ".../.venv/lib/python3.10/site-packages/tensorflow/python/keras/saving/saving_utils.py", line 74, in model_input_signature
    input_specs = model._get_save_spec(dynamic_batch=not keep_original_batch_size)  # pylint: disable=protected-access
AttributeError: 'Functional' object has no attribute '_get_save_spec'. Did you mean: '_set_save_spec'?

Zyrin avatar Jul 29 '24 09:07 Zyrin

Didn't upgrade tf2onnx to support 2.17 yet, it's in the plan.

fatcat-z avatar Aug 01 '24 11:08 fatcat-z

Hello, Is this will be fixed one day?

xjorma avatar Nov 10 '24 23:11 xjorma

Hello, Is this will be fixed one day?

Sure.

fatcat-z avatar Nov 11 '24 01:11 fatcat-z

Hello, Is this will be fixed one day?

Sure.

Thanks, Project are often very complex, and downgrading tensorflow version is not always possible.

xjorma avatar Nov 11 '24 03:11 xjorma

Does tf2onnx support TF 2.17 version?

suhaspillai avatar Jan 23 '25 22:01 suhaspillai

Does tf2onnx support TF 2.17 version?

Not yet, there are couple of code issues need to be figured out.

fatcat-z avatar Jan 25 '25 05:01 fatcat-z

Thanks for the quick response.

suhaspillai avatar Jan 25 '25 05:01 suhaspillai

In the meantime, setting the input_signature manually seems to work:


tspecs = [tf.TensorSpec(i.shape, name=i.name) for i in model.inputs]

tf2onnx.convert.from_keras(
    model=model,
    input_signature=tspecs,
    opset=18,
    output_path=output_path,
)

SNeugber avatar Mar 10 '25 09:03 SNeugber