onnxmltools


Nameonnxmltools JSON
Version 1.12.0 PyPI version JSON
download
home_pagehttps://github.com/onnx/onnxmltools
SummaryConverts Machine Learning models to ONNX
upload_time2023-12-18 05:51:17
maintainer
docs_urlNone
authorONNX
requires_python
licenseApache License v2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            <!--- SPDX-License-Identifier: Apache-2.0 -->
#

![ONNXMLTools_logo_main](docs/ONNXMLTools_logo_main.png)

| Linux                                                                                                                                                                                                                          | Windows                                                                                                                                                                                                                        |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [![Build Status](https://dev.azure.com/onnxmltools/onnxmltools/_apis/build/status/onnxmltools-linux-conda-ci?branchName=master)](https://dev.azure.com/onnxmltools/onnxmltools/_build/latest?definitionId=3?branchName=master) | [![Build Status](https://dev.azure.com/onnxmltools/onnxmltools/_apis/build/status/onnxmltools-win32-conda-ci?branchName=master)](https://dev.azure.com/onnxmltools/onnxmltools/_build/latest?definitionId=3?branchName=master) |

## Introduction

ONNXMLTools enables you to convert models from different machine learning toolkits into [ONNX](https://onnx.ai). Currently the following toolkits are supported:

* Tensorflow (a wrapper of [tf2onnx converter](https://github.com/onnx/tensorflow-onnx/))
* scikit-learn (a wrapper of [skl2onnx converter](https://github.com/onnx/sklearn-onnx/))
* Apple Core ML
* Spark ML (experimental)
* LightGBM
* libsvm
* XGBoost
* H2O
* CatBoost

Pytorch has its builtin ONNX exporter check [here](https://pytorch.org/docs/stable/onnx.html) for details.

## Install

You can install latest release of ONNXMLTools from [PyPi](https://pypi.org/project/onnxmltools/):

```bash
pip install onnxmltools
```

or install from source:

```bash
pip install git+https://github.com/microsoft/onnxconverter-common
pip install git+https://github.com/onnx/onnxmltools
```

If you choose to install `onnxmltools` from its source code, you must set the environment variable `ONNX_ML=1` before installing the `onnx` package.

## Dependencies

This package relies on ONNX, NumPy, and ProtoBuf. If you are converting a model from scikit-learn, Core ML, Keras, LightGBM, SparkML, XGBoost, H2O, CatBoost or LibSVM, you will need an environment with the respective package installed from the list below:

1. scikit-learn
2. CoreMLTools (version 3.1 or lower)
3. Keras (version 2.0.8 or higher) with the corresponding Tensorflow version
4. LightGBM
5. SparkML
6. XGBoost
7. libsvm
8. H2O
9. CatBoost

ONNXMLTools is tested with Python **3.7+**.

# Examples

If you want the converted ONNX model to be compatible with a certain ONNX version, please specify the target_opset parameter upon invoking the convert function. The following Keras model conversion example demonstrates this below. You can identify the mapping from ONNX Operator Sets (referred to as opsets) to ONNX releases in the [versioning documentation](https://github.com/onnx/onnx/blob/master/docs/Versioning.md#released-versions).

## Keras to ONNX Conversion

Next, we show an example of converting a Keras model into an ONNX model with `target_opset=7`, which corresponds to ONNX release version 1.2.

```python
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 a model built upon the previous two sub-models
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], outputs=sub_sum)

# Convert it! The target_opset parameter is optional.
onnx_model = onnxmltools.convert_keras(keras_model, target_opset=7)
```

## CoreML to ONNX Conversion

Here is a simple code snippet to convert a Core ML model into an ONNX model.

```python
import onnxmltools
import coremltools

# Load a Core ML model
coreml_model = coremltools.utils.load_spec('example.mlmodel')

# Convert the Core ML model into ONNX
onnx_model = onnxmltools.convert_coreml(coreml_model, 'Example Model')

# Save as protobuf
onnxmltools.utils.save_model(onnx_model, 'example.onnx')
```

## H2O to ONNX Conversion

Below is a code snippet to convert a H2O MOJO model into an ONNX model. The only prerequisite is to have a MOJO model saved on the local file-system.

```python
import onnxmltools

# Convert the Core ML model into ONNX
onnx_model = onnxmltools.convert_h2o('/path/to/h2o/gbm_mojo.zip')

# Save as protobuf
onnxmltools.utils.save_model(onnx_model, 'h2o_gbm.onnx')
```

# Testing model converters

*onnxmltools* converts models into the ONNX format which
can be then used to compute predictions with the
backend of your choice.

## Checking the operator set version of your converted ONNX model

You can check the operator set of your converted ONNX model using [Netron](https://github.com/lutzroeder/Netron), a viewer for Neural Network models. Alternatively, you could identify your converted model's opset version through the following line of code.

```python
opset_version = onnx_model.opset_import[0].version
```

If the result from checking your ONNX model's opset is smaller than the `target_opset` number you specified in the onnxmltools.convert function, be assured that this is likely intended behavior. The ONNXMLTools converter works by converting each operator to the ONNX format individually and finding the corresponding opset version that it was most recently updated in. Once all of the operators are converted, the resultant ONNX model has the maximal opset version of all of its operators.

To illustrate this concretely, let's consider a model with two operators, Abs and Add. As of December 2018, [Abs](https://github.com/onnx/onnx/blob/master/docs/Operators.md#abs) was most recently updated in opset 6, and [Add](https://github.com/onnx/onnx/blob/master/docs/Operators.md#add) was most recently updated in opset 7. Therefore, the converted ONNX model's opset will always be 7, even if you request `target_opset=8`. The converter behavior was defined this way to ensure backwards compatibility.

Documentation for the [ONNX Model format](https://github.com/onnx/onnx) and more examples for converting models from different frameworks can be found in the [ONNX tutorials](https://github.com/onnx/tutorials) repository.

## Test all existing converters

All converter unit test can generate the original model and converted model to automatically be checked with
[onnxruntime](https://pypi.org/project/onnxruntime/) or
[onnxruntime-gpu](https://pypi.org/project/onnxruntime-gpu/).
The unit test cases are all the normal python unit test cases, you can run it with pytest command line, for example:

```bash
python -m pytest --ignore .\tests\
```

It requires *onnxruntime*, *numpy* for most models,
*pandas* for transforms related to text features, and
*scipy* for sparse features. One test also requires
*keras* to test a custom operator. That means
*sklearn* or any machine learning library is requested.

## Add a new converter

Once the converter is implemented, a unit test is added
to confirm that it works. At the end of the unit test, function
*dump_data_and_model* or any equivalent function must be called
to dump the expected output and the converted model.
Once these file are generated, a corresponding test must
be added in *tests_backend* to compute the prediction
with the runtime.

# License

[Apache License v2.0](LICENSE)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/onnx/onnxmltools",
    "name": "onnxmltools",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "ONNX",
    "author_email": "onnx-technical-discuss@lists.lfaidata.foundation",
    "download_url": "https://files.pythonhosted.org/packages/01/3b/7e78d3b3fe34a41516be124d0991c0cb87184d569e9d2b87acab48d6b80b/onnxmltools-1.12.0.tar.gz",
    "platform": null,
    "description": "<!--- SPDX-License-Identifier: Apache-2.0 -->\r\n#\r\n\r\n![ONNXMLTools_logo_main](docs/ONNXMLTools_logo_main.png)\r\n\r\n| Linux                                                                                                                                                                                                                          | Windows                                                                                                                                                                                                                        |\r\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\r\n| [![Build Status](https://dev.azure.com/onnxmltools/onnxmltools/_apis/build/status/onnxmltools-linux-conda-ci?branchName=master)](https://dev.azure.com/onnxmltools/onnxmltools/_build/latest?definitionId=3?branchName=master) | [![Build Status](https://dev.azure.com/onnxmltools/onnxmltools/_apis/build/status/onnxmltools-win32-conda-ci?branchName=master)](https://dev.azure.com/onnxmltools/onnxmltools/_build/latest?definitionId=3?branchName=master) |\r\n\r\n## Introduction\r\n\r\nONNXMLTools enables you to convert models from different machine learning toolkits into [ONNX](https://onnx.ai). Currently the following toolkits are supported:\r\n\r\n* Tensorflow (a wrapper of [tf2onnx converter](https://github.com/onnx/tensorflow-onnx/))\r\n* scikit-learn (a wrapper of [skl2onnx converter](https://github.com/onnx/sklearn-onnx/))\r\n* Apple Core ML\r\n* Spark ML (experimental)\r\n* LightGBM\r\n* libsvm\r\n* XGBoost\r\n* H2O\r\n* CatBoost\r\n\r\nPytorch has its builtin ONNX exporter check [here](https://pytorch.org/docs/stable/onnx.html) for details.\r\n\r\n## Install\r\n\r\nYou can install latest release of ONNXMLTools from [PyPi](https://pypi.org/project/onnxmltools/):\r\n\r\n```bash\r\npip install onnxmltools\r\n```\r\n\r\nor install from source:\r\n\r\n```bash\r\npip install git+https://github.com/microsoft/onnxconverter-common\r\npip install git+https://github.com/onnx/onnxmltools\r\n```\r\n\r\nIf you choose to install `onnxmltools` from its source code, you must set the environment variable `ONNX_ML=1` before installing the `onnx` package.\r\n\r\n## Dependencies\r\n\r\nThis package relies on ONNX, NumPy, and ProtoBuf. If you are converting a model from scikit-learn, Core ML, Keras, LightGBM, SparkML, XGBoost, H2O, CatBoost or LibSVM, you will need an environment with the respective package installed from the list below:\r\n\r\n1. scikit-learn\r\n2. CoreMLTools (version 3.1 or lower)\r\n3. Keras (version 2.0.8 or higher) with the corresponding Tensorflow version\r\n4. LightGBM\r\n5. SparkML\r\n6. XGBoost\r\n7. libsvm\r\n8. H2O\r\n9. CatBoost\r\n\r\nONNXMLTools is tested with Python **3.7+**.\r\n\r\n# Examples\r\n\r\nIf you want the converted ONNX model to be compatible with a certain ONNX version, please specify the target_opset parameter upon invoking the convert function. The following Keras model conversion example demonstrates this below. You can identify the mapping from ONNX Operator Sets (referred to as opsets) to ONNX releases in the [versioning documentation](https://github.com/onnx/onnx/blob/master/docs/Versioning.md#released-versions).\r\n\r\n## Keras to ONNX Conversion\r\n\r\nNext, we show an example of converting a Keras model into an ONNX model with `target_opset=7`, which corresponds to ONNX release version 1.2.\r\n\r\n```python\r\nimport onnxmltools\r\nfrom keras.layers import Input, Dense, Add\r\nfrom keras.models import Model\r\n\r\n# N: batch size, C: sub-model input dimension, D: final model's input dimension\r\nN, C, D = 2, 3, 3\r\n\r\n# Define a sub-model, it will become a part of our final model\r\nsub_input1 = Input(shape=(C,))\r\nsub_mapped1 = Dense(D)(sub_input1)\r\nsub_model1 = Model(inputs=sub_input1, outputs=sub_mapped1)\r\n\r\n# Define another sub-model, it will become a part of our final model\r\nsub_input2 = Input(shape=(C,))\r\nsub_mapped2 = Dense(D)(sub_input2)\r\nsub_model2 = Model(inputs=sub_input2, outputs=sub_mapped2)\r\n\r\n# Define a model built upon the previous two sub-models\r\ninput1 = Input(shape=(D,))\r\ninput2 = Input(shape=(D,))\r\nmapped1_2 = sub_model1(input1)\r\nmapped2_2 = sub_model2(input2)\r\nsub_sum = Add()([mapped1_2, mapped2_2])\r\nkeras_model = Model(inputs=[input1, input2], outputs=sub_sum)\r\n\r\n# Convert it! The target_opset parameter is optional.\r\nonnx_model = onnxmltools.convert_keras(keras_model, target_opset=7)\r\n```\r\n\r\n## CoreML to ONNX Conversion\r\n\r\nHere is a simple code snippet to convert a Core ML model into an ONNX model.\r\n\r\n```python\r\nimport onnxmltools\r\nimport coremltools\r\n\r\n# Load a Core ML model\r\ncoreml_model = coremltools.utils.load_spec('example.mlmodel')\r\n\r\n# Convert the Core ML model into ONNX\r\nonnx_model = onnxmltools.convert_coreml(coreml_model, 'Example Model')\r\n\r\n# Save as protobuf\r\nonnxmltools.utils.save_model(onnx_model, 'example.onnx')\r\n```\r\n\r\n## H2O to ONNX Conversion\r\n\r\nBelow is a code snippet to convert a H2O MOJO model into an ONNX model. The only prerequisite is to have a MOJO model saved on the local file-system.\r\n\r\n```python\r\nimport onnxmltools\r\n\r\n# Convert the Core ML model into ONNX\r\nonnx_model = onnxmltools.convert_h2o('/path/to/h2o/gbm_mojo.zip')\r\n\r\n# Save as protobuf\r\nonnxmltools.utils.save_model(onnx_model, 'h2o_gbm.onnx')\r\n```\r\n\r\n# Testing model converters\r\n\r\n*onnxmltools* converts models into the ONNX format which\r\ncan be then used to compute predictions with the\r\nbackend of your choice.\r\n\r\n## Checking the operator set version of your converted ONNX model\r\n\r\nYou can check the operator set of your converted ONNX model using [Netron](https://github.com/lutzroeder/Netron), a viewer for Neural Network models. Alternatively, you could identify your converted model's opset version through the following line of code.\r\n\r\n```python\r\nopset_version = onnx_model.opset_import[0].version\r\n```\r\n\r\nIf the result from checking your ONNX model's opset is smaller than the `target_opset` number you specified in the onnxmltools.convert function, be assured that this is likely intended behavior. The ONNXMLTools converter works by converting each operator to the ONNX format individually and finding the corresponding opset version that it was most recently updated in. Once all of the operators are converted, the resultant ONNX model has the maximal opset version of all of its operators.\r\n\r\nTo illustrate this concretely, let's consider a model with two operators, Abs and Add. As of December 2018, [Abs](https://github.com/onnx/onnx/blob/master/docs/Operators.md#abs) was most recently updated in opset 6, and [Add](https://github.com/onnx/onnx/blob/master/docs/Operators.md#add) was most recently updated in opset 7. Therefore, the converted ONNX model's opset will always be 7, even if you request `target_opset=8`. The converter behavior was defined this way to ensure backwards compatibility.\r\n\r\nDocumentation for the [ONNX Model format](https://github.com/onnx/onnx) and more examples for converting models from different frameworks can be found in the [ONNX tutorials](https://github.com/onnx/tutorials) repository.\r\n\r\n## Test all existing converters\r\n\r\nAll converter unit test can generate the original model and converted model to automatically be checked with\r\n[onnxruntime](https://pypi.org/project/onnxruntime/) or\r\n[onnxruntime-gpu](https://pypi.org/project/onnxruntime-gpu/).\r\nThe unit test cases are all the normal python unit test cases, you can run it with pytest command line, for example:\r\n\r\n```bash\r\npython -m pytest --ignore .\\tests\\\r\n```\r\n\r\nIt requires *onnxruntime*, *numpy* for most models,\r\n*pandas* for transforms related to text features, and\r\n*scipy* for sparse features. One test also requires\r\n*keras* to test a custom operator. That means\r\n*sklearn* or any machine learning library is requested.\r\n\r\n## Add a new converter\r\n\r\nOnce the converter is implemented, a unit test is added\r\nto confirm that it works. At the end of the unit test, function\r\n*dump_data_and_model* or any equivalent function must be called\r\nto dump the expected output and the converted model.\r\nOnce these file are generated, a corresponding test must\r\nbe added in *tests_backend* to compute the prediction\r\nwith the runtime.\r\n\r\n# License\r\n\r\n[Apache License v2.0](LICENSE)\r\n",
    "bugtrack_url": null,
    "license": "Apache License v2.0",
    "summary": "Converts Machine Learning models to ONNX",
    "version": "1.12.0",
    "project_urls": {
        "Homepage": "https://github.com/onnx/onnxmltools"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a8c41e585300582074dff8b1b5768c24b3875e5b1556e6bd5b8adfaf8e4bb2e0",
                "md5": "6588e268fb8f1a54e837eafdd49cb8cf",
                "sha256": "c51608ead3806a50f2cd70d466ee46c6e71a3f315ac629b0dffaa1081206a66c"
            },
            "downloads": -1,
            "filename": "onnxmltools-1.12.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6588e268fb8f1a54e837eafdd49cb8cf",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 329039,
            "upload_time": "2023-12-18T05:51:15",
            "upload_time_iso_8601": "2023-12-18T05:51:15.115526Z",
            "url": "https://files.pythonhosted.org/packages/a8/c4/1e585300582074dff8b1b5768c24b3875e5b1556e6bd5b8adfaf8e4bb2e0/onnxmltools-1.12.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "013b7e78d3b3fe34a41516be124d0991c0cb87184d569e9d2b87acab48d6b80b",
                "md5": "87798a37907ce9397f0cdce4a9a26a1f",
                "sha256": "ebdc529a78ce79c4be767cd6535544f983fe9dc0ca10f434194086f1c5be64d1"
            },
            "downloads": -1,
            "filename": "onnxmltools-1.12.0.tar.gz",
            "has_sig": false,
            "md5_digest": "87798a37907ce9397f0cdce4a9a26a1f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 197555,
            "upload_time": "2023-12-18T05:51:17",
            "upload_time_iso_8601": "2023-12-18T05:51:17.322075Z",
            "url": "https://files.pythonhosted.org/packages/01/3b/7e78d3b3fe34a41516be124d0991c0cb87184d569e9d2b87acab48d6b80b/onnxmltools-1.12.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-18 05:51:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "onnx",
    "github_project": "onnxmltools",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "lcname": "onnxmltools"
}
        
Elapsed time: 0.18205s