onnx / keras-onnx

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

Can't convert CRF model with input dtype variant #732

Open saeedehkhoshgoftar opened 3 years ago

saeedehkhoshgoftar commented 3 years ago

Hi,

I am trying to accelerate a NLP pipeline using keras-onnx. The Keras model consisting a CRF layer (tf2crf) on top of the BiLSTM layer. I faced a following error: ValueError: Unable to find out a correct type for tensor type = <dtype: 'variant'> of sequential/crf/scan/while/exit/_103:0

I would appreciate it if you could direct me how to run the model via keras_onnx

def build_model():
    model = tf.keras.models.Sequential()

    model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(internal_unit, return_sequences=True), input_shape=(seq_length, vector_dim)))
    if drop_out_rate >0 and drop_out_rate <1:
        model.add(tf.keras.layers.Dropout(drop_out_rate))
    model.add(tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(num_tags, activation=activation)))
    crf = CRF(num_tags)
    model.add(crf)
   model.compile (loss=crf.loss, metrics=[crf.accuracy], optimizer=model_optimizer)

    return model
model = build_model()
model.load_weights("model.h5")
onnx_model = keras2onnx.convert_keras(model, model.name)

The output is: ValueError: Unable to find out a correct type for tensor type = <dtype: 'variant'> of sequential_1/crf_1/scan/while/exit/_103:0

Is it an onnx error or am I missing something?

Versions:

Python: 3.7.10 Tensorflow: 2.3.0 Keras: 2.2.4
tf2crf: 0.1.13

Thanks!