Instructions to use kerasformers/sam_vit_base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- KerasFormers
How to use kerasformers/sam_vit_base with KerasFormers:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Keras
How to use kerasformers/sam_vit_base with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://kerasformers/sam_vit_base") - Notebooks
- Google Colab
- Kaggle
| pipeline_tag: mask-generation | |
| license: apache-2.0 | |
| base_model: facebook/sam-vit-base | |
| library_name: kerasformers | |
| tags: | |
| - keras | |
| - kerasformers | |
| - sam | |
| - mask-generation | |
| - image-segmentation | |
| - arxiv:2304.02643 | |
| - pytorch | |
| - jax | |
| - tf | |
| ## ***See [our collection](https://huggingface.co/collections/kerasformers/sam-v1-v2-v3-6a6a8c261dabbc2996e1b4a2) for all versions of SAM.*** | |
| # Run SAM with Keras 3: JAX, PyTorch, or TensorFlow | |
| [](https://github.com/IMvision12/KerasFormers) [](https://imvision12.github.io/KerasFormers/sam/) [](https://huggingface.co/collections/kerasformers/sam-v1-v2-v3-6a6a8c261dabbc2996e1b4a2) | |
| # kerasformers/sam_vit_base | |
| Paper: [Segment Anything (arXiv:2304.02643)](https://arxiv.org/abs/2304.02643) · [HF Papers](https://huggingface.co/papers/2304.02643) | |
| SAM (Segment Anything Model) segments whatever you point at. It has no class vocabulary: you give it a prompt (a click or a box) and it returns a mask. A heavy ViT image encoder runs once per image; the prompt encoder and mask decoder are light enough for interactive use. | |
| For more details on the model, please go to Meta's original [model card](https://huggingface.co/facebook/sam-vit-base). | |
| Pure-**Keras 3** conversion of [`facebook/sam-vit-base`](https://huggingface.co/facebook/sam-vit-base) for [kerasformers](https://github.com/IMvision12/KerasFormers). One implementation runs unmodified on **TensorFlow / Torch / JAX**. | |
| This is a **promptable segmentation** checkpoint (`SAMPromptableSegment`): point (and optional box) prompts, backbone ViT-B. | |
| ## ✨ Quick start | |
| ```python | |
| import os | |
| os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow" | |
| import numpy as np | |
| from PIL import Image | |
| from kerasformers.models.sam import ( | |
| SAMPromptableSegment, | |
| SAMImageProcessorWithPrompts, | |
| ) | |
| model = SAMPromptableSegment.from_weights("kerasformers/sam_vit_base") | |
| processor = SAMImageProcessorWithPrompts() | |
| 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("kerasformers/<variant>")` (use `SAMPromptableSegment` 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 [SAM docs](https://imvision12.github.io/KerasFormers/sam/) and [Loading Weights](https://imvision12.github.io/KerasFormers/loading_weights/). | |
| - Community / upstream safetensors still work via the `hf:` prefix, e.g. `SAMPromptableSegment.from_weights("hf:facebook/sam-vit-base")`. | |
| ## Special Thanks | |
| A huge thank you to the Meta Segment Anything authors for creating and releasing these models. | |
| License: Apache 2.0. | |