mindspore.onnx.export

View Source On Gitee
mindspore.onnx.export(net, *inputs, file_name, input_names=None, output_names=None, export_params=True, keep_initializers_as_inputs=False, dynamic_axes=None)[source]

Export the MindSpore network into an ONNX model.

Note

  • Support exporting network larger than 2GB. When the network exceeds 2GB, parameters are saved in additional binary files stored in the same directory as the ONNX file.

  • When file_name does not have a suffix, the system will automatically add the suffix .onnx .

Parameters
  • net (Union[Cell, function]) – MindSpore network.

  • inputs (Union[Tensor, list, tuple, Number, bool]) – It represents the inputs of the net , if the network has multiple inputs, set them together.

  • file_name (str) – File name of the model to be exported.

  • input_names (list, optional) – Names to assign to the input nodes of the graph, in order. Default: None .

  • output_names (list, optional) – Names to assign to the output nodes of the graph, in order. Default: None .

  • export_params (bool, optional) – If false, parameters (weights) will not be exported, parameters will add input nodes as input of the graph. Default: True .

  • keep_initializers_as_inputs (bool, optional) – If True, all the initializers (model parameters/weights) will add as inputs to the graph. This allows modifying any or all weights when running the exported ONNX model. Default: False .

  • dynamic_axes (dict[str, dict[int, str]], optional) –

    To specify axes of input tensors as dynamic (at runtime). Default: None .

    • Set a dict with scheme: {input_node_name: {axis_index:axis_name}}, for example, {"input1": {0:"batch_size", 1: "seq_len"}, "input2": {{0:"batch_size"}}.

    • By default, the shapes of all input tensors in the exported model exactly match those specified in inputs.

Raises

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> from mindspore import Tensor
>>>
>>> # Define the network structure of LeNet5. Refer to
>>> # https://gitee.com/mindspore/docs/blob/br_base/docs/mindspore/code/lenet.py
>>> net = LeNet5()
>>> input_tensor = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32))
>>> ms.onnx.export(net, input_tensor, file_name='lenet.onnx', input_names=['input1'], output_names=['output1'])