Models · Matting

BiRefNet

BiRefNet (Nankai University and partners, CAAI AIR 2024): cuts the foreground out of a photo as a soft mask at full resolution (background removal), with a Swin Transformer encoder; general and lite versions (MIT code and weights) and BRIA's RMBG-2.0 weights (non-commercial, gated download).

experimental Fork of ZhengPeng7/BiRefNet@f707963 · backends: portable, kernels

Checkpoint

Paper 2024 Added in 0.2.0 Evaluated with 0.2.0

Kernels, RTX 5060 Ti 16 GB 121 ms 1080x1920, fp16
vs the faster original 1.9× original fp16: 234 ms
Peak memory 1,674 MiB original: 1,666 MiB
Accuracy vs original fp32 ±0.0000 kernels mae, DIS5K validation (DIS-VD) (0.0338 original)

Use it

pip install fastforks

Lean install with only what this model needs: pip install fastforks-core

from fastforks import matting

model = matting.load("birefnet")  # kernels on a supported GPU, else portable PyTorch
result = model(image)
result.alpha
result.backend  # what ran: mode, device, dtype, kernels
fastforks bench birefnet

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)

BackenddtypeTime per call (ms)vs original fp16Peak memory
allocated / reserved
Load (s)First call (s)
Original fp32fp32not measured ––––
Original fp16the authors' float16 model option; HF ZhengPeng7/BiRefNet@e2bf8e4460fc birefnet.py, the card's resize / normalisation / sigmoid / resize backfp16 234–1,666 MiB/1,930 MiB3.20.19
Portable fp32fp32not measured ––––
Portablefp16 1311.8×1,660 MiB/1,908 MiB0.50.56
Kernelsfp16 1211.9×1,674 MiB/1,916 MiB13.60.37
Portable + torch.compile 1381.7×1,874 MiB/1,932 MiB94 (build)–
TensorRT fp16fp16 87.82.7×1,230 MiBenginenot comparable201 (build)–

Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-12 to 2026-11-14. 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

DeviceBackenddtypems / callPeak memoryOutput vs referenceInput
Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14Originalfp322710 9,880 MiB process–1080x1920
Portablefp161152 5,433 MiB processmatches1080x1920
Portablefp321333 9,488 MiB processmatches1080x1920
Kernelsfp16not 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

Weights

birefnet.safetensors 444 MB· hosted on Hugging Face

fastforks downloads and checks these files on first use (fastforks download birefnet).

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.

DIS5K validation (DIS-VD)

all 470 images, MAE / max F-beta

Rundtypemae ↑max_fbeta ↑imagesms / image
Original fp32not measured
Original fp16not measured
Portable fp32not measured
Portablenot measured
Kernelsfp160.03250.9074 470 218

The original code was not run on this dataset, so the table cannot show the distance to it.

Measured on RTX 5060 Ti, torch 2.14.1+cu130, fastforks a49aaba7, 2026-11-12.

DIS5K validation (DIS-VD)

every 2nd image (235), MAE / max F-beta

Rundtypemae ↑max_fbeta ↑Δ mae vs fp32 originalimagesms / image
Original fp32fp320.03380.9037 reference 235 383
Original fp16fp160.03380.9037 ±0.0000 235 151
Portable fp32fp320.03380.9037 ±0.0000 235 410
Portablefp160.03380.9037 ±0.0000 235 229
Kernelsfp160.03380.9037 ±0.0000 235 219

Each image at its own size against its binary mask: MAE of the alpha, max F-beta (beta^2 = 0.3) over 256 thresholds (BiRefNet'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 a49aaba7, 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| <= 7.8e-05 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 6e-06 and p99 <= 2.1e-05 (absolute error of the output alpha).

CheckToleranceMeasured
CPU fp32, max |diff|≤ 0.000078 not recorded
GPU default dtypes, mean |diff|≤ 0.000006 0
GPU default dtypes, p99 |diff|≤ 0.000021 0

Recorded for birefnet on RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-12, with these kernels: deform_conv

Details

BiRefNet (Zheng et al., CAAI AIR 2024): bilateral reference network for high-resolution dichotomous image segmentation and background removal. A Swin Transformer v1 encoder at full and half resolution feeds a decoder with deformable ASPP blocks, image patches as reference and gradient attention; one foreground mask per image. `rmbg-2.0`: BRIA's RMBG-2.0 weights for this network (non-commercial, gated).

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 (1,788 MiB allocated)
Outputs
alpha
Checkpoint
backbone="swin_v1_l"
CUDA kernels
  • deform_convdeformable convolution fused into one kernel
Apple (MPS)
fp32, fp16
Full reference, generated from the repository

BiRefNet (Nankai University and partners, CAAI AIR 2024): cuts the foreground out of a photo as a soft mask at full resolution (background removal), with a Swin Transformer encoder; general and lite versions (MIT code and weights) and BRIA's RMBG-2.0 weights (non-commercial, gated download).

Commercial use: birefnet, birefnet-lite: yes; rmbg-2.0: no (weights: CC-BY-NC-4.0).

variantinputbackendtime (ms)peak memory (MiB, allocated / reserved)
birefnetsynthetic (1080x1920)original
portable
kernels
portable + torch.compile
TensorRT fp16
234
131
121
138
87.8
1666 / 1930
1660 / 1908
1674 / 1916
1874 / 1932
1753 / 1814
birefnet-litesynthetic (1080x1920)original
portable
kernels
portable + torch.compile
TensorRT fp16
75.1
58.0
48.1
50.2
49.0
913 / 974
810 / 952
754 / 960
1469 / 1552
874 / 934
rmbg-2.0synthetic (1080x1920)original
portable
kernels
portable + torch.compile
TensorRT fp16
149
132
121
110
88.1
1672 / 1990
1660 / 1908
1674 / 1936
1874 / 1932
1723 / 1784
Details
  • Original code: https://github.com/ZhengPeng7/BiRefNet at f7079632d681
  • Code licence: MIT (permissive); files: LICENSE
  • Weights licence: MIT
    • rmbg-2.0: CC-BY-NC-4.0
  • Licence evidence: https://huggingface.co/ZhengPeng7/BiRefNet/blob/e2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4/README.md ; https://huggingface.co/ZhengPeng7/BiRefNet_lite/blob/aa62cd87eafb9cc43056d08ef3615a14628b831d/README.md ; https://github.com/ZhengPeng7/BiRefNet/blob/f7079632d681ecf12161fcea8905682934e59e51/LICENSE
  • Training data: DIS5K (dichotomous image segmentation), DUTS (salient object detection), HRSOD (high-resolution salient object detection), UHRSD (ultra-high-resolution salient object detection), HRS10K (high-resolution salient object detection), P3M-10k (privacy-preserving portrait matting) (General-use checkpoints: DIS5K-TR, DIS-TEs, DUTS-TR/TE, HRSOD-TR/TE, UHRSD-TR/TE, HRS10K-TR/TE, TR-P3M-10k, TE-P3M-500-NP, TE-P3M-500-P, TR-humans (HF model card). Some of these sets carry research-only terms; only the weights licence (MIT) counts for the commercial flag.)
    • rmbg-2.0: BRIA proprietary background-removal data (BRIA's own data: over 15,000 manually labelled images it says are fully licensed (HF model card).)
  • Kernel libraries: deform_conv
  • Variants: birefnet, birefnet-lite, rmbg-2.0
  • 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): birefnet 3000x4000 (1.7 GiB peak allocated, 2.8 reserved); birefnet-lite 3000x4000 (0.9 GiB peak allocated, 1.8 reserved); rmbg-2.0 3000x4000 (1.7 GiB peak allocated, 2.6 reserved)
  • birefnet 1080x1920, first call: portable + torch.compile 94 s (cold Inductor cache), TensorRT fp16 201 s (ONNX + engine).
  • birefnet-lite 1080x1920, first call: portable + torch.compile 75 s (cold Inductor cache), TensorRT fp16 156 s (ONNX + engine).
  • rmbg-2.0 1080x1920, first call: portable + torch.compile 83 s (cold Inductor cache), TensorRT fp16 219 s (ONNX + engine).
  • birefnet original 1080x1920: the authors' float16 model option; HF ZhengPeng7/BiRefNet@e2bf8e4460fc birefnet.py, the card's resize / normalisation / sigmoid / resize back
  • birefnet-lite original 1080x1920: the authors' float16 model option; HF ZhengPeng7/BiRefNet_lite@aa62cd87eafb birefnet.py, the card's resize / normalisation / sigmoid / resize back
  • rmbg-2.0 original 1080x1920: the authors' float16 model option; HF briaai/RMBG-2.0@2fd7b4b0f239 birefnet.py, the card's resize / normalisation / sigmoid / resize back
  • birefnet load / first call: original 3.6 s / 0.387 s, portable 0.46 s / 0.556 s, kernels 13.58 s / 0.368 s
  • birefnet-lite load / first call: original 8.04 s / 1.104 s, portable 0.67 s / 0.42 s, kernels 0.2 s / 0.471 s
  • rmbg-2.0 load / first call: original 6.42 s / 0.942 s, portable 0.94 s / 0.494 s, kernels 0.64 s / 0.471 s
  • Protocol, first call, 3 warm-up, peak reset, median of 3 rounds x median of 5

Measured

Each image at its own size against its binary mask: MAE of the alpha, max F-beta (beta^2 = 0.3) over 256 thresholds (BiRefNet's evaluation). Original = the original code named under Details, same checkpoint and protocol.

variantdatasetoriginal fp32original fp16portableportable fp32kernelsnote
birefnetDIS5K validation (DIS-VD), all 470 images, MAE / max F-beta0.0325 / 0.9074
birefnetDIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta0.0338 / 0.90370.0338 / 0.90370.0338 / 0.90370.0338 / 0.90370.0338 / 0.9037
birefnet-liteDIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta0.0446 / 0.86200.0446 / 0.86190.0446 / 0.86200.0446 / 0.86200.0446 / 0.8620
rmbg-2.0DIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta0.0205 / 0.93240.0206 / 0.93240.0205 / 0.93260.0205 / 0.93270.0205 / 0.9326

Measured on portable: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 45b1fe90 / a49aaba7, 2026-11-12 to 2026-11-14; portable32: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks a49aaba7 / dc38c37c, 2026-11-12 to 2026-11-17; kernels: NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 45b1fe90 / a49aaba7, 2026-11-12 to 2026-11-14.

Parity

CPU, float32: max |diff| <= 7.8e-05 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 6e-06 and p99 <= 2.1e-05 (absolute error of the output alpha).

variantkernels error vs original, mean / p99GPU, torchkernels used
birefnet0.0 / 0.0RTX 5060 Ti, torch 2.14.1+cu130deform_conv@a38662ea1bfc:sm_120
birefnet-lite0.0 / 1e-05RTX 5060 Ti, torch 2.14.1+cu130deform_conv@a38662ea1bfc:sm_120
rmbg-2.09e-05 / 0.00038RTX 5060 Ti, torch 2.14.1+cu130deform_conv@a38662ea1bfc:sm_120

Example output

No example rendered for this checkpoint yet.