onnxmltools

onnxApache-2.0活跃维护

把 Keras、scikit-learn、LightGBM 等训练好的模型转成 ONNX 格式,方便跨框架部署和推理。

ONNXMLTools enables conversion of models to ONNX

1.16.0发布于 2026-01-30 12:44 UTC1,176star225fork同步于 2026-11-10 23:50 UTC

怎么上手

由 README 提炼,执行前请确认
pip install onnxmltools
硬件要求按 README 说明

README 未说明硬件要求,仅要求 Python 3.9+ 及 ONNX、NumPy、ProtoBuf 依赖

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

把多种机器学习工具包训练出的模型统一转成 ONNX 格式的转换器集合

把 Keras、scikit-learn、LightGBM 等训练好的模型转成 ONNX 格式,方便跨框架部署和推理。

核心能力

  • 支持 TensorFlow、scikit-learn、Core ML、LightGBM、XGBoost 等九种工具包
  • TensorFlow 与 scikit-learn 分别封装 tf2onnx 和 skl2onnx 转换器
  • 用 target_opset 参数指定目标 ONNX 算子集版本
  • 提供 convert_keras、convert_coreml、convert_h2o 等按框架区分的转换入口
  • 转换后可用 onnxruntime 或 onnxruntime-gpu 跑单元测试验证

容易踩的坑

从源码安装时须先设置环境变量 ONNX_ML=1 再安装 onnx 包;转换某框架模型需自行安装对应库,如 CoreMLTools 限 3.1 或更低版本、Keras 需 2.0.8 以上;转换结果的 opset 取所有算子中最大的版本,可能小于你指定的 target_opset

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本1.16.0
  1. 1.16.0最新正式版2026-01-30 · 2026-01-30 12:44 UTC
    • Add LGBMRanker support initial PR 754, finalized in PR 755
    • Add partial support for custom objective #753
    • Improve xgboost categorical feature support #743
  2. v1.15.02026-01-14 · 2026-01-14 16:47 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/onnx/onnxmltools/compare/1.14.0...v1.15.0

  3. v1.15.0rc1pre-release2026-01-07 · 2026-01-07 15:52 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/onnx/onnxmltools/compare/1.14.0...v1.15.0rc1

  4. 1.14.02025-06-10 · 2025-06-10 15:33 UTC
    查看更新说明
    • Add tweedie objective to LightGBM options #722
    • Support for "huber" objective in the LGBM Booster #705
    • Remove import of split_complex_to_pairs and unused functions #714
    • Removes dependency on onnxconveter-common #718
  5. v1.13Release 1.13.02024-12-17 · 2024-12-17 07:46 UTC
    查看更新说明
    • Add missing dependency onnxconverter_common, fix multi regression with xgboost #679
    • Handle issue with binary classifier setting output to [N,1] vs [N,2] #681
    • Fix pkg name of onnxconverter_common #683
    • Update tree_ensemble_common.py #691

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    ONNXMLTools_logo_main

    CI

    Introduction

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

    Pytorch has its builtin ONNX exporter check here for details.

    Install

    You can install latest release of ONNXMLTools from PyPi:

    pip install onnxmltools
    

    or install from source:

    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.9+.

    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.

    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.

    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.

    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.

    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, a viewer for Neural Network models. Alternatively, you could identify your converted model's opset version through the following line of code.

    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 was most recently updated in opset 6, and 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 and more examples for converting models from different frameworks can be found in the 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 or onnxruntime-gpu. The unit test cases are all the normal python unit test cases, you can run it with pytest command line, for example:

    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