ViTMatte
ViTMatte (Huazhong University of Science and Technology, 2023): refines a rough trimap (sure background, sure foreground, unknown band) into a soft alpha matte, e.g. hair and fur edges; a plain Vision Transformer plus a small detail decoder; small and base versions. Apache-2.0 code and weights.
experimental Fork of hustvl/ViTMatte@8cd7ef0 · backends: portable, kernels
- Paper ViTMatte: Boosting Image Matting with Pretrained Plain Vision Transformers Information Fusion 2024 · first released 24 May 2023
Paper 2023 Added in 0.2.0 Evaluated with 0.2.0
Use it
pip install fastforks Lean install with only what this model needs: pip install fastforks-core
from fastforks import matting
model = matting.load("vitmatte-s") # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.alpha
result.backend # what ran: mode, device, dtype, kernels fastforks bench vitmatte-s Times the model on this machine. load(…, mode="portable") or mode="kernels" picks a backend; usage guide.
Speed and memory
Every backend on one GPU (RTX 5060 Ti 16 GB) and the same input, the authors’ code included. Kernels runs fp16. Portable runs fp16 on CUDA.
Input: synthetic, 1080x1920 (the task’s standard size)
| Backend | dtype | Time per call (ms) | vs original fp16 | Peak memory allocated / reserved | Load (s) | First call (s) |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | not measured | – | – | – | – |
| Original fp16the authors' mixed-precision option (float16); transformers 5.19.0 VitMatteForImageMatting, normalise / trimap / pad 32 / crop | fp16 | 140 | – | 2,536 MiB/2,582 MiB | 0.1 | 0.39 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 102 | 1.4× | 1,401 MiB/1,524 MiB | 0.2 | 0.41 |
| Kernels | fp16 | 84.1 | 1.7× | 1,305 MiB/1,404 MiB | 0.8 | 0.35 |
| Portable + torch.compile | 81.9 | 1.7× | 1,025 MiB/1,284 MiB | 31 (build) | – | |
| TensorRT fp16 | fp16 | 54.6 | 2.6× | 696 MiBenginenot comparable | 51 (build) | – |
Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-12 to 2026-11-15. First call, 3 warm-up, peak reset, median of 3 rounds x median of 5. Ratios divide two
times of this table (same GPU, same input). Original fp16 is the authors’ own precision option, else torch.autocast. torch.compile and TensorRT: build time instead of load time. TensorRT memory (engine, not comparable) is the GPU memory the engine took, read from the driver because TensorRT allocates outside PyTorch, so it can't be compared with the PyTorch peaks of the other rows.
Other devices
| Device | Backend | dtype | ms / call | Peak memory | Output vs reference | Input |
|---|---|---|---|---|---|---|
| Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14 | Original | fp32 | 3605 | 9,787 MiB process | – | 1080x1920 |
| Portable | fp16 | 1360 | 2,542 MiB process | matches | 1080x1920 | |
| Portable | fp32 | 2333 | 5,185 MiB process | matches | 1080x1920 | |
| Kernels | fp16 | not implemented | – | 1080x1920 | ||
CUDA memory is torch’s peak allocated / reserved; Apple has no such counter, so the Mac rows show the process footprint (weights, Python and torch included). “Output vs reference” compares the device’s output with the stored golden output under the card’s tolerance; – = not checked on that device. Kernels “not implemented” = no kernels for this model on that backend yet: it runs portable there.
Licence and weights
Commercial use allowed
- Code
- Apache-2.0 LICENSE
- Weights
- Apache-2.0 source
- Training data
-
- Composition-1k (Adobe Deep Image Matting) research-only, non-commercial
Our reading of the licences, not legal advice.
Weights
vitmatte-s-composition-1k.safetensors 103 MB· hosted on Hugging Facefastforks downloads and checks these files on first use (fastforks download vitmatte-s).
Accuracy
The test set through the authors’ code and through ours: same checkpoint, same protocol. The distance between the original’s fp32 and fp16 runs is what half precision alone costs; our runs should stay within it.
AIM-500
every 5th image (100), generated trimaps, SAD / MSE x 1000 (unknown region)
| Run | dtype | sad ↑ | mse ↑ | Δ sad vs fp32 original | images | ms / image |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 13.86 | 11.727 | reference | 100 | 215 |
| Original fp16 | fp16 | 13.85 | 11.727 | −0.01 | 100 | 110 |
| Portable fp32 | fp32 | 13.86 | 11.727 | ±0.00 | 100 | 293 |
| Portable | fp16 | 13.85 | 11.726 | −0.01 | 100 | 85.3 |
| Kernels | fp16 | 13.85 | 11.723 | −0.01 | 100 | 70.2 |
Trimap-guided models, AIM-500: each image at its own size with a trimap made from its alpha (alpha 255 / 0 eroded by a 21 px ellipse = foreground / background, the rest unknown); SAD / 1000 and MSE of the 8-bit alpha in the unknown region (ViTMatte's evaluation). Original = the authors’ code, same checkpoint and protocol. Measured on original fp32, original fp16: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-12; portable fp32, portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 4068530d, 2026-11-12.
Closeness to the original
Beyond the test-set scores: the raw output of ours against the original’s fp32 output on the same input, pixel by pixel. CPU, float32: max |diff| <= 5.8e-06 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 5.3e-05 and p99 <= 0.0012 (absolute error of the output alpha).
| Check | Tolerance | Measured |
|---|---|---|
| CPU fp32, max |diff| | ≤ 0.0000058 | not recorded |
| GPU default dtypes, mean |diff| | ≤ 0.000053 | 0.00003 |
| GPU default dtypes, p99 |diff| | ≤ 0.0012 | 0.0007 |
Recorded for vitmatte-s on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-12, with these kernels: cudnn_hacccutlass_convdpt_fusedigev
Details
ViTMatte (Yao et al., HUST, Information Fusion 2024): trimap-based natural image matting. A plain ViT (ViTDet: windowed attention with relative positions plus four global blocks with convolutional residual blocks) sees the image and its trimap as one 4-channel input; a light convolutional detail-capture decoder fuses the ViT features with a full-resolution conv stream into the alpha. Needs a trimap (0 background, 128 unknown, 255 foreground).
- Input / output
- Input: one RGB image (trimap-guided models also a trimap: background / unknown / foreground). Output: an alpha matte
[H, W]in [0, 1] at the input size: 1 = foreground (background removal, dichotomous / salient object segmentation). - Input size
- any H x W >= 32, padded to a multiple of 32; tested up to 3000x4000 (7,954 MiB allocated)
- Outputs
alpha- CUDA kernels
dpt_fusedfused fp16 matrix multiplies and convolutions for DINOv2 backbones and DPT depth headscudnn_haccconvolutions with fp16 accumulation (cuDNN)igevIGEV stereo: convolution epilogues, correlation lookups, GRU gatescutlass_convCUTLASS convolutions with the recurrent update (GRU) fused in
- Apple (MPS)
- fp32, fp16
Full reference, generated from the repository
ViTMatte (Huazhong University of Science and Technology, 2023): refines a rough trimap (sure background, sure foreground, unknown band) into a soft alpha matte, e.g. hair and fur edges; a plain Vision Transformer plus a small detail decoder; small and base versions. Apache-2.0 code and weights.
Commercial use: yes.
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
vitmatte-s | synthetic (1080x1920) | original portable kernels portable + torch.compile TensorRT fp16 | 140 102 84.1 81.9 54.6 | 2536 / 2582 1401 / 1524 1305 / 1404 1025 / 1284 876 / 920 |
vitmatte-b | synthetic (1080x1920) | original portable kernels portable + torch.compile TensorRT fp16 | 257 182 148 157 108 | 5070 / 5128 1548 / 1692 1452 / 1552 1166 / 1472 1233 / 1262 |
Details
- Original code: https://github.com/hustvl/ViTMatte at
8cd7ef068380 - Code licence: Apache-2.0 (permissive); files:
LICENSE - Weights licence: Apache-2.0
- Licence evidence: https://huggingface.co/hustvl/vitmatte-small-composition-1k/blob/6a58ad7646403c1df626fbd746900aec7361ea1d/README.md ; https://huggingface.co/hustvl/vitmatte-base-composition-1k/blob/bf486d01a7d9e3dbcc8400f7942835caf0eaf76e/README.md ; https://github.com/hustvl/ViTMatte/blob/8cd7ef068380977c3962c4cb733cb1fe7f2241a5/LICENSE
- Training data: Composition-1k (Adobe Deep Image Matting) (The HF cards: "ViTMatte model trained on Composition-1k". Adobe Deep Image Matting (Composition-1k) is released for non-commercial research on request; only the weights licence (Apache-2.0) counts for the commercial flag.)
- Kernel libraries:
dpt_fused,cudnn_hacc,igev,cutlass_conv - Variants:
vitmatte-s,vitmatte-b - Outputs: alpha
- Input size: any H x W >= 32, padded to a multiple of 32
- Tested up to (portable, fp16, RTX 5060 Ti 16 GB):
vitmatte-s3000x4000 (7.8 GiB peak allocated, 13.9 reserved);vitmatte-b3000x4000 (8.0 GiB peak allocated, 14.3 reserved) vitmatte-s1080x1920, first call: portable + torch.compile 31 s (cold Inductor cache), TensorRT fp16 51 s (ONNX + engine).vitmatte-b1080x1920, first call: portable + torch.compile 33 s (cold Inductor cache), TensorRT fp16 49 s (ONNX + engine).vitmatte-soriginal 1080x1920: the authors' mixed-precision option (float16); transformers 5.19.0 VitMatteForImageMatting, normalise / trimap / pad 32 / cropvitmatte-boriginal 1080x1920: the authors' mixed-precision option (float16); transformers 5.19.0 VitMatteForImageMatting, normalise / trimap / pad 32 / cropvitmatte-sload / first call: original 2.85 s / 0.722 s, portable 0.16 s / 0.413 s, kernels 1.3 s / 0.202 svitmatte-bload / first call: original 0.21 s / 0.562 s, portable 0.52 s / 0.35 s, kernels 1.32 s / 0.249 s- Protocol, first call, 3 warm-up, peak reset, median of 3 rounds x median of 5
Measured
Trimap-guided models, AIM-500: each image at its own size with a trimap made from its alpha (alpha 255 / 0 eroded by a 21 px ellipse = foreground / background, the rest unknown); SAD / 1000 and MSE of the 8-bit alpha in the unknown region (ViTMatte's evaluation). Original = the original code named under Details, same checkpoint and protocol.
| variant | dataset | original fp32 | original fp16 | portable | portable fp32 | kernels | note |
|---|---|---|---|---|---|---|---|
vitmatte-s | AIM-500, every 5th image (100), generated trimaps, SAD / MSE x 1000 (unknown region) | 13.86 / 11.727 | 13.85 / 11.727 | 13.85 / 11.726 | 13.86 / 11.727 | 13.85 / 11.723 | |
vitmatte-b | AIM-500, all 500 images, generated trimaps, SAD / MSE x 1000 (unknown region) | 13.72 / 12.180 | |||||
vitmatte-b | AIM-500, every 5th image (100), generated trimaps, SAD / MSE x 1000 (unknown region) | 16.39 / 13.937 | 16.40 / 13.936 | 16.40 / 13.936 | 16.39 / 13.937 | 16.39 / 13.938 |
Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 4068530d, 2026-11-12.
Parity
CPU, float32: max |diff| <= 5.8e-06 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 5.3e-05 and p99 <= 0.0012 (absolute error of the output alpha).
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
vitmatte-s | 3e-05 / 0.0007 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400, cutlass_conv@988138e042cb:sm_120, dpt_fused@48e0065bb7cf:sm_120, igev@f075827d68c6:sm_120 |
vitmatte-b | 3e-05 / 0.00064 | RTX 5060 Ti, torch 2.14.1+cu130 | cudnn_hacc@cudnn92400, cutlass_conv@988138e042cb:sm_120, dpt_fused@48e0065bb7cf:sm_120, igev@f075827d68c6:sm_120 |
Example output
No example rendered for this checkpoint yet.