1 Star 0 Fork 0

mayanhui/keras-onnx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

keras2onnx

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.

All Keras layers have been supported for conversion using keras2onnx since ONNX opset 7. Please refer to the Keras documentation for details on Keras layers. The keras2onnx converter also supports the lambda/custom layer by working with the tf2onnx converter which is embedded directly into the source tree to avoid version conflicts and installation complexity.

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

keras2onnx has been tested on Python 3.5, 3.6, and 3.7, with tensorflow 1.x (CI build). It does not support Python 2.x.

Notes

tf.keras v.s. keras.io

Both Keras model types are now supported in the keras2onnx converter. If the user's Keras package was installed from Keras.io, 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.

Usage

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

Validated pre-trained Keras models

We converted successfully for all the keras application models, and several other pretrained models. See below:

Model Name Category Notes
Xception Computer Vision
VGG16, VGG19 Computer Vision
ResNet50 Computer Vision
InceptionV3, InceptionResNetV2 Computer Vision
MobileNet, MobileNetV2 Computer Vision
DenseNet121, DenseNet169, DenseNet201 Computer Vision
NASNetMobile, NASNetLarge Computer Vision
FCN, FCN-Resnet Computer Vision
PSPNet Computer Vision
Segnet, VGG-Segnet Computer Vision
UNet Computer Vision
LPCNet Speech
Temporal Convolutional Network Time sequence
ACGAN (Auxiliary Classifier GAN) GAN
Adversarial Autoencoder GAN
BGAN (Boundary-Seeking GAN) GAN
BIGAN (Bidirectional GAN) GAN
CGAN (Conditional GAN) GAN
Coupled GAN GAN
Deep Convolutional GAN GAN
DualGAN GAN
Generative Adversarial Network GAN
InfoGAN GAN
LSGAN GAN
Pix2Pix GAN
Semi-Supervised GAN GAN
Super-Resolution GAN GAN
Wasserstein GAN GAN
Wasserstein GAN GP GAN
keras-team examples Text and Sequence addition_rnn, babi_rnn, imdb_bidirectional_lstm, imdb_cnn_lstm, imdb_lstm, lstm_text_generation, reuters_mlp

The following models need customed conversion, see the instruction column.

Model Name Category Instruction
YOLOv3 Computer Vision Readme
Mask RCNN Computer Vision Readme
Context-Conditional GAN GAN Unit test
Cycle GAN GAN Unit test
Disco GAN GAN Unit test
PixelDA (Domain Adaptation) GAN Unit test

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)

An alternative way to load onnx model to runtime session is to save the model first:

import onnx
temp_model_file = 'model.onnx'
onnx.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

MIT License

MIT License Copyright (c) 2018 Microsoft Corporation Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Convert tf.keras/Keras models to ONNX 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/TimVerion/keras-onnx.git
git@gitee.com:TimVerion/keras-onnx.git
TimVerion
keras-onnx
keras-onnx
master

搜索帮助