Inference /SAM 2.1 segmentation

SAM 2.1 segmentation

Point-prompted image segmentation on any 12 GB card: click a point, get masks.

What it is

Folder: sam2.1_segmentation_min_6gb_vram. Promptable segmentation with SAM 2.1 hiera-large (Apache-2.0): send an image and a point or box, get pixel-accurate masks back, ranked by confidence. A general tool — the base for interactive labeling, QC tooling, or any click-to-select feature.

It runs the standard predictor contract: one class, Sam2Segmenter, whose predict() accepts JSON with prompts or bare image bytes (which default to a center-point prompt). Weights (~3.5 GB) pull from HuggingFace Hub inside __init__; there is no weights upload.

inference.yaml (core)
model:
  module: "sam2_serve"
  class_name: "Sam2Segmenter"
  init_args:
    model_id: "facebook/sam2.1-hiera-large"
    max_masks: 3                # top-K masks per prompt
    dtype: "auto"               # fp16 on GPU, fp32 on CPU

cluster:
  num_workers: 1
  gpus_per_worker: 1
  cpus_per_worker: 8
  min_vram_gb_per_worker: 6     # ~3.5 GB weights fp16 + activations
  max_ongoing_requests: 8
  device: "auto"

requirements:
  - "torch"
  - "transformers>=4.56,<5"     # the Sam2 classes landed in 4.56
  - "Pillow>=10.0.0"
  - "numpy"

Request and response

Prompts ride in a JSON body sent as octet-stream (the predict_json pattern; plain application/json would drop the prompt fields, see serve.py):

request
{
  "image_b64": "<base64 png/jpeg>",     // or send raw image bytes instead
  "points": [[x, y]],                   // prompt points, in pixels
  "labels": [1],                        // 1 = foreground, 0 = background
  "boxes": [[x1, y1, x2, y2]],          // optional box prompts
  "multimask": true                     // return top-K candidates
}
response
{
  "masks": [
    {"score": 0.97, "area": 12345, "bbox": [x1, y1, x2, y2],
     "png_b64": "<base64 1-bit PNG, white = mask>"}
  ],
  "image_size": {"width": W, "height": H},
  "device": "cuda", "elapsed_ms": 42.0
}

Submit and call

Shell
cd sam2.1_segmentation_min_6gb_vram
cp .env.example .env        # RESON_API_KEY + S3 keys
python submit.py            # prints endpoint + predict key
python predict.py                        # segments the sample image
python predict.py ./photo.jpg 420 310    # your image, point at (420, 310)

From your own code, via the SDK client:

client.py
import base64
from resontech import InferenceClient

client = InferenceClient(url=ENDPOINT, predict_api_key=KEY, timeout=300.0)

result = client.predict_json({
    "image_b64": base64.b64encode(open("photo.jpg", "rb").read()).decode(),
    "points": [[420, 310]],
    "labels": [1],
    "multimask": True,
})
for mask in result["masks"]:
    print(mask["score"], mask["bbox"])

Hardware

VRAM min → recCPURAMFree disk
6 → 12 GB4 cores24 GB~13 GB (image + ~3.5 GB weights)
note
Any 12 GB card runs it comfortably (RTX 5070 / 4070 / 3060-12G). For shared-worker deployments, switching model_id to sam2.1-hiera-small (~1 GB) halves the footprint at some quality cost.