Skip to content
mask-generation · vision 93.7M Params · ViT-B/16 + 2-Way Decoder · 3 Masks WASM q8 · 106.0 MB License: Apache-2.0

SAM ViT-Base — Meta's Canonical Promptable Segment Anything

Meta's canonical Segment Anything ViT-Base (facebook/sam-vit-base) pairs a 12-layer ViT-B/16 image encoder (768 hidden, 14×14 windowed + 4 global attention blocks, cached once per photo) with a two-way transformer prompt/mask decoder that turns foreground (+1) and background (0) clicks into 3 ambiguity-aware masks (Sub-part, Part, Whole) with live predicted IoU scores in 106.0 MB CPU WASM q8.

At a glance

SAM ViT-Base model specification and verified runtime contract
Upstream model facebook/sam-vit-base
Loaded ONNX repo Xenova/sam-vit-base (onnx/vision_encoder_quantized.onnx + onnx/prompt_encoder_mask_decoder_quantized.onnx)
Task / API mask-generation · SamModel + AutoProcessor (get_image_embeddings → post_process_masks)
Architecture SamModel (~93.7M params): 12-layer ViT-B/16 encoder (1024×1024 → 64×64 patches, hidden_size: 768, window_size: 14, global layers [2, 5, 8, 11], neck 256 ch) + 2-layer two-way transformer decoder (8 heads, num_multimask_outputs: 3, 3-layer IoU MLP)
Preprocessor SamImageProcessor — longest-edge 1024 bilinear resize, bottom/right pad to 1024×1024, rescale 1/255, ImageNet RGB mean/std normalisation
Backend & size CPU WASM q8 — 105.99 MB (~106.0 MB, 105,993,185 B: 101,088,469 B vision encoder + 4,903,810 B prompt/mask decoder + 906 B configs) via shared @huggingface/transformers@3.7.5 in lib/webai.js
License Apache-2.0 (upstream Meta AI weights and Xenova ONNX export)

Interactive Click-to-Segment Studio & 3-Mask Ambiguity Inspector

Pick a rights-cleared starter photo, drop or upload an image, press Ctrl/Cmd+V to paste, or capture a live webcam snapshot. Click anywhere on the photo (or move the keyboard crosshair) to add + Foreground or − Exclude points — the heavy ViT-B/16 encoder runs once per image, and every click decodes all 3 ambiguity masks off the main thread.

Initialising model loader…

Select a photo to begin. Left-click adds active point type; right-click adds a negative exclusion point.

x 0.50, y 0.50

3 Ambiguity Masks (click to switch instantly)

Encode ms (cached)–
Decode ms (click)–
Predicted IoU–
Coverage / Area–
Bounding box–
Compactness–
Embedding tensor[1,256,64,64]
NaN / invalid0

Transparent Alpha Cutout Preview

Click the image to generate a downloadable transparent PNG cutout.

Raw mask & prompt diagnostics (JSON)
{}

How SAM ViT-Base compares to SlimSAM-77 & SAM 2.1 Hiera Tiny

Architectural comparison of browser Segment Anything models
Model Image encoder Decoder & prompts Transformers.js WASM footprint
SAM ViT-Base 12-layer ViT-B/16 (768 dim, 4 global blocks) 2-way transformer · 3 masks + IoU · points 3.7.5 (shared) 105.99 MB (q8)
SlimSAM-77 Pruned ViT-B (77% structured pruning) Shared SAM 2-way decoder · 3 masks + IoU 3.7.5 (shared) 25.44 MB (q8/fp32)
SAM 2.1 Hiera Tiny Hierarchical Hiera-T multi-scale pyramid SAM 2 decoder + object score · points/boxes 4.3.0 (local pin) ~90 MB (fp16/fp32)

How the API works

Interactive point prompting calls SamModel and AutoProcessor directly inside a Web Worker: compute the [1, 256, 64, 64] ViT-B/16 image embedding once per photo, then feed every new positive (1n) or negative (0n) point to the lightweight mask decoder:

import { SamModel, AutoProcessor, RawImage, Tensor } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.5";

const model = await SamModel.from_pretrained("Xenova/sam-vit-base", { device: "wasm", dtype: "q8" });
const processor = await AutoProcessor.from_pretrained("Xenova/sam-vit-base");

// Stage 1 (cached once per photo):
const imageInputs = await processor(rawImage);
const embeddings = await model.get_image_embeddings(imageInputs);

// Stage 2 (milliseconds per click):
const [rH, rW] = imageInputs.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]);
const outputs = await model({ ...embeddings, input_points, input_labels });
const masks = await processor.post_process_masks(
  outputs.pred_masks, imageInputs.original_sizes, imageInputs.reshaped_input_sizes,
); // masks[0]: [1, 3, H, W] bool + outputs.iou_scores

See inside the code

Show the code — worker.js: embedImage() (ViT-B/16 vision encoder + per-image tensor cache)
Show the code — worker.js: segmentPrompt() (3-mask decoder + off-thread OffscreenCanvas overlays)
Show the code — sam-base-core.mjs: labelAmbiguityMasks() (Sub-part / Part / Whole IoU ranking)

Explore the 5-route use-case ladder

References & model card