ONNX (.onnx)
Background & Context

-
- Open format for neural network models.
- Stores models and various metadata.
- ONNX is an acronym for Open Neural Network Exchange.
- Binary file format based on Protobuf.
- First released in 2017 by Microsoft and Facebook.
Import & Export

- Import["file.onnx"] imports the net stored in the specified file, returning it as a NetChain, NetGraph or a similar construct.
- Import["file.onnx",elem] imports the specified element elem.
- The import format can be specified with Import["file","ONNX"] or Import["file",{"ONNX",elem,…}].
- Export["file.onnx",net] exports net as an ONNX model.
- The following expressions are supported:
-
NetChain[…] a net represented as a chain of layers NetGraph[…] a net represented as a graph of layers layer any net layer such as LinearLayer, ... - See the following reference pages for full general information:
-
Import, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import from or export to a byte array
Import Elements

- General Import elements:
-
"Elements" list of elements and options available in this file "Summary" summary of the file "Rules" list of rules for all available elements - Import elements include:
-
"ArrayAssociation" parameter arrays as an association "ArrayList" parameter arrays as a list "Description" human-readable description for the model "ONNXGraph" ONNX representation of the net "ModelDomain" model namespace or domain "ModelVersion" integer version number of the model "Net" Wolfram Language representation of the net, including all initialized arrays (default) "NetExternalObject" NetExternalObject representation of the net "IRVersion" version of the ONNX intermediate representation used by the model "OperatorSetVersion" operator sets the model is compatible with "ProducerName" name of the tool used to generate the model "ProducerVersion" version of the generating tool "RawData" full, unprocessed contents of the file, including the graph representation and all metadata "UninitializedNet" Wolfram Language representation of the net, without any arrays
Options

- Import options:
-
"Arrays" None path or list of paths to valid ONNX tensor proto files containing additional arrays for the net - Export options:
-
"Description" None model documentation string in ONNX metadata "ExternalArrays" Automatic export net arrays to separate files "GraphName" Automatic graph name in ONNX metadata "ModelVersion" 0 model version in ONNX metadata "OperatorSetVersion" Automatic version of the standard ONNX operator set used by the model - Option "ExternalArrays" can be set to the following:
-
Automatic create external files for net arrays depending on the model size None include net arrays in the model file All export net arrays to separate files, one file per array "Single" export all net arrays to a single file - Setting Automatic for "ExternalArrays" is equivalent to None when the total model size is less than 2GB and to "Single" when above 2GB. For settings All and "Single", array files are created in the same directory of the main model file. Any ONNX importer will expect such files to be in the same relative path.
Examples
open allclose allBasic Examples (2)Summary of the most common use cases

https://wolfram.com/xid/0f1gc6nnye18-10h9r5

Show the Import elements available in this file:

https://wolfram.com/xid/0f1gc6nnye18-x8xs37


https://wolfram.com/xid/0f1gc6nnye18-mdkgtw


https://wolfram.com/xid/0f1gc6nnye18-42aj9h

Import Elements (14)
ArrayAssociation (1)
ArrayList (1)
Description (1)
ONNXGraph (1)
ModelDomain (1)
ModelVersion (1)
Net (1)
NetExternalObject (1)
Import the ONNX model as a NetExternalObject:

https://wolfram.com/xid/0f1gc6nnye18-utakpq

IRVersion (1)
OperatorSetVersion (1)
ProducerName (1)
ProducerVersion (1)
RawData (1)
Import Options (1)
"Arrays" (1)
Arrays used by a net can be contained in separate ONNX tensor proto files. Import an uninitialized net:

https://wolfram.com/xid/0f1gc6nnye18-6inv6

Import the net together with its arrays by specifying an array file (multiple array files can be specified in a list):

https://wolfram.com/xid/0f1gc6nnye18-fq6t7y

Export Options (5)
"Description" (1)
"ExternalArrays" (1)
Create a simple net and export it with multiple external array files:

https://wolfram.com/xid/0f1gc6nnye18-d715qt


https://wolfram.com/xid/0f1gc6nnye18-mhivfg

Every array of the net was exported to an individual file named after the array, for example, "1_Weights":

https://wolfram.com/xid/0f1gc6nnye18-ccb870


https://wolfram.com/xid/0f1gc6nnye18-f5mfny

The main model file keeps a reference to the relative path of all array files, so the arrays are automatically included when the model is imported:

https://wolfram.com/xid/0f1gc6nnye18-hglpl


https://wolfram.com/xid/0f1gc6nnye18-e833kw

Export the net with all external arrays in a single file:

https://wolfram.com/xid/0f1gc6nnye18-gd2m7c

A single file called "Arrays" is created that contains all the arrays of the net:

https://wolfram.com/xid/0f1gc6nnye18-2496ko

The main model file keeps a reference to the relative path of the combined array file, so the arrays are automatically included when the model is imported:

https://wolfram.com/xid/0f1gc6nnye18-m1azvy


https://wolfram.com/xid/0f1gc6nnye18-k8er8d

When the model size is larger than 2GB, a single external array file is automatically created and a warning message is emitted:

https://wolfram.com/xid/0f1gc6nnye18-15tk7d


https://wolfram.com/xid/0f1gc6nnye18-1at5t0


https://wolfram.com/xid/0f1gc6nnye18-3hu35n


"GraphName" (1)
"ModelVersion" (1)
"OperatorSetVersion" (1)
Create a PaddingLayer and export it in operator set 10:

https://wolfram.com/xid/0f1gc6nnye18-bo1ilz


https://wolfram.com/xid/0f1gc6nnye18-jg4yiv

Check the operator set version:

https://wolfram.com/xid/0f1gc6nnye18-lih7wk

In operator set 10, the ONNX "Pad" operator takes one input and has static attributes for its padding sizes and values:

https://wolfram.com/xid/0f1gc6nnye18-ocuam3

https://wolfram.com/xid/0f1gc6nnye18-etnyor


https://wolfram.com/xid/0f1gc6nnye18-t78kad

The model file can be converted back because Import supports operator set 10:

https://wolfram.com/xid/0f1gc6nnye18-hfl27q

Export the same layer in operator set 11:

https://wolfram.com/xid/0f1gc6nnye18-1u06aw

Check the operator set version:

https://wolfram.com/xid/0f1gc6nnye18-ze770c

In operator set 11, padding sizes and values of the "Pad" operator were moved from attributes to inputs. The operator now has two additional inputs and two fewer attributes:

https://wolfram.com/xid/0f1gc6nnye18-s1tq6g

https://wolfram.com/xid/0f1gc6nnye18-zw7n9n


https://wolfram.com/xid/0f1gc6nnye18-5nbi7r

The model file can be converted back because Import supports operator set 11:

https://wolfram.com/xid/0f1gc6nnye18-wjhdy8

Possible Issues (1)Common pitfalls and unexpected behavior
Any NetEncoder or NetDecoder is automatically removed by Export, as ONNX does not support them. Create a net with a NetEncoder and a NetDecoder and export it:

https://wolfram.com/xid/0f1gc6nnye18-wzh8qy


https://wolfram.com/xid/0f1gc6nnye18-s4eqvm

Importing the model back shows that no NetEncoder or NetDecoder are present:

https://wolfram.com/xid/0f1gc6nnye18-4pondm
