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

AttributeError: 'KerasTensor' object has no attribute 'graph'

Open sgreene opened this issue 4 years ago • 28 comments

Basically I tried following this Transfer Learning tutorial . I made minor adjustments like used the Stanford Dog Breed dataset and used softmax in the final layer instead of sigmoid. Everything works fine until I try exporting the model using keras2onnx.

import keras2onnx import onnxruntime onnx_model = keras2onnx.convert_keras(model, model.name)

I get the following errors:

AttributeError                            Traceback (most recent call last)
<ipython-input-62-9f084ca2714f> in <module>
----> 1 onnx_model = keras2onnx.convert_keras(model, model.name)

~\.conda\envs\py37_gpu\lib\site-packages\keras2onnx\main.py in convert_keras(model, name, doc_string, target_opset, channel_first_inputs, debug_mode, custom_op_conversions)
     60     output_dict = {}
     61     if is_tf2 and is_tf_keras:
---> 62         tf_graph = build_layer_output_from_model(model, output_dict, input_names, output_names)
     63     else:
     64         tf_graph = model.outputs[0].graph if is_tf2 else keras.backend.get_session().graph

~\.conda\envs\py37_gpu\lib\site-packages\keras2onnx\_parser_tf.py in build_layer_output_from_model(model, output_dict, input_names, output_names)
    302         return extract_outputs_from_subclassing_model(model, output_dict, input_names, output_names)
    303     else:
--> 304         graph = model.outputs[0].graph
    305         output_names.extend([n.name for n in model.outputs])
    306         output_dict.update(extract_outputs_from_inbound_nodes(model))

AttributeError: 'KerasTensor' object has no attribute 'graph'
print(tf.__version__)
2.5.0-dev20201107
print(tf.keras.__version__)
2.5.0
python -V
Python 3.7.9

sgreene avatar Nov 11 '20 01:11 sgreene

Having the same issue too with keras version 2.4.3 and python 3.8.5

phil288 avatar Nov 12 '20 10:11 phil288

I was only able to export the model after I saved it and used the tf2onnx command line tool.

sgreene avatar Nov 12 '20 20:11 sgreene

did it start to occur on tf 2.4.x or 2.5.x? I will try to find some time to setup the env. and fix the issue.

wenbingl avatar Nov 13 '20 18:11 wenbingl

I'm not sure if this helps, but I was able to run the conversion when I removed a data augmentation layer: layers.experimental.preprocessing.RandomCrop(280,280) @sgreene do you have the same setup maybe?

phil288 avatar Nov 13 '20 19:11 phil288

I do have a data augmentation layer!

data_augmentation = tf.keras.Sequential([
  tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),
  tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),
])

I'll try removing this when I get back to the machine I created it on and see if that makes a difference.

sgreene avatar Nov 13 '20 20:11 sgreene

hey i also get this error with this code:

import tensorflow as tf
import numpy as np
import onnxmltools

encoding_dimension = 2
batch_size = 50
epochs = 300

r = lambda: np.random.randint(1,3) 
x = np.array([[r(),r(),r()] for _ in range(1000)])

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(x[0].shape)),
    tf.keras.layers.Dense(2, activation='relu'),
    tf.keras.layers.Dense(3)        
    ])

model.compile(optimizer='sgd', loss='mse')

model.fit(x, x, epochs=300, batch_size=50, verbose=0)

onnxmltools.convert_keras(model, target_opset=9)

The error:

tf executing eager_mode: True
tf.keras model eager_mode: False

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-891bc6eae7d5> in <module>
      1 import onnxmltools
----> 2 onnxmltools.convert_keras(model, target_opset=9)

~\Anaconda3\envs\experiment\lib\site-packages\onnxmltools\convert\main.py in convert_keras(model, name, initial_types, doc_string, target_opset, targeted_onnx, channel_first_inputs, custom_conversion_functions, custom_shape_calculators, default_batch_size)
     31 
     32     from keras2onnx import convert_keras as convert
---> 33     return convert(model, name, doc_string, target_opset, channel_first_inputs)
     34 
     35 

~\Anaconda3\envs\experiment\lib\site-packages\keras2onnx\main.py in convert_keras(model, name, doc_string, target_opset, channel_first_inputs, debug_mode, custom_op_conversions)
     60     output_dict = {}
     61     if is_tf2 and is_tf_keras:
---> 62         tf_graph = build_layer_output_from_model(model, output_dict, input_names, output_names)
     63     else:
     64         tf_graph = model.outputs[0].graph if is_tf2 else keras.backend.get_session().graph

~\Anaconda3\envs\experiment\lib\site-packages\keras2onnx\_parser_tf.py in build_layer_output_from_model(model, output_dict, input_names, output_names)
    302         return extract_outputs_from_subclassing_model(model, output_dict, input_names, output_names)
    303     else:
--> 304         graph = model.outputs[0].graph
    305         output_names.extend([n.name for n in model.outputs])
    306         output_dict.update(extract_outputs_from_inbound_nodes(model))

AttributeError: 'KerasTensor' object has no attribute 'graph'

lucasfischerFHNW avatar Nov 26 '20 13:11 lucasfischerFHNW

Having the same issue with tf 2.4.0, keras 2.4.3, and keras2onnx 1.7.0.

harupy avatar Dec 14 '20 22:12 harupy

I had the same problem. I resolved the issue by uninstalling tf 2.4.0 and installing tf 2.2.0.

EMichaelC avatar Dec 18 '20 12:12 EMichaelC

face same problem too..Please fix this as soon as possible.

imrankhan441 avatar Dec 23 '20 05:12 imrankhan441

Same problem here with tensorflow 2.4

jemaw avatar Jan 08 '21 16:01 jemaw

@EMichaelC I have successfully used such a workaround: downgrading from TF 2.4.0 to TF 2.3.1 did the trick. Keep in mind that both ONNX and SHAP share the same problem (https://github.com/slundberg/shap/issues/1694).

Possibly, TF's team has recently changed something in their API. Therefore ONNX and SHAP will have some delays in trying to keep up with TF's changes.

The configuration that worked for me: keras2onnx==1.7.0 shap==0.37.0 tensorflow==2.3.1

PaoloRanzi81 avatar Jan 11 '21 14:01 PaoloRanzi81

I'm having the same problem. Dowgrading tensforflow to 2.2.0 or 2.3.1 does not fix the issue. Did anyone manage to solve it?

raulsf6 avatar Jan 21 '21 09:01 raulsf6

In fact, when I downgrade to 2.3.1 the error is not anymore when I try to convert the model, but when I try to save the model:

Traceback (most recent call last):
  File "keras-onnx.py", line 8, in <module>
    keras2onnx.save_model(keras_model, "yolov2-tiny-voc.onnx")
  File "/home/rsanchez/.local/share/virtualenvs/updated-converter-NI6dBiu3/lib/python3.6/site-packages/onnx/__init__.py", line 188, in save_model
    proto = write_external_data_tensors(proto, basepath)
  File "/home/rsanchez/.local/share/virtualenvs/updated-converter-NI6dBiu3/lib/python3.6/site-packages/onnx/external_data_helper.py", line 229, in write_external_data_tensors
    for tensor in _get_all_tensors(model):
  File "/home/rsanchez/.local/share/virtualenvs/updated-converter-NI6dBiu3/lib/python3.6/site-packages/onnx/external_data_helper.py", line 174, in _get_initializer_tensors
    for initializer in onnx_model_proto.graph.initializer:
AttributeError: 'Functional' object has no attribute 'graph'

Any idea?

raulsf6 avatar Jan 21 '21 10:01 raulsf6

changed the configuration to- The configuration that worked for me: keras2onnx==1.7.0 shap==0.37.0 tensorflow==2.3.1 error is coming now- 'KerasTensor' object has no attribute 'graph'

ArpitSisodia288 avatar Feb 15 '21 09:02 ArpitSisodia288

I do have the same issue. Is there any update on this?

mhoeferlin avatar Mar 30 '21 18:03 mhoeferlin

I have the same issue, is there going to be a fix soon? Thanks!

evabalini avatar Apr 29 '21 21:04 evabalini

I'm too Python 3.7.4 TensorFlow 2.5.0

JuanHauara avatar May 24 '21 12:05 JuanHauara

I met the same issue (TF 2.4.0, python 3.7.10, keras2onnx 1.7.0 ), my colleague solved the problem in this way:

  1. add functions which disabled some actions in TF2
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
tf.compat.v1.disable_eager_execution()
  1. after adding the functions mentioned above, different error occur. Refer to this , revise the code

then the model can be converted successfully! Hope this help 😄

cocoyen1995 avatar May 26 '21 03:05 cocoyen1995

Also running into this same issue.

python 3.6.5 tensorflow 2.5.0 keras2onnx 1.7.0 pip 21.1.2

Tried to downgrade tensorflow to 2.2.0, but pip complains: ERROR: No matching distribution found for tensorflow==2.2.0

Any suggestions on a workaround?

mikerosiak avatar Jun 23 '21 13:06 mikerosiak

getting same error in python==3.9.5 tensorflow==2.5.0 keras2onnx==1.7.0 onnxruntime==1.8.0 keras==2.4.3

        onnx_model = keras2onnx.convert_keras(model, model_name, debug_mode=1)
        content = onnx_model.SerializeToString()
        sess = onnxruntime.InferenceSession(content)
ERROR:AutoMLWebApi:500 Internal Server Error: 'KerasTensor' object has no attribute 'graph'
Traceback (most recent call last):
  File "C:\prg\utils.py", line 3858, in uploadONNX
    sess = onnxruntime.InferenceSession(content)
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\keras2onnx\main.py", line 62, in convert_keras
    tf_graph = build_layer_output_from_model(model, output_dict, input_names, output_names)
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\keras2onnx\_parser_tf.py", line 304, in build_layer_output_from_model
    graph = model.outputs[0].graph
AttributeError: 'KerasTensor' object has no attribute 'graph'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\flask\app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\prg\webApi.py", line 96, in getModel
    abort(500, err)
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\werkzeug\exceptions.py", line 940, in abort
    _aborter(status, *args, **kwargs)
  File "C:\Users\prg\Anaconda3\envs\automl_py395elk7120_2\lib\site-packages\werkzeug\exceptions.py", line 923, in __call__
    raise self.mapping[code](*args, **kwargs)
werkzeug.exceptions.InternalServerError: 500 Internal Server Error: 'KerasTensor' object has no attribute 'graph'

Any help is much appreciated Same works perfect in python 3.8.6, tensorflow==2.3.1, keras==2.4.3, keras2onnx==1.7.0, onnxruntime==1.3.0 But looking for in python 3.9.5

hanzigs avatar Jul 01 '21 08:07 hanzigs

I was only able to export the model after I saved it and used the tf2onnx command line tool.

I managed to transform an H5 Keras model to an ONNX model via tf2onnx. Here is my code:

import tensorflow as tf
import onnx
import tf2onnx.convert

with open('model.json') as f:
    model_json = f.read()
model = tf.keras.models.model_from_json(model_json)
model.load_weights('model.h5'))

onnx_model, _ = tf2onnx.convert.from_keras(model)

onnx.save(onnx_model, 'model.onnx')

By the way, this issue seems to be one of most encountered issue, as per the number of similar issues (#662, #683, #724).

yan12125 avatar Jul 01 '21 08:07 yan12125

Thank you @yan12125, that worked for me!

WeberJulian avatar Jul 29 '21 10:07 WeberJulian

Thank you @yan12125, that worked perfect for me in python==3.9.5, tensorflow==2.5.0, keras2onnx==1.7.0, onnxruntime==1.8.0, keras==2.4.3, tf2onnx==1.9.1

hanzigs avatar Jul 30 '21 01:07 hanzigs

Thanks @cocoyen1995 !

Z223I avatar Jul 30 '21 19:07 Z223I

It has been almost two months since the post stating the fix has been merged.

It is still a problem. I did an install on July 29 and still get the same error.

Z223I avatar Jul 30 '21 19:07 Z223I

I'm getting a strange error from onnxruntime

Below code works perfect in standalone (python==3.9.5, tensorflow==2.5.0, keras2onnx==1.7.0, onnxruntime==1.8.0, keras==2.4.3, tf2onnx==1.9.1)

autoKeras_model = StructuredDataClassifier(max_trials=10)
autoKeras_model.fit(x=X_train, y=y_train, validation_data=(X_valid, y_valid), epochs=100, verbose=1)
ExportedautoKeras_model = autoKeras_model.export_model()

onnx_model, _ = tf2onnx.convert.from_keras(ExportedautoKeras_model )
content = onnx_model.SerializeToString()
sess = onnxruntime.InferenceSession(content)

Same code inside Flask App, InferenceSession throws error

sess = onnxruntime.InferenceSession(content)
  File "C:\Users\plg\Anaconda3\envs\automl04augpy395elk7120\lib\site-packages\onnxruntime\capi\onnxruntime_inference_collection.py", line 283, in __init__
    self._create_inference_session(providers, provider_options, disabled_optimizers)
  File "C:\Users\plg\Anaconda3\envs\automl04augpy395elk7120\lib\site-packages\onnxruntime\capi\onnxruntime_inference_collection.py", line 312, 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:model/multi_category_encoding/AsString : No Op registered for AsString with domain_version of 9

I am mainly after input_name

input_name = sess.get_inputs()[0].name

Is there a way to extract the "input_name" or solve this error Any help please, Thanks

hanzigs avatar Aug 04 '21 01:08 hanzigs

For getting input_name

model.graph.input[0].name 

above works, But If that's a converter bug, how should I find the correct opset? (I have tried opset from 9 to 13, all throws error) then why that error not raised in standalone run?

hanzigs avatar Aug 04 '21 23:08 hanzigs

issue solved with

pip uninstall tf2onnx
pip install git+https://github.com/onnx/tensorflow-onnx@tom/keras_hash_tables

hanzigs avatar Aug 09 '21 02:08 hanzigs