Main Content

importONNXNetwork

Import pretrained ONNX network

Description

net = importONNXNetwork(modelfile,'OutputLayerType',outputtype) imports a pretrained network from the ONNX™ (Open Neural Network Exchange) file modelfile and specifies the output layer type of the imported network.

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

net = importONNXNetwork(modelfile,'OutputLayerType',outputtype,'Classes',classes) additionally specifies the classes for a classification network.

Examples

collapse all

Download and install the Deep Learning Toolbox Converter for ONNX Model Format support package.

Type importONNXNetwork at the command line.

importONNXNetwork

If Deep Learning Toolbox Converter for ONNX Model Format is not installed, then the function 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. Check that the installation is successful by importing the network from the model file 'cifarResNet.onnx' at the command line. If the support package is installed, then the function returns a DAGNetwork object.

modelfile = 'cifarResNet.onnx';
classes = ["airplane" "automobile" "bird" "cat" "dee" "dog" "frog" "horse" "ship" "truck"];
net = importONNXNetwork(modelfile,'OutputLayerType','classification','Classes',classes)
net = 

  DAGNetwork with properties:

         Layers: [77×1 nnet.cnn.layer.Layer]
    Connections: [85×2 table]

Import a residual neural network trained on the CIFAR-10 data set. Specify the file containing the ONNX network, its output type, and its output classes.

modelfile = 'cifarResNet.onnx';
classes = ["airplane" "automobile" "bird" "cat" "deer" "dog" "frog" "horse" "ship" "truck"];
net = importONNXNetwork(modelfile,'OutputLayerType','classification','Classes',classes)
net = 
  DAGNetwork with properties:

         Layers: [77×1 nnet.cnn.layer.Layer]
    Connections: [85×2 table]
     InputNames: {'Input_input'}
    OutputNames: {'ClassificationLayer_softmax'}

Analyze the imported network.

analyzeNetwork(net)

Input Arguments

collapse all

Name of ONNX model file containing the network, specified as a character vector or a string scalar. The file must be in the current folder, in a folder on the MATLAB® path, or you must include a full or relative path to the file.

Example: 'cifarResNet.onnx'

Type of the output layer that the function appends to the end of the imported network, specified as 'classification', 'regression', or 'pixelclassification'. Using 'pixelclassification' appends a pixelClassificationLayer (Computer Vision Toolbox) object (requires Computer Vision Toolbox™).

If a network in modelfile has multiple outputs, then you cannot specify the output layer types using this argument. Use importONNXLayers instead. importONNXLayers inserts placeholder layers for the outputs. After importing, you can find and replace the placeholder layers by using findPlaceholderLayers and replaceLayer, respectively.

Example: 'regression'

Classes of the output layer, specified as a categorical vector, string array, cell array of character vectors, or 'auto'. If Classes is 'auto', then the software sets the classes to categorical(1:N), where N is the number of classes. If you specify a string array or cell array of character vectors str, then the software sets the classes of the output layer to categorical(str,str).

Data Types: char | categorical | string | cell

Output Arguments

collapse all

Pretrained network, returned as DAGNetwork object.

Limitations

  • importONNXNetwork supports ONNX versions as follows:

    • The function supports ONNX intermediate representation version 6.

    • The function fully supports ONNX operator sets 6, 7, 8, and 9.

    • The function offers limited support for ONNX operator sets 10 and 11.

Note

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

More About

collapse all

Supported ONNX Layers

importONNXNetwork supports the following ONNX layers, with some limitations:

ONNX LayerDeep Learning Toolbox Layer

Add

additionLayer or nnet.onnx.layer.ElementwiseAffineLayer

AveragePool

averagePooling2dLayer

BatchNormalization

batchNormalizationLayer

Concat

concatenationLayer

Constant

None (Imported as weights)

Conv*

convolution2dLayer

ConvTranspose

transposedConv2dLayer

Dropout

dropoutLayer

Elu

eluLayer

Gemm

fullyConnectedLayer if ONNX network is recurrent, otherwise nnet.onnx.layer.FlattenLayer followed by convolution2dLayer

GlobalAveragePool

globalAveragePooling2dLayer

GlobalMaxPool

globalMaxPooling2dLayer

GRU

gruLayer

InstanceNormalization

groupNormalizationLayer with numGroups specified as "channel-wise"

LeakyRelu

leakyReluLayer

LRN

CrossChannelNormalizationLayer

LSTM

lstmLayer or bilstmLayer

MatMul

fullyConnectedLayer if ONNX network is recurrent, otherwise convolution2dLayer

MaxPool

maxPooling2dLayer

Mul

multiplicationLayer

Relu

reluLayer or clippedReluLayer

Sigmoid

sigmoidLayer

Softmax

softmaxLayer

Sum

additionLayer

Tanh

tanhLayer

*If the pads attribute of the Conv operator is a vector with only two elements [p1,p2], importONNXNetwork imports Conv as a convolution2dLayer with the name-value argument 'Padding' specified as [p1,p2,p1,p2].

ONNX LayerONNX Importer Custom Layer

Clip

nnet.onnx.layer.ClipLayer

Div

nnet.onnx.layer.ElementwiseAffineLayer

Flatten

nnet.onnx.layer.FlattenLayer or nnet.onnx.layer.Flatten3dLayer

Identity

nnet.onnx.layer.IdentityLayer

ImageScaler

nnet.onnx.layer.ElementwiseAffineLayer

PRelu

nnet.onnx.layer.PReluLayer

Reshape

nnet.onnx.layer.FlattenLayer

Sub

nnet.onnx.layer.ElementwiseAffineLayer
ONNX LayerImage Processing Toolbox™
DepthToSpacedepthToSpace2dLayer (Image Processing Toolbox)
Resizeresize2dLayer (Image Processing Toolbox) or resize3dLayer (Image Processing Toolbox)
SpaceToDepthspaceToDepthLayer (Image Processing Toolbox)
Upsampleresize2dLayer (Image Processing Toolbox) or resize3dLayer (Image Processing Toolbox)

Tips

  • If the ONNX network contains a layer that Deep Learning Toolbox Converter for ONNX Model Format does not support (see Supported ONNX Layers), then importONNXNetwork returns an error message. In this case, you can still use importONNXLayers to import the network architecture and weights.

  • You can import an ONNX network with multiple inputs and a single output using importONNXNetwork. If the network has multiple outputs, use importONNXLayers. The importONNXLayers function inserts placeholder layers for the outputs. After importing, you can find and replace the placeholder layers by using findPlaceholderLayers and replaceLayer, respectively. For an example, see Import and Assemble ONNX Network with Multiple Outputs. To learn about a deep learning network with multiple inputs and multiple outputs, see Multiple-Input and Multiple-Output Networks.

  • To use a pretrained network for prediction or transfer learning on new images, you must preprocess your images in the same way the images that were used to train the imported model were preprocessed. The most common preprocessing steps are resizing images, subtracting image average values, and converting the images from BGR images to RGB.

    • To resize images, use imresize. For example, imresize(image,[227,227,3]).

    • To convert images from RGB to BGR format, use flip. For example, flip(image,3).

    For more information on preprocessing images for training and prediction, see Preprocess Images for Deep Learning.

Compatibility Considerations

expand all

Not recommended starting in R2018b

References

[1] Open Neural Network Exchange. https://github.com/onnx/.

[2] ONNX. https://onnx.ai/.

Introduced in R2018a