See our collection for all versions of SAM.

Run SAM2 with Keras 3: JAX, PyTorch, or TensorFlow

GitHub Docs Collection

kerasformers/sam2_hiera_large

Paper: SAM 2: Segment Anything in Images and Videos (arXiv:2408.00714) · HF Papers

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.

Pure-Keras 3 conversion of facebook/sam2.1-hiera-large for kerasformers. One implementation runs unmodified on TensorFlow / Torch / JAX.

This is a promptable segmentation checkpoint (SAM2PromptableSegment): point (and optional box) prompts, backbone Hiera-L.

✨ Quick start

import os
os.environ["KERAS_BACKEND"] = "torch"  # or "jax" / "tensorflow"

import numpy as np
from PIL import Image
from kerasformers.models.sam2 import (
    SAM2PromptableSegment,
    SAM2ImageProcessorWithPrompts,
)

model = SAM2PromptableSegment.from_weights("kerasformers/sam2_hiera_large")
processor = SAM2ImageProcessorWithPrompts()

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 SAM2PromptableSegment for this repo):

Variant Hub Family
sam_vit_base kerasformers/sam_vit_base SAM
sam_vit_large kerasformers/sam_vit_large SAM
sam_vit_huge kerasformers/sam_vit_huge SAM
sam2_hiera_small kerasformers/sam2_hiera_small SAM2
sam2_hiera_base_plus kerasformers/sam2_hiera_base_plus SAM2
sam2_hiera_large kerasformers/sam2_hiera_large SAM2
sam3 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 and Loading Weights.
  • Community / upstream safetensors still work via the hf: prefix, e.g. SAM2PromptableSegment.from_weights("hf:facebook/sam2.1-hiera-large").

Special Thanks

A huge thank you to the Meta SAM 2 authors for creating and releasing these models.

License: Apache 2.0.

Downloads last month
19
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for kerasformers/sam2_hiera_large

Finetuned
(7)
this model

Collection including kerasformers/sam2_hiera_large

Paper for kerasformers/sam2_hiera_large