onnx/tensorflow-onnx

cannot convert keras model to onnx : 'Sequential' object has no attribute 'output_names'I

LaurentBerger opened this issue ยท 9 comments

How can I convert this model to onnx?

def my_model_cnn():
    model = tf.keras.Sequential()
    couche0 = tf.keras.layers.Conv2D(6, kernel_size=(3, 3), activation='relu')
    couche1 = tf.keras.layers.MaxPooling2D((2, 2))
    couche2 = tf.keras.layers.Conv2D(16, activation='relu',kernel_size=(3, 3))
    couche3 = tf.keras.layers.MaxPooling2D((2, 2))
    couche4 = tf.keras.layers.Flatten()
    couche5 = tf.keras.layers.Dense(120, activation='relu')
    couche6 = tf.keras.layers.Dense(84, activation='relu')
    couche7 = tf.keras.layers.Dense(10, activation='softmax', name="output")
    model.add(tf.keras.Input(shape=(28,28, 1), name="digit"))
    model.add(couche0)
    model.add(couche1)
    model.add(couche2)
    model.add(couche3)
    model.add(couche4)
    model.add(couche5)
    model.add(couche6)
    model.add(couche7)
    return model

Question

After model trained I convert to onnx :

historique = model.fit(train_dataset,steps_per_epoch=train_img.shape[0] // BATCH_SIZE -1,
                           epochs=num_epochs+epoch_save,
                           initial_epoch = epoch_save,
                           validation_data=test_dataset,
                           callbacks=[tensorboard_callback, checkpoint_callback])

input_signature = [tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype, name='digit')]
onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=13)

I have got error :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 196, in <module>
  File "C:\Users\laurent\AppData\Roaming\Python\Python310\site-packages\tf2onnx\convert.py", line 442, in from_keras
    old_out_names = _rename_duplicate_keras_model_names(model)
  File "C:\Users\laurent\AppData\Roaming\Python\Python310\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'?

What's wrong in model or in code?

Further information

tensorflow 2.16.1
tensorflow-addons 0.22.0
tensorflow-cpu 2.10.0
tensorflow-datasets 4.9.3
tensorflow-estimator 2.12.0
tensorflow-intel 2.16.1
tensorflow-io-gcs-filesystem 0.31.0
tensorflow-metadata 1.14.0
tf2onnx 1.16.1

onnx 1.15.0
onnxruntime 1.17.1
onnxruntime-tools 1.7.0
onnxscript 0.1.0.dev20230625 C:\lib\onnxscript
onnxsim 0.4.36

I solved my problem :

model.output_names=['output']

and error vanished...
Where is it written in manual?

Calling code below, it works well without errors you mentioned. So I'm wondering if output_names are lost after calling model.fit(). Could you please check it?

model = my_model_cnn()

input_signature = [tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype, name='digit')]
onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=13)

Ok I Changed my code. Now I wrote


    model = my_model_cnn()
    model_name = "mymnist_cnn"
    input_signature = [tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype, name='digit')]
    # model.output_names=['output']
    onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=13)
    onnx.save(onnx_model, model_name + ".onnx")
    model.compile(optimizer='adam',loss=tf.keras.losses.categorical_crossentropy,metrics=['CategoricalAccuracy'])
....

    historique = model.fit(train_dataset,steps_per_epoch=69000 // BATCH_SIZE -1,
                           epochs=num_epochs+epoch_save,
                           initial_epoch = epoch_save,
                           validation_data=test_dataset,
                           callbacks=[tensorboard_callback, checkpoint_callback])

There is still an error :

2024-03-21 06:56:44.746665: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-03-21 06:56:45.100064: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
WARNING:tensorflow:From C:\Users\laurent\AppData\Roaming\Python\Python310\site-packages\tf2onnx\tf_loader.py:68: The name tf.reset_default_graph is deprecated. Please use tf.compat.v1.reset_default_graph instead.

WARNING:tensorflow:From C:\Users\laurent\AppData\Roaming\Python\Python310\site-packages\tf2onnx\tf_loader.py:72: The name tf.train.import_meta_graph is deprecated. Please use tf.compat.v1.train.import_meta_graph instead.

2024-03-21 06:56:46.739390: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
  File "C:\data\mnist\create_dataset_tf2.py", line 175, in <module>
    onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=13)
  File "C:\Users\laurent\AppData\Roaming\Python\Python310\site-packages\tf2onnx\convert.py", line 442, in from_keras
    old_out_names = _rename_duplicate_keras_model_names(model)
  File "C:\Users\laurent\AppData\Roaming\Python\Python310\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'?


but now I note this warning (sorry I did not post all error message previously)

WARNING:tensorflow:From C:\Users\laurent\AppData\Roaming\Python\Python310\site-packages\tf2onnx\tf_loader.py:72: The name tf.train.import_meta_graph is deprecated. Please use tf.compat.v1.train.import_meta_graph instead.

I work on windows so i use same code with WSL2 no warning and no error model is saved with new code but it is

tensorflow 2.15.1
tensorflow-addons 0.22.0
tensorflow-datasets 4.9.3
tensorflow-estimator 2.15.0
tensorflow-io-gcs-filesystem 0.32.0
tensorflow-metadata 1.14.0
tensorrt 8.5.3.1

tf2onnx 1.16.1
onnx 1.14.0
onnxruntime 1.15.1
onnxsim 0.4.33

python3 create_dataset_tf2.py
2024-03-21 07:00:44.449223: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-03-21 07:00:44.467172: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-03-21 07:00:44.467207: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-03-21 07:00:44.467680: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-03-21 07:00:44.470713: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-03-21 07:00:44.800301: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
2024-03-21 07:00:45.358000: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:45.371909: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:45.371962: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:45.375284: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:45.375335: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:45.375362: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.059354: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.059422: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.059439: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2022] Could not identify NUMA node of platform GPU id 0, defaulting to 0.  Your kernel may not have been built with NUMA support.
2024-03-21 07:00:46.059469: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.059491: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1929] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 21596 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:01:00.0, compute capability: 8.6
2024-03-21 07:00:46.790528: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.790569: I tensorflow/core/grappler/devices.cc:66] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 1
2024-03-21 07:00:46.790925: I tensorflow/core/grappler/clusters/single_machine.cc:361] Starting new session
2024-03-21 07:00:46.791286: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.791333: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.791358: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.791513: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.791544: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2022] Could not identify NUMA node of platform GPU id 0, defaulting to 0.  Your kernel may not have been built with NUMA support.
2024-03-21 07:00:46.791562: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.791572: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1929] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 21596 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:01:00.0, compute capability: 8.6
2024-03-21 07:00:46.815331: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.815379: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.815403: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.815573: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.815745: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2022] Could not identify NUMA node of platform GPU id 0, defaulting to 0.  Your kernel may not have been built with NUMA support.
2024-03-21 07:00:46.815827: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.815849: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1929] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 21596 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:01:00.0, compute capability: 8.6
2024-03-21 07:00:46.818966: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.818999: I tensorflow/core/grappler/devices.cc:66] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 1
2024-03-21 07:00:46.819064: I tensorflow/core/grappler/clusters/single_machine.cc:361] Starting new session
2024-03-21 07:00:46.819249: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.819273: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.819293: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.819394: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.819409: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2022] Could not identify NUMA node of platform GPU id 0, defaulting to 0.  Your kernel may not have been built with NUMA support.
2024-03-21 07:00:46.819437: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node
Your kernel may have been built without NUMA support.
2024-03-21 07:00:46.819454: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1929] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 21596 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:01:00.0, compute capability: 8.6
Epoch 1/2
2024-03-21 07:01:05.241732: I external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:454] Loaded cuDNN version 8904
2024-03-21 07:01:06.098691: I external/local_xla/xla/service/service.cc:168] XLA service 0x5597c1b82fe0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2024-03-21 07:01:06.098714: I external/local_xla/xla/service/service.cc:176]   StreamExecutor device (0): NVIDIA GeForce RTX 3090, Compute Capability 8.6
2024-03-21 07:01:06.101540: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:269] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1711000866.141881    2374 device_compiler.h:186] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.
534/538 [============================>.] - ETA: 0s - loss: 3.3772 - categorical_accuracy: 0.4657
Epoch 1: val_loss improved from 1000000000.00000 to 2.42523, saving model to /mnt/c/tmp/mnistv1/chk/mnist_0000001_.weights.h5
538/538 [==============================] - 24s 8ms/step - loss: 3.3665 - categorical_accuracy: 0.4622 - val_loss: 2.4252 - val_categorical_accuracy: 0.1082
Epoch 2/2
537/538 [============================>.] - ETA: 0s - loss: 1.8717 - categorical_accuracy: 0.5926
Epoch 2: val_loss did not improve from 2.42523
538/538 [==============================] - 5s 9ms/step - loss: 1.8683 - categorical_accuracy: 0.5934 - val_loss: 10.5518 - val_categorical_accuracy: 0.1271

Seeing the same issue on my side after upgrading to tf 2.16.1 from 2.15, with tf2onnx 1.16.1
I can confirm that using model.output_names = ['output'] is a workaround.

However, I later hit another issue similar to this one; tensorflow/tensorflow#63867

Seeing the same issue on my side after upgrading to tf 2.16.1 from 2.15, with tf2onnx 1.16.1 I can confirm that using model.output_names = ['output'] is a workaround.

However, I later hit another issue similar to this one; tensorflow/tensorflow#63867

I am also running into the same problem. model.output_names = ['output'] fixed the first error, and led to the error you linked.

After installing tf 2.15.1 all the errors went away (still using tf2onnx 1.16.1).
Not sure who his to blame here, but there is at least a compatibility issue with tf 2.16

I posted another issue about tflite : problem with tf 2.16 but not with 2.15
Tensorflow or onnx?

I posted another issue about tflite : problem with tf 2.16 but not with 2.15 Tensorflow or onnx?

By the issue you described in TensorFlow repo, I think we should make sure the tflite model is correct and then check if it could be converted to ONNX successfully.

By the issue you described in TensorFlow repo, I think we should make sure the tflite model is correct and then check if it could be converted to ONNX successfully.

I think that there is a bug in TF2.16.1 with consequence on tfonnx and saving tflite model.
For fun model is lenet5 and may be I can call tf2.16.1 oedipe version