Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

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

Open
sgreene opened this issue Nov 11, 2020 · 28 comments
Open

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

sgreene opened this issue Nov 11, 2020 · 28 comments

Comments

@sgreene
Copy link

sgreene commented Nov 11, 2020

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

@phil288
Copy link

phil288 commented Nov 12, 2020

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

@sgreene
Copy link
Author

sgreene commented Nov 12, 2020

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

@wenbingl
Copy link
Member

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.

@phil288
Copy link

phil288 commented Nov 13, 2020

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?

@sgreene
Copy link
Author

sgreene commented Nov 13, 2020

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.

@lucasfischerFHNW
Copy link

lucasfischerFHNW commented Nov 26, 2020

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'

@harupy
Copy link

harupy commented Dec 14, 2020

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

@EMichaelC
Copy link

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

@imrankhan441
Copy link

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

@jemaw
Copy link

jemaw commented Jan 8, 2021

Same problem here with tensorflow 2.4

@PaoloRanzi81
Copy link

@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 (shap/shap#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

@raulsf6
Copy link

raulsf6 commented Jan 21, 2021

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
Copy link

raulsf6 commented Jan 21, 2021

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?

@ArpitSisodia288
Copy link

ArpitSisodia288 commented Feb 15, 2021

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'

@mhoeferlin
Copy link

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

@evabalini
Copy link

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

@JuanHauara
Copy link

I'm too
Python 3.7.4
TensorFlow 2.5.0

@cocoyen1995
Copy link

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 😄

@mikerosiak
Copy link

mikerosiak commented Jun 23, 2021

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?

@hanzigs
Copy link

hanzigs commented Jul 1, 2021

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

@yan12125
Copy link
Contributor

yan12125 commented Jul 1, 2021

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 pushed a commit to EMCLab-Sinica/Stateful-CNN that referenced this issue Jul 1, 2021
The former is incompatible with Tensorflow 2.4+ [1]

[1] onnx/keras-onnx#651
@WeberJulian
Copy link

Thank you @yan12125, that worked for me!

@hanzigs
Copy link

hanzigs commented Jul 30, 2021

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

@Z223I
Copy link

Z223I commented Jul 30, 2021

Thanks @cocoyen1995 !

@Z223I
Copy link

Z223I commented Jul 30, 2021

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.

@hanzigs
Copy link

hanzigs commented Aug 4, 2021

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
Copy link

hanzigs commented Aug 4, 2021

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
Copy link

hanzigs commented Aug 9, 2021

issue solved with

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests