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

Syntax

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

Description

example

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.

example

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.

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'

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

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.

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.

  • 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