Simple ONNX Name Generator

2.0.1 · active · verified Thu Apr 16

sng4onnx is a Python library and CLI tool designed to automatically generate and assign an operation (OP) name to each operation within an ONNX (Open Neural Network Exchange) file, particularly useful for older format models lacking explicit OP names. The library is currently active, with version 2.0.1, and maintains a frequent release cadence, often rolling out updates for bug fixes and minor feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `sng4onnx` to process an ONNX model. It begins by programmatically creating a simple ONNX model, then passes it to the `generate` function to automatically assign operation names. Finally, it saves and verifies the processed model.

import onnx
from onnx import helper, TensorProto
import numpy as np
from sng4onnx import generate
import os

# 1. Create a dummy ONNX model for demonstration
def create_dummy_onnx(path):
    # Define graph inputs
    X = helper.make_tensor_value_info('X', TensorProto.FLOAT, [1, 2, 3])
    Y = helper.make_tensor_value_info('Y', TensorProto.FLOAT, [1, 2, 3])

    # Define graph outputs
    Z = helper.make_tensor_value_info('Z', TensorProto.FLOAT, [1, 2, 3])

    # Create a node (Mul operator)
    node_def = helper.make_node(
        'Add',
        ['X', 'Y'],
        ['Z'],
        name='MyAddOperation' # Can be empty in an 'old format' model
    )

    # Create the graph
    graph_def = helper.make_graph(
        [node_def],
        'simple_graph',
        [X, Y],
        [Z]
    )

    # Create the model
    model_def = helper.make_model(graph_def, producer_name='dummy-model')

    # Save the model
    onnx.save(model_def, path)

input_onnx_path = 'input_model.onnx'
output_onnx_path = 'output_model_renamed.onnx'
create_dummy_onnx(input_onnx_path)

# 2. Use sng4onnx to generate/assign OP names
print(f"Processing {input_onnx_path}...")
renamed_model = generate(
    input_onnx_file_path=input_onnx_path,
    output_onnx_file_path=output_onnx_path,
    non_verbose=False
)

# 3. Verify the output (optional)
if os.path.exists(output_onnx_path):
    print(f"Successfully generated {output_onnx_path}")
    loaded_model = onnx.load(output_onnx_path)
    print(f"Nodes in renamed model: {[node.name for node in loaded_model.graph.node]}")
else:
    print("Error: Output model not found.")

# Clean up dummy files
os.remove(input_onnx_path)
os.remove(output_onnx_path)

view raw JSON →