Error in loading pytorch model with torch version >1.10 on ubuntu 20.04

14 views (last 30 days)
I'm running MATLAB R2022b on Ubuntu 20.04. I'm trying to load a pytorch model in MATLAB by running the following script:
pyenv('Version','/home/usrname/anaconda3/envs/arm/bin/python','ExecutionMode','OutOfProcess')
torch = py.importlib.import_module('torch');
a = torch.tensor([1, 2, 3, 4]);
torch.save(a, 'a.pth');
torch.load('a.pth');
However, there always exists an error:
Error using serialization>__init__
Python Error: RuntimeError: [enforce fail at inline_container.cc:120] . archive does
not contain any files
Error in serialization>load (line 777)
I made several tests and located that the error could be due to the torch version I'm using. As discussed here: https://www.mathworks.com/matlabcentral/answers/1694510-python-error-runtimeerror-enf[…]ontainer-cc-106-archive-does-not-contain-any?s_tid=srchtitle, I was using torch version 1.13.0 that caused the error. When I downgraded the torch version to 1.10, the error disappeared. However, my other scripts need to use torch version > 1.10. If there's any way can let me successfully load torch model with torch version greater than 1.10? Thank you!
  2 Comments
Abhijeet
Abhijeet on 9 Mar 2023
Hi Yiting,
The error you encountered may be due to compatibility issues between the version of PyTorch you were using and the version of MATLAB you have.The supported versions of PyTorch for MATLAB R2022b are:
  • PyTorch 1.8.0
  • PyTorch 1.9.0
  • PyTorch 1.11.0
It's recommended to use one of these versions to ensure the compatibility between PyTorch and MATLAB. Using unsupported versions may lead to compatibility issues and errors.
Thanks
Yiting Zhang
Yiting Zhang on 15 Mar 2023
Hi Abhijeet,
Thank you for your reply. Unfortunately, I tried PyTorch 1.11.0 (with CPU enabled) but it still didn't work... PyTorch 1.9.0 worked but I need to use a package that depends on PyTorch version >1.10.0. Could you please help me out? Thank you so much!

Sign in to comment.

Answers (1)

Amith
Amith on 27 Apr 2023
Hi,
As per your understanding you wanted to load pytorch model in matlab and are currently facing problems with the error message shown above.
The error message above suggests that there is a problem with loading a serialized object in MATLAB that was created using PyTorch 1.11.0.
Here are a few things you can try to resolve the issue:
1. Check that the serialized object file exists and is not empty. You can do this by opening the file in a text editor and verifying that it contains some data.
2. Make sure that you are using the correct version of PyTorch for your MATLAB installation. The supported versions are 1.8.0 ,1.9.0 ,1.11.0.
3. Check that you have the necessary dependencies installed for both PyTorch and MATLAB. You can refer to the documentation for both software packages to see which dependencies are required.
4. Try using a different serialization method to save the PyTorch object. For example, you can try using the `pickle` module in Python to save the object, and then load it in MATLAB using the `load` function.

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!