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
Migrate to zeromodels (rename kf_*.json -> zm_*.json, fix refs in config + README, ensure tag + badge)
Browse filesFiles changed (3)
- README.md+19-19
- kf_config.json → zm_config.jsonRenamed+57-57
- kf_preprocessor.json → zm_preprocessor.jsonRenamed+18-18
1 unmodified line2345678910113 unmodified lines151617181920212223242526271 unmodified line293031323334355 unmodified lines414243444546474849505152539 unmodified lines636465666768697071727374757677787980818283848586871 unmodified linepipeline_tag: mask-generationlicense: apache-2.0base_model: facebook/sam2.1-hiera-base-pluslibrary_name: kerasformerstags:- keras- kerasformers- sam2- mask-generation- image-segmentation3 unmodified lines- tf---## ***See [our collection](https://huggingface.co/collections/kerasformers/sam-v1-v2-v3-6a6a8c261dabbc2996e1b4a2) for all versions of SAM.***# Run SAM2 with Keras 3: JAX, PyTorch, or TensorFlow[](https://github.com/IMvision12/KerasFormers) [](https://imvision12.github.io/KerasFormers/sam2/) [](https://huggingface.co/collections/kerasformers/sam-v1-v2-v3-6a6a8c261dabbc2996e1b4a2)# kerasformers/sam2_hiera_base_plusPaper: [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)1 unmodified lineFor 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 [kerasformers](https://github.com/IMvision12/KerasFormers). One implementation runs unmodified on **TensorFlow / Torch / JAX**.This is a **promptable segmentation** checkpoint (`SAM2PromptableSegment`): point (and optional box) prompts, backbone Hiera-B+.5 unmodified linesimport numpy as npfrom PIL import Imagefrom kerasformers.models.sam2 import (SAM2PromptableSegment,SAM2Processor,)model = SAM2PromptableSegment.from_weights("kerasformers/sam2_hiera_base_plus")processor = SAM2Processor.from_weights("kerasformers/sam2_hiera_base_plus")image = Image.open("your_image.jpg").convert("RGB")inputs = processor(9 unmodified linesprint(output["iou_scores"].shape, masks.shape)```Load any SAM / SAM2 / SAM3 variant the same way with `from_weights("kerasformers/<variant>")` (use `SAM2PromptableSegment` for this repo):| Variant | Hub | Family ||---|---|---|| `sam_vit_base` | [`kerasformers/sam_vit_base`](https://huggingface.co/kerasformers/sam_vit_base) | SAM || `sam_vit_large` | [`kerasformers/sam_vit_large`](https://huggingface.co/kerasformers/sam_vit_large) | SAM || `sam_vit_huge` | [`kerasformers/sam_vit_huge`](https://huggingface.co/kerasformers/sam_vit_huge) | SAM || `sam2_hiera_small` | [`kerasformers/sam2_hiera_small`](https://huggingface.co/kerasformers/sam2_hiera_small) | SAM2 || `sam2_hiera_base_plus` | [`kerasformers/sam2_hiera_base_plus`](https://huggingface.co/kerasformers/sam2_hiera_base_plus) | SAM2 || `sam2_hiera_large` | [`kerasformers/sam2_hiera_large`](https://huggingface.co/kerasformers/sam2_hiera_large) | SAM2 || `sam3` | [`kerasformers/sam3`](https://huggingface.co/kerasformers/sam3) | SAM3 |## Tips- Set `KERAS_BACKEND` **before** importing Keras / kerasformers.- 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/KerasFormers/sam2/) and [Loading Weights](https://imvision12.github.io/KerasFormers/loading_weights/).- Community / upstream safetensors still work via the `hf:` prefix, e.g. `SAM2PromptableSegment.from_weights("hf:facebook/sam2.1-hiera-base-plus")`.## Special Thanks1 unmodified line2345678910113 unmodified lines151617181920212223242526271 unmodified line293031323334355 unmodified lines414243444546474849505152539 unmodified lines636465666768697071727374757677787980818283848586871 unmodified linepipeline_tag: mask-generationlicense: apache-2.0base_model: facebook/sam2.1-hiera-base-pluslibrary_name: zeromodelstags:- keras- zeromodels- sam2- mask-generation- image-segmentation3 unmodified lines- tf---## ***See [our collection](https://huggingface.co/collections/zeromodels/sam-v1-v2-v3-6a6a8c261dabbc2996e1b4a2) 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-6a6a8c261dabbc2996e1b4a2)# zeromodels/sam2_hiera_base_plusPaper: [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)1 unmodified lineFor 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+.5 unmodified linesimport numpy as npfrom PIL import Imagefrom 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(9 unmodified linesprint(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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758{"library_name": "kerasformers","kerasformers_version": "1.2.1","model_module": "kerasformers.models.sam2","model_class": "SAM2PromptableSegment","variant": "sam2_hiera_base_plus","weights": "model.weights.h5","schema_version": 2,"weight_dtype": "float32","model_type": "sam2","vision_config": {"hidden_dim": 112,"blocks_per_stage": [2,3,16,3],"embed_dim_per_stage": [112,224,448,896],"num_attention_heads_per_stage": [2,4,8,16],"window_size_per_stage": [8,4,14,7],"global_attention_blocks": [12,16,20],"backbone_channel_list": [896,448,224,112],"window_pos_embed_bg_size": [14,14],"num_multimask_outputs": 3,"include_box_input": false,"include_mask_input": false,"multimask_output": true,"image_size": 1024}}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758{"library_name": "zeromodels","zeromodels_version": "1.2.1","model_module": "zeromodels.models.sam2","model_class": "SAM2PromptableSegment","variant": "sam2_hiera_base_plus","weights": "model.weights.h5","schema_version": 2,"weight_dtype": "float32","model_type": "sam2","vision_config": {"hidden_dim": 112,"blocks_per_stage": [2,3,16,3],"embed_dim_per_stage": [112,224,448,896],"num_attention_heads_per_stage": [2,4,8,16],"window_size_per_stage": [8,4,14,7],"global_attention_blocks": [12,16,20],"backbone_channel_list": [896,448,224,112],"window_pos_embed_bg_size": [14,14],"num_multimask_outputs": 3,"include_box_input": false,"include_mask_input": false,"multimask_output": true,"image_size": 1024}}
12345678910111213141516171819{"library_name": "kerasformers","kerasformers_version": "1.1.3","preprocessor_module": "kerasformers.models.sam2","preprocessor_class": "SAM2ImageProcessor","variant": "sam2_hiera_base_plus","target_length": 1024,"image_mean": [0.485,0.456,0.406],"image_std": [0.229,0.224,0.225],"data_format": null}12345678910111213141516171819{"library_name": "zeromodels","zeromodels_version": "1.1.3","preprocessor_module": "zeromodels.models.sam2","preprocessor_class": "SAM2ImageProcessor","variant": "sam2_hiera_base_plus","target_length": 1024,"image_mean": [0.485,0.456,0.406],"image_std": [0.229,0.224,0.225],"data_format": null}
README.md+19-19
kf_config.json → zm_config.jsonRenamed+57-57
kf_preprocessor.json → zm_preprocessor.jsonRenamed+18-18