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

Hello:

I am new to Pytorch, but I have some experience with ONNX on Windows (C#).

I found someone’s StyleGAN2 pretrained model(256px) with FFHQ dataset.

Which can be downloaded from this URL:

https://drive.google.com/file/d/1PQutd-JboOCOZqmd95XWxWrO8gGEvRcO/view

I downloaded it and save it as C:\Models\550000.pt

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.

But I can’t figure out how to write some Python code to export PT model to ONNX model.

I am using Python 3.9 on Windows 10, I have installed torch version: 1.11.0+cu113

Please advise on how I can write some Python code for this job.

Thanks,

Hi,

You can probably use:

 model = torch.load('C:\Models\550000.pt')

from Saving and Loading Models — PyTorch Tutorials 1.11.0+cu102 documentation

To load the saved model and now you can use the following to save it in onnx format:

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'}})

from (optional) Exporting a Model from PyTorch to ONNX and Running it using ONNX Runtime — PyTorch Tutorials 1.11.0+cu102 documentation

Hello:
Thanks for your reply. I have done this:
Copy 550000.pt to D:\Models.
Write the following Python code (Export550000ONNX.py):
import torch

model = torch.load(‘550000.pt’)

x = torch.randn(1000, 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 : ‘1000’}, # variable length axes
‘output’ : {0 : ‘1000’}})
But when I run this script, I got error message:
D:\models>dir
Volume in drive D is SATABackup
Volume Serial Number is 4425-E62C

Directory of D:\models

03/16/2022 09:36 AM .
03/16/2022 09:36 AM …
03/14/2022 04:56 PM 827,025,138 550000.pt
03/16/2022 09:36 AM 1,017 Export550000ONNX.py
2 File(s) 827,026,155 bytes
2 Dir(s) 567,487,598,592 bytes free

D:\models>python Export550000ONNX.py
Traceback (most recent call last):
File “D:\models\Export550000ONNX.py”, line 6, in
output = model(x)
TypeError: ‘dict’ object is not callable

D:\models>
I don’t understand the error message: TypeError: ‘dict’ object is not callable.
Please advise!