Main Content

Pretrained Networks from External Platforms

R2026b
Use pretrained networks from external deep learning platforms

There are two approaches for using pretrained networks from external deep learning platforms: import the model, or use coexecution to train and test external models. The preferred approach is to import the model whenever possible.

Deep Learning Toolbox™ supports neural networks import from TensorFlow™ 2, TensorFlow-Keras, Keras 3, PyTorch®, the ONNX™ (Open Neural Network Exchange) model format, and Caffe. Import networks interactively using the Deep Network Designer app or programmatically using command line functions. The app generates an import report that summarizes the import process and highlights any required actions. For more information, see Pretrained Deep Neural Networks and Interoperability Between Deep Learning Toolbox, TensorFlow, PyTorch, and ONNX.

Import functions in Deep Learning Toolbox require specific support packages. If a required support package is not installed, each function provides a download link to the corresponding support package in the Add-On Explorer. Download the support package to the default location for the MATLAB® version in use. Support packages are also available for direct download:

Interoperation between MATLAB and External Deep Learning Platforms

If importing a pretrained network is not possible, you can use coexecution to run external models. Coexecution is supported for Python®-based models, including TensorFlow, ONNX, PyTorch, and custom Python models. For PyTorch models, you can run the model directly in MATLAB using a PyTorchModel object. This object acts as a wrapper that executes the model in Python through the MATLAB–Python interface, enabling integration without requiring model import. You also can convert between MATLAB numeric arrays and PyTorch Tensors by using the arrayToTorchTensor and torchTensorToArray functions.

Apps

Deep Network DesignerDesign and visualize deep learning networks

Functions

expand all

TensorFlow Import

importNetworkFromTensorFlowImport TensorFlow network as MATLAB network (Since R2023b)
importNetworkFromKerasImport Keras 3 network as MATLAB network (Since R2026a)

PyTorch Import

importNetworkFromPyTorchImport PyTorch network as MATLAB network (Since R2022b)

Caffe Import

importCaffeNetworkImport pretrained convolutional neural network models from Caffe
importCaffeLayersImport convolutional neural network layers from Caffe

ONNX Import

importNetworkFromONNXImport ONNX network as MATLAB network (Since R2023b)

Parameters Imported by importONNXFunction

ONNXParametersParameters of imported ONNX network for deep learning
freezeParametersConvert learnable network parameters in ONNXParameters to nonlearnable
unfreezeParametersConvert nonlearnable network parameters in ONNXParameters to learnable
addParameterAdd parameter to ONNXParameters object
removeParameterRemove parameter from ONNXParameters object
dlnetworkDeep learning neural network
functionLayerFunction layer
replaceLayerReplace layer in neural network
addLayersAdd layers to neural network
removeLayersRemove layers from neural network
arrayToTorchTensorConvert MATLAB numeric array to PyTorch Tensor (Since R2026b)
torchTensorToArrayConvert PyTorch Tensor to MATLAB numeric array (Since R2026b)
forwardCompute PyTorchModel output by invoking Python model on input (Since R2026b)
addFunctionConfigure data transfer settings for Python method or function of PyTorchModel (Since R2026b)
callFunctionCall method of underlying Python PyTorch model instance or function taking model as its first argument (Since R2026b)
exportSave underlying PyTorch model in different PyTorch file format (Since R2026b)
reloadReload PyTorch model from stored model loading properties (Since R2026b)

Objects

PyTorchModelReference to a PyTorch model for Python execution (Since R2026b)

Topics

Import

Python Coexecution for Simulink blocks

PyTorch Coexecution in MATLAB Command Line

Custom Layers