Import layers from ONNX network
imports the layers of an ONNX™ (Open Neural Network Exchange) network from the file
layers
= importONNXLayers(modelfile
)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.
imports the layers from an ONNX network with additional options specified by one or more name-value pair
arguments.layers
= importONNXLayers(modelfile
,Name,Value
)
For example, importONNXLayers(modelfile,'ImportWeights',false)
imports the network architecture without weights from the file
modelfile
.
importONNXLayers
supports ONNX versions as follows:
importONNXLayers
supports ONNX intermediate representation version 6.
importONNXLayers
fully supports ONNX operator sets 6, 7, 8, and 9.
importONNXLayers
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.
If the ONNX network contains a layer that Deep Learning Toolbox Converter for ONNX Model Format does not support, then importONNXLayers
inserts a
placeholder 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
layers, with some limitations:
ONNX Layer | Deep Learning Toolbox Layer |
---|---|
| additionLayer or
nnet.onnx.layer.ElementwiseAffineLayer |
| averagePooling2dLayer |
| batchNormalizationLayer |
| nnet.onnx.layer.ClipLayer |
| concatenationLayer |
| None (Imported as weights) |
| convolution2dLayer |
| transposedConv2dLayer |
| nnet.onnx.layer.ElementwiseAffineLayer |
| dropoutLayer |
| nnet.onnx.layer.FlattenLayer or
nnet.onnx.layer.Flatten3dLayer |
| eluLayer |
| fullyConnectedLayer if ONNX network is recurrent, otherwise
nnet.onnx.layer.FlattenLayer followed by convolution2dLayer |
| globalAveragePooling2dLayer |
| globalMaxPooling2dLayer |
| gruLayer |
| nnet.onnx.layer.IdentityLayer |
| nnet.onnx.layer.ElementwiseAffineLayer |
| groupNormalizationLayer with numGroups
specified as "channel-wise" |
| leakyReluLayer |
| CrossChannelNormalizationLayer |
| lstmLayer or bilstmLayer |
| fullyConnectedLayer if ONNX network is recurrent, otherwise
convolution2dLayer |
| maxPooling2dLayer |
| multiplicationLayer |
| nnet.onnx.layer.PReluLayer |
| reluLayer or clippedReluLayer |
| nnet.onnx.layer.FlattenLayer |
| sigmoidLayer |
| |
| nnet.onnx.layer.ElementwiseAffineLayer |
| additionLayer |
| tanhLayer |
ONNX Layer | Computer Vision Toolbox Layer |
---|---|
| spaceToDepthLayer (Computer Vision Toolbox) |
ONNX Layer | Image Processing Toolbox™ |
---|---|
Resize | resize2dLayer (Image Processing Toolbox) or resize3dLayer (Image Processing Toolbox) |
Upsample | resize2dLayer (Image Processing Toolbox) or resize3dLayer (Image Processing Toolbox) |
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.
You can import an ONNX network with multiple inputs and multiple outputs. If the
network has multiple inputs and a single output, use 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 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. Most common preprocessing steps are resizing images, subtracting image average values, and converting the images from BGR images to RGB.
For more information on preprocessing images for training and prediction, see Preprocess Images for Deep Learning.
assembleNetwork
| exportONNXNetwork
| findPlaceholderLayers
| importCaffeLayers
| importCaffeNetwork
| importKerasLayers
| importKerasNetwork
| importONNXFunction
| importONNXNetwork
| replaceLayer