Instructions to use zeromodels/sam2_hiera_base_plus with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- ZeroModels
How to use zeromodels/sam2_hiera_base_plus with ZeroModels:
# pip install -U zeromodels # ZeroModels is pure Keras 3, so pick a backend: "jax", "torch" or "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" from zeromodels import AutoZModel # AutoZModel reads the repo's model_type and loads the matching class. # For a task head use the matching loader, e.g. AutoZMImageClassify / AutoZMDetect / # AutoZMSemanticSegment / AutoZMTextGenerate (see zeromodels.auto). model = AutoZModel.from_weights("zeromodels/sam2_hiera_base_plus") - Keras
How to use zeromodels/sam2_hiera_base_plus with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://zeromodels/sam2_hiera_base_plus") - sam2
How to use zeromodels/sam2_hiera_base_plus with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained("zeromodels/sam2_hiera_base_plus") with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16): predictor.set_image(<your_image>) masks, _, _ = predictor.predict(<input_prompts>)# Use SAM2 with videos import torch from sam2.sam2_video_predictor import SAM2VideoPredictor predictor = SAM2VideoPredictor.from_pretrained("zeromodels/sam2_hiera_base_plus") with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16): state = predictor.init_state(<your_video>) # add new prompts and instantly get the output on the same frame frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>) # propagate the prompts to get masklets throughout the video for frame_idx, object_ids, masks in predictor.propagate_in_video(state): ... - Notebooks
- Google Colab
- Kaggle
|
Download README.md from zeromodels/sam2_hiera_base_plus: direct link, hf CLI and curl.
- Browser
- Download file 4.34 kB
-
https://huggingface.co/zeromodels/sam2_hiera_base_plus/resolve/main/README.md
- Command line
-
hf download hf://zeromodels/sam2_hiera_base_plus/README.md
-
curl -L -o README.md https://huggingface.co/zeromodels/sam2_hiera_base_plus/resolve/main/README.md
4.34 kB
| pipeline_tag: mask-generation | |
| license: apache-2.0 | |
| base_model: facebook/sam2.1-hiera-base-plus | |
| library_name: zeromodels | |
| tags: | |
| - keras | |
| - zeromodels | |
| - sam2 | |
| - mask-generation | |
| - image-segmentation | |
| - arxiv:2408.00714 | |
| - pytorch | |
| - jax | |
| - tf | |
| ## ***See [our collection](https://huggingface.co/collections/zeromodels/sam-v1-v2-v3-6a8eaf73a274fffa2ea83cfd) for all versions of SAM.*** | |
| # Run SAM2 with Keras 3: JAX, PyTorch, or TensorFlow | |
| [](https://github.com/IMvision12/ZeroModels) [](https://imvision12.github.io/ZeroModels/sam2/) [](https://huggingface.co/collections/zeromodels/sam-v1-v2-v3-6a8eaf73a274fffa2ea83cfd) | |
| # zeromodels/sam2_hiera_base_plus | |
| Paper: [SAM 2: Segment Anything in Images and Videos (arXiv:2408.00714)](https://arxiv.org/abs/2408.00714) · [HF Papers](https://huggingface.co/papers/2408.00714) | |
| SAM2 keeps SAM's promptable formulation and replaces the plain ViT with a Hiera backbone plus FPN neck. This Keras port covers the image path only: point or box prompts on a single image (no video memory / frame propagation). | |
| For more details on the model, please go to Meta's original [model card](https://huggingface.co/facebook/sam2.1-hiera-base-plus). | |
| Pure-**Keras 3** conversion of [`facebook/sam2.1-hiera-base-plus`](https://huggingface.co/facebook/sam2.1-hiera-base-plus) for [zeromodels](https://github.com/IMvision12/ZeroModels). One implementation runs unmodified on **TensorFlow / Torch / JAX**. | |
| This is a **promptable segmentation** checkpoint (`SAM2PromptableSegment`): point (and optional box) prompts, backbone Hiera-B+. | |
| ## ✨ Quick start | |
| ```python | |
| import os | |
| os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow" | |
| import numpy as np | |
| from PIL import Image | |
| from zeromodels.models.sam2 import ( | |
| SAM2PromptableSegment, | |
| SAM2Processor, | |
| ) | |
| model = SAM2PromptableSegment.from_weights("zeromodels/sam2_hiera_base_plus") | |
| processor = SAM2Processor.from_weights("zeromodels/sam2_hiera_base_plus") | |
| image = Image.open("your_image.jpg").convert("RGB") | |
| inputs = processor( | |
| image, | |
| input_points=np.array([[[[450, 200]]]], dtype="float32"), | |
| input_labels=np.array([[[1]]], dtype="int32"), | |
| ) | |
| META = ("original_size", "reshaped_size") | |
| output = model({k: v for k, v in inputs.items() if k not in META}) | |
| masks = processor.post_process_masks( | |
| output["pred_masks"], original_size=inputs["original_size"] | |
| ) | |
| print(output["iou_scores"].shape, masks.shape) | |
| ``` | |
| Load any SAM / SAM2 / SAM3 variant the same way with `from_weights("zeromodels/<variant>")` (use `SAM2PromptableSegment` for this repo): | |
| | Variant | Hub | Family | | |
| |---|---|---| | |
| | `sam_vit_base` | [`zeromodels/sam_vit_base`](https://huggingface.co/zeromodels/sam_vit_base) | SAM | | |
| | `sam_vit_large` | [`zeromodels/sam_vit_large`](https://huggingface.co/zeromodels/sam_vit_large) | SAM | | |
| | `sam_vit_huge` | [`zeromodels/sam_vit_huge`](https://huggingface.co/zeromodels/sam_vit_huge) | SAM | | |
| | `sam2_hiera_small` | [`zeromodels/sam2_hiera_small`](https://huggingface.co/zeromodels/sam2_hiera_small) | SAM2 | | |
| | `sam2_hiera_base_plus` | [`zeromodels/sam2_hiera_base_plus`](https://huggingface.co/zeromodels/sam2_hiera_base_plus) | SAM2 | | |
| | `sam2_hiera_large` | [`zeromodels/sam2_hiera_large`](https://huggingface.co/zeromodels/sam2_hiera_large) | SAM2 | | |
| | `sam3` | [`zeromodels/sam3`](https://huggingface.co/zeromodels/sam3) | SAM3 | | |
| ## Tips | |
| - Set `KERAS_BACKEND` **before** importing Keras / zeromodels. | |
| - SAM / SAM2: point coordinates are in original pixel space; box prompts need `enable_boxes=True` / `include_box_input=True` when building the graph. | |
| - SAM2 in this port is image-only (no video memory bank). | |
| - SAM3: prefer `SAM3InstanceSegment.predict(...)` for text prompts; upstream `facebook/sam3` is gated. | |
| - See [SAM2 docs](https://imvision12.github.io/ZeroModels/sam2/) and [Loading Weights](https://imvision12.github.io/ZeroModels/loading_weights/). | |
| - Community / upstream safetensors still work via the `hf:` prefix, e.g. `SAM2PromptableSegment.from_weights("hf:facebook/sam2.1-hiera-base-plus")`. | |
| ## Special Thanks | |
| A huge thank you to the Meta SAM 2 authors for creating and releasing these models. | |
| License: Apache 2.0. | |