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
For more information, check out the official repository.
Usage (Transformers.js)
If you haven't already, you can install the Transformers.js JavaScript library from NPM using:
npm i @huggingface/transformers
You can then use the model for image matting, as follows:
js
1import{AutoModel,AutoProcessor,RawImage}from'@huggingface/transformers';23// Load model and processor4const model_id ='onnx-community/BiRefNet-ONNX';5const model =awaitAutoModel.from_pretrained(model_id,{dtype:'fp32'});6const processor =awaitAutoProcessor.from_pretrained(model_id);78// Load image from URL9const url ='https://images.pexels.com/photos/5965592/pexels-photo-5965592.jpeg?auto=compress&cs=tinysrgb&w=1024';10const image =awaitRawImage.fromURL(url);1112// Pre-process image13const{ pixel_values }=awaitprocessor(image);1415// Predict alpha matte16const{ output_image }=awaitmodel({input_image: pixel_values });1718// Save output mask19const mask =awaitRawImage.fromTensor(output_image[0].sigmoid().mul(255).to('uint8')).resize(image.width, image.height);20mask.save('mask.png');
Input image
Output mask
image/png
image/png
Citation
@article{BiRefNet,
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}
}
Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using 🤗 Optimum and structuring your repo like this one (with ONNX weights located in a subfolder named onnx).