Package: onnxmltools
LinuxWindowsIntroductionONNXMLTools enables you to convert models from different machine learning toolkits intoONNX. Currently the following toolkits are supported:Tensorflow (a wrapper oftf2onnx converter)scikit-learn (a wrapper ofskl2onnx converter)Apple Core MLSpark ML (experimental)LightGBMlibsvmXGBoostH2OCatBoostPytorch has its builtin ONNX exporter checkherefor details.InstallYou can install latest release of ONNXMLTools fromPyPi:pipinstallonnxmltoolsor install from source:pipinstallgit+https://github.com/microsoft/onnxconverter-common
pipinstallgit+https://github.com/onnx/onnxmltoolsIf you choose to installonnxmltoolsfrom its source code, you must set the environment variableONNX_ML=1before installing theonnxpackage.DependenciesThis 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:scikit-learnCoreMLTools (version 3.1 or lower)Keras (version 2.0.8 or higher) with the corresponding Tensorflow versionLightGBMSparkMLXGBoostlibsvmH2OCatBoostONNXMLTools is tested with Python3.7+.ExamplesIf 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 theversioning documentation.Keras to ONNX ConversionNext, we show an example of converting a Keras model into an ONNX model withtarget_opset=7, which corresponds to ONNX release version 1.2.importonnxmltoolsfromkeras.layersimportInput,Dense,Addfromkeras.modelsimportModel# N: batch size, C: sub-model input dimension, D: final model's input dimensionN,C,D=2,3,3# Define a sub-model, it will become a part of our final modelsub_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 modelsub_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-modelsinput1=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 ConversionHere is a simple code snippet to convert a Core ML model into an ONNX model.importonnxmltoolsimportcoremltools# Load a Core ML modelcoreml_model=coremltools.utils.load_spec('example.mlmodel')# Convert the Core ML model into ONNXonnx_model=onnxmltools.convert_coreml(coreml_model,'Example Model')# Save as protobufonnxmltools.utils.save_model(onnx_model,'example.onnx')H2O to ONNX ConversionBelow 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.importonnxmltools# Convert the Core ML model into ONNXonnx_model=onnxmltools.convert_h2o('/path/to/h2o/gbm_mojo.zip')# Save as protobufonnxmltools.utils.save_model(onnx_model,'h2o_gbm.onnx')Testing model convertersonnxmltoolsconverts 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 modelYou can check the operator set of your converted ONNX model usingNetron, 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].versionIf the result from checking your ONNX model's opset is smaller than thetarget_opsetnumber 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,Abswas most recently updated in opset 6, andAddwas most recently updated in opset 7. Therefore, the converted ONNX model's opset will always be 7, even if you requesttarget_opset=8. The converter behavior was defined this way to ensure backwards compatibility.Documentation for theONNX Model formatand more examples for converting models from different frameworks can be found in theONNX tutorialsrepository.Test all existing convertersAll converter unit test can generate the original model and converted model to automatically be checked withonnxruntimeoronnxruntime-gpu.
The unit test cases are all the normal python unit test cases, you can run it with pytest command line, for example:python-mpytest--ignore.\tests\It requiresonnxruntime,numpyfor most models,pandasfor transforms related to text features, andscipyfor sparse features. One test also requireskerasto test a custom operator. That meanssklearnor any machine learning library is requested.Add a new converterOnce the converter is implemented, a unit test is added
to confirm that it works. At the end of the unit test, functiondump_data_and_modelor 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 intests_backendto compute the prediction
with the runtime.LicenseApache License v2.0