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

Allow changing the window size of pretrained models #594

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mranzinger
Copy link

After digging into this excellent model, I found that, with an input size of 1024, and a patch size of 16, we're left with a 64x64 feature map. Then, I found that SAM uses a window size of 14, which causes padding to be necessary.

It turns out that the pretrained SAM (Large at least) is robust to changes in the window size, as long as you handle the relative position embedding for the attention layer. This PR will lerp the position embedding from state dict into the embedding for how the model was created. Fortunately, the embeddings based on L1 distance seem perfectly fine with this.

Once I got model loading, I ran the resulting model through the COCO instance segmentation script, as implemented by the EfficientViT researchers (https://github.com/mit-han-lab/efficientvit/blob/master/eval_sam_coco.py), and ran the evaluation with a few different window sizes:

+-------------+-------+-------+--------+-------+------------+
| Window Size | mIOU  | Large | Medium | Small | Throughput |
+-------------+-------+-------+--------+-------+------------+
| 8           | 76.85 | 82.02 | 79.65  | 71.53 | 15.33      |
| 14*         | 77.01 | 82.11 | 80.26  | 71.38 | 13.47      |
| 16          | 77.29 | 82.19 | 80.28  | 71.98 | 14.23      |
+-------------+-------+-------+--------+-------+------------+

As we can see, if you change the window size to 16, not only do these mIOU metrics improve slightly across all object sizes, but also the throughput increases (I used an A100 with batch size 16, 100 forward passes, reported as im/sec). I suspect that throughput improves for sizes 8 and 16 for a couple of reasons: (A) GPUs prefer powers of two, and those two window size choices result in gemm's with size 16^2 or 64^2, and (B) Padding in Attention is no longer necessary around every windowed attention operation.

So, this PR optionally allows api consumers to specify a different window size during model construction, and implements the weight lerping during state_dict loading so that existing model weights may be used.

@facebook-github-bot
Copy link

Hi @mranzinger!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 19, 2023
@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants