How to transfer a trained model from Pytorch to Matlab?

Reza_Akbari_Movahed: <p>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?<br>
Thank you for advising me on this topic.<br>
Reza.</p>

taha: <p>You can extract the state dictionary of a model using <a href="http://pytorch.org/docs/master/nn.html?highlight=state_dict#torch.nn.Module.state_dict" rel="nofollow noopener"><code>.state_dict()</code></a> and load it into a similar model designed in MATLAB.</p>

Reza_Akbari_Movahed: <p>Can you explain more about this? I’m New to the PyTorch and actually, I’m Confuse about this issue</p>

Adam_Stevens: <p>It seems they use different format. I saved my .state_dict() as a .mat file as:</p>
<p>import numpy, <a href="http://scipy.io" rel="nofollow noopener">scipy.io</a><br>
scipy.io.savemat(params[‘restore_folder’] +<br>
‘encoder’+params[‘restore_model’]+’.mat’, mdict=encoder.state_dict())</p>
<p>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.</p>
<p>Any suggestions?</p>

taha: <p><a class="mention" href="/u/adam_stevens">@Adam_Stevens</a>: A better way is probably to use the ONNX as a model exchange tool:<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="32" height="32">
      <a href="https://github.com/onnx/tutorials" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars3.githubusercontent.com/u/31675368?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="400" height="400">

<h3><a href="https://github.com/onnx/tutorials" target="_blank" rel="nofollow noopener">onnx/tutorials</a></h3>

<p>Tutorials for using ONNX. Contribute to onnx/tutorials development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>

Jan_Drgona: <aside class="quote no-group" data-username="taha" data-post="5" data-topic="13695">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/ea5d25/48.png" class="avatar"> taha:</div>
<blockquote>
<p>ONNX</p>
</blockquote>
</aside>
<p>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.</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://www.mathworks.com/help/deeplearning/ref/importonnxlayers.html">
  <header class="source">

      <a href="https://www.mathworks.com/help/deeplearning/ref/importonnxlayers.html" target="_blank" rel="noopener nofollow ugc">mathworks.com</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://www.mathworks.com/help/deeplearning/ref/importonnxlayers.html" target="_blank" rel="noopener nofollow ugc">(To be removed) Import layers from ONNX network - MATLAB importONNXLayers</a></h3>

  <p>This MATLAB function imports the layers and weights of a pretrained ONNX (Open Neural Network Exchange) network from the file modelfile.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Does anyone know about other options?</p>
<p>Thanks,<br>
Jan</p>

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