Aidge ONNX API#

Import#

Method related to the import of an ONNX graph into Aidge.

aidge_onnx.load_onnx(filename: str, verbose: bool = False)#

Load an ONNX file and convert it into a aidge_core.GraphView.

Parameters:
  • filename (str) – Path to the ONNX file to load

  • verbose (bool, optional) – If True, display information on the terminal, default=False

Returns:

Aidge aidge_core.GraphView corresponding to the ONNX model described by the onnx file filename

Return type:

aidge_core.GraphView

Register import functions#

aidge_onnx.node_import.register_import(key: str, converter_function: Callable[[NodeProto, List[Node]], Node]) None#

Add a new conversion function to the aidge_onnx.node_import.ONNX_NODE_CONVERTER_ dictionnary. A conversion function must have the following signature : (onnx.NodeProto, List[aidge_core.Node], int) -> aidge_core.Node

Parameters:
  • key (str) – This chain of characters must correspond to the ONNX type (onnx/onnx) of the operator (in lowercase).

  • converter_function (Callable[[onnx.NodeProto, List[Tuple[aidge_core.Node], int], int], aidge_core.Node]) – Function which take as an input the ONNX node and a list of aidge nodes and output the corresponding Aidge node. This function must not connect the node. If the function fails to convert the operator, it must return None.

aidge_onnx.node_import.supported_operators() List[str]#

Return a list of operators supported by the ONNX import.

Returns:

List of string representing the operators supported by the ONNX import.

Return type:

List[str]

aidge_onnx.node_import.auto_register_import(*args) Callable#

Decorator used to register a converter to the aidge_onnx.node_import.ONNX_NODE_CONVERTER_

Example:

@auto_register_import("myOp")
def my_op_onverter(onnx_node, input_nodes, opset):
    ...
Parameters:

args – Set of keys (str) which should correspond to the operator type defined by ONNX (onnx/onnx).

aidge_onnx.node_import.ONNX_NODE_CONVERTER_#

This defaultdict maps the ONNX type to a function which can convert an ONNX Node into an Aidge Node. This means that if a key is missing from aidge_onnx.node_import.ONNX_NODE_CONVERTER_, it will return the function aidge_onnx.node_import.generic.import_generic() which import the ONNX node as an Aidge generic operator. It is possible to add keys to this dictionnary at runtime using aidge_onnx.node_import.register_converter() or aidge_onnx.node_import.auto_register()

Converters ONNX to Aidge#

aidge_onnx.node_import.generic.import_generic(onnx_node: NodeProto, input_nodes: List[Tuple[Node, int]], opset=None) Node#
Parameters:
  • onnx_node (onnx.NodeProto) – ONNX node to convert

  • input_nodes (List[Tuple[aidge_core.Node, int]]) – List of tuple of Aidge nodes with their output index, which constitute the input of the current node

  • opset (int, optional) – Indicate opset version of the ONNX model, default=None

Export#

Method related to the export of an Aidge aidge_core.GraphView to an ONNX file.

aidge_onnx.export_onnx(graph_view: GraphView, path_to_save: str, inputs_dims: Mapping[str, List[List[int]]] | None = None, outputs_dims: Mapping[str, List[List[int]]] | None = None, verbose: bool = False, enable_custom_op: bool = False, opset: int | None = None)#

Export a aidge_core.GraphView to an ONNX file.

Parameters:
  • graph_view (aidge_core.GraphView) – aidge_core.GraphView to convert.

  • path_to_save (str) – Path where to save the ONNX file, example test.onnx

  • inputs_dims (Mapping[str, List[List[int]]], optional) – input dimensions of the network, if provided, outputs_dims must also be filled, this argument is a map, where the key is the name of the input node and the value is a list of dimensions ordered by the input index, defaults to None

  • outputs_dims (Mapping[str, List[List[int]]], optional) – output dimensions of the network, if provided, inputs_dims must also be filled, this argument is a map, where the key is the name of the output node and the value is a list of dimensions ordered by the output index, defaults to None

  • verbose (bool, optional) – If true, verbosity is activated, defaults to False

  • enable_custom_op (bool, optional) – If True, export will not fail for aidge_core.GenericOperator and will add the operator schema to a custom aidge domain, defaults to False

  • opset (int, optional) – The version of the ONNX opset generated, defaults to None

Register export functions#

aidge_onnx.node_export.register_export(key, parser_function) None#

Add a new conversion function to the aidge_onnx.node_export.AIDGE_NODE_CONVERTER_ dictionnary. A conversion function must have the following signature : (aidge_core.Node, List[str], List[str], int, bool) -> onnx.NodeProto

Parameters:
  • key (str) – This chain of characters must correspond to the Aidge type.

  • converter_function (Callable[[aidge_core.Node, List[str], List[str], int, bool], onnx.NodeProto]) – Function which take as an input an Aidge node, list of inputs name, outputs name and a boolean to handle verbosity level.

aidge_onnx.node_export.supported_operators() List[str]#

Return a list of operators supported by the ONNX export.

Returns:

List of string representing the operators supported by the ONNX import.

Return type:

List[str]

aidge_onnx.node_export.auto_register_export(*args) Callable#

Decorator used to register a converter to the aidge_onnx.node_export.AIDGE_NODE_CONVERTER_

Example:

@auto_register_export("myOp")
def my_op_converter(aidge_node, node_inputs_name, node_outputs_name, verbose):
    ...
Parameters:

args – Set of keys (str) which should correspond to the operator type defined by Aidge.

Converters Aidge to ONNX#

aidge_onnx.node_export.generic_export.generic_export(aidge_node: Node, node_inputs_name, node_outputs_name, opset: int | None = None, enable_custom_op: bool = False, verbose: bool = False) None#

Function to export a aidge_core.GenericOperator to an ONNX node

Parameters:
  • aidge_node (aidge_core.Node) – Aidge node containing a aidge_core.GenericOperator

  • node_inputs_name (list[str]) – list of names of inputs node

  • node_outputs_name (list[str]) – list of names of outputs node

  • opset (int, optional) – opset to use for the export, defaults to None

  • enable_custom_op (bool, optional) – If True, the export will not fait if the type associated to the aidge_core.GenericOperator is not , defaults to False

  • verbose (bool, optional) – If true, verbosity is activated, defaults to False