exportONNXNetwork - Export network to ONNX model format - MATLAB (original) (raw)

Export network to ONNX model format

Syntax

Description

exportONNXNetwork([net](#mw%5F844c3879-ae5e-4704-8595-20250a86a2ef),[filename](#mw%5F247bf7b4-1227-4b76-93ea-604cc68161a9)) exports the deep learning network net with weights to the ONNX™ format file filename. Iffilename exists, then exportONNXNetwork overwrites the file.

This function requires the Deep Learning Toolbox™ Converter for ONNX Model Format support package. If this support package is not installed, then the function provides a download link.

example

exportONNXNetwork([net](#mw%5F844c3879-ae5e-4704-8595-20250a86a2ef),[filename](#mw%5F247bf7b4-1227-4b76-93ea-604cc68161a9),[Name=Value](#namevaluepairarguments)) exports a network using additional options specified by one or more name-value arguments. For example, you can specify the name and batch size of the ONNX network.

Examples

collapse all

Export Network to ONNX Format

Load the pretrained SqueezeNet convolutional neural network.

net = imagePretrainedNetwork("squeezenet")

net = dlnetwork with properties:

     Layers: [68×1 nnet.cnn.layer.Layer]
Connections: [75×2 table]
 Learnables: [52×3 table]
      State: [0×3 table]
 InputNames: {'data'}
OutputNames: {'prob_flatten'}
Initialized: 1

View summary with summary.

net is a dlnetwork object that contains the layers and learnable parameters of the network, among other properties.

Analyze the network.

exportONNXNetwork_analyeNetwork.png

analyzeNetwork displays an interactive plot of the network architecture and a table containing information about the network layers. You can also detect errors and issues in the network net before exporting it to the ONNX format. net is error free.

Export the network net as an ONNX format file named squeezenet.onnx. Save the file to the current folder. If the Deep Learning Toolbox Converter for ONNX Model Format support package is not installed, then exportONNXNetwork provides a link to the required support package in the Add-On Explorer. To install the support package, click the link, and then click Install.

filename = "squeezenet.onnx"; exportONNXNetwork(net,filename)

Now you can import the squeezenet.onnx file into any deep learning framework that supports ONNX import.

Input Arguments

collapse all

net — Trained network or graph of network layers

dlnetwork object

Trained network or graph of network layers, specified as a dlnetwork object.

You can get a trained network in these ways:

exportONNXNetwork requires net to be error free. You can detect errors and issues in a trained network before exporting it to an ONNX network by using analyzeNetwork.

filename — Name of file

character vector | string scalar

Name of file, specified as a character vector or string scalar.

Example: "network.onnx"

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: exportONNXNetwork(net,filename,NetworkName="my_net") exports a network and specifies "my_net" as the network name in the saved ONNX network.

NetworkName — Name of ONNX network

"Network" (default) | character vector | string scalar

Name of ONNX network to store in the saved file, specified as a character vector or a string scalar.

Example: NetworkName="my_squeezenet"

OpsetVersion — Version of ONNX operator set

14 (default) | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |16 | 17 | 18

Version of ONNX operator set to use in the exported model, specified as a positive integer in the range [6 18]. If the default operator set does not support the network you are trying to export, then try using a later version. If you import the exported network to another framework and you used an operator set during export that the importer does not support, then the import can fail.

To ensure that you use the appropriate operator set version, consult the ONNX operator documentation [3]. For example,OpsetVersion=9 exports the maxUnpooling2dLayer to the MaxUnpool-9 ONNX operator.

Example: OpsetVersion=6

BatchSize — Batch size of ONNX network

[] (default) | positive integer

Batch size of the ONNX network, specified as [] or as a positive integer. If you specify BatchSize as [], the ONNX network has a dynamic batch size. If you specifyBatchSize as a positive integer k, the ONNX network has a fixed batch size of k.

Example: BatchSize=10

Limitations

Note

If you import an exported network, layers of the reimported network might differ from layers of the original network, and might not be supported.

More About

collapse all

Layers Supported for ONNX Export

exportONNXNetwork can export the following:

Tips

References

Version History

Introduced in R2018a

expand all

R2024b: Export networks with five new layers

R2024b: Updated support for ONNX intermediate representation and operator sets

exportONNXNetwork now supports ONNX intermediate representation version 9 and ONNX operator sets 6 to 18.