Icevision: Failed to export an ONNX attribute 'onnx::Sub', since it's not constant, please try to make things (e.g., kernel size) static if possible

Created on 24 Sep 2020  路  2Comments  路  Source: airctic/icevision

馃悰 Bug

Describe the bug
It is not possible to export the trained model to onnx format. My model is a fasterRCNN, trained using PyTroch Lightning. After

light_model.to_onnx("model_lightnining_export.onnx", 
                batch[0],
                export_params=True,)
                 input_names = ['input'],  
                 output_names = ['output'], 
                 dynamic_axes={'input' : {0 : 'batch_size'},'output' : {0 : 'batch_size'}})

The method crushes

RuntimeError: Failed to export an ONNX attribute 'onnx::Sub', since it's not constant, please try to make things (e.g., kernel size) static if possible

To Reproduce
1.Train the model

trainer = Trainer(max_epochs=10,
                      checkpoint_callback=False,
                      logger=False,
                      gpus=0,
                      resume_from_checkpoint = '/home/piotr/Downloads/epoch=4(1).ckpt'
                      )
trainer.fit(light_model, dm)
  1. Generate an example batch:
i = 0 
samples = [valid_ds[i] for i in range(i, i+2)]
batch, samples = faster_rcnn.build_infer_batch(samples)
  1. Try to save the model using ONNX format
light_model.to_onnx("model_lightnining_export.onnx", 
                batch[0],
                export_params=True,)
                 input_names = ['input'],  
                 output_names = ['output'], 
                 dynamic_axes={'input' : {0 : 'batch_size'},'output' : {0 : 'batch_size'}})

Expected behavior
The model saved successfully as ONNX

Desktop (please complete the following information):

  • OS: ubuntu 18.04
bug

Most helpful comment

Okk, nevermind, I have found the solution. The error was due to the resnet, not due to the icevision. I had to change opset_version, to 11

light_model.to_onnx("model_lightnining_export.onnx", 
                batch[0],
                export_params=True,
                opset_version=11,
                input_names = ['input'],  
                output_names = ['output'], 
                dynamic_axes={'input' : {0 : 'batch_size'},'output' : {0 : 'batch_size'}})

All 2 comments

Okk, nevermind, I have found the solution. The error was due to the resnet, not due to the icevision. I had to change opset_version, to 11

light_model.to_onnx("model_lightnining_export.onnx", 
                batch[0],
                export_params=True,
                opset_version=11,
                input_names = ['input'],  
                output_names = ['output'], 
                dynamic_axes={'input' : {0 : 'batch_size'},'output' : {0 : 'batch_size'}})

Thank you @tugot17 for sharing the solution that you found. This will help other who might face a similar issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oke-aditya picture oke-aditya  路  5Comments

partham16 picture partham16  路  3Comments

lgvaz picture lgvaz  路  4Comments

tugot17 picture tugot17  路  3Comments

lgvaz picture lgvaz  路  5Comments