keras-onnx
keras-onnx copied to clipboard
KerasTensor object has no attribute 'graph'
I am using Ubuntu 18 and Tensorflow 2.5, ONNX 1.7, and Keras 2.4. I'm trying to convert Keras code to Onnx. I have Python code I tried to convert it to ONNX with this:
from tensorflow.keras.models import load_model
import onnx
import keras2onnx
import tensorflow as tf
onnx_model_name = 'name.onnx'
model = load_model('name.keras')
onnx_model = keras2onnx.convert_keras(model, model.name)
onnx.save_model(onnx_model, onnx_model_name)
but it gave the error
File "/home/me/anaconda3/lib/python3.8/site-packages/keras2onnx/main.py", line 82, in convert_keras
tf_graph = build_layer_output_from_model(model, output_dict, input_names, output_names)
File "/home/me/anaconda3/lib/python3.8/site-packages/keras2onnx/_parser_tf.py", line 308, in build_layer_output_from_model
graph = model.outputs[0].graph
AttributeError: 'KerasTensor' object has no attribute 'graph'
maybe I'm getting this error because ONNX Only works with Tensorflow 1 instead of 2? can anyone help with this?
keras2onnx works for both tf 1 and 2. For tf2, when you load the model, it loads as tf.keras model by default. Is your model a keras one or tf.keras one? Can you try keras2onnx from master?
It's keras and not tf.keras, right? How can I tell if its tf.keras?
I get the same error if I do from tensorflow.keras.models import load_model
or from keras.models import load_model
or model=tf.keras.models.load_model(..)
and if I uninstall keras2onnx and install it with:
pip install -U git+https://github.com/microsoft/onnxconverter-common
pip install -U git+https://github.com/onnx/keras-onnx
and then attempt to load the keras model again, I get the same error
can you answer my question please?
Facing same issue:- Python 3.7.7 tensorflow 2.4.0 onnx 1.8.0 2.4.3
Error Message
AttributeError Traceback (most recent call last)
\python\python37\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
\python\python37\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
\python\python37\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'
Code :
`#!/usr/bin/env python
coding: utf-8
In[1]:
import keras
In[2]:
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() print(train_images.shape) print(train_labels.shape) print(test_images.shape) print(test_labels.shape)
In[3]:
import matplotlib.pyplot as plt
n = 10
fig, ax = plt.subplots(1, n, figsize=(10, 2)) for idx, image in enumerate(train_images[:10]): ax[idx].imshow(image, cmap=plt.cm.Greys) ax[idx].set_xticks([]) ax[idx].set_yticks([]) ax[idx].set_title(train_labels[idx], fontsize=18)
plt.show()
In[4]:
Flatten images
train_images = train_images.reshape((60000, 28 * 28))
Normalise images
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28)) test_images = test_images.astype('float32') / 255
In[5]:
from keras.utils import to_categorical
train_labels = to_categorical(train_labels) test_labels = to_categorical(test_labels)
In[6]:
from keras import models from keras import layers
nn = models.Sequential() nn.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,))) nn.add(layers.Dense(10, activation='softmax'))
nn.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
In[7]:
nn.fit(train_images, train_labels, epochs=5, batch_size=128)
In[8]:
test_loss, test_accuracy = nn.evaluate(test_images, test_labels)
print('test_acc:', test_accuracy)
In[9]:
import onnxmltools
onnx_model = onnxmltools.convert_keras(nn)
onnxmltools.utils.save_model(onnx_model, 'keras_example.onnx')
In[ ]:
`
Currently we don't support tensorflow 2.4.0, is it possible you downgrade to tf 2.3.0?
Any progress on this issue?
Has anyone a solution for this issue? I've a Mac M1 and can only use Tensorflow 2.4
You have to downgrade your version of tensorflow. I was having this problem in google colab where the default version is currently 2.4.0. Try: pip install tensorflow==2.2.0
That doesn't really work. On mac M1 only TF 2.4. is available from Apple.