yformer / EfficientSAM

EfficientSAM: Leveraged Masked Image Pretraining for Efficient Segment Anything
Apache License 2.0
2.12k stars 151 forks source link

torch.onnx.errors.UnsupportedOperatorError: Exporting the operator 'aten::tile' to ONNX opset version 17 is not supported. #47

Open xwhkkk opened 8 months ago

xwhkkk commented 8 months ago

Thank you for sharing the great work ! I want to export the onnx model and when I run export_to_onnx.py, the following error encounters: torch.onnx.errors.UnsupportedOperatorError: Exporting the operator 'aten::tile' to ONNX opset version 18 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub: https://github.com/pytorch/pytorch/issues.

Based on the documents of ONNX supported TorchScript operators, the aten:tile is supported since opset version 13. Could you give me some suggestions ?

duxuan11 commented 8 months ago

same question.

yformer commented 8 months ago

@xwhkkk and @duxuan11, what version of onnx lib is used? I also met that issue before. After using onnx==1.15.0 and onnxruntime=1.16.3=py39haa01933_0_cpu, the issue was gone. Let me know if that works for you.

xwhkkk commented 8 months ago

@xwhkkk and @duxuan11, what version of onnx lib is used? I also met that issue before. After using onnx==1.15.0 and onnxruntime=1.16.3=py39haa01933_0_cpu, the issue was gone. Let me know if that works for you.

Thank you for your suggestions and sorry for my late reply ! My onnx version = 1.14.1, onnxruntime = 1.16.1 , and python=3.10. I followed your suggestion to change the version of onnx and onnxruntime, but the same problem still exists. I found the problem may be the following code in _efficient_samdecoder.py:

    image_embeddings_tiled = torch.tile(
        image_embeddings[:, None, :, :, :], [1, max_num_queries, 1, 1, 1]
    ).view(
        batch_size * max_num_queries,
        image_embed_dim_c,
        image_embed_dim_h,
        image_embed_dim_w,
    )

The above code snippet can be replaced as the following potential fix:

   image_embeddings_tiled = image_embeddings.unsqueeze(1).repeat(1, max_num_queries, 1, 1, 1).view(
    batch_size * max_num_queries,
    image_embed_dim_c,
    image_embed_dim_h,
    image_embed_dim_w,
  )

Replace the usage of torch.tile() with torch.repeat() and torch.unsqueeze() to achieve the same functionality. It works for this problem !