Skip to content

Integrate with Hugging Face - #128

Merged
haithamkhedr merged 18 commits into
facebookresearch:mainfrom
NielsRogge:add_hf
Aug 7, 2024
Merged

Integrate with Hugging Face#128
haithamkhedr merged 18 commits into
facebookresearch:mainfrom
NielsRogge:add_hf

Conversation

@NielsRogge

@NielsRogge NielsRogge commented Aug 3, 2024

Copy link
Copy Markdown
Contributor

This PR fixes #1. It aims to make it possible to load any SAM 2 model directly from the hub.

The checkpoints are already part of the facebook org:

This PR allows to do the following:

from sam2.sam2_image_predictor import SAM2ImagePredictor

predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2-hiera-small")

This also ensures that download stats work, meaning you'll be able to see how many times people actually load one of these checkpoints from the hub. This will also allow us to compare to the download stats of Transformers which will be interesting :)

To do:

  • update README to link to HF artifacts
  • update notebooks (?)
  • support video predictor

@bhack

bhack commented Aug 4, 2024

Copy link
Copy Markdown

Is this also in the to-do list at huggingface/transformers#32394 ?

@haithamkhedr haithamkhedr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NielsRogge Thanks for supporting ckpt download from HF. Can we make huggingface_hub a soft dependency?

Comment thread sam2/sam2_image_predictor.py Outdated
Comment thread sam2/build_sam.py Outdated
Comment thread README.md
Comment thread sam2/build_sam.py Outdated
Comment thread sam2/build_sam.py Outdated
Comment thread README.md Outdated

@haithamkhedr haithamkhedr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding support to load the models through HF hub.

@haithamkhedr
haithamkhedr merged commit 6ba4c65 into facebookresearch:main Aug 7, 2024
@giswqs

giswqs commented Aug 7, 2024

Copy link
Copy Markdown

This is great! Is there a plan to make it compatible with transformers, like SAM 1?

https://huggingface.co/docs/transformers/en/tasks/mask_generation

@NielsRogge

Copy link
Copy Markdown
Contributor Author

Yes there is, the authors are adding a PR here: huggingface/transformers#32394

@giswqs

giswqs commented Aug 7, 2024

Copy link
Copy Markdown

Fantastic! Can't wait for the PR to be merged!

@sueszli

sueszli commented Aug 7, 2024

Copy link
Copy Markdown

This is just amazing <3

@leonmkim

Copy link
Copy Markdown

Sorry potentially dumb question, but should this line in the video predictor be changed from return cls(sam_model) to return sam_model? It seems like the former would work for the image predictor based on its constructor but not the video predictor. But I could be wrong :p

@haithamkhedr

haithamkhedr commented Aug 13, 2024

Copy link
Copy Markdown
Contributor

@leonmkim Good catch! Fixed in #205

yanyilyu pushed a commit to yanyilyu/segment-anything-2 that referenced this pull request Sep 17, 2024
maximuswatkins pushed a commit to owen24819/CellSam2 that referenced this pull request May 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve discoverability on HF

7 participants