facebookresearch / detr

End-to-End Object Detection with Transformers
Apache License 2.0
13.09k stars 2.37k forks source link

How to export onnx with dynamic_axes? #556

Open Agoniii opened 1 year ago

Agoniii commented 1 year ago

❓ How to export onnx with dynamic_axes using DETR?

Describe what you want to do, including:

  1. what inputs you will provide, if any: I can export onnx with fix shape and it works well. But when I export onnx with dynamic_axes in batch axis, the exported model is not as expected.

    dummy_image = [torch.ones(args.batch_size, 3, 800, 800, device=device)]
    input_names=['inputs'],
    output_names=["pred_logits", "pred_boxes"]
    torch.onnx.export(model, dummy_image , 'detr_dynamic.onnx', 
                             dynamic_axes={input_names[0]: {0: "batch_size"}},
                             input_names=input_names, output_names=output_names, 
                             export_params=True, training=torch.onnx.TrainingMode.EVAL, opset_version=12)

    The inputs will split to 4, do squeeze, Pad, Unsqueeze and then concat. So the shape of onnx model is fixed to batch_size=4. image

  2. what outputs you are expecting: onnx model of Detr can really support dynamic shape.