Skip to content

Cannot start inference session with tf.keras model converted to onnx #2806

Description

@mxdillon

Describe the bug
When training a simple tf.keras model and converting to onnx with tf2onnx,
sess = rt.InferenceSession("model.onnx") gives the error

onnxruntime.capi.onnxruntime_pybind11_state.NotImplemented: [ONNXRuntimeError] : 9 : NOT_IMPLEMENTED : Could not find an implementation for the node sequential/dense/Relu:Relu(6)

This is similar to #967, but I am specifying --opset 10 in my conversion

Urgency

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS

  • ONNX Runtime installed from (source or binary): pip (whl)

  • ONNX Runtime version: 1.1.0

  • Python version: 3.7.2 64-bit

  • Visual Studio version (if applicable): Version: 1.41.1
    Commit: 26076a4de974ead31f97692a0d32f90d735645c0
    Date: 2019-12-18T14:57:51.166Z
    Electron: 6.1.5
    Chrome: 76.0.3809.146
    Node.js: 12.4.0
    V8: 7.6.303.31-electron.0
    OS: Darwin x64 19.2.0

  • GCC/Compiler version (if compiling from source): na

  • CUDA/cuDNN version: na

  • GPU model and memory: na

To Reproduce
Run the code below using the package versions specified

onnx==1.6.0
onnxruntime==1.1.0
tensorflow==1.15.0
tf2onnx==1.5.4

import os
import tf2onnx
import tensorflow as tf
import onnx
import onnxruntime

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]

train_ds = (
    tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32)
)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)

model = tf.keras.models.Sequential(
    [
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128, activation="relu"),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(10, activation="softmax"),
    ]
)

model.compile(
    optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
)

history = model.fit(train_ds, epochs=1)

tf.keras.experimental.export_saved_model(model, "./saved_model")
command = f"python -m tf2onnx.convert --opset 10 --fold_const --verbose --saved-model ./saved_model --output model.onnx"
os.system(command)

# load model for inference
model = onnx.load("model.onnx")

onnx.checker.check_model(model)

sess = rt.InferenceSession("model.onnx")

Expected behavior
I expect the inference session to start.
I am using onnxruntime==1.1.0 and tf2onnx==1.5.4 which are listed as compatible in https://github.com/Microsoft/onnxruntime/blob/master/docs/Versioning.md#tool-compatibility
The process doesn't throw an error at 'onnx.checker.check_model(model)' so there isn't an issue with the onnx model.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestrequest for unsupported feature or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions