注意
请前往文末下载完整的示例代码。
日志记录,verbose¶
如果 pipeline 中包含没有关联转换器的对象,或者其中某个对象由自定义转换器映射,转换可能会失败。如果错误消息不够明确,可以启用日志记录。
训练模型¶
使用随机森林和 iris 数据集的一个非常基本的例子。
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()
将模型转换为 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]
使用 verbose 参数进行转换¶
verbose 是一个参数,用于在标准输出上打印消息。它会告知调用了哪个转换器。verbose=1 通常表示 skl2onnx 在转换 pipeline 时正在做什么。verbose=2+ 保留用于转换器内部的信息。
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
}
使用日志记录进行转换¶
这是非常详细的日志记录。它会告知哪些操作符或变量(转换器的输出)正在被处理,创建了哪个节点……这些信息在实现自定义转换器时可能非常有用。
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
}
然后禁用它。
logger.setLevel(logging.INFO)
convert_sklearn(clr, initial_types=initial_type, target_opset=12)
logger.setLevel(logging.WARNING)
本例使用的版本
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
脚本总运行时间: (0 分钟 0.088 秒)