Apache-2.0 · Python ≥ 3.10 · torch ≥ 2.0

Background removal that stops at the hair.

nobg is an open-source library for background removal and image matting. Matting models, image processors, losses and metrics behind one small API — with the HuggingFace Hub on both ends.

uv add nobg
from nobg import AutoModelmodel = AutoModel.from_pretrained("feyninc/FeyNobg")model.process("input.jpg").save("output.png")
Input photo with its original background
input.jpg
The same photo with its background removed
output.png

What you get

One call, no boilerplate

process() loads, preprocesses, runs under no_grad, post-processes and composites. Paths, URLs, base64, numpy arrays and PIL images all work as input.

Two models, one contract

FeyNobg (BiRefNet) for hair-level edges, MultiMatte (SAM3) for text-promptable cutouts. Both return (B, 1, H, W) matte logits, so they are interchangeable.

Hub-native

Built on PyTorchModelHubMixin: from_pretrained, push_to_hub and generated model cards, plus an AutoModel / AutoProcessor pair that dispatch on repo tags.

Trainable

The processor emits labels, the model returns loss, and the criterion is a swappable function attribute — so the HuggingFace Trainer works out of the box.

Halo-free compositing

refine_foreground solves for unmixed foreground colors in pure torch, on whatever device the tensors already live on.

ONNX export

onnx_save_pretrained / onnx_from_pretrained mirror the Hub methods, and the returned graph keeps the same process / predict API.

Start here