Metadata

ONNX format contains metadata related to how the model was produced. It is useful when the model is deployed to production to keep track of which instance was used at a specific time. Let’s see how to do that with a simple logistic regression model trained with scikit-learn.

import skl2onnx
import onnxruntime
import sklearn
import numpy
from onnxruntime import InferenceSession
import onnx
from onnxruntime.datasets import get_example

example = get_example("logreg_iris.onnx")

model = onnx.load(example)

print("doc_string={}".format(model.doc_string))
print("domain={}".format(model.domain))
print("ir_version={}".format(model.ir_version))
print("metadata_props={}".format(model.metadata_props))
print("model_version={}".format(model.model_version))
print("producer_name={}".format(model.producer_name))
print("producer_version={}".format(model.producer_version))
doc_string=
domain=onnxml
ir_version=3
metadata_props=[]
model_version=0
producer_name=OnnxMLTools
producer_version=1.2.0.0116

With ONNX Runtime:

sess = InferenceSession(example, providers=["CPUExecutionProvider"])
meta = sess.get_modelmeta()

print("custom_metadata_map={}".format(meta.custom_metadata_map))
print("description={}".format(meta.description))
print("domain={}".format(meta.domain))
print("graph_name={}".format(meta.graph_name))
print("producer_name={}".format(meta.producer_name))
print("version={}".format(meta.version))
custom_metadata_map={}
description=
domain=onnxml
graph_name=3c59201b940f410fa29dc71ea9d5767d
producer_name=OnnxMLTools
version=0

Versions used for this example

print("numpy:", numpy.__version__)
print("scikit-learn:", sklearn.__version__)
print("onnx: ", onnx.__version__)
print("onnxruntime: ", onnxruntime.__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.102 seconds)

Gallery generated by Sphinx-Gallery