Deep Learning Toolbox Converter for PyTorch Models
Import pretrained PyTorch models into MATLAB
1.7K Downloads
Updated
11 Sep 2024
The converter for PyTorch models enables you to import pretrained PyTorch models and weights into MATLAB.
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 Release Compatibility
Created with
R2022b
Compatible with R2022b to R2024b
Platform Compatibility
Windows macOS (Apple silicon) macOS (Intel) LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.