Models · Matting

U²-Net

U²-Net (University of Alberta, Pattern Recognition 2020): finds the main object of a photo and cuts it out as a soft mask at 320 x 320 (rembg's default background remover); a full model, a 4.7 MB small one and one trained on people. Apache-2.0 code and weights.

experimental Fork of xuebinqin/U-2-Net@ac7e1c8 · backends: portable, kernels

Checkpoint

Paper 2020 Added in 0.2.0 Evaluated with 0.2.0

Kernels, RTX 5060 Ti 16 GB 5.18 ms 1080x1920, fp16
vs the faster original 3.1× original fp32: 15.9 ms
Peak memory 161 MiB original: 476 MiB
Accuracy vs original fp32 −0.0001 kernels mae, DIS5K validation (DIS-VD) (0.1484 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("u2net")  # fastest backend that runs here
result = model("photo.jpg")

# result fields: alpha
alpha = result.alpha  # a torch tensor
arrays = result.numpy()  # the tensor fields as NumPy arrays: arrays["alpha"]
result.backend  # what ran: mode, device, dtype
Other ways to run it
  • Backend: matting.load("u2net", mode="kernels") # or "portable", "auto" (default)
  • Precision: matting.load("u2net", dtype="float32") # full precision: slower, portable only
  • Options: matting.load("u2net", options={"cuda_graphs": True})
fastforks bench u2net

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 originalPeak memory
allocated / reserved
Load (s)First call (s)
Original fp32xuebinqin/U-2-Net@ac7e1c817eca, float pre- and post-processingfp32 15.9–476 MiB/502 MiB0.60.40
Original fp16fp16not measured ––––
Portable fp32fp32not measured ––––
Portablefp16 9.771.6×240 MiB/278 MiB–0.16
Kernelsfp16 5.183.1×161 MiB/236 MiB–0.17
Portable + torch.compile 9.091.8×221 MiB/258 MiB––
TensorRT fp16fp16 3.075.2×160 MiBenginenot comparable36 (build)–

Measured on RTX 5060 Ti 16 GB, torch 2.14.1+cu130, 2026-11-13 to 2026-11-18. 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 memoryLoad / first call (s)Output vs referenceInput
NVIDIA runs on Linux and Windows
NVIDIA L4 CUDA, torch 2.14.1+cu130, 2026-11-29Portablefp1613.6 235 MiB/258 MiB1.6 / 0.1matches1080x1920
Kernelsfp165.70 154 MiB/238 MiB1.5 / 0.1matches1080x1920
NVIDIA L40S CUDA, torch 2.14.1+cu130, 2026-11-30Portablefp1610.1 235 MiB/258 MiB4.0 / 0.1matches1080x1920
Kernelsfp162.99 154 MiB/218 MiB1.1 / 0.1matches1080x1920
NVIDIA A10 CUDA, torch 2.14.1+cu130, 2026-11-30Portablefp167.97 235 MiB/258 MiB1.1 / 0.1matches1080x1920
Kernelsfp165.74 154 MiB/218 MiB1.2 / 0.1matches1080x1920
Apple silicon runs on macOS
Apple M1 Pro 16 GB MPS, torch 2.14.1, 2026-11-14Originalfp3280.7 1,910 MiB process0.7 / 0.5–1080x1920
Portablefp1679.4 1,885 MiB process0.6 / 0.5matches1080x1920
Portablefp3281.6 1,910 MiB process0.6 / 0.5matches1080x1920
Kernelsfp16not implemented––1080x1920
Apple M1 Pro 16 GB (MLX, fp16) MLX, torch 2.14.1, 2026-11-27Portablefp1657.8 723 MiB process0.3 / 0.4matches1080x1920
Portablefp3269.6 966 MiB process0.4 / 0.1matches1080x1920
Kernelsfp32not 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

Our reading of the licences, not legal advice.

Weights

u2net.pth 176 MB· hosted on Google Drive

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

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)

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

Rundtypemae ↓max_fbeta ↑Δ mae vs fp32 originalimagesms / image
Original fp32fp320.14840.5278 reference 235 34.9
Original fp16not measured
Portable fp32fp320.14840.5278 ±0.0000 235 20.8
Portablefp160.14850.5270 −0.0001 235 32.1
Kernelsfp160.14850.5273 −0.0001 235 23.9

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: RTX 5060 Ti, torch 2.14.1+cu130, 2026-11-13; portable fp32, portable, kernels: RTX 5060 Ti, torch 2.14.1+cu130, fastforks 45a5c91f, 2026-11-13.

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| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.25 (absolute error of the output alpha).

CheckToleranceMeasured
CPU fp32, max |diff|≤ 0.002 not recorded
GPU default dtypes, mean |diff|≤ 0.02 0.00023
GPU default dtypes, p99 |diff|≤ 0.25 0.00721

Recorded for u2net on RTX 5060 Ti, torch 2.10.0+cu130, 2026-11-18, with these kernels: cudnn_hacccutlass_convigevresize_cat

Details

U²-Net (Qin et al., Pattern Recognition 2020): salient object detection with a two-level nested U-structure: six residual U-blocks (RSU) in the encoder, five in the decoder, a fused sigmoid map at 320 x 320. rembg's default background remover. `u2net` / `u2netp` (1.1 M parameters) are the DUTS-TR salient object models, `u2net-human-seg` the human segmentation model (Supervisely Person Dataset).

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 (788 MiB allocated)
Options
  • cuda_graphs=true
Outputs
alpha
CUDA kernels
  • resize_cat
  • cudnn_haccconvolutions with fp16 accumulation (cuDNN)
  • igevIGEV stereo: convolution epilogues, correlation lookups, GRU gates
  • cutlass_convCUTLASS convolutions with the recurrent update (GRU) fused in
Apple (MPS)
fp32, fp16
Full reference, generated from the repository

U²-Net (University of Alberta, Pattern Recognition 2020): finds the main object of a photo and cuts it out as a soft mask at 320 x 320 (rembg's default background remover); a full model, a 4.7 MB small one and one trained on people. Apache-2.0 code and weights.

Commercial use: yes.

variantinputbackendtime (ms)peak memory (MiB, allocated / reserved)
u2netsynthetic (1080x1920)original
portable
kernels
portable + torch.compile
TensorRT fp16
15.9
9.77
5.18
9.09
3.07
476 / 502
240 / 278
161 / 236
221 / 258
335 / 390
u2netpsynthetic (1080x1920)original
portable
kernels
portable + torch.compile
TensorRT fp16
14.8
15.1
2.92
5.73
1.87
280 / 314
139 / 180
72 / 124
129 / 144
206 / 236
u2net-human-segsynthetic (1080x1920)original
portable
kernels
portable + torch.compile
TensorRT fp16
16.0
10.0
5.14
6.18
3.08
476 / 502
240 / 278
161 / 236
193 / 234
335 / 390
Details
  • Original code: https://github.com/xuebinqin/U-2-Net at ac7e1c817eca
  • Code licence: Apache-2.0 (permissive); files: LICENSE
  • Weights licence: Apache-2.0
  • Licence evidence: https://github.com/xuebinqin/U-2-Net/blob/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/LICENSE ; https://github.com/xuebinqin/U-2-Net/blob/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/README.md
  • Training data: DUTS (salient object detection) (README / paper: trained on DUTS-TR (10553 images).)
    • u2net-human-seg: Supervisely Person Dataset (human segmentation) (README (2021-Feb-06): the human segmentation model, trained on the Supervisely Person Dataset.)
  • Options: cuda_graphs = True (speed only)
  • Kernel libraries: resize_cat, cudnn_hacc, igev, cutlass_conv
  • Variants: u2net, u2netp, u2net-human-seg
  • 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): u2net 3000x4000 (0.8 GiB peak allocated, 1.0 reserved); u2netp 3000x4000 (0.7 GiB peak allocated, 1.0 reserved); u2net-human-seg 3000x4000 (0.8 GiB peak allocated, 1.0 reserved)
  • u2net 1080x1920, first call: TensorRT fp16 36 s (ONNX + engine).
  • u2netp 1080x1920, first call: TensorRT fp16 26 s (ONNX + engine).
  • u2net-human-seg 1080x1920, first call: TensorRT fp16 35 s (ONNX + engine).
  • u2net original 1080x1920: xuebinqin/U-2-Net@ac7e1c817eca, float pre- and post-processing
  • u2netp original 1080x1920: xuebinqin/U-2-Net@ac7e1c817eca, float pre- and post-processing
  • u2net-human-seg original 1080x1920: xuebinqin/U-2-Net@ac7e1c817eca, float pre- and post-processing
  • u2net load / first call: original 0.64 s / 0.396 s, portable 0.49 s / 0.21 s, kernels 1.37 s / 0.109 s
  • u2netp load / first call: original 0.18 s / 0.474 s, portable 0.11 s / 0.261 s, kernels 1.28 s / 0.113 s
  • u2net-human-seg load / first call: original 0.54 s / 0.383 s, portable 0.32 s / 0.297 s, kernels 1.37 s / 0.113 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 fp32portableportable fp32kernelsnote
u2netDIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta0.1484 / 0.52780.1485 / 0.52700.1484 / 0.52780.1485 / 0.5273
u2netpDIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta0.1533 / 0.53740.1531 / 0.53910.1533 / 0.53740.1531 / 0.5405
u2net-human-segDIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta0.1717 / 0.32640.1717 / 0.32650.1717 / 0.32640.1717 / 0.3266

Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 45a5c91f, 2026-11-13.

Parity

CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.25 (absolute error of the output alpha).

variantkernels error vs original, mean / p99GPU, torchkernels used
u2net0.00023 / 0.00721RTX 5060 Ti, torch 2.10.0+cu130cudnn_hacc@cudnn91501, cutlass_conv@988138e042cb:sm_120, igev@f075827d68c6:sm_120, resize_cat@60f9273249a3:sm_120
u2netp0.00061 / 0.01642RTX 5060 Ti, torch 2.10.0+cu130cudnn_hacc@cudnn91501, cutlass_conv@988138e042cb:sm_120, igev@f075827d68c6:sm_120, resize_cat@60f9273249a3:sm_120
u2net-human-seg0.00013 / 0.00207RTX 5060 Ti, torch 2.10.0+cu130cudnn_hacc@cudnn91501, cutlass_conv@988138e042cb:sm_120, igev@f075827d68c6:sm_120, resize_cat@60f9273249a3:sm_120

Example output

No example rendered for this checkpoint yet.