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)
imports the layers of an ONNX (Open Neural Network Exchange) network from the file
layers = importONNXLayers(
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.
Download and install the Deep Learning Toolbox Converter for ONNX Model Format support package.
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
the command line. If the support package is installed, then the function returns a
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', ... 'ImportWeights',true)
lgraph = LayerGraph with properties: Layers: [77×1 nnet.cnn.layer.Layer] Connections: [85×2 table]
Analyze the imported network architecture.
modelfile— Name of ONNX model file
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.
comma-separated pairs of
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
importONNXLayers(modelfile,'OutputLayerType','classification')imports the network layers from
modelfileand adds an output layer for a classification output layer at the end of the imported layers.
'OutputLayerType'— Type of output layer
Type of the output layer that the function appends to the end of the imported
network architecture, specified as
'pixelclassification' appends a
pixelClassificationLayer object (requires Computer
'ImportWeights'— Indicator to import weights
false(default) | true
Indicator to import weights as well as the network architecture, specified as
layers— Network architecture
Network architecture, returned as a
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
importONNXLayers supports the following ONNX operators, with some limitations:
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.