AI SeedbankHelp preserve open and free AI for humanity's future

← All models

ZhengPeng7_BiRefNet

ZhengPeng7 · View on Hugging Face ↗

Get this model

Download TorrentMagnet Link

Seeders: · Leechers:

Model card

The complete upstream card, rendered from this payload's README.md — the same hash-verified bytes the torrent distributes. Images and off-site links are removed; the original card on Hugging Face carries them.


library_name: birefnet tags:

  • background-removal
  • mask-generation
  • Dichotomous Image Segmentation
  • Camouflaged Object Detection
  • Salient Object Detection
  • pytorch_model_hub_mixin
  • model_hub_mixin
  • transformers repo_url: https://github.com/ZhengPeng7/BiRefNet pipeline_tag: image-segmentation license: mit

Bilateral Reference for High-Resolution Dichotomous Image Segmentation

Peng Zheng 1,4,5,6,  Dehong Gao 2,  Deng-Ping Fan 1*,  Li Liu 3,  Jorma Laaksonen 4,  Wanli Ouyang 5,  Nicu Sebe 6 1 Nankai University  2 Northwestern Polytechnical University  3 National University of Defense Technology  4 Aalto University  5 Shanghai AI Laboratory  6 University of Trento 
DIS-Sample_1 DIS-Sample_2

This repo is the official implementation of "Bilateral Reference for High-Resolution Dichotomous Image Segmentation" (CAAI AIR 2024).

Visit our GitHub repo: https://github.com/ZhengPeng7/BiRefNet for more details -- codes, docs, and model zoo!

How to use

0. Install Packages:

pip install -qr https://raw.githubusercontent.com/ZhengPeng7/BiRefNet/main/requirements.txt

1. Load BiRefNet:

Use codes + weights from HuggingFace

Only use the weights on HuggingFace -- Pro: No need to download BiRefNet codes manually; Con: Codes on HuggingFace might not be latest version (I'll try to keep them always latest).

# Load BiRefNet with weights
from transformers import AutoModelForImageSegmentation
birefnet = AutoModelForImageSegmentation.from_pretrained('ZhengPeng7/BiRefNet', trust_remote_code=True)

Use codes from GitHub + weights from HuggingFace

Only use the weights on HuggingFace -- Pro: codes are always latest; Con: Need to clone the BiRefNet repo from my GitHub.

# Download codes
git clone https://github.com/ZhengPeng7/BiRefNet.git
cd BiRefNet
# Use codes locally
from models.birefnet import BiRefNet

# Load weights from Hugging Face Models
birefnet = BiRefNet.from_pretrained('ZhengPeng7/BiRefNet')

Use codes from GitHub + weights from local space

Only use the weights and codes both locally.

# Use codes and weights locally
import torch
from utils import check_state_dict

birefnet = BiRefNet(bb_pretrained=False)
state_dict = torch.load(PATH_TO_WEIGHT, map_location='cpu')
state_dict = check_state_dict(state_dict)
birefnet.load_state_dict(state_dict)

Use the loaded BiRefNet for inference

# Imports
from PIL import Image
import matplotlib.pyplot as plt
import torch
from torchvision import transforms
from models.birefnet import BiRefNet

birefnet = ... # -- BiRefNet should be loaded with codes above, either way.
torch.set_float32_matmul_precision(['high', 'highest'][0])
birefnet.to('cuda')
birefnet.eval()
birefnet.half()

def extract_object(birefnet, imagepath):
    # Data settings
    image_size = (1024, 1024)
    transform_image = transforms.Compose([
        transforms.Resize(image_size),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])

    image = Image.open(imagepath)
    input_images = transform_image(image).unsqueeze(0).to('cuda').half()

    # Prediction
    with torch.no_grad():
        preds = birefnet(input_images)[-1].sigmoid().cpu()
    pred = preds[0].squeeze()
    pred_pil = transforms.ToPILImage()(pred)
    mask = pred_pil.resize(image.size)
    image.putalpha(mask)
    return image, mask

# Visualization
plt.axis("off")
plt.imshow(extract_object(birefnet, imagepath='PATH-TO-YOUR_IMAGE.jpg')[0])
plt.show()

2. Use inference endpoint locally:

You may need to click the deploy and set up the endpoint by yourself, which would make some costs.

import requests
import base64
from io import BytesIO
from PIL import Image


YOUR_HF_TOKEN = 'xxx'
API_URL = "xxx"
headers = {
	"Authorization": "Bearer {}".format(YOUR_HF_TOKEN)
}

def base64_to_bytes(base64_string):
    # Remove the data URI prefix if present
    if "data:image" in base64_string:
        base64_string = base64_string.split(",")[1]

    # Decode the Base64 string into bytes
    image_bytes = base64.b64decode(base64_string)
    return image_bytes

def bytes_to_base64(image_bytes):
    # Create a BytesIO object to handle the image data
    image_stream = BytesIO(image_bytes)

    # Open the image using Pillow (PIL)
    image = Image.open(image_stream)
    return image

def query(payload):
	response = requests.post(API_URL, headers=headers, json=payload)
	return response.json()

output = query({
	"inputs": "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg",
	"parameters": {}
})

output_image = bytes_to_base64(base64_to_bytes(output))
output_image

This BiRefNet for standard dichotomous image segmentation (DIS) is trained on DIS-TR and validated on DIS-TEs and DIS-VD.

This repo holds the official model weights of "Bilateral Reference for High-Resolution Dichotomous Image Segmentation" (CAAI AIR 2024).

This repo contains the weights of BiRefNet proposed in our paper, which has achieved the SOTA performance on three tasks (DIS, HRSOD, and COD).

Go to my GitHub page for BiRefNet codes and the latest updates: https://github.com/ZhengPeng7/BiRefNet :)

Try our online demos for inference:

  • Online Image Inference on Colab:
  • Online Inference with GUI on Hugging Face with adjustable resolutions:
  • Inference and evaluation of your given weights:

Acknowledgement:

  • Many thanks to @Freepik for their generous support on GPU resources for training higher resolution BiRefNet models and more of my explorations.
  • Many thanks to @fal for their generous support on GPU resources for training better general BiRefNet models.
  • Many thanks to @not-lain for his help on the better deployment of our BiRefNet model on HuggingFace.

Citation

@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},
  volume = {3},
  pages = {9150038},
  year={2024}
}

Magnet link

Opens the swarm directly in your torrent client — no file download needed. Copy-paste works too:

magnet:?xt=urn:btih:cca54e5191f28d2ab7f3227c06511b4acc3308fb&dn=ZhengPeng7_BiRefNet

Open magnet in torrent client · infohash cca54e5191f28d2ab7f3227c06511b4acc3308fb

Files & hashes

PathSizesha1sha256
BiRefNet_config.py298 B (298 B)37c8ac58bec2f52dac34204978a7b61b69e3da76e7b8c2a74f6cea6a59553d517f71d47f2c1d90e670a13416af17c25fe2f3dc52
README.md9.7 KB (9,965 B)83cda9675fd4096e9b3a65bb4e1f8fa3d652929fceac4a1bb69b807eac5510bff80ff5599f606ef7458bee3b54b68d866e868532
birefnet.py89.7 KB (91,896 B)8dea801e2b56facb8486ba351e6f91c3e664eb6d208771ae626f653d64128fbf2d6ac9f8e645c5cc5e286258a73ec3322bbfe5ef
config.json405 B (405 B)06d8fa9d7f2f4c6f1cf0dc6e7bfd194153176a42c97ea21569daf66b205491a4635147dd3bc42c7c168b89d7d75b53f67ef548ae
handler.py4.7 KB (4,762 B)a7125cb498ca55e30b3632caa474056dff1f59a5b95f5806bfcd575ccff2a9f08816ae0e5d4df1e81b48797f9997f68b531404a5
model.safetensors423.9 MB (444,473,596 B)8ebce1af95cb8df169ebad5f4a00e599fca0a2439ab37426bf4de0567af6b5d21b16151357149139362e6e8992021b8ce356a154
requirements.txt149 B (149 B)ed8583eb7e7264c7a0c76523882b265a3297423fbdfdd83a090d31a2acf64f7350653fa63cd8b5d9d6e0ff3e135bea7ce3786341

Cite this release

Canonical URL
https://aiseedbank.org/models/ZhengPeng7_BiRefNet/
Slug
ZhengPeng7_BiRefNet
Infohash
cca54e5191f28d2ab7f3227c06511b4acc3308fb
License
mit
Signing key fingerprint
85a3b32c3712427b

Every file carries a locally computed sha256 — verify a download against the signed sums: ZhengPeng7_BiRefNet.SHA256SUMS (+ minisign signature).

Provenance

Upstream repositoryZhengPeng7/BiRefNet
Revision (pinned)e2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4
Fetched at2026-09-03T20:47:34Z
License at fetchmit
Snapshot toolhuggingface · seedbank 0.1.0

Trackers

✓ verified · rehash-vs-hf-metadata at 2026-09-03T20:47:41Z

mit424.0 MB (444,581,071 bytes)birefnetsafetensorsimage-segmentationbackground-removalmask-generationDichotomous Image SegmentationCamouflaged Object DetectionSalient Object Detectionpytorch_model_hub_mixinmodel_hub_mixintransformerscustom_codeendpoints_compatiblepaper: 2401.03407