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

sklearn-onnx enables you to convert models from sklearn-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.

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.

# Train a model.
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_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 convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
initial_type = [('float_input', FloatTensorType([None, 4]))]
onx = convert_sklearn(clr, initial_types=initial_type)
with open("rf_iris.onnx", "wb") as f:
    f.write(onx.SerializeToString())

# Compute the prediction with ONNX Runtime
import onnxruntime as rt
import numpy
sess = rt.InferenceSession("rf_iris.onnx")
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(numpy.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

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.