Loading sample photo and generating initial auto-trimap…
ViTMatte Soft Alpha Output
See inside — 4-channel ViTDet + ConvStream fusion
Unlike unconditional background removers that guess the foreground from 3 RGB channels alone,
ViTMatte concatenates your single-channel trimap (0 = background,
0.5 = unknown hair/fur transition, 1.0 = foreground) directly onto the
normalized RGB tensor to form [1, 4, H_pad, W_pad] (padded to multiples of
32). A plain ViTDet-Small encoder (hidden_size: 384,
window_size: 14, global residual blocks at layers 2, 5, 8, 11) captures long-range
context while a parallel lightweight ConvStream ([48, 96, 192]) preserves crisp
sub-pixel hair strands through the 4-stage Fusion decoder ([256, 128, 64, 32]).
Channel 4 — Grayscale Trimap Tensor
Predicted Alpha Distribution (16 bins)
| Component | Configuration | Role in Matting |
|---|
Show the code — worker.js model & processor initialisation
Show the code — 4-channel RGB+trimap matting & right/bottom pad crop
At a glance
| Upstream model | hustvl/vitmatte-small-distinctions-646 (Yao et al., ViTMatte trained on Distinctions-646) |
|---|---|
| Browser ONNX repo | Xenova/vitmatte-small-distinctions-646 → onnx/model_quantized.onnx |
| Task & classes | image-segmentation (trimap image matting) · VitMatteForImageMatting + VitMatteImageProcessor |
| Input / Output shape | Input: pixel_values [1, 4, H_pad, W_pad] (RGB normalized + trimap / 255, size_divisibility: 32) → Output: alphas [1, 1, H_pad, W_pad] in [0, 1] |
| Quantization & size | q8: 27.50 MB decimal (27,501,604 B total: 27,499,369 B quantized ONNX + configs; unquantized fp32 103.89 MB unused) |
| Runtime & backend | Shared ../../lib/webai.js with Transformers.js 3.7.5 · CPU WASM inside a FIFO module Web Worker |
| License | apache-2.0 (commercial-safe upstream license) |
Why trimap matting matters
Single-click background removers work well until a portrait has backlit curly hair, a pet has fine whiskers, or a product includes glass or netting. Trimap matting lets a human brush (or an upstream foreground mask in the Multi-model pipeline below) mark what is definitely foreground, definitely background, and where the model should focus its entire capacity on sub-pixel translucency — all locally with zero cloud GPU fees.
Use-case ladder
Basics · Band & Calibration
Sweep unknown-band widths (tight vs balanced vs wide), working resolutions (384–768 px), and live 0 ms alpha calibration curves.
Practical · Batch Cutout Queue
Process multiple portraits and product shots off-main-thread with Pause/Resume/Cancel, individual PNG downloads, and OWASP-safe CSV/JSON manifests.
Wild · Compositor & Fringe Lab
Swap studio backdrops with color-spill decontamination and empirically ablate tight, wide, shifted, and leaking trimaps with live delta heatmaps.
Multi-model · ORMBG → ViTMatte
Chain Apache-2.0 ORMBG (44.3 MB) → Euclidean erode/dilate morphological trimap → ViTMatte-Small (27.5 MB) for fully automatic soft-edge refinement.
How the API works
import { AutoProcessor, VitMatteForImageMatting, RawImage } from "@huggingface/transformers";
const processor = await AutoProcessor.from_pretrained("Xenova/vitmatte-small-distinctions-646");
const model = await VitMatteForImageMatting.from_pretrained(
"Xenova/vitmatte-small-distinctions-646",
{ device: "wasm", dtype: "q8" },
);
const image = new RawImage(rgbUint8, width, height, 3);
const trimap = new RawImage(trimapUint8, width, height, 1); // 0 = BG, 128 = Unknown, 255 = FG
const inputs = await processor(image, trimap); // pixel_values: [1, 4, padH, padW]
const { alphas } = await model(inputs); // alphas: [1, 1, padH, padW] float32 in [0, 1]