importONNXNetwork
(To be removed) Import pretrained ONNX network
importONNXNetwork
will be removed in a future release. Use importNetworkFromONNX
instead. (since R2023b) For more information about updating
your code, see Version History.
Description
imports a pretrained ONNX™ (Open Neural Network Exchange) network from the file
net
= importONNXNetwork(modelfile
)modelfile
. The function returns the network net
as
a DAGNetwork
or dlnetwork
object.
importONNXNetwork
requires the Deep Learning Toolbox™ Converter for ONNX Model Format support package. If this support package is not installed, then
importONNXNetwork
provides a download link.
Note
By default, importONNXNetwork
tries to generate a custom layer when
the software cannot convert an ONNX operator into an equivalent built-in MATLAB® layer. For a list of operators for which the software supports conversion,
see ONNX Operators Supported for Conversion into Built-In MATLAB Layers.
importONNXNetwork
saves the generated custom layers in the namespace
+
.modelfile
importONNXNetwork
does not automatically generate a custom layer for
each ONNX operator that is not supported for conversion into a built-in MATLAB layer. For more information on how to handle unsupported layers, see Alternative Functionality.
imports a pretrained ONNX network with additional
options specified by one or more name-value arguments. For example,
net
= importONNXNetwork(modelfile
,Name=Value
)OutputLayerType="classification"
imports the network as a
DAGNetwork
object with a classification output layer appended to the end
of the first output branch of the imported network architecture.
Examples
Input Arguments
Name-Value Arguments
Output Arguments
Limitations
importONNXNetwork
supports these:ONNX intermediate representation version 9
ONNX operator sets 6–20
Note
If you import an exported network, layers of the reimported network might differ from layers of the original network, and might not be supported.
More About
Tips
To use a pretrained network for prediction or transfer learning on new images, you must preprocess your images in the same way as the images that you use to train the imported model. The most common preprocessing steps are resizing images, subtracting image average values, and converting the images from BGR format to RGB format.
For more information about preprocessing images for training and prediction, see Preprocess Images for Deep Learning.
MATLAB uses one-based indexing, whereas Python® uses zero-based indexing. In other words, the first element in an array has an index of 1 and 0 in MATLAB and Python, respectively. For more information about MATLAB indexing, see Array Indexing. In MATLAB, to use an array of indices (
ind
) created in Python, convert the array toind+1
.For more tips, see Tips on Importing Models from TensorFlow, PyTorch, and ONNX.
Alternative Functionality
Deep Learning Toolbox Converter for ONNX Model Format provides three functions to import a pretrained ONNX network: importONNXNetwork
, importONNXLayers
,
and importONNXFunction
.
If the imported network contains an ONNX operator not supported for conversion into a built-in MATLAB layer (see ONNX Operators Supported for Conversion into Built-In MATLAB Layers) and
importONNXNetwork
does not generate a custom layer, then
importONNXNetwork
returns an error. In this case, you can still use
importONNXLayers
to import the network architecture and weights or
importONNXFunction
to import the network as an ONNXParameters
object
and a model function.