Import layers from ONNX network

Import layers from an ONNX™ (Open Neural Network Exchange) network.

By using ONNX as an intermediate format, you can import models from other deep learning frameworks that support ONNX model export, such as TensorFlow™, PyTorch, Caffe2, Microsoft® Cognitive Toolkit (CNTK), Core ML, and Apache MXNet™.


layers = importONNXLayers(modelfile)
layers = importONNXLayers(modelfile,Name,Value)



layers = importONNXLayers(modelfile) imports the layers of an ONNX (Open Neural Network Exchange) network from the file modelfile. You can train the imported layers on a new data set or assemble the layers into a network ready for prediction. For an example of the workflow of assembling a network, see Assemble Network from Pretrained Keras Layers.

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.


layers = importONNXLayers(modelfile,Name,Value) imports the layers from an ONNX network with additional options specified by one or more name-value pair arguments.

For example, importONNXLayers(modelfile,'ImportWeights',false) imports the network architecture without weights from the file modelfile.


collapse all

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

Type importONNXLayers at the command line.


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';
layers = importONNXLayers(modelfile,'OutputLayerType','classification')
layers = 

  LayerGraph with properties:

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

Import the architecture and weights of a residual neural network trained on the CIFAR-10 data set. Specify the file containing the ONNX network and the type of the output layer to add to the imported network.

modelfile = 'cifarResNet.onnx';
lgraph = importONNXLayers(modelfile, ...
    'OutputLayerType','classification', ...
lgraph = 
  LayerGraph with properties:

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

Analyze the imported network architecture.


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'

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: importONNXLayers(modelfile,'OutputLayerType','classification') imports the network layers from modelfile and adds an output layer for a classification output layer at the end of the imported layers.

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

Example: 'OutputLayerType','regression'

Indicator to import weights as well as the network architecture, specified as either false or true.

Example: 'ImportWeights',true

Data Types: logical

Output Arguments

collapse all

Network architecture, returned as a LayerGraph object.


  • If the ONNX network contains a layer that Deep Learning Toolbox Converter for ONNX Model Format does not support, then importONNXLayers inserts a place holder layer in place of the unsupported layer. To find the names and indices of the unsupported layers in the network, use the findPlaceholderLayers function. You then can replace a placeholder layer with a new layer that you define. To replace a layer, use replaceLayer.

    importONNXLayers supports the following ONNX operators, with some limitations:

    • Add

    • AveragePool

    • BatchNormalization

    • Clip

    • Concat

    • Conv

    • ConvTranspose

    • Div

    • Dropout

    • Flatten

    • Gemm

    • GlobalAveragePool

    • Identity

    • LRN

    • LSTM

    • LeakyRelu

    • MaxPool

    • MatMul

    • Mul

    • PRelu

    • Relu

    • Reshape

    • Sigmoid

    • Softmax

    • Sub

    • Sum

    • Tanh

    • ImageScaler

  • The workflow for assembling layers imported from ONNX into a network ready for prediction is the same as assembling layers imported from Keras. For an example of this workflow, see Assemble Network from Pretrained Keras Layers.


[1] Open Neural Network Exchange.

[2] ONNX.

Introduced in R2018a