← All models

mask-generation Β· vision Β· WASM Β· ~90 MB

SAM 2.1 β€” click or box a point, get its mask

Segment Anything 2 is Meta's newer promptable segmenter: a hierarchical Hiera backbone that emits a multi-scale feature pyramid, driving a prompt encoder and mask decoder. Point at anything β€” or drag a box around it β€” and SAM 2.1 returns three candidate masks with predicted-IoU scores. The heavy image encoder runs once; every extra prompt is fast. The same architecture powers SAM 2 video tracking; this page runs the image path, on-device.

At a glance

Modelonnx-community/sam2.1-hiera-tiny-ONNX
Task / pipelinemask-generation (SAM 2) via Sam2Model + Sam2Processor, point / box prompt
ArchitectureHiera hierarchical ViT encoder (multi-scale pyramid) + prompt encoder + mask decoder
PromptPoints (foreground / background) and/or a box; returns 3 candidate masks + predicted-IoU + object score
Quantizationfp16 vision encoder (~67 MB), fp32 prompt/mask decoder (crisp masks)
BackendWebAssembly β€” runs anywhere; no WebGPU required
Download~90 MB, cached after first load
LicenseApache-2.0 (SAM 2 / Meta)
RuntimeTransformers.js v4 (Sam2Model β€” not present in v3.7.5)
Web APIsWebAssembly, Web Workers, Canvas, FileReader β€” all Baseline widely available

Run it

Load the model, pick an image (the encoder runs once), then choose a prompt mode: click a point on the picture, or switch to Box and drag a rectangle. SAM 2 returns three candidate masks β€” pick whichever fits best.

Drop an image here, or click to choose
Two cats sleeping on a pink couch A cozy sunlit bedroom with a bookshelf A riverside at golden hour with a swan
Prompt: Next point:

Keyboard: focus the image, move the crosshair with arrow keys, press Enter to add a point. Or jump to a preset point:

See inside

SAM 2 predicts three candidate masks per prompt β€” roughly "the part / the thing / the whole region" β€” and rates each with a predicted IoU, its own guess of mask quality. It also emits an object-presence score (a logit for "is there really an object here"). The Hiera backbone hands the decoder a multi-scale feature pyramid rather than a single grid β€” that's the SAM-2 upgrade over SAM 1. Below are the raw numbers for the current prompt.

Add a prompt to see the raw mask numbers.

Why it matters

SAM 2 makes "select exactly the thing I mean" a fast, local, interactive operation β€” with better masks than SAM 1 and a box prompt when a click is ambiguous. It's the building block under smart selection, cut-out, measurement, and annotation tools, and its memory architecture extends the same prompt to video.

Use cases

Basics

One click, three candidate masks β€” feel promptable segmentation first-hand.

Practical

Box-to-cutout & measure: lift the object onto transparency, read its pixel area.

Wild

Thin & occluded objects β€” refine with foreground / background points.

Multi-model

SAM 2 mask β†’ crop β†’ MobileViT classifies what you segmented.

How the API works

SAM 2 uses Sam2Model + Sam2Processor directly. Encode the image once, then decode per prompt:

import { Sam2Model, Sam2Processor, RawImage, Tensor } from "@huggingface/transformers"; // v4+

const model = await Sam2Model.from_pretrained("onnx-community/sam2.1-hiera-tiny-ONNX", {
  dtype: { vision_encoder: "fp16", prompt_encoder_mask_decoder: "fp32" }, device: "wasm",
});
const processor = await Sam2Processor.from_pretrained("onnx-community/sam2.1-hiera-tiny-ONNX");

// 1. Encode the image ONCE β€” the Hiera backbone returns a multi-scale pyramid (image_embeddings.0/1/2):
const image = await RawImage.read(imageURL);
const inputs = await processor(image);
const embeddings = await model.get_image_embeddings(inputs);

// 2. Per prompt, decode masks. SAM 2 pads to 1024Γ—1024, so scale prompts into reshaped_input_sizes:
const [rH, rW] = inputs.reshaped_input_sizes[0];
const input_points = new Tensor("float32", [nx * rW, ny * rH], [1, 1, 1, 2]);
const input_labels = new Tensor("int64", [1n], [1, 1, 1]); // 1 = foreground, 0 = background
// (or a box:) const input_boxes = new Tensor("float32", [x1*rW,y1*rH,x2*rW,y2*rH], [1, 1, 4]);
const out = await model({ ...embeddings, input_points, input_labels });

const masks = await processor.post_process_masks(
  out.pred_masks, inputs.original_sizes, inputs.reshaped_input_sizes,
); // masks[0]: [1, 3, H, W] bool β€” three candidates; rank by out.iou_scores

This page runs all of that in a Web Worker and caches the embeddings, so the first prompt pays for the vision encoder and every prompt after is just the lightweight mask decoder.

References