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)
[1 0 1 0 0 2 1 0 2 2 1 2 2 0 2 0 0 0 2 0 1 2 1 2 2 2 2 0 1 0 1 0 0 1 0 0 1
0]
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
ModelProto(ir_version=7, opset_import={'': 9, 'ai.onnx.ml': 1}, domain='ai.onnx', producer_name='skl2onnx', producer_version='1.20.0', graph=GraphProto('c6db1949e52e4909946116470abbafa5', input=<1 inputs>, output=<2 outputs>, node=<3 nodes>))
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)
ModelProto(ir_version=7, opset_import={'': 9, 'ai.onnx.ml': 1}, domain='ai.onnx', producer_name='skl2onnx', producer_version='1.20.0', graph=GraphProto('3ee1a4aa02a84ae6b608d78f61d3bdb1', input=<1 inputs>, output=<2 outputs>, node=<3 nodes>))
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.4.1
scikit-learn: 1.8.0
onnx: 1.21.0
onnxruntime: 1.24.0
skl2onnx: 1.20.0
Total running time of the script: (0 minutes 0.043 seconds)