Open Neural Network Exchange for AI model interoperability and machine learning frameworks
—
Convert ONNX models between different IR versions and operator set versions to maintain compatibility across framework versions. This module enables model migration and ensures compatibility with different ONNX runtime versions.
Convert models between different ONNX versions with automatic operator and type updates.
def convert_version(model, target_version):
"""
Convert ONNX model to target version.
Parameters:
- model: ModelProto to convert
- target_version: Target ONNX opset version (integer)
Returns:
ModelProto: Converted model with updated operators and types
Raises:
ValueError: If conversion is not possible or target version is invalid
RuntimeError: If conversion fails due to unsupported operators
"""import onnx
from onnx import version_converter
# Load model with older opset version
model = onnx.load_model("model_v11.onnx")
print(f"Original opset version: {model.opset_import[0].version}")
# Convert to newer version
try:
converted_model = version_converter.convert_version(model, 14)
print(f"Converted to opset version: {converted_model.opset_import[0].version}")
# Validate converted model
onnx.checker.check_model(converted_model)
print("Conversion successful and model is valid!")
# Save converted model
onnx.save_model(converted_model, "model_v14.onnx")
except Exception as e:
print(f"Conversion failed: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-onnx