Deep Learning Toolbox Converter for PyTorch Models

Import pretrained PyTorch models into MATLAB
2.8K 次下载
更新时间 2025/10/15
The converter for PyTorch models enables you to import pretrained PyTorch models and weights into MATLAB.
To import a PyTorch network in MATLAB, please refer to importNetworkFromPyTorch.
Limitation: the importNetworkFromPyTorch function fully supports PyTorch version 2.0. The function can import most models created in other PyTorch versions (1.10 to 2.0).
Note: the model must be traced in PyTorch before importing into MATLAB. See below for an example:
# This example loads a pretrained PyTorch model from torchvision,
# traces it with example inputs, and saves the trace as a .pt file.
import torch
from torchvision import models
# Load the model with pretrained weights
model = models.mobilenet_v2(pretrained=True)
# Call "eval" to ensure that layers like batch norm and dropout are set to
# inference mode
model.eval()
# Move the model to the CPU
model.to("cpu")
# Create example inputs
X = torch.rand(1, 3, 224, 224)
# Trace model with the example input
traced_model = torch.jit.trace(model.forward, X)
# Save the traced model to a .pt file
traced_model.save('traced_mobilenetv2.pt')
The initial release in R2022b supports importing image classification models. Support for other model types will be added in future updates.
MATLAB 版本兼容性
创建方式 R2022b
兼容 R2022b 到 R2026a 的版本
平台兼容性
Windows macOS (Apple 芯片) macOS (Intel) Linux
类别
Help CenterMATLAB Answers 中查找有关 Pretrained Networks from External Platforms 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!