Tools#

check_model#

onnx.checker.check_model(model: Union[ModelProto, str, bytes], full_check: bool = False) None#

Check the consistency of a model. An exception is raised if the test fails.

Parameters:
  • model (ModelProto) – model to check

  • full_check (bool) – if True, the function checks shapes can be inferred

convert_version#

onnx.version_converter.convert_version(model: ModelProto, target_version: int) ModelProto#

Apply the version conversion on the serialized ModelProto.

Parameters:
  • input (ModelProto) – model

  • target_version (int) – target opset version

Returns:

return (ModelProto) converted model

Raises Exceptions:

RuntimeError when some necessary conversion is not supported

Supported adapters:
  • Add from Opset 7 to Opset 6

  • Add from Opset 6 to Opset 5

  • Add from Opset 6 to Opset 7

  • Add from Opset 5 to Opset 6

  • Mul from Opset 6 to Opset 7

  • Mul from Opset 7 to Opset 6

  • Mul from Opset 6 to Opset 5

  • Mul from Opset 5 to Opset 6

  • Gemm from Opset 7 to Opset 6

  • Gemm from Opset 6 to Opset 5

  • Gemm from Opset 6 to Opset 7

  • Gemm from Opset 5 to Opset 6

  • Relu from Opset 6 to Opset 5

  • Relu from Opset 5 to Opset 6

  • BatchNorm from Opset 7 to Opset 6

  • BatchNorm from Opset 6 to Opset 7

  • BatchNorm from Opset 6 to Opset 5

  • BatchNorm from Opset 5 to Opset 6

  • Concat from Opset 4 to Opset 3

  • Concat from Opset 3 to Opset 4

  • Reshape from Opset 5 to Opset 4

  • Reshape from Opset 4 to Opset 5

  • Sum from Opset 7 to Opset 8

  • Sum from Opset 8 to Opset 7

  • Sum from Opset 6 to Opset 5

  • Sum from Opset 5 to Opset 6

  • MaxPool from Opset 8 to Opset 7

  • MaxPool from Opset 7 to Opset 8

  • AveragePool from Opset 7 to Opset 6

  • AveragePool from Opset 6 to Opset 7

  • Dropout from Opset 7 to Opset 6

  • Dropout from Opset 6 to Opset 5

  • Dropout from Opset 6 to Opset 7

  • Dropout from Opset 5 to Opset 6

  • RNN from Opset 13 to Opset 14

  • RNN from Opset 14 to Opset 13

  • GRU from Opset 13 to Opset 14

  • GRU from Opset 14 to Opset 13

  • LSTM from Opset 13 to Opset 14

  • LSTM from Opset 14 to Opset 13

Unsupported adapters:
  • Min from Opset 8 to Opset 7

  • Min from Opset 7 to Opset 8

  • Min from Opset 6 to Opset 5

  • Min from Opset 5 to Opset 6

  • Mean from Opset 8 to Opset 7

  • Mean from Opset 7 to Opset 8

  • Mean from Opset 6 to Opset 5

  • Mean from Opset 5 to Opset 6

  • Max from Opset 8 to Opset 7

  • Max from Opset 7 to Opset 8

  • Max from Opset 6 to Opset 5

  • Max from Opset 5 to Opset 6

  • Xor from Opset 6 to Opset 7

  • Xor from Opset 7 to Opset 6

  • Upsample from Opset 6 to Opset 7

  • Upsample from Opset 7 to Opset 6

  • Sub from Opset 6 to Opset 7

  • Sub from Opset 7 to Opset 6

  • Sub from Opset 6 to Opset 5

  • Sub from Opset 5 to Opset 6

  • RNN from Opset 6 to Opset 7

  • RNN from Opset 7 to Opset 6

  • Pow from Opset 6 to Opset 7

  • Pow from Opset 7 to Opset 6

  • PRelu from Opset 6 to Opset 7

  • PRelu from Opset 7 to Opset 6

  • PRelu from Opset 6 to Opset 5

  • PRelu from Opset 5 to Opset 6

  • Or from Opset 6 to Opset 7

  • Or from Opset 7 to Opset 6

  • Less from Opset 6 to Opset 7

  • Less from Opset 7 to Opset 6

  • LSTM from Opset 6 to Opset 7

  • LSTM from Opset 7 to Opset 6

  • Greater from Opset 6 to Opset 7

  • Greater from Opset 7 to Opset 6

  • GRU from Opset 6 to Opset 7

  • GRU from Opset 7 to Opset 6

  • GRU from Opset 3 to Opset 2

  • GRU from Opset 2 to Opset 3

  • Equal from Opset 6 to Opset 7

  • Equal from Opset 7 to Opset 6

  • Div from Opset 6 to Opset 7

  • Div from Opset 7 to Opset 6

  • Div from Opset 6 to Opset 5

  • Div from Opset 5 to Opset 6

  • And from Opset 6 to Opset 7

  • And from Opset 7 to Opset 6

  • And from Opset 6 to Opset 5

  • And from Opset 5 to Opset 6

  • Tile from Opset 6 to Opset 5

  • Tile from Opset 5 to Opset 6

  • Sqrt from Opset 6 to Opset 5

  • Sqrt from Opset 5 to Opset 6

  • Sigmoid from opset 6 to opset 5

  • Sigmoid from opset 5 to opset 6

  • Selu from opset 6 to opset 5

  • Selu from opset 5 to opset 6

  • Reciprocal from opset 6 to opset 5

  • Reciprocal from opset 5 to opset 6

  • Neg from opset 6 to opset 5

  • Neg from opset 5 to opset 6

  • Log from opset 6 to opset 5

  • Log from opset 5 to opset 6

  • LeakyRelu from opset 6 to opset 5

  • LeakyRelu from opset 5 to opset 6

  • InstanceNormalization from opset 6 to opset 5

  • InstanceNormalization from opset 5 to opset 6

  • HardSigmoid from opset 6 to opset 5

  • HardSigmoid from opset 5 to opset 6

  • Floor from opset 6 to opset 5

  • Floor from opset 5 to opset 6

  • Exp from opset 6 to opset 5

  • Exp from opset 5 to opset 6

  • Elu from opset 6 to opset 5

  • Elu from opset 5 to opset 6

  • Clip from opset 6 to opset 5

  • Clip from opset 5 to opset 6

  • Ceil from opset 6 to opset 5

  • Ceil from opset 5 to opset 6

  • Cast from opset 6 to opset 5

  • Cast from opset 5 to opset 6

  • Abs from opset 6 to opset 5

  • Abs from opset 5 to opset 6

  • Split from opset 2 to opset 1

  • Split from opset 1 to opset 2

  • Pad from opset 2 to opset 1

  • Pad from opset 1 to opset 2

  • LpPool from opset 2 to opset 1

  • LpPool from opset 1 to opset 2

  • GlobalLpPool from opset 2 to opset 1

  • GlobalLpPool from opset 1 to opset 2

extract_model#

onnx.utils.extract_model(input_path: str, output_path: str, input_names: List[str], output_names: List[str], check_model: bool = True) None#

Extracts sub-model from an ONNX model.

The sub-model is defined by the names of the input and output tensors exactly.

Note: For control-flow operators, e.g. If and Loop, the _boundary of sub-model_, which is defined by the input and output tensors, should not _cut through_ the subgraph that is connected to the _main graph_ as attributes of these operators.

Parameters:
  • input_path (string) – The path to original ONNX model.

  • output_path (string) – The path to save the extracted ONNX model.

  • input_names (list of string) – The names of the input tensors that to be extracted.

  • output_names (list of string) – The names of the output tensors that to be extracted.

  • check_model (bool) – Whether to run model checker on the extracted model.

merge_graphs#

onnx.compose.merge_graphs(g1: GraphProto, g2: GraphProto, io_map: List[Tuple[str, str]], inputs: Optional[List[str]] = None, outputs: Optional[List[str]] = None, prefix1: Optional[str] = None, prefix2: Optional[str] = None, name: Optional[str] = None, doc_string: Optional[str] = None) GraphProto#

Combines two ONNX graphs into a single one.

The combined graph is defined by connecting the specified set of outputs/inputs. Those inputs/outputs not specified in the io_map argument will remain as inputs/outputs of the combined graph.

Parameters:
  • g1 (GraphProto) – First graph

  • g2 (GraphProto) – Second graph

  • io_map (list of pairs of string) – The pairs of names [(out0, in0), (out1, in1), …] representing outputs of the first graph and inputs of the second to be connected

  • inputs (list of string) – Optional list of inputs to be included in the combined graph By default, all inputs not present in the io_map argument will be included in the combined model

  • outputs (list of string) – Optional list of outputs to be included in the combined graph By default, all outputs not present in the io_map argument will be included in the combined model

  • prefix1 (string) – Optional prefix to be added to all names in g1

  • prefix2 (string) – Optional prefix to be added to all names in g2

  • name (string) – Optional name for the combined graph By default, the name is g1.name and g2.name concatenated with an undescore delimiter

  • doc_string (string) – Optional docstring for the combined graph If not provided, a default docstring with the concatenation of g1 and g2 docstrings is used

Returns:

GraphProto

merge_models#

onnx.compose.merge_models(m1: ModelProto, m2: ModelProto, io_map: List[Tuple[str, str]], inputs: Optional[List[str]] = None, outputs: Optional[List[str]] = None, prefix1: Optional[str] = None, prefix2: Optional[str] = None, name: Optional[str] = None, doc_string: Optional[str] = None, producer_name: Optional[str] = 'onnx.compose.merge_models', producer_version: Optional[str] = '1.0', domain: Optional[str] = '', model_version: Optional[int] = 1) ModelProto#

Combines two ONNX models into a single one.

The combined model is defined by connecting the specified set of outputs/inputs. Those inputs/outputs not specified in the io_map argument will remain as inputs/outputs of the combined model.

Both models should have the same IR version, and same operator sets imported.

Parameters:
  • m1 (ModelProto) – First model

  • m2 (ModelProto) – Second model

  • io_map (list of pairs of string) – The pairs of names [(out0, in0), (out1, in1), …] representing outputs of the first graph and inputs of the second to be connected

  • inputs (list of string) – Optional list of inputs to be included in the combined graph By default, all inputs not present in the io_map argument will be included in the combined model

  • outputs (list of string) – Optional list of outputs to be included in the combined graph By default, all outputs not present in the io_map argument will be included in the combined model

  • prefix1 (string) – Optional prefix to be added to all names in m1

  • prefix2 (string) – Optional prefix to be added to all names in m2

  • name (string) – Optional name for the combined graph By default, the name is g1.name and g2.name concatenated with an undescore delimiter

  • doc_string (string) – Optional docstring for the combined graph If not provided, a default docstring with the concatenation of g1 and g2 docstrings is used

  • producer_name (string) – Optional producer name for the combined model. Default: ‘onnx.compose’

  • producer_version (string) – Optional producer version for the combined model. Default: “1.0”

  • domain (string) – Optional domain of the combined model. Default: “”

  • model_version (int) – Optional version of the graph encoded. Default: 1

Returns:

ModelProto

prefix#

onnx.compose.add_prefix_graph(graph: GraphProto, prefix: str, rename_nodes: Optional[bool] = True, rename_edges: Optional[bool] = True, rename_inputs: Optional[bool] = True, rename_outputs: Optional[bool] = True, rename_initializers: Optional[bool] = True, rename_value_infos: Optional[bool] = True, inplace: Optional[bool] = False) GraphProto#

Adds a prefix to names of elements in a graph: nodes, edges, inputs, outputs, initializers, sparse initializer, value infos.

It can be used as a utility before merging graphs that have overlapping names. Empty names are not prefixed.

Parameters:
  • graph (GraphProto) – Graph

  • prefix (str) – Prefix to be added to each name in the graph

  • rename_nodes (bool) – Whether to prefix node names

  • rename_edges (bool) – Whether to prefix node edge names

  • rename_inputs (bool) – Whether to prefix input names

  • rename_outputs (bool) – Whether to prefix output names

  • rename_initializers (bool) – Whether to prefix initializer and sparse initializer names

  • rename_value_infos (bool) – Whether to prefix value info names

  • inplace (bool) – If True, mutates the graph directly. Otherwise, a copy will be created

Returns:

GraphProto

onnx.compose.add_prefix(model: ModelProto, prefix: str, rename_nodes: Optional[bool] = True, rename_edges: Optional[bool] = True, rename_inputs: Optional[bool] = True, rename_outputs: Optional[bool] = True, rename_initializers: Optional[bool] = True, rename_value_infos: Optional[bool] = True, rename_functions: Optional[bool] = True, inplace: Optional[bool] = False) ModelProto#

Adds a prefix to names of elements in a graph: nodes, edges, inputs, outputs, initializers, sparse initializer, value infos, and local functions.

It can be used as a utility before merging graphs that have overlapping names. Empty names are not _prefixed.

Parameters:
  • model (ModelProto) – Model

  • prefix (str) – Prefix to be added to each name in the graph

  • rename_nodes (bool) – Whether to prefix node names

  • rename_edges (bool) – Whether to prefix node edge names

  • rename_inputs (bool) – Whether to prefix input names

  • rename_outputs (bool) – Whether to prefix output names

  • rename_initializers (bool) – Whether to prefix initializer and sparse initializer names

  • rename_value_infos (bool) – Whether to prefix value info nanes

  • rename_functions (bool) – Whether to prefix local function names

  • inplace (bool) – If True, mutates the model directly. Otherwise, a copy will be created

Returns:

ModelProto

dimension#

onnx.compose.expand_out_dim(model: ModelProto, dim_idx: int, inplace: Optional[bool] = False) ModelProto#

Inserts an extra dimension with extent 1 to each output in the graph.

Inserts an Unsqueeze node for each output. It can be used as a utility before merging graphs, for example when the second one expects a batch dimension.

Parameters:
  • model (ModelProto) – Model

  • dim_idx (int) – Index of the dimension to be inserted. A negative value means counting dimensions from the back.

  • inplace (bool) – If True, mutates the model directly. Otherwise, a copy will be created

Returns:

ModelProto

onnx.compose.expand_out_dim_graph(graph: GraphProto, dim_idx: int, inplace: Optional[bool] = False) GraphProto#

Inserts an extra dimension with extent 1 to each output in the graph.

Inserts an Unsqueeze node for each output. It can be used as a utility before merging graphs, for example when the second one expects a batch dimension.

Parameters:
  • graph (GraphProto) – Graph

  • dim_idx (int) – Index of the dimension to be inserted. A negative value means counting dimensions from the back.

  • inplace (bool) – If True, mutates the model directly. Otherwise, a copy will be created

Returns:

GraphProto