onnx / keras-onnx

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

Unrecognized attribute: axes for operator Squeeze/RecduceSum #708

Open chan4cc opened 3 years ago

chan4cc commented 3 years ago

System information

To Reproduce

  1. convert h5 model created by tf.keras(tf1.12) to ONNX with code:
    onnx_model = keras2onnx.convert_keras(model, model.name, target_opset=13, debug_mode=False)
  2. load onnx_model into sess:
    sess = onnxruntime.InferenceSession(onnx_model.SerializeToString())
  3. then step 2 report error like this:
    Traceback (most recent call last):
    File "trans_model_format.py", line 85, in <module>
    keras_model_to_onnx(file_path)
    File "trans_model_format.py", line 49, in keras_model_to_onnx
    sess = onnxruntime.InferenceSession(onnx_model.SerializeToString())
    File "/home/zhanshen/.local/lib/python3.6/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 280, in __init__
    self._create_inference_session(providers, provider_options)
    File "/home/zhanshen/.local/lib/python3.6/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 309, in _create_inference_session
    sess = C.InferenceSession(session_options, self._model_bytes, False, self._read_config_from_model)
    onnxruntime.capi.onnxruntime_pybind11_state.InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : This is an invalid model. Error in Node:cos_sim/einsum/Shape:0_squeeze : Unrecognized attribute: axes for operator Squeeze

    In my model build code with tf.keras, there is a tf.einsum op for calculating cosine sim. And if I use tf.keras.layers.Dot layer to calculate cosine sim, the h5 model was also converted successfully and report cos_sim/dot/l2_normalize_1/Sum_reduce_min : Unrecognized attribute: axes for operator ReduceSum when loading onnx_model to sess

chan4cc commented 3 years ago

Before this, I opened microsoft/onnxruntime#7338.

TruscaPetre commented 3 years ago

This converter has worked for me. Although you have to train your model in tensorflow version 2.2, it might not work in other versions. https://github.com/onnx/onnxmltools