You are now following this Submission
- You will see updates in your followed content feed
- You may receive emails, depending on your communication preferences
Import pretrained PyTorch models into MATLAB for inference, transfer learning, and other deep learning workflows. See the documentation for examples: https://www.mathworks.com/help/deeplearning/ref/importnetworkfrompytorch.html
Step 1: Prepare Model for Import
(Recommended) Exported Models (R2026a and later)
Export your model in PyTorch:
model.eval()
model.to("cpu")
X = torch.rand(1,3,224,224)
exported_model = torch.export.export(model, (X,))
torch.export.save(exported_model, 'myModel.pt2')
Exported models produce the best results. More layers map to built-in Deep Learning Toolbox layers, which means better compatibility with downstream MATLAB workflows.
Traced Models (R2025b and earlier)
Exported models are not supported in R2025b and earlier. You can still import traced models created with torch.jit.trace() in PyTorch:
model.eval()
model.to("cpu")
X = torch.rand(1,3,224,224)
traced_model = torch.jit.trace(model, X)
traced_model.save('myModel.pt')
Tracing generally works across multiple PyTorch versions, but traced models cannot be converted into built-in layers as reliably. Provide input sizes when importing traced models to improve layer conversion. Import via Deep Network Designer automatically prompts you for input sizes. Programmatically, specify them with the PyTorchInputSizes name-value argument:
net = importNetworkFromPyTorch("model.pt", PyTorchInputSizes={[1 3 224 224]});
Step 2: Import using Deep Network Designer
Launch Deep Network Designer from the MATLAB command prompt:
deepNetworkDesigner
Then click From PyTorch and select your model file. The app guides you through resolving any import issues and maximizes the number of layers converted to built-in equivalents.
You can also import programmatically with importNetworkFromPyTorch. If you import a traced model programmatically, pass the PyTorchInputSizes name-value argument for the best results.
Categories
Find more on Pretrained Networks from External Platforms in Help Center and MATLAB Answers
MATLAB Release Compatibility
- Compatible with R2022b to R2026b
Platform Compatibility
- Windows
- macOS (Apple Silicon)
- macOS (Intel)
- Linux
