Views
No views yet
image_cutout() for background removal, or with apply_colormap() to visualize saliency.| File | Params | Size | Use |
|---|---|---|---|
u2netp.onnx | 4.7M | ~4.7 MB | Recommended default. Distilled lite variant — CPU/mobile/edge-friendly |
u2net.onnx | 176M | ~170 MB | Full network — sharper edges on hair, fur, lace, thin structures |
| Spec | |
|---|---|
| Input name | input.1 (verify in Netron) |
| Input shape | [1, 3, 320, 320] (NCHW) |
| Input dtype | float32 |
| Input color order | RGB |
| Preprocessing | Resize to 320×320, scale to [0,1], normalize with ImageNet stats: mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] |
| Outputs | 7 tensors: d0..d6, saliency maps at decreasing resolution. d0 is the final fused mask — the other six are intermediate supervisions used during training; ignore them at inference. |
| Output shape (per map) | [1, 1, 320, 320] |
| Output meaning | Per-pixel saliency in [0, 1] — higher = more likely to be the subject. Threshold (typically ~0.5) for a binary mask, or use raw values as a soft alpha. |
1import onnxruntime as ort
2import numpy as np
3from PIL import Image
4
5sess = ort.InferenceSession("u2netp.onnx") # or "u2net.onnx" — same signature
6
7# Remember the original size so we can resize the mask back at the end
8orig = Image.open("photo.jpg").convert("RGB")
9W, H = orig.size
10
11# Preprocess
12img = orig.resize((320, 320), Image.BILINEAR)
13arr = np.asarray(img, dtype=np.float32) / 255.0
14arr = (arr - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225]
15arr = arr.transpose(2, 0, 1)[None, ...].astype(np.float32)
16
17# Inference — outputs is a list of 7 tensors; d0 is index 0
18outputs = sess.run(None, {sess.get_inputs()[0].name: arr})
19d0 = outputs[0][0, 0] # 320x320 saliency
20
21# Normalize (U²-Net outputs aren't strictly in [0,1] before squashing)
22d0 = (d0 - d0.min()) / (d0.max() - d0.min() + 1e-8)
23
24# Resize mask back to original image dimensions
25mask = Image.fromarray((d0 * 255).astype(np.uint8)).resize((W, H), Image.BILINEAR)mask as the alpha channel to the original RGB image (RGBA cutout).u2netp is the right default. 4.7 MB on disk, ~30 ms / image on CPU, mask quality good enough for >90% of background-removal and saliency-mapping use cases. Loads instantly.u2net earns its disk + latency cost on fine-edge subjects: hair, fur, lace, complex foliage, transparent objects. If the lite variant's edges look "blocky" on your inputs, the full model is the upgrade.u2net_portrait (line-drawing portrait sketches). It's deliberately not bundled here — it was trained on the APDrawing dataset, which carries non-commercial restrictions that would taint the otherwise-clean Apache-2.0 status of this bundle. If you need it, grab it directly from the upstream repo and read the dataset terms first.LICENSE file included. The danielgatis/rembg release just bundles the original weights; no relicensing occurred.