Implementation:Microsoft Onnxruntime PlotMetadata
| Knowledge Sources | |
|---|---|
| Domains | Documentation, Python, Examples |
| Last Updated | 2026-02-10 04:00 GMT |
Overview
A Python example script demonstrating how to inspect ONNX model metadata using both the onnx library and ONNX Runtime.
Description
The `plot_metadata.py` script is a Sphinx gallery documentation example showing two methods for accessing ONNX model metadata:
1. Using the onnx library: Loads the model via `onnx.load()` and prints `ModelProto` fields:
- `doc_string`: Model documentation string. - `domain`: Model domain. - `ir_version`: ONNX IR version. - `metadata_props`: Custom metadata key-value pairs. - `model_version`: Model version number. - `producer_name`: Name of the tool that produced the model. - `producer_version`: Version of the producer tool.
2. Using ONNX Runtime: Creates an `InferenceSession` and calls `get_modelmeta()` to access:
- `custom_metadata_map`: Custom metadata dictionary. - `description`: Model description. - `domain`: Model domain. - `graph_name`: Name of the computation graph. - `producer_name`: Producer tool name. - `version`: Model version.
The example uses the bundled `logreg_iris.onnx` dataset model, a logistic regression trained on Iris data and converted with sklearn-onnx.
Usage
Use this script as a reference for extracting model provenance and metadata from ONNX models, which is important for model governance and deployment tracking.
Code Reference
Source Location
- Repository: Microsoft_Onnxruntime
- File: docs/python/examples/plot_metadata.py
- Lines: 1-46
Signature
from onnxruntime.datasets import get_example
import onnx
import onnxruntime as rt
example = get_example("logreg_iris.onnx")
model = onnx.load(example)
# Access: model.doc_string, model.domain, model.ir_version, etc.
sess = rt.InferenceSession(example, providers=rt.get_available_providers())
meta = sess.get_modelmeta()
# Access: meta.custom_metadata_map, meta.description, meta.domain, etc.
Import
from onnxruntime.datasets import get_example
import onnx
import onnxruntime as rt
I/O Contract
| Method | Input | Output | Description |
|---|---|---|---|
| onnx.load | file path | ModelProto | Loads ONNX model with all metadata fields |
| sess.get_modelmeta | (none) | ModelMetadata | Returns ORT-accessible model metadata |
Usage Examples
from onnxruntime.datasets import get_example
import onnxruntime as rt
example = get_example("logreg_iris.onnx")
sess = rt.InferenceSession(example, providers=rt.get_available_providers())
meta = sess.get_modelmeta()
print(f"Producer: {meta.producer_name}")
print(f"Domain: {meta.domain}")
print(f"Custom metadata: {meta.custom_metadata_map}")