How to transfer a trained model from Pytorch to Matlab?

Hi, Everyone. I have a trained CNN model in Pytorch and I want to transfer it to the Matlab. is there any solution for this purpose?
Thank you for advising me on this topic.
Reza.

You can extract the state dictionary of a model using .state_dict() and load it into a similar model designed in MATLAB.

1 Like

Can you explain more about this? I’m New to the PyTorch and actually, I’m Confuse about this issue

It seems they use different format. I saved my .state_dict() as a .mat file as:

import numpy, scipy.io
scipy.io.savemat(params[‘restore_folder’] +
‘encoder’+params[‘restore_model’]+’.mat’, mdict=encoder.state_dict())

However, when I load the .mat file to matlab it is able to read the names of the encoder layers but they are not proper structures and matlab gives an error.

Any suggestions?

@Adam_Stevens: A better way is probably to use the ONNX as a model exchange tool:

Unfortunately ONNX import in Matlab seems to be underdeveloped and works only for smaller set of architectures. I don’t think it works for other than CNNs at the moment.
https://www.mathworks.com/help/deeplearning/ref/importonnxlayers.html

Does anyone know about other options?

Thanks,
Jan

Maybe error is due to the name of value contaion ‘.’, which is not supported in matlab. You can replace ‘.’ with ‘_’.