mask-generation Β· vision Β· WASM Β· ~90 MB
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.
| Model | onnx-community/sam2.1-hiera-tiny-ONNX |
|---|---|
| Task / pipeline | mask-generation (SAM 2) via Sam2Model + Sam2Processor, point / box prompt |
| Architecture | Hiera hierarchical ViT encoder (multi-scale pyramid) + prompt encoder + mask decoder |
| Prompt | Points (foreground / background) and/or a box; returns 3 candidate masks + predicted-IoU + object score |
| Quantization | fp16 vision encoder (~67 MB), fp32 prompt/mask decoder (crisp masks) |
| Backend | WebAssembly β runs anywhere; no WebGPU required |
| Download | ~90 MB, cached after first load |
| License | Apache-2.0 (SAM 2 / Meta) |
| Runtime | Transformers.js v4 (Sam2Model β not present in v3.7.5) |
| Web APIs | WebAssembly, Web Workers, Canvas, FileReader β all Baseline widely available |
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.
Keyboard: focus the image, move the crosshair with arrow keys, press Enter to add a point. Or jump to a preset point:
Candidate masks (SAM 2 predicts three β pick one):
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.
Predicted IoU of the shown mask: β
| Prompt encoding | β |
|---|---|
| Candidate IoU scores | β |
| Object-presence logit | β |
| Encoder pyramid levels | β |
| Shown mask | β |
| Mask area | β |
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.
One click, three candidate masks β feel promptable segmentation first-hand.
Box-to-cutout & measure: lift the object onto transparency, read its pixel area.
Thin & occluded objects β refine with foreground / background points.
SAM 2 mask β crop β MobileViT classifies what you segmented.
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.