onnx / keras-onnx

Convert tf.keras/Keras models to ONNX
Apache License 2.0
379 stars 110 forks source link

Failure when converting Dense layer with hard_sigmoid activation #718

Open yrjok opened 3 years ago

yrjok commented 3 years ago

Dear maintainers

When attempting to convert a model containing a Dense layer with a hard_sigmoid activation I stumbled upon the following error:

ValueError: Failed to create ONNX node. Undefined attribute pair (alpha, None)

The conversion seems works fine when using other activation functions however (e.g. relu, sigmoid).

Traceback:

Traceback (most recent call last):
  File "convert.py", line 10, in <module>
    onnx_model = keras2onnx.convert_keras(create_model())
  File "/home/yrjo/.local/lib/python3.7/site-packages/keras2onnx/main.py", line 83, in convert_keras
    return convert_topology(topology, name, doc_string, target_opset, channel_first_inputs)
  File "/home/yrjo/.local/lib/python3.7/site-packages/keras2onnx/topology.py", line 322, in convert_topology
    cvt(scope, operator, container)
  File "/home/yrjo/.local/lib/python3.7/site-packages/keras2onnx/ke2onnx/dense.py", line 43, in convert_keras_dense
    activation_process(scope, operator, container, biased_tensor_name)
  File "/home/yrjo/.local/lib/python3.7/site-packages/keras2onnx/ke2onnx/common.py", line 33, in activation_process
    apply_activation_function(scope, biased_tensor_name, operator.outputs[0].full_name, container)
  File "/home/yrjo/.local/lib/python3.7/site-packages/onnxconverter_common/onnx_ops.py", line 536, in apply_hard_sigmoid
    alpha=alpha, beta=beta)
  File "/home/yrjo/.local/lib/python3.7/site-packages/onnxconverter_common/onnx_ops.py", line 32, in _apply_unary_operation
    container.add_node(op_type, input_name, output_name, op_version=op_version, **attrs)
  File "/home/yrjo/.local/lib/python3.7/site-packages/keras2onnx/common/interim.py", line 123, in add_node
    raise ValueError('Failed to create ONNX node. Undefined attribute pair (%s, %s) found' % (k, v))
ValueError: Failed to create ONNX node. Undefined attribute pair (alpha, None) found

Steps to reproduce:

The following code reproduces the error:

import tensorflow as tf
import keras2onnx

def create_model():
    input_shape = (1, 256, 256, 3)
    inputs = tf.keras.layers.Input(input_shape)
    outputs = tf.keras.layers.Dense(1, activation=tf.keras.activations.hard_sigmoid)(inputs)
    return tf.keras.Model(inputs=inputs, outputs=outputs)

model = create_model()
onnx_model = keras2onnx.convert_keras(model)

Using