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)
[2 0 0 1 0 0 0 1 1 0 2 0 0 1 2 1 0 0 0 2 1 0 0 1 2 2 2 1 2 0 2 1 1 2 0 0 0
 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

ir_version: 7
opset_import {
  domain: ""
  version: 9
}
opset_import {
  domain: "ai.onnx.ml"
  version: 1
}
producer_name: "skl2onnx"
producer_version: "1.16.0"
domain: "ai.onnx"
model_version: 0
doc_string: ""
graph {
  node {
    input: "float_input"
    output: "label"
    output: "probabilities"
    name: "TreeEnsembleClassifier"
    op_type: "TreeEnsembleClassifier"
    domain: "ai.onnx.ml"
    attribute {
      name: "class_ids"
      type: INTS
      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
      ints: 0
      ints: 1
      ints: 2
      ints: 0
      ints: 1
      ints: 2
      ints: 0
      ints: 1
      ints: 2
    }
    attribute {
      name: "class_nodeids"
      type: INTS
      ints: 1
      ints: 1
      ints: 1
      ints: 5
      ints: 5
      ints: 5
      ints: 6
      ints: 6
      ints: 6
      ints: 8
      ints: 8
      ints: 8
      ints: 10
      ints: 10
      ints: 10
      ints: 11
      ints: 11
      ints: 11
      ints: 14
      ints: 14
      ints: 14
      ints: 15
      ints: 15
      ints: 15
      ints: 16
      ints: 16
      ints: 16
    }
    attribute {
      name: "class_treeids"
      type: INTS
      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
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
    }
    attribute {
      name: "class_weights"
      type: FLOATS
      floats: 1
      floats: 0
      floats: 0
      floats: 0
      floats: 0
      floats: 1
      floats: 0
      floats: 1
      floats: 0
      floats: 0
      floats: 0
      floats: 1
      floats: 0
      floats: 1
      floats: 0
      floats: 0
      floats: 0
      floats: 1
      floats: 0
      floats: 1
      floats: 0
      floats: 0
      floats: 0
      floats: 1
      floats: 0
      floats: 0
      floats: 1
    }
    attribute {
      name: "classlabels_int64s"
      type: INTS
      ints: 0
      ints: 1
      ints: 2
    }
    attribute {
      name: "nodes_falsenodeids"
      type: INTS
      ints: 2
      ints: 0
      ints: 12
      ints: 7
      ints: 6
      ints: 0
      ints: 0
      ints: 9
      ints: 0
      ints: 11
      ints: 0
      ints: 0
      ints: 16
      ints: 15
      ints: 0
      ints: 0
      ints: 0
    }
    attribute {
      name: "nodes_featureids"
      type: INTS
      ints: 2
      ints: 0
      ints: 3
      ints: 2
      ints: 0
      ints: 0
      ints: 0
      ints: 3
      ints: 0
      ints: 2
      ints: 0
      ints: 0
      ints: 2
      ints: 0
      ints: 0
      ints: 0
      ints: 0
    }
    attribute {
      name: "nodes_hitrates"
      type: FLOATS
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
    }
    attribute {
      name: "nodes_missing_value_tracks_true"
      type: INTS
      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
    }
    attribute {
      name: "nodes_modes"
      type: STRINGS
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "BRANCH_LEQ"
      strings: "BRANCH_LEQ"
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "LEAF"
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "LEAF"
      strings: "BRANCH_LEQ"
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "LEAF"
      strings: "LEAF"
    }
    attribute {
      name: "nodes_nodeids"
      type: INTS
      ints: 0
      ints: 1
      ints: 2
      ints: 3
      ints: 4
      ints: 5
      ints: 6
      ints: 7
      ints: 8
      ints: 9
      ints: 10
      ints: 11
      ints: 12
      ints: 13
      ints: 14
      ints: 15
      ints: 16
    }
    attribute {
      name: "nodes_treeids"
      type: INTS
      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
    }
    attribute {
      name: "nodes_truenodeids"
      type: INTS
      ints: 1
      ints: 0
      ints: 3
      ints: 4
      ints: 5
      ints: 0
      ints: 0
      ints: 8
      ints: 0
      ints: 10
      ints: 0
      ints: 0
      ints: 13
      ints: 14
      ints: 0
      ints: 0
      ints: 0
    }
    attribute {
      name: "nodes_values"
      type: FLOATS
      floats: 2.44999981
      floats: 0
      floats: 1.75
      floats: 4.95
      floats: 4.95
      floats: 0
      floats: 0
      floats: 1.55
      floats: 0
      floats: 5.45
      floats: 0
      floats: 0
      floats: 4.85
      floats: 5.95
      floats: 0
      floats: 0
      floats: 0
    }
    attribute {
      name: "post_transform"
      type: STRING
      s: "NONE"
    }
  }
  node {
    input: "label"
    output: "output_label"
    name: "Cast"
    op_type: "Cast"
    domain: ""
    attribute {
      name: "to"
      type: INT
      i: 7
    }
  }
  node {
    input: "probabilities"
    output: "output_probability"
    name: "ZipMap"
    op_type: "ZipMap"
    domain: "ai.onnx.ml"
    attribute {
      name: "classlabels_int64s"
      type: INTS
      ints: 0
      ints: 1
      ints: 2
    }
  }
  name: "52f3f06613cc4ccfa49e4e385ad0febc"
  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
              }
            }
          }
        }
      }
    }
  }
}

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
opset_import {
  domain: ""
  version: 9
}
opset_import {
  domain: "ai.onnx.ml"
  version: 1
}
producer_name: "skl2onnx"
producer_version: "1.16.0"
domain: "ai.onnx"
model_version: 0
doc_string: ""
graph {
  node {
    input: "float_input"
    output: "label"
    output: "probabilities"
    name: "TreeEnsembleClassifier"
    op_type: "TreeEnsembleClassifier"
    domain: "ai.onnx.ml"
    attribute {
      name: "class_ids"
      type: INTS
      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
      ints: 0
      ints: 1
      ints: 2
      ints: 0
      ints: 1
      ints: 2
      ints: 0
      ints: 1
      ints: 2
    }
    attribute {
      name: "class_nodeids"
      type: INTS
      ints: 1
      ints: 1
      ints: 1
      ints: 5
      ints: 5
      ints: 5
      ints: 6
      ints: 6
      ints: 6
      ints: 8
      ints: 8
      ints: 8
      ints: 10
      ints: 10
      ints: 10
      ints: 11
      ints: 11
      ints: 11
      ints: 14
      ints: 14
      ints: 14
      ints: 15
      ints: 15
      ints: 15
      ints: 16
      ints: 16
      ints: 16
    }
    attribute {
      name: "class_treeids"
      type: INTS
      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
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
      ints: 0
    }
    attribute {
      name: "class_weights"
      type: FLOATS
      floats: 1
      floats: 0
      floats: 0
      floats: 0
      floats: 0
      floats: 1
      floats: 0
      floats: 1
      floats: 0
      floats: 0
      floats: 0
      floats: 1
      floats: 0
      floats: 1
      floats: 0
      floats: 0
      floats: 0
      floats: 1
      floats: 0
      floats: 1
      floats: 0
      floats: 0
      floats: 0
      floats: 1
      floats: 0
      floats: 0
      floats: 1
    }
    attribute {
      name: "classlabels_int64s"
      type: INTS
      ints: 0
      ints: 1
      ints: 2
    }
    attribute {
      name: "nodes_falsenodeids"
      type: INTS
      ints: 2
      ints: 0
      ints: 12
      ints: 7
      ints: 6
      ints: 0
      ints: 0
      ints: 9
      ints: 0
      ints: 11
      ints: 0
      ints: 0
      ints: 16
      ints: 15
      ints: 0
      ints: 0
      ints: 0
    }
    attribute {
      name: "nodes_featureids"
      type: INTS
      ints: 2
      ints: 0
      ints: 3
      ints: 2
      ints: 0
      ints: 0
      ints: 0
      ints: 3
      ints: 0
      ints: 2
      ints: 0
      ints: 0
      ints: 2
      ints: 0
      ints: 0
      ints: 0
      ints: 0
    }
    attribute {
      name: "nodes_hitrates"
      type: FLOATS
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
      floats: 1
    }
    attribute {
      name: "nodes_missing_value_tracks_true"
      type: INTS
      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
    }
    attribute {
      name: "nodes_modes"
      type: STRINGS
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "BRANCH_LEQ"
      strings: "BRANCH_LEQ"
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "LEAF"
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "LEAF"
      strings: "BRANCH_LEQ"
      strings: "BRANCH_LEQ"
      strings: "LEAF"
      strings: "LEAF"
      strings: "LEAF"
    }
    attribute {
      name: "nodes_nodeids"
      type: INTS
      ints: 0
      ints: 1
      ints: 2
      ints: 3
      ints: 4
      ints: 5
      ints: 6
      ints: 7
      ints: 8
      ints: 9
      ints: 10
      ints: 11
      ints: 12
      ints: 13
      ints: 14
      ints: 15
      ints: 16
    }
    attribute {
      name: "nodes_treeids"
      type: INTS
      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
    }
    attribute {
      name: "nodes_truenodeids"
      type: INTS
      ints: 1
      ints: 0
      ints: 3
      ints: 4
      ints: 5
      ints: 0
      ints: 0
      ints: 8
      ints: 0
      ints: 10
      ints: 0
      ints: 0
      ints: 13
      ints: 14
      ints: 0
      ints: 0
      ints: 0
    }
    attribute {
      name: "nodes_values"
      type: FLOATS
      floats: 2.44999981
      floats: 0
      floats: 1.75
      floats: 4.95
      floats: 4.95
      floats: 0
      floats: 0
      floats: 1.55
      floats: 0
      floats: 5.45
      floats: 0
      floats: 0
      floats: 4.85
      floats: 5.95
      floats: 0
      floats: 0
      floats: 0
    }
    attribute {
      name: "post_transform"
      type: STRING
      s: "NONE"
    }
  }
  node {
    input: "label"
    output: "output_label"
    name: "Cast"
    op_type: "Cast"
    domain: ""
    attribute {
      name: "to"
      type: INT
      i: 7
    }
  }
  node {
    input: "probabilities"
    output: "output_probability"
    name: "ZipMap"
    op_type: "ZipMap"
    domain: "ai.onnx.ml"
    attribute {
      name: "classlabels_int64s"
      type: INTS
      ints: 0
      ints: 1
      ints: 2
    }
  }
  name: "9b935cbce5714423910a13167b122e44"
  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
              }
            }
          }
        }
      }
    }
  }
}

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: 1.23.5
scikit-learn: 1.4.dev0
onnx:  1.15.0
onnxruntime:  1.16.0+cu118
skl2onnx:  1.16.0

Total running time of the script: (0 minutes 0.063 seconds)

Gallery generated by Sphinx-Gallery