This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

importONNXLayers

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™.

Syntax

layers = importONNXLayers(modelfile,'OutputLayerType',outputtype)
layers = importONNXLayers(modelfile,'OutputLayerType',outputtype,'ImportWeights',true)

Description

example

layers = importONNXLayers(modelfile,'OutputLayerType',outputtype) imports the layers of an ONNX (Open Neural Network Exchange) network from the file modelfile and specifies the output layer type of the imported network architecture. 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.

example

layers = importONNXLayers(modelfile,'OutputLayerType',outputtype,'ImportWeights',true) additionally imports the weights of the network.

For example, importONNXLayers(modelfile,'OutputLayerType','classification','ImportWeights',true) imports the network architecture with weights from the file modelfile and adds a classification output layer to the imported network.

Examples

collapse all

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

Type importONNXLayers at the command line.

importONNXLayers

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 its output layer. To import the architecture without weights, specify false as the 'ImportWeights' value.

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

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

Analyze the imported network architecture.

analyzeNetwork(lgraph);

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' or 'regression'.

Example: 'regression'

Output Arguments

collapse all

Network architecture, returned as a LayerGraph object.

Tips

  • 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

    • LRN

    • LSTM

    • LeakyRelu

    • MaxPool

    • Mul

    • Relu

    • Reshape

    • Softmax

    • Sub

    • Sum

    • 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.

References

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

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

Introduced in R2018a