Importing pytorch models in matlab using importNetworkFromPyTorch

146 views (last 30 days)
Hello,
I am trying to import the pre-trained pytorch model in matlab using the importNetworkFromPyTorch command supported by deep learning toolbox. However I am getting an error as below
Error using pytorchmex
Traced model failed to load. Trace the model in the fully supported version of PyTorch as described in Deep
Learning Toolbox Converter for PyTorch Models.
Error in nnet.internal.cnn.pytorch_importer.architecture.ModuleManager/loadModule (line 28)
PropertyCell = nnet.internal.cnn.pytorch_importer.architecture.pytorchmex(this.Filename);
Error in nnet.internal.cnn.pytorch_importer.architecture.ModuleManager (line 16)
PropertyCell = loadModule(this);
Error in nnet.internal.cnn.pytorch_importer.architecture.util.translatePyTorchFile (line 9)
nnet.internal.cnn.pytorch_importer.architecture.ModuleManager(filename);
Error in nnet.internal.cnn.pytorch_importer.architecture.importNetworkFromPyTorch (line 18)
importManager = nnet.internal.cnn.pytorch_importer.architecture.util.translatePyTorchFile(filename,
customLayerPath);
Error in importNetworkFromPyTorch (line 36)
Network = nnet.internal.cnn.pytorch_importer.architecture.importNetworkFromPyTorch(modelfile, varargin{:});
Error in mnist2mat (line 1)
net = importNetworkFromPyTorch("mnist_cnn.pt");

Answers (2)

Surya
Surya on 18 May 2023
Hi,
The error message suggests that the PyTorch model you are trying to import using the importNetworkFromPyTorch function in MATLAB is not compatible or properly traced. To resolve this issue, you can try the following steps:
  1. Make sure you have a fully supported version of PyTorch installed. The deep learning toolbox in MATLAB requires a specific version of PyTorch to be compatible with the importNetworkFromPyTorch function. Verify that you have the correct version installed.
  2. Ensure that your PyTorch model is saved in a format that is compatible with the deep learning toolbox. The importNetworkFromPyTorch function supports models that have been traced and saved in a specific way. Refer to the documentation or guidelines provided by the deep learning toolbox for the correct procedure to trace and save your PyTorch model.
  3. Check if you have provided the correct file path for your PyTorch model (mnist_cnn.pt in your case). Verify that the file exists at the specified location and that you have read permissions to access it.
  4. If you are using custom layers or any other dependencies in your PyTorch model, make sure they are supported by the deep learning toolbox. If not, you may need to implement alternative solutions or convert the custom layers to MATLAB equivalents.
If you have followed these steps and are still encountering issues, it would be helpful to provide more information about the PyTorch model you are trying to import and the version of PyTorch and MATLAB you are using.
Alternatively,
If you have knowledge of Python, you have the option to utilize the MATLAB Python Interface. You can find details on calling Python functions from MATLAB on link.
However, to use this feature, you must have a MATLAB-compatible version of Python installed on your computer. check
  2 Comments
Jayesh
Jayesh on 3 Jul 2023
Facing the same problem again and agian,
Checked all the points mentioned above.
acoording to below link pytorch 1.10.0 is need to execute importnetworkfrompytorch function.
I tried with 1.10 , 1.9 etc , it is showing me the same error.
please check and do help.
THANK YOU !!
MathWorks Deep Learning Toolbox Team
MathWorks Deep Learning Toolbox Team on 16 Apr 2024 at 19:37
Please see answers below and let us know if the problem is solved once your model is traced in PyTorch. Thank you.

Sign in to comment.


MathWorks Deep Learning Toolbox Team
MathWorks Deep Learning Toolbox Team on 16 Apr 2024 at 19:37
The model must be traced in PyTorch first before importing into MATLAB. Please see PyTorch documentation fo some details on how it's done. https://pytorch.org/docs/stable/generated/torch.jit.trace.html
As a simple example, try something similar to the following in PyTorch:
# 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_mnasnet.pt')

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!