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
package.
imports a pretrained ONNX network with additional options specified by one or more name-value arguments.
For example, net
= importNetworkFromONNX(modelfile
,Name=Value
)PackageName="CustomLayers"
saves any generated custom layers
and associated functions in the +CustomLayers
package in the current
folder.
Examples
Input Arguments
Output Arguments
More About
Tips
To use a pretrained network for prediction or transfer learning on new images, you must preprocess your images in the as same way 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
+
package (custom layers and operators) are not accessible if the package parent folder is not on the MATLAB path. For more information, see Packages and the MATLAB Path.PackageName
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+
package. For an example, see Import ONNX Network with Automatically Generated Custom Layers.PackageName
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/.
Version History
Introduced in R2023b
See Also
importNetworkFromPyTorch
| importNetworkFromTensorFlow
| exportONNXNetwork
| exportNetworkToTensorFlow
| trainnet
| dlnetwork
| dlarray
Topics
- Interoperability Between Deep Learning Toolbox, TensorFlow, PyTorch, and ONNX
- Tips on Importing Models from TensorFlow, PyTorch, and ONNX
- Pretrained Deep Neural Networks
- Inference Comparison Between ONNX and Imported Networks for Image Classification
- Select Function to Import ONNX Pretrained Network
- Make Predictions Using dlnetwork Object