sklearn-onnx: Convert your scikit-learn model into ONNX#

sklearn-onnx enables you to convert models from scikit-learn toolkits into ONNX.

Issues, questions

You should look for existing issues or submit a new one. Sources are available on onnx/sklearn-onnx.

ONNX version

The converter can convert a model for a specific version of ONNX. Every ONNX release is labelled with an opset number returned by function onnx_opset_version. This function returns the default value for parameter target opset (parameter target_opset) if it is not specified when converting the model. Every operator is versioned. The library chooses the most recent version below or equal to the targetted opset number for every operator. The ONNX model has one opset number for every operator domain, this value is the maximum opset number among all onnx nodes.

<<<

from skl2onnx import __max_supported_opset__, __version__

print("documentation for version:", __version__)
print("Last supported opset:", __max_supported_opset__)

>>>

    documentation for version: 1.17.0
    Last supported opset: 19

Backend

sklearn-onnx converts models in ONNX format which can be then used to compute predictions with the backend of your choice. However, there exists a way to automatically check every converter with onnxruntime, onnxruntime-gpu. Every converter is tested with this backend.

Getting started

import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

iris = load_iris()
X, y = iris.data, iris.target
X = X.astype(np.float32)
X_train, X_test, y_train, y_test = train_test_split(X, y)
clr = RandomForestClassifier()
clr.fit(X_train, y_train)

# Convert into ONNX format.
from skl2onnx import to_onnx

onx = to_onnx(clr, X[:1])
with open("rf_iris.onnx", "wb") as f:
    f.write(onx.SerializeToString())

# Compute the prediction with onnxruntime.
import onnxruntime as rt

sess = rt.InferenceSession("rf_iris.onnx", providers=["CPUExecutionProvider"])
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
pred_onx = sess.run([label_name], {input_name: X_test.astype(np.float32)})[0]

Related converters

sklearn-onnx only converts models from scikit-learn. onnxmltools can be used to convert models for libsvm, lightgbm, xgboost. Other converters can be found on github/onnx, torch.onnx, ONNX-MXNet API, Microsoft.ML.Onnx

Change Logs

See CHANGELOGS.md.

Credits

The package was started by the following engineers and data scientists at Microsoft starting from winter 2017: Zeeshan Ahmed, Wei-Sheng Chin, Aidan Crook, Xavier Dupré, Costin Eseanu, Tom Finley, Lixin Gong, Scott Inglis, Pei Jiang, Ivan Matantsev, Prabhat Roy, M. Zeeshan Siddiqui, Shouheng Yi, Shauheen Zahirazami, Yiwen Zhu, Du Li, Xuan Li, Wenbing Li.

License

It is licensed with Apache License v2.0.

Older versions