MATLAB with Python for AI

By integrating with PyTorch and TensorFlow, MATLAB enables you to:

  • Facilitate cross-platform and cross-team collaboration

  • Test model performance and system integration

  • Access MATLAB and Simulink tools for engineered system design

Matlab Python Image

Convert Between MATLAB, PyTorch, and TensorFlow

With Deep Learning Toolbox and MATLAB, you can access pretrained models and design all types of deep neural networks. But, not all AI practitioners work in MATLAB. To facilitate cross-platform and cross-team collaboration when designing AI-enabled systems, Deep Learning Toolbox integrates with PyTorch and TensorFlow.

Why import PyTorch and TensorFlow models into MATLAB

When you convert a PyTorch or TensorFlow model to a MATLAB network, you can use your converted network with all MATLAB AI built-in tools, such as functions and apps, for transfer learning, explainable AI and verification, system-level simulation and testing, network compression, and automatic code generation for target deployment.

Prepare PyTorch and TensorFlow models for import

Before importing PyTorch and TensorFlow models into MATLAB, you must prepare and save the models in the correct format. You can use the code below in Python to prepare your models.

The PyTorch importer expects a traced PyTorch model. After you trace the PyTorch model, save it. For more information on how to trace a PyTorch model, go to Torch documentation: Tracing a function.

X = torch.rand(1,3,224,224)
traced_model = torch.jit.trace(model.forward,X)
traced_model.save("torch_model.pt")

Your TensorFlow model must be saved in the SavedModel format.

model.save("myModelTF")

How to import PyTorch and TensorFlow models

You can import models from PyTorch and TensorFlow into MATLAB, converting them into MATLAB networks with just one line of code.

Use the importNetworkFromPyTorch function and specify PyTorchInputSizes with the correct input size for the specific PyTorch model. This allows the function to create an image input layer for the imported network because PyTorch models do not inherently have input layers. For more information, see Tips on Importing Models from PyTorch and TensorFlow.

To import a network from TensorFlow, use the importNetworkFromTensorFlow function.

Import PyTorch and TensorFlow models interactively

Import Matlab Image

You can import models from PyTorch interactively with the Deep Network Designer app. Then, you can view, edit, and analyze the imported network from the app. You can even export the network directly to Simulink from the app.

How to export models from MATLAB to PyTorch and TensorFlow

Export Matlab Image

You can export and share your MATLAB networks to TensorFlow and PyTorch. Use exportNetworkToTensorFlow to directly export to TensorFlow and the exportONNXNetwork function to export to PyTorch via ONNX™.

exportNetworkToTensorFlow(net,"myModel")