Skip to main content

Converts Machine Learning models to ONNX

Project description

https://github.com/onnx/onnxmltools/raw/master/docs/ONNXMLTools_logo_main.png
Build Status Linux Build Status Windows

Introduction

ONNXMLTools enables you to convert models from different machine learning toolkits into ONNX. Currently the following toolkits are supported:

  • Apple Core ML

  • scikit-learn (subset of models convertible to ONNX)

  • Keras (version 2.0.0 or higher)

  • LightGBM (through its scikit-learn interface)

Install

pip install onnxmltools

Dependencies

This package uses ONNX, NumPy, and ProtoBuf. If you are converting a model from scikit-learn, Apple Core ML, or Keras you need the following packages installed respectively: 1. scikit-learn 2. coremltools 3. Keras

Example

Here is a simple example to convert a Core ML model:

import onnxmltools
import coremltools

model_coreml = coremltools.utils.load_spec('image_recognition.mlmodel')
model_onnx = onnxmltools.convert_coreml(model_coreml, 'Image_Reco')

# Save as text
onnxmltools.utils.save_text(model_onnx, 'image_recognition.json')

# Save as protobuf
onnxmltools.utils.save_model(model_onnx, 'image_recognition.onnx')

Next, we show a simple usage of the Keras converter.

::

import onnxmltools from keras.layers import Input, Dense, Add from keras.models import Model

# N: batch size, C: sub-model input dimension, D: final model’s input dimension N, C, D = 2, 3, 3

# Define a sub-model, it will become a part of our final model sub_input1 = Input(shape=(C,)) sub_mapped1 = Dense(D)(sub_input1) sub_model1 = Model(inputs=sub_input1, outputs=sub_mapped1)

# Define another sub-model, it will become a part of our final model sub_input2 = Input(shape=(C,)) sub_mapped2 = Dense(D)(sub_input2) sub_model2 = Model(inputs=sub_input2, outputs=sub_mapped2)

# Define our final model input1 = Input(shape=(D,)) input2 = Input(shape=(D,)) mapped1_2 = sub_model1(input1) mapped2_2 = sub_model2(input2) sub_sum = Add()([mapped1_2, mapped2_2]) keras_model = Model(inputs=[input1, input2], output=sub_sum)

# Convert it! onnx_model = onnxmltools.convert_keras(keras_model)

License

MIT License

Acknowledgments

The initial version of this package was developed by the following developers and data scientists at Microsoft during winter 2017: Zeeshan Ahmed, Wei-Sheng Chin, Aidan Crook, Xavier Dupre, Costin Eseanu, Tom Finley, Lixin Gong, Scott Inglis, Pei Jiang, Ivan Matantsev, Prabhat Roy, M. Zeeshan Siddiqui, Shouheng Yi, Shauheen Zahirazami, Yiwen Zhu.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

onnxmltools-1.2.0.116-py2.py3-none-any.whl (239.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file onnxmltools-1.2.0.116-py2.py3-none-any.whl.

File metadata

  • Download URL: onnxmltools-1.2.0.116-py2.py3-none-any.whl
  • Upload date:
  • Size: 239.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for onnxmltools-1.2.0.116-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6a5bdb9b43748003f565f8a2b8beab195f6941d11271b4f0397bbfb1b7216cb1
MD5 376980f13a2c1b4eb9416e83ad34841d
BLAKE2b-256 9db95602f1facbed26dade48939df2c662feaf304b0cfaebf8578eb4e5fb16c0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page