File size: 4,289 Bytes
e5ce39a
 
 
630ae4f
14fa3e4
e5ce39a
 
14fa3e4
e5ce39a
630ae4f
 
 
e5ce39a
630ae4f
 
e5ce39a
 
05a4f57
630ae4f
 
 
05a4f57
630ae4f
14fa3e4
630ae4f
 
 
 
 
 
 
14fa3e4
e5ce39a
630ae4f
 
 
e5ce39a
 
630ae4f
 
 
 
 
14fa3e4
630ae4f
bebd808
630ae4f
 
14fa3e4
 
630ae4f
 
 
 
 
 
 
 
 
 
 
 
 
e5ce39a
630ae4f
14fa3e4
630ae4f
 
 
14fa3e4
 
 
 
 
 
 
630ae4f
 
 
14fa3e4
630ae4f
 
 
14fa3e4
630ae4f
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
pipeline_tag: mask-generation
license: apache-2.0
base_model: facebook/sam-vit-large
library_name: zeromodels
tags:
- keras
- zeromodels
- sam
- mask-generation
- image-segmentation
- arxiv:2304.02643
- pytorch
- jax
- tf
---

## ***See [our collection](https://huggingface.co/collections/zeromodels/sam-v1-v2-v3-6a8eaf73a274fffa2ea83cfd) for all versions of SAM.***

# Run SAM with Keras 3: JAX, PyTorch, or TensorFlow

[![GitHub](https://img.shields.io/badge/GitHub-ZeroModels-black?logo=github)](https://github.com/IMvision12/ZeroModels) [![Docs](https://img.shields.io/badge/Docs-SAM-blue)](https://imvision12.github.io/ZeroModels/sam/) [![Collection](https://img.shields.io/badge/HF-SAM%20collection-yellow)](https://huggingface.co/collections/zeromodels/sam-v1-v2-v3-6a8eaf73a274fffa2ea83cfd)

# zeromodels/sam_vit_large

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-large).

Pure-**Keras 3** conversion of [`facebook/sam-vit-large`](https://huggingface.co/facebook/sam-vit-large) for [zeromodels](https://github.com/IMvision12/ZeroModels). One implementation runs unmodified on **TensorFlow / Torch / JAX**.

This is a **promptable segmentation** checkpoint (`SAMPromptableSegment`): point (and optional box) prompts, backbone ViT-L.

## ✨ Quick start

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

import numpy as np
from PIL import Image
from zeromodels.models.sam import (
    SAMPromptableSegment,
    SAMProcessor,
)

model = SAMPromptableSegment.from_weights("zeromodels/sam_vit_large")
processor = SAMProcessor.from_weights("zeromodels/sam_vit_large")

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 `SAMPromptableSegment` 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 [SAM docs](https://imvision12.github.io/ZeroModels/sam/) and [Loading Weights](https://imvision12.github.io/ZeroModels/loading_weights/).
- Community / upstream safetensors still work via the `hf:` prefix, e.g. `SAMPromptableSegment.from_weights("hf:facebook/sam-vit-large")`.

## Special Thanks

A huge thank you to the Meta Segment Anything authors for creating and releasing these models.

License: Apache 2.0.