Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to convert the model to onnx? #6

Closed
VyBui opened this issue Dec 22, 2019 · 1 comment
Closed

how to convert the model to onnx? #6

VyBui opened this issue Dec 22, 2019 · 1 comment

Comments

@VyBui
Copy link

VyBui commented Dec 22, 2019

Dear @PeikeLi,

The results are pretty amazing. I would love to convert the model to onnx for further development.

Here are the steps that I have done:

  1. I loaded the model

  2. Active the inference-mode:
    model = network(num_classes=num_classes, pretrained=None) model = nn.DataParallel(model) state_dict = torch.load(args.restore_weight,map_location=torch.device('cpu')) model.load_state_dict(state_dict) model.train(False)

  3. Set size for input:
    x = torch.randn(batch_size, 3, 512, 512, requires_grad=True)

  4. Export to onnx:
    torch.onnx.export( model.module, # model being run x, 'schp.onnx', export_params=True, # store the trained parameter weights inside the model file opset_version=11, # 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 lenght axes 'output': {0: 'batch_size'},}, verbose=False)

However, I am getting the error like this:

RuntimeError: ONNX export failed: Couldn't export operator aten::adaptive_avg_pool2d

Here is the full error message:
pytorch_onnx_error.pdf

I would like to ask if you or anyone have done any of this before, if so Can you please share me the code that you make it work?

Thank you in advance!

Vy

@GoGoDuck912
Copy link
Owner

@VyBui

Pytorch uses a dynamic graph, you need to transform all dynamic parameters into static parameters when export to onnx.

For your case, do not use an adaptive avg pool, instead use the avg pool with a fixed kernel size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants