At a glance
| 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.
Camera frames stay strictly on your device. Approve permission when prompted, then capture a snapshot.
Camera idle.
Initialising model loader…
Select a photo to begin. Left-click adds active point type; right-click adds a negative exclusion point.
3 Ambiguity Masks (click to switch instantly)
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
| 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
Basics: Ambiguity & Latency
View all 3 ambiguity masks side-by-side, trace +1/−1 refinement, and benchmark 1× encode : 5× decode.
Practical: Batch Sticker Cutouts
Extract multi-object transparent PNG stickers in a cancellable queue with polygon hulls & OWASP CSV/JSON.
Wild: Edge & Exclusion Stress Lab
Stress-test thin spokes, glass refraction, night clutter, occlusion, and negative-click exclusion live.
Multi-model: SlimSAM-77 vs SAM ViT-Base
Compare pruned SlimSAM-77 against full SAM ViT-Base on identical clicks with live IoU & Boundary Dice.