question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Having issue in converting autokeras functional model to onnx?

See original GitHub issue

Hi, I am trying to convert autokeras model to onnx, i get error

from autokeras import StructuredDataClassifier
model = StructuredDataClassifier(max_trials=100)
model.fit(x=X_train, y=y_train, validation_data=(X_valid, y_valid), epochs=1000, verbose=1)
autoKeras_model = model.export_model()
autoKeras_model.summary()

Model: "functional_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 18)]              0         
_________________________________________________________________
multi_category_encoding (Mul (None, 18)                0         
_________________________________________________________________
dense (Dense)                (None, 32)                608       
_________________________________________________________________
re_lu (ReLU)                 (None, 32)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                1056      
_________________________________________________________________
re_lu_1 (ReLU)               (None, 32)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 33        
_________________________________________________________________
classification_head_2 (Activ (None, 1)                 0         
=================================================================
Total params: 1,697
Trainable params: 1,697
Non-trainable params: 0
_________________________________________________________________

converting to onnx

import onnxruntime
import keras2onnx
onnx_model = keras2onnx.convert_keras(autoKeras_model, "autokeras", debug_mode=1)

I get error

tf executing eager_mode: True
INFO:keras2onnx:tf executing eager_mode: True
tf.keras model eager_mode: False
INFO:keras2onnx:tf.keras model eager_mode: False
Model: "functional_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 18)]              0         
_________________________________________________________________
multi_category_encoding (Mul (None, 18)                0         
_________________________________________________________________
dense (Dense)                (None, 32)                608       
_________________________________________________________________
re_lu (ReLU)                 (None, 32)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                1056      
_________________________________________________________________
re_lu_1 (ReLU)               (None, 32)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 33        
_________________________________________________________________
classification_head_2 (Activ (None, 1)                 0         
=================================================================
Total params: 1,697
Trainable params: 1,697
Non-trainable params: 0
_________________________________________________________________
None
Traceback (most recent call last):

  File "<ipython-input-76-0567e6de6858>", line 1, in <module>
    onnx_model = keras2onnx.convert_keras(ExportedautoKeras_model, model_name, debug_mode=1)

  File "C:\Users\Pe\Anaconda3\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\Pe\Anaconda3\lib\site-packages\keras2onnx\_parser_tf.py", line 302, in build_layer_output_from_model
    return extract_outputs_from_subclassing_model(model, output_dict, input_names, output_names)

  File "C:\Users\Pe\Anaconda3\lib\site-packages\keras2onnx\_parser_tf.py", line 264, in extract_outputs_from_subclassing_model
    concrete_func, lower_control_flow=True)

  File "C:\Users\Pe\Anaconda3\lib\site-packages\keras2onnx\_graph_cvt.py", line 437, in convert_variables_to_constants_v2
    tensor_data = _get_tensor_data(func)

  File "C:\Users\Pe\Anaconda3\lib\site-packages\keras2onnx\_graph_cvt.py", line 209, in _get_tensor_data
    data = val_tensor.numpy()

  File "C:\Users\Pe\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1063, in numpy
    maybe_arr = self._numpy()  # pylint: disable=protected-access

  File "C:\Users\Pe\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1031, in _numpy
    six.raise_from(core._status_to_exception(e.code, e.message), None)  # pylint: disable=protected-access

  File "<string>", line 3, in raise_from

InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array.

May i have some help Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
hanzigscommented, Nov 26, 2021

@torronen Following combinations working fine for me

pip install tensorflow==2.5.0
pip install keras==2.4.3
pip install keras-tuner==1.0.3
pip install autokeras==1.0.16
pip install tf2onnx==1.9.2
pip install keras2onnx==1.7.0
pip install onnxruntime==1.8.0
pip install onnxmltools==1.7.0

model = StructuredDataClassifier(...........)
akmodel.fit(......)
onnx_model, _ = tf2onnx.convert.from_keras(model)
0reactions
torronencommented, Nov 27, 2021

Thank you @hanzigs , that solved the issue.

I believe my problem was using keras2onnx instead of tf2onnx. Package versioning may have had something to do with it, but probably not.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Convert Your Keras Model to ONNX - Medium
There are some points for converting Keras model to ONNX: Remember to import onnx and keras2onnx packages. keras2onnx.convert_keras() function ...
Read more >
Export Model - AutoKeras
You can easily export your model the best model found by AutoKeras as a Keras Model. ... All the tasks and the AutoModel...
Read more >
What I Like and Don't Like About AutoML as a Data Scientist
As for ONNX, it also proved problematic to convert a scikit-learn ensemble pipeline to ONNX. Often AutoML candidate models grow very complex, and...
Read more >
Unable to convert .h5 model to ONNX for inferencing through ...
Try converting it to saved model format and then to onnx. import numpy as np import tensorflow as tf from tensorflow import keras...
Read more >
Available CRAN Packages By Name
base.rms, Convert Regression Between Base Function and 'rms' Package. base64, Base64 Encoder and Decoder ... BTM, Biterm Topic Models for Short Text.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found