Bilateral Reference for High-Resolution Image Segmentation
A research-minded model workspace for sharp masks, difficult edges and repeatable visual experiments.
This is the official birefnet workspace for Bilateral Reference for High-Resolution Dichotomous Image Segmentation, adapted here as a practical starting point for high-resolution foreground extraction, matting and related binary image tasks.
Note
High-resolution work benefits from capable hardware. The model family is designed to preserve structure as images get larger, while the surrounding toolkit keeps experiments easy to reproduce.
News 📰
Sep 23, 2025: The attention path was refreshed with an official PyTorch SDPA implementation to reduce memory pressure during training and inference.Jun 30, 2025: Foreground refinement was accelerated with a GPU-assisted fast-estimation path and cleaner array preprocessing.May 15, 2025: A recorded tutorial for fine-tuning birefnet on custom data was released for people who prefer a guided walkthrough.Mar 31, 2025: The dynamic model path was expanded across resolutions from256x256to2304x2304for more robust general use.Feb 12, 2025: A higher-resolution matting variant was added for workflows that need softer edges and more natural transitions.Feb 1, 2025: The high-resolution general-use model was published with training centered on2048x2048images.Jan 6, 2025: FP16 inference was validated with nearly unchanged quality and lower memory use on supported GPUs.Dec 5, 2024: Compatibility fixes landed for current PyTorch compilation and accelerated mixed-precision training.Nov 28, 2024: A student project built with birefnet received recognition in a national innovation competition.Oct 26, 2024: The documentation gained a dedicated guide for fine-tuning existing weights on custom datasets.Oct 6, 2024: Trimap-free matting weights were added for general foreground workflows.Sep 24, 2024: A lightweight 2K model and a video inference notebook were published for higher-resolution inputs.Aug 30, 2024: Local inference and ONNX conversion notebooks were added to the tutorials folder.Aug 23, 2024: The research work was formally published online, alongside a public project release.Aug 19, 2024: ONNX model files for the available weights were collected with conversion notes.Jul 28, 2024: Box-guided segmentation examples were added for more controlled object selection.Jul 15, 2024: One-line model loading was documented for a faster first experiment.May 28, 2024: The model zoo was expanded with weights for general use, matting, DIS, HRSOD and COD.May 7, 2024: A multiple-image inference notebook was published for batch experimentation.Mar 7, 2024: Training code, evaluation utilities and task-specific weights were organized into the public project.Jan 7, 2024: The original paper and initial implementation were released.
🚀 Load birefnet in ONE LINE
from transformers import AutoModelForImageSegmentation birefnet = AutoModelForImageSegmentation.from_pretrained( "birefnet/model", trust_remote_code=True )
🛬 Inference Partner:
You can access the birefnet inference workflow through the model workspace or use the deployment route to set up your own environment.
Our birefnet models are designed for demanding high-resolution tasks
Try our online demos for inference:
- Inference and evaluation of your chosen weights: open the evaluation workflow
- Online inference with a GUI and adjustable resolutions: open the visual workspace
- Multiple image inference for a small batch: open the batch workflow
Awards:
Some strong student and community projects have been built around birefnet.
Model Zoo
For broader practical use, the original research implementation is extended into model variants for different image sizes, tasks and deployment constraints.
Packaged datasets and backbone weights are organized by task so experiments can begin from a known baseline.
Performance logs and predicted maps are kept alongside the corresponding experiment settings.
Models from the original paper for benchmark comparison
| Task | Training sets | Backbone | Download |
|---|---|---|---|
| DIS | DIS5K-TR | swin_v1_large | model file |
| COD | COD10K-TR, CAMO-TR | swin_v1_large | model file |
| HRSOD | DUTS-TR | swin_v1_large | model file |
| HRSOD | DUTS-TR, HRSOD-TR | swin_v1_large | model file |
| HRSOD | DUTS-TR, UHRSD-TR | swin_v1_large | model file |
| HRSOD | DUTS-TR, HRSOD-TR, UHRSD-TR | swin_v1_large | model file |
Models trained for general use and matting
| Task | Training sets | Backbone | Test set | Metric | Download |
|---|---|---|---|---|---|
| general use, 2048x2048 | AIM-500, DIS, HIM2K, PPM-100 and high-resolution human sets | swin_v1_large | DIS-VD | 0.927, 0.894 | model file |
| general use | DIS5K, DIS-TEs, DUTS, HRSOD and UHRSD | swin_v1_large | DIS-VD | 0.911, 0.875 | model file |
| general use | DIS5K and DIS-TEs | swin_v1_tiny | DIS-VD | 0.882, 0.830 | model file |
| general matting | P3M, human, AIM-500, HIM2K and synthesized backgrounds | swin_v1_large | TE-P3M | 0.979, 0.988 | model file |
| portrait matting | P3M and human image sets | swin_v1_large | P3M-500-P | 0.983, 0.989 | model file |
Segmentation with box guidance
Model efficiency
| Runtime | FP32 | FP16 |
|---|---|---|
| A100 | 86.8ms | 69.4ms |
| 4090 | 95.8ms | 57.7ms |
| V100 | 384ms | 152ms |
ONNX conversion
Third-Party Creations
Several community applications use birefnet as a backend or node. The examples below keep the same practical, click-first spirit as the original project.
-
Applications
Background eraser: a compact browser workflow for removing photo backgrounds with a lightweight birefnet variant.
Open workflow
In-browser removal: a local-first interface keeps the image on the device while the mask is computed in the browser.
Open workflow
Flexible inference workflow: a visual pipeline connects image input, model configuration, refinement and output preview.
Open workflow
ComfyUI integration: a node-based panel makes segmentation and mask conversion available inside a visual workflow.
Open workflow
Video cutout service: birefnet can serve as the foreground engine for short video background-removal workflows.
Open workflow
Invoke-style node: a simple image primitive feeds a background-removal node with optional foreground refinement.
Open workflow
2D asset generation: foreground extraction combines with creative tooling to produce clean cutout assets for design and previs.
Open workflow
Text-behind-subject effects: extracting the foreground makes it possible to place typography between the subject and its original scene.
Open workflow
Stable diffusion utility: a model panel adds foreground extraction to an image-generation workflow with mask and transparent output views.
Open workflow
Online model playground: a focused input-and-result surface exposes useful controls without requiring a local setup.
Open workflow
-
Model Extensions
Anime foreground models: fine-tuned data can adapt birefnet to illustrated characters and stylized artwork where hair, ribbons and costume edges matter.
Explore extension
Lightweight inference: alternate runtimes and compact formats make it easier to bring segmentation to consumer hardware.
image_data image = image_load("input.png"); backend_device device = backend_init(); birefnet_model model = birefnet_load_model("BiRefNet-F16", device); image_data mask = birefnet_compute(model, image);
Higher-quality private data: task-specific training can improve binary foreground masks, especially on varied scenes and difficult object boundaries.
Explore extension
-
More Visual Comparisons
Side-by-side comparisons show how birefnet behaves on product scenes, portraits and detailed foreground edges.
Video comparisons focus on continuity, silhouette stability and preservation of small moving details.
Usage
Environment Setup
# PyTorch >= 2.5.0 is used for faster training with compilation.
conda create -n birefnet python=3.11 -y && conda activate birefnet
pip install -r requirements.txt
Dataset Preparation
Download the combined training and test sets organized for DIS, COD and HRSOD, or use the official source datasets in the single_ones folder. Keep image and ground-truth folders aligned inside each task.
Weights Preparation
Download backbone weights from the model workspace or their official project pages before launching training or local inference.
Run
# Train & Test & Evaluation ./train_test.sh RUN_NAME GPU_NUMBERS_FOR_TRAINING GPU_NUMBERS_FOR_TEST # Example: ./train_test.sh tmp-proj 0,1,2,3 0 # See train.sh / test.sh for only training or evaluation. # After evaluation, use gen_best_ep.py to select the best checkpoint.
🖊️ Fine-tuning on Custom Data
A recorded tutorial explains the birefnet fine-tuning path from dataset layout to checkpoint selection.
- Pre-requisites: place datasets at
${data_root_dir}/TASK_NAME/DATASET_NAME, withimandgtfolders inside each dataset. - Change an existing task: replace the existing task name throughout the project with your custom task name.
- Adapt settings: update
sys_home_dir,testsets,training_setand the loss weights for your task. - Use existing weights: resume from a checkpoint and continue the epoch range from the number encoded in the file name.
- Validate the result: compare masks on the validation set before promoting a checkpoint to a production workflow.
Well-trained weights:
Download the BiRefNet-{TASK}-{EPOCH}.pth files from the model workspace and release collection. Predicted maps, performance summaries and training logs live beside the corresponding experiment settings.
The results can vary slightly with hardware and training configuration. A single capable GPU can be enough for inference at 1024x1024, while higher-resolution training benefits from additional memory.
When more GPUs are available, increase the batch size and configure the device IDs in config.py to move smoothly from single-card to multi-card training.
Some of my messages:
This project began as a DIS implementation and grew into a broader toolkit for binary image segmentation. It can support DIS, COD, SOD, medical imagery and anomaly segmentation when the task configuration is adapted carefully.
- Multi-GPU training can be opened or closed with a single setting.
- Backbone choices include Swin variants, PVT variants and convolutional networks.
- Weighted losses include BCE, IoU, SSIM, MAE and regression-oriented terms.
- Training tricks include multi-scale supervision, backbone freezing and multi-scale inputs.
- The data collator can combine datasets in memory for coordinated training and testing.
- The goal is a reusable foundation for new visual segmentation work.
Quantitative Results
Benchmark summaries compare birefnet with established methods across high-resolution and low-resolution datasets.
| Test set | Structure score | Weighted F score | MAE | HCE |
|---|---|---|---|---|
| DIS-TE1 | 0.860 | 0.819 | 0.037 | 106 |
| DIS-TE2 | 0.894 | 0.857 | 0.036 | 266 |
| DIS-TE3 | 0.925 | 0.893 | 0.028 | 569 |
| DIS-VD | 0.891 | 0.854 | 0.038 | 989 |
Qualitative Results
Visual comparisons focus on narrow structures, partial occlusion, repeated patterns and camouflage.
Acknowledgement:
Many thanks to the companies, institutes and community contributors who help make high-resolution visual research more accessible.
Citation
@article{birefnet2024,
title={Bilateral Reference for High-Resolution Dichotomous Image Segmentation},
author={Birefnet Research Team},
journal={CAAI Artificial Intelligence Research},
volume={3},
pages={9150038},
year={2024}
}
Contact
Questions, discussions and implementation notes can be shared through the project workspace. For a guided start, use the birefnet workflow and bring your own image or prompt.
MIT license
The project documentation and core implementation are presented under the MIT license. Review the accompanying license file before using any model weights or third-party extensions.