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

Keras2onnx error KeyError: <function softsign at 0x00000207378BED38>

Open hanzigs opened this issue 4 years ago • 1 comments

Hi, Using versions tensorflow 2.3.0, keras2onnx 1.7.0 keras 2.4.3

from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.keras.models import Sequential #initialise NN as sequence of layers
from tensorflow.keras.layers import Dense, Dropout #build ANN
from tensorflow.keras.constraints import MaxNorm
from tensorflow.keras.optimizers import SGD 

I have my keras model as

    Keras_model = Sequential()
    Keras_model.add(Dense(neurons_1L,activation=activation, kernel_initializer=init_mode, kernel_constraint=MaxNorm(weight_constraint), input_shape=(num_cols,))) 
    Keras_model.add(Dropout(rate=dropout_rate))
    Keras_model.add(Dense(neurons_2L,activation=activation, kernel_initializer=init_mode, kernel_constraint=MaxNorm(weight_constraint)))
    Keras_model.add(Dropout(rate=dropout_rate))
    Keras_model.add(Dense(1, activation='sigmoid', kernel_initializer=init_mode)) 
    Keras_model.compile(optimizer = optimizer, loss = 'binary_crossentropy', metrics = ['accuracy'])
    Keras_model.fit(X_train, y_train, validation_data=(X_valid, y_valid), batch_size = batch_size, epochs = epoch, verbose = 2)

where all the parameters were passed While I convert to onnx

        onnx_model = keras2onnx.convert_keras(Keras_model, model_name, debug_mode=1)
        content = onnx_model.SerializeToString()
        sess = onnxruntime.InferenceSession(content)
        input_name = sess.get_inputs()[0].name
        label_name = sess.get_outputs()[0].name

I get below error

 onnx_model = keras2onnx.convert_keras(model, model_name, debug_mode=1)
  File "C:\Users\PG\Anaconda3\lib\site-packages\keras2onnx\main.py", line 83, in convert_keras
    return convert_topology(topology, name, doc_string, target_opset, channel_first_inputs)
  File "C:\Users\PG\Anaconda3\lib\site-packages\keras2onnx\topology.py", line 322, in convert_topology
    cvt(scope, operator, container)
  File "C:\Users\PG\Anaconda3\lib\site-packages\keras2onnx\ke2onnx\dense.py", line 43, in convert_keras_dense
    activation_process(scope, operator, container, biased_tensor_name)
  File "C:\Users\PG\Anaconda3\lib\site-packages\keras2onnx\ke2onnx\common.py", line 24, in activation_process
    apply_activation_function = activation_map[operator.raw_operator.activation]
KeyError: <function softsign at 0x00000207378BED38>

May I have some help please,

the activation parameter is passed as

activation = hyperparameters.get('activation')

which is returned as 'Adam'

This issue https://github.com/onnx/keras-onnx/issues/200 solution didn't help

This link have a solution, not sure about that https://qiita.com/natsutan/items/6ba14753236f386ec00b

Thanks

hanzigs avatar Aug 11 '20 23:08 hanzigs

The solution in the link https://qiita.com/natsutan/items/6ba14753236f386ec00b worked Splitted the Activation separate, the error went

    Keras_model.add(Dense(neurons_1L,kernel_initializer=init_mode, kernel_constraint=MaxNorm(weight_constraint), input_shape=(num_cols,))) 
    Keras_model.add(tensorflow.keras.layers.Activation(activation=activation))
    Keras_model.add(Dropout(rate=dropout_rate))
    Keras_model.add(Dense(neurons_2L,kernel_initializer=init_mode, kernel_constraint=MaxNorm(weight_constraint)))
    Keras_model.add(tensorflow.keras.layers.Activation(activation=activation))

hanzigs avatar Aug 13 '20 01:08 hanzigs