importNetworkFromONNX
Description
imports a pretrained Open Neural Network Exchange (ONNX™) network from the file net
= importNetworkFromONNX(modelfile
)modelfile
. The function returns
the network net
as an initialized dlnetwork
object.
importNetworkFromONNX
requires the Deep Learning Toolbox™ Converter for ONNX Model Format support package. If this support package is not installed, then
importNetworkFromONNX
provides a download link.
Note
The importNetworkFromONNX
function can generate a custom layer when
importing an ONNX layer. For more information, see Algorithms
. The
function saves the generated custom layers in the
+
modelfile
namespace.
imports a pretrained ONNX network with additional options specified by one or more name-value arguments.
For example, net
= importNetworkFromONNX(modelfile
,Name=Value
)Namespace="CustomLayers"
saves any generated custom layers
and associated functions in the +CustomLayers
namespace in the current
folder.
Examples
Input Arguments
Output Arguments
Limitations
importNetworkFromONNX
supports these ONNX:ONNX intermediate representation version 9
ONNX operator sets 6–18
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.
The members of the
+
namespace (custom layers and operators) are not accessible if the namespace parent folder is not on the MATLAB path. For more information, see Namespaces and the MATLAB Path.Namespace
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.
Algorithms
The importNetworkFromONNX
function imports an ONNX operator into MATLAB by trying these steps in order:
The function tries to import the ONNX operator as a built-in MATLAB layer. For more information, see ONNX Operators Supported for Conversion into Built-In MATLAB Layers.
The function tries to import the ONNX operator as a custom layer.
importNetworkFromONNX
saves the generated custom layers and the associated functions in the+
namespace. For an example, see Import ONNX Network with Automatically Generated Custom Layers.Namespace
The function imports the ONNX operator as a custom layer with a placeholder function. You must complete the placeholder function before you can use the network.
If these steps do not result in an imported network, you can use the importONNXFunction
function. importONNXFunction
imports the model as an ONNXParameters
object, which contains the network parameters, and a model function, which contains the
network architecture.
In some cases, the software indicates that you need to initialize the imported network.
References
[1] GitHub. “Open Neural Network Exchange.” Accessed July 3, 2023. https://github.com/onnx/.
[2] "ONNX | Home.” Accessed July 3, 2023. https://onnx.ai/.