How to Use ONNXMLTools for Model Conversion to ONNX

Nov 26, 2022 | Data Science

Machine learning models often exist in different frameworks, which can complicate deployment. ONNXMLTools is a powerful tool that simplifies this by converting models from various frameworks into the ONNX (Open Neural Network Exchange) format. This article will guide you through the process of using ONNXMLTools, from installation to conversion examples, while also providing troubleshooting tips.

Installation of ONNXMLTools

To start with, you will need to install ONNXMLTools. You can do this in one of two ways:

  • Install from PyPi:
    pip install onnxmltools
  • Install from source:
    pip install git+https://github.com/microsoft/onnxconverter-common
    pip install git+https://github.com/onnx/onnxmltools

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

Dependencies

ONNXMLTools relies on several dependencies that must be installed based on the machine learning libraries you intend to use:

  • ONNX
  • NumPy
  • ProtoBuf
  • Other libraries based on the framework used:
    • scikit-learn
    • CoreMLTools (version 3.1 or lower)
    • Keras (version 2.0.8 or higher)
    • LightGBM
    • SparkML
    • XGBoost
    • libsvm
    • H2O
    • CatBoost

ONNXMLTools is tested with Python 3.7+.

Examples of Model Conversion

One of the key features of ONNXMLTools is its ability to convert models from different frameworks into ONNX format. Let’s look at examples of how to convert Keras, Core ML, and H2O models into ONNX:

Keras to ONNX Conversion

Imagine you have a Keras model that you want to convert. Here’s how you can do it:

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 sub-models
sub_input1 = Input(shape=(C,))
sub_mapped1 = Dense(D)(sub_input1)
sub_model1 = Model(inputs=sub_input1, outputs=sub_mapped1)

sub_input2 = Input(shape=(C,))
sub_mapped2 = Dense(D)(sub_input2)
sub_model2 = Model(inputs=sub_input2, outputs=sub_mapped2)

# Define the 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], outputs=sub_sum)

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

CoreML to ONNX Conversion

Here’s a snippet for converting a Core ML model:

import onnxmltools
import coremltools

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

# Convert to 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

For H2O models, use the following code:

import onnxmltools

# Convert H2O MOJO model to ONNX
onnx_model = onnxmltools.convert_h2o('pathtoh2ogbm_mojo.zip')

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

Troubleshooting Tips

If you encounter any issues while using ONNXMLTools, here are some troubleshooting tips:

  • Check that all dependencies are installed correctly, particularly the framework you’re converting from.
  • Verify the versions of the frameworks to ensure compatibility with ONNXMLTools.
  • If the converted model’s opset version is lower than expected, this might be intended behavior due to operator compatibility. Use the line below to confirm:
  • opset_version = onnx_model.opset_import[0].version
  • If you need further assistance or want to share insights, for more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Conclusion

In this article, we explored how to utilize ONNXMLTools to convert various machine learning models into the ONNX format. This allows for increased interoperability and makes it easier to deploy AI models across different systems.

Now you are equipped with the knowledge to effectively convert your machine learning models using ONNXMLTools. Happy coding!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox