Bilateral Reference for High-Resolution Dichotomous Image Segmentation
Paper • 2401.03407 • Published • 4
How to use nobg/FeyNobg with BiRefNet:
# Option 1: use with transformers
from transformers import AutoModelForImageSegmentation
birefnet = AutoModelForImageSegmentation.from_pretrained("nobg/FeyNobg", trust_remote_code=True)
# Option 2: use with BiRefNet
# Install from https://github.com/ZhengPeng7/BiRefNet
from models.birefnet import BiRefNet
model = BiRefNet.from_pretrained("nobg/FeyNobg")
This model has been pushed to the Hub using the PytorchModelHubMixin integration.
Library: nobg
pip install nobg
use the AutoModel class
from nobg import AutoModel
model = AutoModel.from_pretrained("nobg/FeyNobg")
or you can use the model class directly
from nobg import BiRefNet
model = BiRefNet.from_pretrained("nobg/FeyNobg")
Load the matching processor with AutoProcessor. It produces the normalized
pixel_values the model expects and post-processes the raw logits
(B, 1, 1024, 1024) into an alpha matte at the original resolution.
import torch
from loadimg import load_img
from nobg import AutoModel, AutoProcessor
model = AutoModel.from_pretrained("nobg/FeyNobg").eval()
processor = AutoProcessor.from_pretrained("nobg/FeyNobg")
image = load_img("input.jpg").convert("RGB")
inputs = processor(image, return_tensors="pt")
with torch.no_grad():
outputs = model(pixel_values=inputs["pixel_values"])
# Alpha matte in [0, 1] resized back to the original image size
alpha = processor.post_process_alpha_matting(
outputs, target_sizes=[(image.height, image.width)]
)[0]
# Composite the cutout onto a transparent background
processor.cutout(image, alpha).save("output.png")
If you use this model, please cite the original paper (https://arxiv.org/abs/2401.03407):
@article{zheng2024birefnet,
title={Bilateral Reference for High-Resolution Dichotomous Image Segmentation},
author={Zheng, Peng and Gao, Dehong and Fan, Deng-Ping and Liu, Li and
Laaksonen, Jorma and Ouyang, Wanli and Sebe, Nicu},
journal={CAAI Artificial Intelligence Research},
year={2024}
}
Any contributions are welcome at https://github.com/feyninc/nobg