How I can export a PT pre-trained model to ONNX?

zydjohn: <p>Hello:</p>
<p>I am new to Pytorch, but I have some experience with ONNX on Windows (C#).</p>
<p>I found someone’s StyleGAN2 pretrained model(256px) with FFHQ dataset.</p>
<p>Which can be downloaded from this URL:</p>
<p><a href="https://drive.google.com/file/d/1PQutd-JboOCOZqmd95XWxWrO8gGEvRcO/view" class="onebox" target="_blank" rel="noopener nofollow ugc">https://drive.google.com/file/d/1PQutd-JboOCOZqmd95XWxWrO8gGEvRcO/view</a></p>
<p>I downloaded it and save it as C:\Models\550000.pt</p>
<p>I want to convert the pre-trained model (550000.pt) to ONNX model (550000.onnx), then I can use MLGen from Microsoft to generate C# wrapper to use the pre-trained model.</p>
<p>But I can’t figure out how to write some Python code to export PT model to ONNX model.</p>
<p>I am using Python 3.9 on Windows 10, I have installed torch version: 1.11.0+cu113</p>
<p>Please advise on how I can write some Python code for this job.</p>
<p>Thanks,</p>

Manuel_Lopez: <p>Hi,</p>
<p>You can probably use:</p>
<pre><code class="lang-auto"> model = torch.load('C:\Models\550000.pt')
</code></pre>
<p>from <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html" class="inline-onebox" rel="noopener nofollow ugc">Saving and Loading Models — PyTorch Tutorials 1.11.0+cu102 documentation</a></p>
<p>To load the saved model and now you can use the following to save it in onnx format:</p>
<pre><code class="lang-auto">x = torch.randn(batch_size, 1, 224, 224, requires_grad=True) # Change for your input shape
output = model(x)

# Export the model
torch.onnx.export(model,               # model being run
                  x,                         # model input (or a tuple for multiple inputs)
                  "super_resolution.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=10,          # the ONNX version to export the model to
                  do_constant_folding=True,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # the model's input names
                  output_names = ['output'], # the model's output names
                  dynamic_axes={'input' : {0 : 'batch_size'},    # variable length axes
                                'output' : {0 : 'batch_size'}})
</code></pre>
<p>from <a href="https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html" class="inline-onebox" rel="noopener nofollow ugc">(optional) Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime — PyTorch Tutorials 1.11.0+cu102 documentation</a></p>

zydjohn: <p>Hello:<br>
Thanks for your reply.  I have done this:<br>
Copy 550000.pt to D:\Models.<br>
Write the following Python code (Export550000ONNX.py):<br>
import torch</p>
<p>model = torch.load(‘550000.pt’)</p>
<p>x = torch.randn(1000, 1, 224, 224, requires_grad=True) # Change for your input shape<br>
output = model(x)</p>
<h1>
<a name="export-the-model-1" class="anchor" href="#export-the-model-1"></a>Export the model</h1>
<p>torch.onnx.export(model,               # model being run<br>
x,                         # model input (or a tuple for multiple inputs)<br>
“super_resolution.onnx”,   # where to save the model (can be a file or file-like object)<br>
export_params=True,        # store the trained parameter weights inside the model file<br>
opset_version=10,          # the ONNX version to export the model to<br>
do_constant_folding=True,  # whether to execute constant folding for optimization<br>
input_names = [‘input’],   # the model’s input names<br>
output_names = [‘output’], # the model’s output names<br>
dynamic_axes={‘input’ : {0 : ‘1000’},    # variable length axes<br>
‘output’ : {0 : ‘1000’}})<br>
But when I run this script, I got error message:<br>
D:\models&gt;dir<br>
Volume in drive D is SATABackup<br>
Volume Serial Number is 4425-E62C</p>
<p>Directory of D:\models</p>
<p>03/16/2022  09:36 AM              .<br>
03/16/2022  09:36 AM              …<br>
03/14/2022  04:56 PM       827,025,138 550000.pt<br>
03/16/2022  09:36 AM             1,017 Export550000ONNX.py<br>
2 File(s)    827,026,155 bytes<br>
2 Dir(s)  567,487,598,592 bytes free</p>
<p>D:\models&gt;python Export550000ONNX.py<br>
Traceback (most recent call last):<br>
File “D:\models\Export550000ONNX.py”, line 6, in <br>
output = model(x)<br>
TypeError: ‘dict’ object is not callable</p>
<p>D:\models&gt;<br>
I don’t understand the error message: TypeError: ‘dict’ object is not callable.<br>
Please advise!</p>