Tools¶
convert_version¶
- onnx.version_converter.convert_version(model: onnx.onnx_ml_pb2.ModelProto, target_version: int) onnx.onnx_ml_pb2.ModelProto [source]¶
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 [source]¶
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: onnx.onnx_ml_pb2.GraphProto, g2: onnx.onnx_ml_pb2.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) onnx.onnx_ml_pb2.GraphProto [source]¶
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 modeloutputs (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 modelprefix1 (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
prefix¶
- onnx.compose.add_prefix_graph(graph: onnx.onnx_ml_pb2.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) onnx.onnx_ml_pb2.GraphProto [source]¶
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 (Text) – 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
- onnx.compose.add_prefix(model: onnx.onnx_ml_pb2.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) onnx.onnx_ml_pb2.ModelProto [source]¶
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 (Text) – 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
dimension¶
- onnx.compose.expand_out_dim_graph(graph: onnx.onnx_ml_pb2.GraphProto, dim_idx: int, inplace: Optional[bool] = False) onnx.onnx_ml_pb2.GraphProto [source]¶
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.
- onnx.compose.expand_out_dim(model: onnx.onnx_ml_pb2.ModelProto, dim_idx: int, inplace: Optional[bool] = False) onnx.onnx_ml_pb2.ModelProto [source]¶
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
merge_models¶
- onnx.compose.merge_models(m1: onnx.onnx_ml_pb2.ModelProto, m2: onnx.onnx_ml_pb2.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) onnx.onnx_ml_pb2.ModelProto [source]¶
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 modeloutputs (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 modelprefix1 (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