Note
Go to the end to download the full example code.
Logging, verbose¶
The conversion of a pipeline fails if it contains an object without any associated converter. It may also fails if one of the object is mapped by a custom converter. If the error message is not explicit enough, it is possible to enable logging.
Train a model¶
A very basic example using random forest and the iris dataset.
import logging
import numpy
import onnx
import onnxruntime as rt
import sklearn
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx import convert_sklearn
import skl2onnx
iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y)
clr = DecisionTreeClassifier()
clr.fit(X_train, y_train)
print(clr)
DecisionTreeClassifier()
Convert a model into ONNX¶
initial_type = [("float_input", FloatTensorType([None, 4]))]
onx = convert_sklearn(clr, initial_types=initial_type, target_opset=12)
sess = rt.InferenceSession(onx.SerializeToString(), 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(numpy.float32)})[0]
print(pred_onx)
[0 2 1 2 1 2 1 0 0 0 1 1 1 2 2 1 2 2 0 2 1 2 2 0 2 1 1 1 1 1 1 1 1 2 1 1 0
1]
Conversion with parameter verbose¶
verbose is a parameter which prints messages on the standard output. It tells which converter is called. verbose=1 usually means what skl2onnx is doing to convert a pipeline. verbose=2+ is reserved for information within converters.
convert_sklearn(clr, initial_types=initial_type, target_opset=12, verbose=1)
[convert_sklearn] parse_sklearn_model
[convert_sklearn] convert_topology
[convert_operators] begin
[convert_operators] iteration 1 - n_vars=0 n_ops=2
[call_converter] call converter for 'SklearnDecisionTreeClassifier'.
[call_converter] call converter for 'SklearnZipMap'.
[convert_operators] end iter: 1 - n_vars=5
[convert_operators] iteration 2 - n_vars=5 n_ops=2
[convert_operators] end iter: 2 - n_vars=5
[convert_operators] end.
[_update_domain_version] +opset 0: name='', version=9
[_update_domain_version] +opset 1: name='ai.onnx.ml', version=1
[convert_sklearn] end
ir_version: 7
producer_name: "skl2onnx"
producer_version: "1.18.0"
domain: "ai.onnx"
model_version: 0
doc_string: ""
graph {
node {
input: "float_input"
output: "label"
output: "probabilities"
name: "TreeEnsembleClassifier"
op_type: "TreeEnsembleClassifier"
attribute {
name: "class_ids"
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
type: INTS
}
attribute {
name: "class_nodeids"
ints: 1
ints: 1
ints: 1
ints: 4
ints: 4
ints: 4
ints: 5
ints: 5
ints: 5
ints: 8
ints: 8
ints: 8
ints: 9
ints: 9
ints: 9
ints: 10
ints: 10
ints: 10
type: INTS
}
attribute {
name: "class_treeids"
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "class_weights"
floats: 1
floats: 0
floats: 0
floats: 0
floats: 1
floats: 0
floats: 0
floats: 0
floats: 1
floats: 0
floats: 0
floats: 1
floats: 0
floats: 1
floats: 0
floats: 0
floats: 0
floats: 1
type: FLOATS
}
attribute {
name: "classlabels_int64s"
ints: 0
ints: 1
ints: 2
type: INTS
}
attribute {
name: "nodes_falsenodeids"
ints: 2
ints: 0
ints: 6
ints: 5
ints: 0
ints: 0
ints: 10
ints: 9
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_featureids"
ints: 2
ints: 0
ints: 3
ints: 2
ints: 0
ints: 0
ints: 2
ints: 1
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_hitrates"
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
type: FLOATS
}
attribute {
name: "nodes_missing_value_tracks_true"
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_modes"
strings: "BRANCH_LEQ"
strings: "LEAF"
strings: "BRANCH_LEQ"
strings: "BRANCH_LEQ"
strings: "LEAF"
strings: "LEAF"
strings: "BRANCH_LEQ"
strings: "BRANCH_LEQ"
strings: "LEAF"
strings: "LEAF"
strings: "LEAF"
type: STRINGS
}
attribute {
name: "nodes_nodeids"
ints: 0
ints: 1
ints: 2
ints: 3
ints: 4
ints: 5
ints: 6
ints: 7
ints: 8
ints: 9
ints: 10
type: INTS
}
attribute {
name: "nodes_treeids"
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_truenodeids"
ints: 1
ints: 0
ints: 3
ints: 4
ints: 0
ints: 0
ints: 7
ints: 8
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_values"
floats: 2.69999981
floats: 0
floats: 1.75
floats: 5.29999971
floats: 0
floats: 0
floats: 4.85
floats: 3.1
floats: 0
floats: 0
floats: 0
type: FLOATS
}
attribute {
name: "post_transform"
s: "NONE"
type: STRING
}
domain: "ai.onnx.ml"
}
node {
input: "label"
output: "output_label"
name: "Cast"
op_type: "Cast"
attribute {
name: "to"
i: 7
type: INT
}
domain: ""
}
node {
input: "probabilities"
output: "output_probability"
name: "ZipMap"
op_type: "ZipMap"
attribute {
name: "classlabels_int64s"
ints: 0
ints: 1
ints: 2
type: INTS
}
domain: "ai.onnx.ml"
}
name: "4da61a0794d947c88ca182179ef062ca"
input {
name: "float_input"
type {
tensor_type {
elem_type: 1
shape {
dim {
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "output_label"
type {
tensor_type {
elem_type: 7
shape {
dim {
}
}
}
}
}
output {
name: "output_probability"
type {
sequence_type {
elem_type {
map_type {
key_type: 7
value_type {
tensor_type {
elem_type: 1
}
}
}
}
}
}
}
}
opset_import {
domain: ""
version: 9
}
opset_import {
domain: "ai.onnx.ml"
version: 1
}
Conversion with logging¶
This is very detailed logging. It which operators or variables (output of converters) is processed, which node is created… This information may be useful when a custom converter is being implemented.
logger = logging.getLogger("skl2onnx")
logger.setLevel(logging.DEBUG)
convert_sklearn(clr, initial_types=initial_type, target_opset=12)
ir_version: 7
producer_name: "skl2onnx"
producer_version: "1.18.0"
domain: "ai.onnx"
model_version: 0
doc_string: ""
graph {
node {
input: "float_input"
output: "label"
output: "probabilities"
name: "TreeEnsembleClassifier"
op_type: "TreeEnsembleClassifier"
attribute {
name: "class_ids"
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
ints: 0
ints: 1
ints: 2
type: INTS
}
attribute {
name: "class_nodeids"
ints: 1
ints: 1
ints: 1
ints: 4
ints: 4
ints: 4
ints: 5
ints: 5
ints: 5
ints: 8
ints: 8
ints: 8
ints: 9
ints: 9
ints: 9
ints: 10
ints: 10
ints: 10
type: INTS
}
attribute {
name: "class_treeids"
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "class_weights"
floats: 1
floats: 0
floats: 0
floats: 0
floats: 1
floats: 0
floats: 0
floats: 0
floats: 1
floats: 0
floats: 0
floats: 1
floats: 0
floats: 1
floats: 0
floats: 0
floats: 0
floats: 1
type: FLOATS
}
attribute {
name: "classlabels_int64s"
ints: 0
ints: 1
ints: 2
type: INTS
}
attribute {
name: "nodes_falsenodeids"
ints: 2
ints: 0
ints: 6
ints: 5
ints: 0
ints: 0
ints: 10
ints: 9
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_featureids"
ints: 2
ints: 0
ints: 3
ints: 2
ints: 0
ints: 0
ints: 2
ints: 1
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_hitrates"
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
floats: 1
type: FLOATS
}
attribute {
name: "nodes_missing_value_tracks_true"
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_modes"
strings: "BRANCH_LEQ"
strings: "LEAF"
strings: "BRANCH_LEQ"
strings: "BRANCH_LEQ"
strings: "LEAF"
strings: "LEAF"
strings: "BRANCH_LEQ"
strings: "BRANCH_LEQ"
strings: "LEAF"
strings: "LEAF"
strings: "LEAF"
type: STRINGS
}
attribute {
name: "nodes_nodeids"
ints: 0
ints: 1
ints: 2
ints: 3
ints: 4
ints: 5
ints: 6
ints: 7
ints: 8
ints: 9
ints: 10
type: INTS
}
attribute {
name: "nodes_treeids"
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_truenodeids"
ints: 1
ints: 0
ints: 3
ints: 4
ints: 0
ints: 0
ints: 7
ints: 8
ints: 0
ints: 0
ints: 0
type: INTS
}
attribute {
name: "nodes_values"
floats: 2.69999981
floats: 0
floats: 1.75
floats: 5.29999971
floats: 0
floats: 0
floats: 4.85
floats: 3.1
floats: 0
floats: 0
floats: 0
type: FLOATS
}
attribute {
name: "post_transform"
s: "NONE"
type: STRING
}
domain: "ai.onnx.ml"
}
node {
input: "label"
output: "output_label"
name: "Cast"
op_type: "Cast"
attribute {
name: "to"
i: 7
type: INT
}
domain: ""
}
node {
input: "probabilities"
output: "output_probability"
name: "ZipMap"
op_type: "ZipMap"
attribute {
name: "classlabels_int64s"
ints: 0
ints: 1
ints: 2
type: INTS
}
domain: "ai.onnx.ml"
}
name: "be257cd5d5d64ede939450f40904c278"
input {
name: "float_input"
type {
tensor_type {
elem_type: 1
shape {
dim {
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "output_label"
type {
tensor_type {
elem_type: 7
shape {
dim {
}
}
}
}
}
output {
name: "output_probability"
type {
sequence_type {
elem_type {
map_type {
key_type: 7
value_type {
tensor_type {
elem_type: 1
}
}
}
}
}
}
}
}
opset_import {
domain: ""
version: 9
}
opset_import {
domain: "ai.onnx.ml"
version: 1
}
And to disable it.
logger.setLevel(logging.INFO)
convert_sklearn(clr, initial_types=initial_type, target_opset=12)
logger.setLevel(logging.WARNING)
Versions used for this example
print("numpy:", numpy.__version__)
print("scikit-learn:", sklearn.__version__)
print("onnx: ", onnx.__version__)
print("onnxruntime: ", rt.__version__)
print("skl2onnx: ", skl2onnx.__version__)
numpy: 2.2.0
scikit-learn: 1.6.0
onnx: 1.18.0
onnxruntime: 1.21.0+cu126
skl2onnx: 1.18.0
Total running time of the script: (0 minutes 0.088 seconds)