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

Add onnx export script for segment anything v2 #22119

Merged
merged 6 commits into from
Sep 18, 2024
Merged

Conversation

tianleiwu
Copy link
Contributor

@tianleiwu tianleiwu commented Sep 18, 2024

Description

Add ONNX export script for segment anything v2 (SAM2).

Limitations

  • Does not support video. Only support image right now.
  • The decoder does not support batch inference.

Credits

The demo that is based on SAM2 notebook, and modified to run with ORT.

The export of decoder is inspired by https://github.com/vietanhdev/samexporter.

Demo

Example output of demo:
sam2_demo

Motivation and Context

Support optimization of SAM2.

@tianleiwu tianleiwu merged commit a9740d6 into main Sep 18, 2024
87 checks passed
@tianleiwu tianleiwu deleted the tlwu/sam2_onnx_export branch September 18, 2024 21:32
tianleiwu added a commit that referenced this pull request Sep 21, 2024
### Description
* Add MultiHeadAttention fusion for SAM2.
* Add LayerNormalization fusion for NCHW format by inserting Transpose
from NCHW to NHWC before layer normalization, and add another Transpose
after layer norm to convert NHWC back to NCHW. Hopefully, those extra
Transpose nodes will be removed when prefer_nhwc is enabled later.
* Add a condition that the input shall be 3D when fuse SkipLayerNorm.
* Update convert_to_onnx.py to add `--optimize` and `--use_gpu` options
to output optimized onnx model for CPU/CUDA eps.
* Add an option `--dtype fp16|fp32` in convert_to_onnx.py to support
converting optimized model to float16.
* Update the demo to use the optimized onnx models.

### Motivation and Context
To support optimization of SAM2 for CPU/CUDA eps that is exported in
#22119
tianleiwu added a commit that referenced this pull request Sep 21, 2024
### Description
Add benchmark script segment anything v2. 
It depends on #22119 for
onnx export, and #22167 for
sam2 graph fusion.

### Motivation and Context

Benchmark SAM2 model performance.
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

Successfully merging this pull request may close these issues.

2 participants