onnx_light.tools.translate#
Translate an ONNX model or graph into Python code that rebuilds it.
Two output flavours (api) are supported:
"onnx-compact"– a single nested expression building the model withonnx_light.onnx.helper(oh.make_model(oh.make_graph([...], ...))), mirroring the onnx-compact API of yet-another-onnx-builder."builder"– a plain Python script that rebuilds the same model with the incrementalonnx_light.onnx_core.graph_builder.GraphBuilder(g.make_input(...),g.make_node(...),g.make_output(...),g.to_onnx(...)).
Both flavours are pure Python and only rely on the attributes of the standard
ONNX message types (ModelProto, GraphProto, NodeProto,
ValueInfoProto, TensorProto, AttributeProto and
TensorShapeProto); they therefore work both with messages built by
onnx_light and with messages built by the upstream onnx package.
Example:
from onnx_light.tools import translate, translate_header
code = translate_header("onnx-compact") + translate(model, api="onnx-compact")
print(code)
- onnx_light.tools.translate.translate(proto: Any, api: str = 'onnx-compact') str#
Translates an ONNX model or graph into Python code that rebuilds it.
- Parameters:
proto – a
ModelProtoorGraphProto(or a file path to load).api – target flavour,
"onnx-compact"(default) or"builder".
- Returns:
The generated Python code as a string (without the import header, see
translate_header()).
- onnx_light.tools.translate.translate_header(api: str = 'onnx-compact') str#
Returns the import header required by the code produced by
translate().- Parameters:
api – target flavour,
"onnx-compact"or"builder".- Returns:
The import header as a string ending with a trailing newline.