u2net / README.md
Flor's picture
card: update from fastforks 1cbf1671
00ecba9 verified
|
Raw History Blame Contribute Delete
7.17 kB
---
library_name: "fastforks"
pipeline_tag: "image-segmentation"
license: "apache-2.0"
tags:
- "matting"
- "background-removal"
- "dichotomous-image-segmentation"
- "cuda"
- "pytorch"
- "fastforks"
- "fastforks-card"
---
# U²-Net (fastforks)
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).
This repo is the model card only: it holds no weights. fastforks downloads the authors' weights from the links below and checks them against the SHA-256 in the model card. Code: <https://github.com/kleinicke/fastforks>. Kernels for the `kernels` backend: <https://huggingface.co/fastforks/kernels>. Original code: <https://github.com/xuebinqin/U-2-Net> at `ac7e1c817eca`.
Task: matting. Tier: experimental. Outputs: `alpha`. Backends: `portable`, `kernels`.
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).
## Variants
Weights are the authors' files (not hosted here). SHA-256 shortened to 12 hex digits; the full hashes are in [`card.toml`](https://github.com/kleinicke/fastforks/blob/main/src/fastforks/matting/models/u2net/card.toml).
| variant | weights (author link) | SHA-256 | weights licence | commercial use |
|---|---|---|---|---|
| `u2net@1` | [u2net.pth](https://drive.usercontent.google.com/download?export=download&confirm=t&id=1ao1ovG1Qtx4b7EoskHXmi2E9rp5CHLcZ) | `10025a17f49c` | Apache-2.0 | yes |
| `u2net-human-seg@1` | [u2net_human_seg.pth](https://drive.usercontent.google.com/download?export=download&confirm=t&id=1m_Kgs91b21gayc2XLW0ou8yugAIadWVP) | `13f0396e3f6f` | Apache-2.0 | yes |
| `u2netp@1` | [u2netp.pth](https://drive.usercontent.google.com/download?export=download&confirm=t&id=1rbSTGKAE-MTxBYHd-51l2hMOQPT_7EPy) | `e7567cde013f` | Apache-2.0 | yes |
## Licence
Original-code strings, recorded as they are (research-use package; check them before any other use). Code: Apache-2.0. Licence files: `LICENSE`.
Training data: DUTS (salient object detection) (README / paper: trained on DUTS-TR (10553 images).).
Licence evidence: https://github.com/xuebinqin/U-2-Net/blob/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/LICENSE ; https://github.com/xuebinqin/U-2-Net/blob/ac7e1c817ecab7c7dff5ce6b1abba61cd213ff29/README.md
## 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); `u2net-human-seg` 3000x4000 (0.8 GiB peak allocated, 1.0 reserved); `u2netp` 3000x4000 (0.7 GiB peak allocated, 1.0 reserved)
## Speed and memory
### Matting, synthetic, 1080x1920
| model | time (ms): original | portable | kernels | kernels speed-up: vs original | vs portable | peak memory (MiB, allocated / reserved): original | portable | kernels |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| `u2net` | 15.9 | 14.9 | 8.83 | 1.8x | 1.7x | 476 / 502 | 248 / 278 | 237 / 260 |
| `u2net-human-seg` | 16.0 | 14.6 | 9.05 | 1.8x | 1.6x | 476 / 502 | 248 / 278 | 237 / 260 |
| `u2netp` | 14.8 | 14.6 | 9.80 | 1.5x | 1.5x | 280 / 314 | 150 / 172 | 138 / 172 |
### Startup and first call
| model | load (s): original | portable | kernels | first call (s): original | portable | kernels |
| --- | --- | --- | --- | --- | --- | --- |
| `u2net` | 0.64 | 0.49 | 1.37 | 0.40 | 0.21 | 0.11 |
| `u2net-human-seg` | 0.54 | 0.32 | 1.37 | 0.38 | 0.30 | 0.11 |
| `u2netp` | 0.18 | 0.11 | 1.28 | 0.47 | 0.26 | 0.11 |
### torch.compile and TensorRT
| model | input | time: portable | compile | TensorRT fp16 | kernels | preparation: compile (cold first call) | ONNX + engine build | peak MiB (alloc / reserved): portable | compile | kernels | TensorRT (engine + torch) |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| `u2net` | 1080x1920 | 14.9 ms | 6.24 ms | 3.07 ms | 8.83 ms | 35.7 s | 36.0 s | 248 / 278 | 235 / 278 | 237 / 260 | 335 / 390 |
| `u2net-human-seg` | 1080x1920 | 14.6 ms | 6.25 ms | 3.08 ms | 9.05 ms | 36.0 s | 34.7 s | 248 / 278 | 235 / 278 | 237 / 260 | 335 / 390 |
| `u2netp` | 1080x1920 | 14.6 ms | 5.44 ms | 1.87 ms | 9.80 ms | 29.9 s | 26.1 s | 150 / 172 | 152 / 188 | 138 / 172 | 206 / 236 |
Fixed-size cells: time (ms) / peak allocated / peak reserved memory (MiB).
Hardware, protocol and caveats: [benchmarks](https://github.com/kleinicke/fastforks/blob/main/docs/benchmarks.md) (RTX 5060 Ti 16 GB, sm_120). Original = the original code named above at the precision of portable (fp16, or `(fp32)` where fp16 is unsupported or not finite); original fp32 = its default fp32 run. Peak memory is the torch allocator's.
## Accuracy
### On public datasets
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 | portable | kernels | note |
|---|---|---|---|---|---|
| `u2net` | DIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta | 0.1484 / 0.5278 | 0.1485 / 0.5270 | 0.1485 / 0.5273 | |
| `u2netp` | DIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta | 0.1533 / 0.5374 | 0.1531 / 0.5391 | 0.1531 / 0.5405 | |
| `u2net-human-seg` | DIS5K validation (DIS-VD), every 2nd image (235), MAE / max F-beta | 0.1717 / 0.3264 | 0.1717 / 0.3265 | 0.1717 / 0.3266 | |
Measured on NVIDIA GeForce RTX 5060 Ti, torch 2.14.1+cu130, fastforks 45a5c91f, 2026-11-13.
### Parity with the original code
Every variant is checked against reference outputs of the original code at its pinned commit. 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`).
## Usage
```python
from fastforks import matting
model = matting.load("u2net") # mode="auto": the preferred backend from recorded measurements
result = model("photo.jpg")
result.alpha # [H, W] float32 in [0, 1], input resolution
result.backend # what ran, e.g. Backend(mode='portable', device='cuda:0', dtype='float16')
```
Install with `pip install fastforks` (see <https://github.com/kleinicke/fastforks>). Other variants: `u2net`, `u2net-human-seg`, `u2netp`.
## Kernels backend
`matting.load("u2net", mode="kernels")` runs our CUDA kernels (`cudnn_hacc`, `igev`, `cutlass_conv`). It needs a prebuilt kernel for your GPU architecture (downloaded from <https://huggingface.co/fastforks/kernels> on first use) and a recent NVIDIA driver; if there is none it raises `BackendUnavailableError` with the reason and never falls back silently. `mode="auto"` picks `kernels` when it can run and logs the choice.
<!-- staged from fastforks 1cbf1671 (2026-11-15) -->