• This repository has been archived on 13/Oct/2021
  • Stars
    star
    379
  • Rank 109,384 (Top 3 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert tf.keras/Keras models to ONNX

keras2onnx

We stopped active development of keras2onnx and keras2onnx is now frozen to tf-2.3 and onnx-1.10.

To convert your Keras models you can head over to tf2onnx which can convert Tensorflow, Keras, Tflite and Tensorflow.js models. All keras2onnx unit tests have been added to the tf2onnx ci pipeline to make sure there are no avoidable regressions. The tf2onnx api tf2onnx.convert.from_keras() is similar to the keras2onnx api and we hope transition is painless.

You can find a simple tutorial how to convert keras models using tf2onnx here.

If you run into issue or need help with the transition, please open issue against tf2onnx here.

Linux Windows
keras.io Build Status Build Status
tf.keras Build Status Build Status

Introduction

The keras2onnx model converter enables users to convert Keras models into the ONNX model format. Initially, the Keras converter was developed in the project onnxmltools. keras2onnx converter development was moved into an independent repository to support more kinds of Keras models and reduce the complexity of mixing multiple converters.

Most of the common Keras layers have been supported for conversion. Please refer to the Keras documentation or tf.keras docs for details on Keras layers.

Windows Machine Learning (WinML) users can use WinMLTools which wrap its call on keras2onnx to convert the Keras models. If you want to use the keras2onnx converter, please refer to the WinML Release Notes to identify the corresponding ONNX opset number for your WinML version.

keras2onnx has been tested on Python 3.5 - 3.8, with tensorflow 1.x/2.0 - 2.2 (CI build). It does not support Python 2.x.

Install

You can install latest release of Keras2ONNX from PyPi:

pip install keras2onnx

or install from source:

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

Before running the converter, please notice that tensorflow has to be installed in your python environment, you can choose tensorflow/tensorflow-cpu package(CPU version) or tensorflow-gpu(GPU version)

Notes

Keras2ONNX supports the new Keras subclassing model which was introduced in tensorflow 2.0 since the version 1.6.5. Some typical subclassing models like huggingface/transformers have been converted into ONNX and validated by ONNXRuntime.

Since its version 2.3, the multi-backend Keras (keras.io) stops the support of the tensorflow version above 2.0. The auther suggests to switch to tf.keras for the new features.

Multi-backend Keras and tf.keras:

Both Keras model types are now supported in the keras2onnx converter. If in the user python env, Keras package was installed from Keras.io and tensorflow package version is 1.x, the converter converts the model as it was created by the keras.io package. Otherwise, it will convert it through tf.keras.

If you want to override this behaviour, please specify the environment variable TF_KERAS=1 before invoking the converter python API.

Development

Keras2ONNX depends on onnxconverter-common. In practice, the latest code of this converter requires the latest version of onnxconverter-common, so if you install this converter from its source code, please install the onnxconverter-common in source code mode before keras2onnx installation.

Validated pre-trained Keras models

Most Keras models could be converted successfully by calling keras2onnx.convert_keras, including CV, GAN, NLP, Speech and etc. See the tutorial here. However some models with a lot of custom operations need custom conversion, the following are some examples, like YOLOv3, and Mask RCNN.

Scripts

It will be useful to convert the models from Keras to ONNX from a python script. You can use the following API:

import keras2onnx
keras2onnx.convert_keras(model, name=None, doc_string='', target_opset=None, channel_first_inputs=None):
    # type: (keras.Model, str, str, int, []) -> onnx.ModelProto
    """
    :param model: keras model
    :param name: the converted onnx model internal name
    :param doc_string:
    :param target_opset:
    :param channel_first_inputs: A list of channel first input.
    :return:
    """

Use the following script to convert keras application models to onnx, and then perform inference:

import numpy as np
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input
import keras2onnx
import onnxruntime

# image preprocessing
img_path = 'street.jpg'   # make sure the image is in img_path
img_size = 224
img = image.load_img(img_path, target_size=(img_size, img_size))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

# load keras model
from keras.applications.resnet50 import ResNet50
model = ResNet50(include_top=True, weights='imagenet')

# convert to onnx model
onnx_model = keras2onnx.convert_keras(model, model.name)

# runtime prediction
content = onnx_model.SerializeToString()
sess = onnxruntime.InferenceSession(content)
x = x if isinstance(x, list) else [x]
feed = dict([(input.name, x[n]) for n, input in enumerate(sess.get_inputs())])
pred_onnx = sess.run(None, feed)

The inference result is a list which aligns with keras model prediction result model.predict(). An alternative way to load onnx model to runtime session is to save the model first:

temp_model_file = 'model.onnx'
keras2onnx.save_model(onnx_model, temp_model_file)
sess = onnxruntime.InferenceSession(temp_model_file)

Contribute

We welcome contributions in the form of feedback, ideas, or code.

License

Apache License v2.0

More Repositories

1

onnx

Open standard for machine learning interoperability
Python
16,640
star
2

models

A collection of pre-trained, state-of-the-art models in the ONNX format
Jupyter Notebook
7,054
star
3

tutorials

Tutorials for creating and using ONNX models
Jupyter Notebook
3,175
star
4

onnx-tensorrt

ONNX-TensorRT: TensorRT backend for ONNX
C++
2,769
star
5

tensorflow-onnx

Convert TensorFlow, Keras, Tensorflow.js and Tflite models to ONNX
Jupyter Notebook
2,224
star
6

onnx-tensorflow

Tensorflow Backend for ONNX
Python
1,238
star
7

onnxmltools

ONNXMLTools enables conversion of models to ONNX
Python
936
star
8

onnx-mlir

Representation and Reference Lowering of ONNX Models in MLIR Compiler Infrastructure
C++
686
star
9

optimizer

Actively maintained ONNX Optimizer
C++
579
star
10

sklearn-onnx

Convert scikit-learn models and pipelines to ONNX
Python
508
star
11

onnx-coreml

ONNX to Core ML Converter
Python
389
star
12

onnx-caffe2

Caffe2 implementation of Open Neural Network Exchange (ONNX)
Python
168
star
13

onnx-docker

Dockerfiles and scripts for ONNX container images
Jupyter Notebook
129
star
14

onnx-mxnet

ONNX model format support for Apache MXNet
Python
96
star
15

turnkeyml

The AI insights toolchain
Python
45
star
16

onnx-r

R Interface to Open Neural Network Exchange (ONNX)
R
42
star
17

backend-scoreboard

Scoreboard for ONNX Backend Compatibility
Python
23
star
18

onnx.github.io

Code of the official webpage of onnx
HTML
22
star
19

steering-committee

Notes and artifacts from the ONNX steering committee
Jupyter Notebook
22
star
20

working-groups

Repository for ONNX working group artifacts
Jupyter Notebook
20
star
21

sigs

Repository for ONNX SIG artifacts
19
star
22

onnx-xla

XLA integration of Open Neural Network Exchange (ONNX)
C++
18
star
23

wheel-builder

Utils for building and publishing ONNX wheels
Shell
7
star
24

onnx-cntk

Python
6
star