Skip to content

image-segmentation · trimap matting WASM q8 · 27.5 MB 4-ch RGB + Trimap Apache-2.0

ViTMatte-Small (Distinctions-646)

Trimap-guided Vision Transformer matting (hustvl/vitmatte-small-distinctions-646) that fuses a 4-channel RGB + trimap tensor through a ViTDet backbone and detail-preserving ConvStream to pull fine hair, whiskers, fur, and translucent glass alpha mattes on-device.

Interactive Trimap Brush Studio

Loading sample photo and generating initial auto-trimap…

Input Photo + Editable Trimap

Paint FG / Unknown / BG directly

ViTMatte Soft Alpha Output

Awaiting inference…
Backend / dtypeWASM · q8
Inference–
4-Ch Input Tensor–
Output Alphas–
Solid FG / Soft Edge–
Unknown Band Share–
Non-finite Values–

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

0 / 128 / 255

Predicted Alpha Distribution (16 bins)

Run inference to inspect
ViTMatte-Small architecture parameters (verified from Xenova/vitmatte-small-distinctions-646/config.json)
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

Model specifications & verified browser delivery metadata
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

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]

References