You can deploy and inference machine learning models from
or PyTorch
that are saved in different formats and converted to the TensorFlow
(Open Neural Network Exchange
) format. ONNX
is an open-source format for representing deep learning models. Developers can use the ONNX
format to train their models in one framework, such as ONNX
or PyTorch
, and then export it to run in another
environment with different performance characteristics. The TensorFlow
format provides a powerful solution for converting a machine learning model to ONNX
and perform inferencing by using the ONNX
runtime.ONNX
Benefits of converting models to ONNX
runtime
ONNX
Converting a model to
runtime offers several benefits, especially in the context of machine learning and deep learning applications. Some of the advantages of converting models to ONNX
runtime are as follows:ONNX
-
Cross-platform compatibility:
provides a standard format for representing machine learning models, which makes it easier to deploy models across different frameworks such as PyTorch or Tensorflow. You can train models in one frameworks and deploy them in another framework that supportsONNX
runtime.ONNX
-
Improved performance:
runtime optimizes models for inferencing by applying various hardware and software-specific optimizaitons, such as graph optimizations. Also, it supports execution on diverse hardware, such as CPUs and GPUs, ensuring efficient utilization of resources.ONNX
-
Interoperability:
provides a way to train models, such as PyTorch, TensorFlow, and scikit-learn in one framework and then export them to run in another environment, which streamlines the workflows. It breaks down the barriers between different deep learning frameworks, allowing developers to leverage the strengths of different libraries without getting locked into a single ecosystem.ONNX
Supported frameworks for conversion
You can convert machine learning models that use the following frameworks to
format:ONNX
PyTorch
TensorFlow
CatBoost
LightGBM
XGBoost
Scikit-learn
Converting PyTorch
models to ONNX
format
PyTorch
ONNX
Follow this process to convert your trained model in
to the PyTorch
format:ONNX
-
Import libraries: Start by importing the essential libraries, such as
for running the model,onnxruntime
for PyTorch functionalities, and other libraries required for your application.torch
-
Create or download
model: You can create aPyTorch
model by using your own data set or use models provided by external open source model repositories like Hugging Face.PyTorch
-
Convert
model to ONNX format: To convert thePyTorch
model to ONNX format:PyTorch
a. Prepare the model: Ensure that your
model is in evaluation mode by usingPyTorch
function. You may need a dummy input tensor to match the shape of the model.model.eval()
b. Export the model: Use the
function to convert the model to ONNX format.torch.onnx.export
-
Verify the conversion: After converting the model, verify that the model is functioning as expected by using the
library.onnx
Converting TensorFlow
models to ONNX
format
TensorFlow
ONNX
Follow this process to convert your model
to the ONNX format:TensorFlow
-
Import libraries: Start by importing the essential libraries, such as
to facilitate conversion oftf2onnx
models to ONNX, and other libraries required for your application.TensorFlow
-
Download
model: You must download the externally createdTensorFlow
model and the data that is used for training the model.TensorFlow
-
Convert
model to ONNX format: Use theTensorFlow
command to convert yourtf2onnx.convert
model that is created in theTensorFlow
format toSavedModel
format. If you want to convert aONNX
model, use theTensorFlow Lite
flag instead of the--tflite
flag.--saved-model
models and Keras
functions can be directly converted within Python by using the tf
or tf2onnx.convert.from_keras
functions.tf2onnx.convert.from_function
- Verify the conversion: After converting the model, verify that the model is functioning as expected by using the
library.onnx
Converting CatBoost
models to ONNX format
CatBoost
Follow this process to convert your trained model in
to the ONNX format:CatBoost
-
Import libraries: Start by importing the essential libraries, such as
for running the model,onnxruntime
forcatboost
functionalities, and other libraries required for your application.CatBoost
-
Create or download
model: You can create aCatBoost
model by using your own data set or use models provided by external open source model repositories such as Hugging Face.CatBoost
-
Convert
model toCatBoost
format: To convert theONNX
model toCatBoost
format:ONNX
a. Load the
model: You can load theCatBoost
model by using libraries such asCatBoost
:pickle
catboost_model = pickle.load(file)
b. Export the model: Use the
function with format parameter set tocatboost_model.save_model
to convert the model toonnx
format.ONNX
catboost_model.save_model( onnx_model_name, format="onnx", export_parameters={ 'onnx_domain': 'ai.catboost', 'onnx_model_version': 1, 'onnx_doc_string': 'test model for Regressor', 'onnx_graph_name': 'CatBoostModel_for_Regression' } )
-
Verify the conversion: After converting the model, verify that the model is functioning as expected by using the
library.onnx
Converting LightGBM
models to ONNX format
LightGBM
Follow this process to convert your trained model in
to the LightGBM
format:ONNX
-
Import libraries: Start by importing the essential libraries, such as
for running the model,onnxruntime
forlightgbm
functionalities, andLightGBM
for conversion and other libraries required for your application.onnxmltools
-
Create or download
model: You can create aLightGBM
model by using your own data set or use models provided by external open source model repositories like Hugging Face.LightGBM
-
Convert
model toLightGBM
format: To convert theONNX
model toLightGBM
format:ONNX
a. Load the
model: You can load theLightGBM
model by using libraries such asLightGBM
:pickle
lgbm_model = pickle.load(file)
b. Export the model: Use the
function to convert the model toconvert_lightgbm
format.ONNX
from onnxmltools import convert_lightgbm from skl2onnx.common.data_types import FloatTensorType from onnxmltools.utils import save_model initial_types = [("float_input", FloatTensorType([None, X.shape[1]]))] onnx_model = convert_lightgbm(model=lgbm_model, initial_types=initial_types) onnx_model_filename = "lgbm_model.onnx" save_model(onnx_model, onnx_model_filename)
Tip:For troubleshooting problems with converting
models toLightGBM
format, see Troubleshooting watsonx.ai Runtime.ONNX
-
Verify the conversion: After converting the model, verify that the model is functioning as expected by using the
library.onnx
Converting XGBoost
models to ONNX
format
XGBoost
ONNX
Follow this process to convert your trained model in
to the XGBoost
format:ONNX
-
Import libraries: Start by importing the essential libraries, such as
for running the model,onnxruntime
forxgboost
functionalities, and other libraries required for your application.XGBoost
-
Create or download
model: You can create aXGBoost
model by using your own data set or use models provided by external open source model repositories like Hugging Face.XGBoost
-
Convert
model to ONNX format: To convert theXGBoost
model to ONNX format:XGBoost
a. Load the
model: You can load theXGboost
model by using libraries such asXGBoost
:pickle
xgboost_model = pickle.load(file)
b. Export the model: Use the
function to convert the model toconvert_xgboost
format.ONNX
from onnxmltools import convert_xgboost from onnxconverter_common.data_types import FloatTensorType from onnxmltools.utils import save_model initial_types = [("float_input", FloatTensorType([None, X.shape[1]]))] onnx_model = convert_xgboost(xgboost_model, initial_types=initial_types) onnx_model_filename = "xgboost_onnx_model.onnx" save_model(onnx_model, onnx_model_filename)
-
Verify the conversion: After converting the model, verify that the model is functioning as expected by using the onnx library.
Converting scikit-learn
models to ONNX
format
scikit-learn
ONNX
Follow this process to convert your trained model in
to the scikit-learn
format:ONNX
-
Import libraries: Start by importing the essential libraries, such as
for running the model,onnxruntime
forsklearn
functionalities,scikit-learn
for conversion and other libraries required for your application.skl2onnx
-
Create or download
model: You can create ascikit-learn
model by using your own data set or use models provided by external open source model repositories like Hugging Face.scikit-learn
-
Convert
model to ONNX format: To convert thescikit-learn
model toscikit-learn
format:ONNX
a. Load the
model: You can load thescikit-learn
model by using libraries such asscikit-learn
:pickle
sklearn_model = pickle.load(file)
b. Export the model: Use the
function to convert the model toto_onnx
format.ONNX
from skl2onnx import to_onnx onnx_model = to_onnx(sklearn_model, X, target_opset=19) with open("sklearn_model.onnx", "wb") as f: f.write(onnx_model.SerializeToString())
-
Verify the conversion: After converting the model, verify that the model is functioning as expected by using the
library.onnx
Additional considerations
Here are some additional considerations for converting your models to
format:ONNX
-
Dynamic axes: Dynamic axes can be used by a model to handle variable input shapes, such as dynamic batch sizes or sequence lengths, which is useful for models deployed in application where the input dimensions may vary. Use dynamic axes if your model handles variable input sizes, such as dynamic batch size or sequence length.
Dynamic axes also reduce memory overhead as they can be used with multiple inputs and outputs to adapt dynamically without re-exporting the model. You can specify the dynamic axes during model export in
orPyTorch
.TensorFlow
-
Opset version: The opset version in
determines the set of operations and their specifications that are supported by the model. It is a critical factor during model conversion and deployment.ONNX
Different
runtimes and frameworks support specific opset versions. Older opset versions may lack features or optimizations present in newer versions. Incompatibility between a model's opset version and theONNX
runtime can cause errors during inferencing. You must ensure that theONNX
opset version that you choose is supported by your target runtime.ONNX
Deploying models converted to ONNX
format
ONNX
Use the
software specification to deploy your machine learning model converted to onnxruntime_opset_19
format. You must specify the software specification and model type when you store the model to the watsonx.ai
Runtime repository. For more information, see Supported software specifications.ONNX
To deploy models converted to
format from the user interface, follow these steps:ONNX
-
In your deployment space, go to the Assets tab.
-
Find your model in the asset list, click the Menu icon Menu icon, and select Deploy.
-
Select the deployment type for your model. Choose between online and batch deployment options.
-
Enter a name for your deployment and optionally enter a serving name, description, and tags.
Note:- Use the Serving name field to specify a name for your deployment instead of deployment ID.
- The serving name must be unique within the namespace.
- The serving name must contain only these characters: [a-z,0-9,_] and must be a maximum 36 characters long.
- In workflows where your custom foundation model is used periodically, consider assigning your model the same serving name each time you deploy it. This way, after you delete and then re-deploy the model, you can keep using the same endpoint in your code.
-
Select a hardware specification for your model.
-
Select a configuration and a software specification for your model.
-
Click Create.
Testing the model
Follow these steps to test your deployed models converted to ONNX format:
- In your deployment space, open the Deployments tab and click the deployment name.
- Click the Test tab to input prompt text and get a response from the deployed asset.
- Enter test data in one of the following formats, depending on the type of asset that you deployed:
- Text: Enter text input data to generate a block of text as output.
- JSON: Enter JSON input data to generate output in JSON format.
- Click Generate to get results that are based on your prompt.
Sample notebooks
The following sample notebooks demonstrate how to deploy machine learning models converted from
or PyTorch
to the TensorFlow
format by using the Python client library:ONNX
Notebook | Framework | Description |
---|---|---|
Convert ONNX neural network from fixed axes to dynamic axes and use it with ibm-watsonx-ai | ONNX | Set up the environment Create and export basic ONNX model Convert model from fixed axes to dynamic axes Persist converted ONNX model Deploy and score ONNX model Clean up Summary and next steps |
Use ONNX model converted from PyTorch with ibm-watsonx-ai | PyTorch, ONNX | Create PyTorch model with dataset. Convert PyTorch model to ONNX format Persist converted model in Watson Machine Learning repository. Deploy model for online scoring using client library. Score sample records using client library. |
Use ONNX model converted from TensorFlow to recognize hand-written digits with ibm-watsonx-ai | Tensorflow, ONNX | Download an externally trained TensorFlow model with dataset. Convert TensorFlow model to ONNX format Persist converted model in Watson Machine Learning repository. Deploy model for online scoring using client library. Score sample records using client library. |
Use ONNX model converted from CatBoost | CatBoost, ONNX | Trained CatBoost model. Convert CatBoost model to ONNX format. Persist converted model in Watson Machine Learning repository. Deploy model for online scoring using client library. Score sample records using client library. |
Use ONNX model converted from LightGBM | LightGBM, ONNX | Train a LightGBM model Convert the LightGBM model to ONNX format Persist the converted model in the watsonx.ai Runtime repository Deploy the model for online scoring using client library Score sample records using the client library |
Use ONNX model converted from XGBoost with ibm-watsonx-ai | XGBoost, ONNX | Train an XGBoost model. Convert the XGBoost model to the ONNX format. Persist the converted model in the watsonx.ai repository. Deploy model for online scoring using the APIClient instance. Score sample records using the APIClient instance. |
Use ONNX model converted from scikit-learn with ibm-watsonx-ai | Scikit-learn, ONNX | Train a scikit-learn model Convert the native scikit-learn model to ONNX format Perform conversion for a custom scikit-learn model wrapped in a sklearn pipeline Persist the converted model in the watsonx.ai Runtime repository Deploy the model for online scoring using client library Score sample records using the client library |
Parent topic: Deploying machine learning assets