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
- Paper Bilateral Reference for High-Resolution Dichotomous Image Segmentation CAAI AIR 2024 · first released 7 Jan 2024
Paper 2024 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("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)
| 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' float16 model option; HF ZhengPeng7/BiRefNet@e2bf8e4460fc birefnet.py, the card's resize / normalisation / sigmoid / resize back | fp16 | 234 | – | 1,666 MiB/1,930 MiB | 3.2 | 0.19 |
| Portable fp32 | fp32 | not measured | – | – | – | – |
| Portable | fp16 | 131 | 1.8× | 1,660 MiB/1,908 MiB | 0.5 | 0.56 |
| Kernels | fp16 | 121 | 1.9× | 1,674 MiB/1,916 MiB | 13.6 | 0.37 |
| Portable + torch.compile | 138 | 1.7× | 1,874 MiB/1,932 MiB | 94 (build) | – | |
| TensorRT fp16 | fp16 | 87.8 | 2.7× | 1,230 MiBenginenot comparable | 201 (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
| 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 | 2710 | 9,880 MiB process | – | 1080x1920 |
| Portable | fp16 | 1152 | 5,433 MiB process | matches | 1080x1920 | |
| Portable | fp32 | 1333 | 9,488 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
- MIT LICENSE
- Weights
- MIT source
- Training data
-
- DIS5K (dichotomous image segmentation) noncommercial, non-commercial
- DUTS (salient object detection) not stated
- HRSOD (high-resolution salient object detection) not stated
- UHRSD (ultra-high-resolution salient object detection) not stated
- HRS10K (high-resolution salient object detection) not stated
- P3M-10k (privacy-preserving portrait matting) MIT
Our reading of the licences, not legal advice.
Weights
birefnet.safetensors 444 MB· hosted on Hugging Facefastforks 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
| Run | dtype | mae ↑ | max_fbeta ↑ | images | ms / image |
|---|---|---|---|---|---|
| Original fp32 | not measured | ||||
| Original fp16 | not measured | ||||
| Portable fp32 | not measured | ||||
| Portable | not measured | ||||
| Kernels | fp16 | 0.0325 | 0.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
| Run | dtype | mae ↑ | max_fbeta ↑ | Δ mae vs fp32 original | images | ms / image |
|---|---|---|---|---|---|---|
| Original fp32 | fp32 | 0.0338 | 0.9037 | reference | 235 | 383 |
| Original fp16 | fp16 | 0.0338 | 0.9037 | ±0.0000 | 235 | 151 |
| Portable fp32 | fp32 | 0.0338 | 0.9037 | ±0.0000 | 235 | 410 |
| Portable | fp16 | 0.0338 | 0.9037 | ±0.0000 | 235 | 229 |
| Kernels | fp16 | 0.0338 | 0.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).
| Check | Tolerance | Measured |
|---|---|---|
| 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).
| variant | input | backend | time (ms) | peak memory (MiB, allocated / reserved) |
|---|---|---|---|---|
birefnet | synthetic (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-lite | synthetic (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.0 | synthetic (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):
birefnet3000x4000 (1.7 GiB peak allocated, 2.8 reserved);birefnet-lite3000x4000 (0.9 GiB peak allocated, 1.8 reserved);rmbg-2.03000x4000 (1.7 GiB peak allocated, 2.6 reserved) birefnet1080x1920, first call: portable + torch.compile 94 s (cold Inductor cache), TensorRT fp16 201 s (ONNX + engine).birefnet-lite1080x1920, first call: portable + torch.compile 75 s (cold Inductor cache), TensorRT fp16 156 s (ONNX + engine).rmbg-2.01080x1920, first call: portable + torch.compile 83 s (cold Inductor cache), TensorRT fp16 219 s (ONNX + engine).birefnetoriginal 1080x1920: the authors' float16 model option; HF ZhengPeng7/BiRefNet@e2bf8e4460fc birefnet.py, the card's resize / normalisation / sigmoid / resize backbirefnet-liteoriginal 1080x1920: the authors' float16 model option; HF ZhengPeng7/BiRefNet_lite@aa62cd87eafb birefnet.py, the card's resize / normalisation / sigmoid / resize backrmbg-2.0original 1080x1920: the authors' float16 model option; HF briaai/RMBG-2.0@2fd7b4b0f239 birefnet.py, the card's resize / normalisation / sigmoid / resize backbirefnetload / first call: original 3.6 s / 0.387 s, portable 0.46 s / 0.556 s, kernels 13.58 s / 0.368 sbirefnet-liteload / first call: original 8.04 s / 1.104 s, portable 0.67 s / 0.42 s, kernels 0.2 s / 0.471 srmbg-2.0load / 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.
| variant | dataset | original fp32 | original fp16 | portable | portable fp32 | kernels | note |
|---|---|---|---|---|---|---|---|
birefnet | DIS5K validation (DIS-VD), all 470 images, MAE / max F-beta | 0.0325 / 0.9074 | |||||
birefnet | DIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta | 0.0338 / 0.9037 | 0.0338 / 0.9037 | 0.0338 / 0.9037 | 0.0338 / 0.9037 | 0.0338 / 0.9037 | |
birefnet-lite | DIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta | 0.0446 / 0.8620 | 0.0446 / 0.8619 | 0.0446 / 0.8620 | 0.0446 / 0.8620 | 0.0446 / 0.8620 | |
rmbg-2.0 | DIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta | 0.0205 / 0.9324 | 0.0206 / 0.9324 | 0.0205 / 0.9326 | 0.0205 / 0.9327 | 0.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).
| variant | kernels error vs original, mean / p99 | GPU, torch | kernels used |
|---|---|---|---|
birefnet | 0.0 / 0.0 | RTX 5060 Ti, torch 2.14.1+cu130 | deform_conv@a38662ea1bfc:sm_120 |
birefnet-lite | 0.0 / 1e-05 | RTX 5060 Ti, torch 2.14.1+cu130 | deform_conv@a38662ea1bfc:sm_120 |
rmbg-2.0 | 9e-05 / 0.00038 | RTX 5060 Ti, torch 2.14.1+cu130 | deform_conv@a38662ea1bfc:sm_120 |
Example output
No example rendered for this checkpoint yet.