Skip to content Skip to sidebar Skip to footer

How To Convert Tensorflow 2.* Trained With Keras Model To .onnx Format?

I use the Python 3.7.4 with TensorFlow 2.0 and Keras 2.2.4-tf to train my own CNN model. Everything goes fine. I can use e.g. model.save(my_model), and then use it in other Python

Solution 1:

The latest version of keras2onnx (in github master) supports TensorFlow 2.

You can install it like this:

pip install git+https://github.com/microsoft/onnxconverter-common
pip install git+https://github.com/onnx/keras-onnx

Solution 2:

You need to create a file which can hold ONNX object. Visit https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb

import tensorflow as tf
import onnx
import keras2onnx
model = tf.keras.models.load_model('Model.h5')
onnx_model = keras2onnx.convert_keras(model, model.name)

file = open("Sample_model.onnx", "wb")
file.write(onnx_model.SerializeToString())
file.close()

Post a Comment for "How To Convert Tensorflow 2.* Trained With Keras Model To .onnx Format?"