Birefnethr
模型简介
BiRefNet是一个高性能的图像分割模型,专门设计用于处理高分辨率图像的二分分割任务,如背景去除和掩膜生成。该模型在多个数据集上表现出色,支持高分辨率推理。
模型特点
高分辨率支持
模型使用2048x2048分辨率图像训练,支持更高分辨率的推理。
双边参考框架
采用双边参考框架,优化高分辨率图像的分割效果。
多任务支持
支持背景去除、掩膜生成、二分图像分割、伪装目标检测和显著目标检测等多种任务。
模型能力
高分辨率图像分割
背景去除
掩膜生成
二分图像分割
伪装目标检测
显著目标检测
使用案例
图像处理
背景去除
从高分辨率图像中精确分离前景和背景。
在DIS-VD数据集上达到0.925的maxFm分数。
掩膜生成
为图像中的目标生成精确的掩膜。
在DIS-VD数据集上达到0.927的Smeasure分数。
计算机视觉
伪装目标检测
检测和分割图像中伪装的目标。
显著目标检测
识别和分割图像中最显著的目标。
🚀 BiRefNet
BiRefNet是一个用于高分辨率二分图像分割的模型,在背景去除、掩码生成等多个图像分割任务中表现出色,能为相关领域的研究和应用提供强大的支持。
🚀 快速开始
本仓库是论文 "Bilateral Reference for High-Resolution Dichotomous Image Segmentation" (CAAI AIR 2024) 的官方实现。访问我们的GitHub仓库 https://github.com/ZhengPeng7/BiRefNet 以获取更多详细信息,包括 代码、文档 和 模型库!
✨ 主要特性
- 该BiRefNet使用
2048x2048
分辨率的图像进行训练,以实现更高分辨率的推理。 - 在二分图像分割(DIS)、高分辨率显著目标检测(HRSOD)和伪装目标检测(COD)三项任务中取得了SOTA性能。
性能表现
所有测试均在FP16模式下进行。
数据集 | 方法 | 分辨率 | maxFm | wFmeasure | MAE | Smeasure | meanEm | HCE | maxEm | meanFm | adpEm | adpFm | mBA | maxBIoU | meanBIoU |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DIS-VD | BiRefNet_HR-general-epoch_130 | 2048x2048 | .925 | .894 | .026 | .927 | .952 | 811 | .960 | .909 | .944 | .888 | .828 | .837 | .817 |
DIS-VD | BiRefNet_HR-general-epoch_130 | 1024x1024 | .876 | .840 | .041 | .893 | .913 | 1348 | .926 | .860 | .930 | .857 | .765 | .769 | .742 |
DIS-VD | BiRefNet-general-epoch_244 | 2048x2048 | .888 | .858 | .037 | .898 | .934 | 811 | .941 | .878 | .927 | .862 | .802 | .790 | .776 |
DIS-VD | BiRefNet-general-epoch_244 | 1024x1024 | .908 | .877 | .034 | .912 | .943 | 1128 | .953 | .894 | .944 | .881 | .796 | .812 | .789 |
示例图片
DIS-Sample_1 | DIS-Sample_2 |
---|---|
📦 安装指南
0. 安装依赖包
pip install -qr https://raw.githubusercontent.com/ZhengPeng7/BiRefNet/main/requirements.txt
💻 使用示例
基础用法
1. 加载BiRefNet
使用HuggingFace的代码和权重
仅使用HuggingFace上的权重 -- 优点:无需手动下载BiRefNet代码;缺点:HuggingFace上的代码可能不是最新版本(我会尽量保持其为最新版本)。
# Load BiRefNet with weights
from transformers import AutoModelForImageSegmentation
birefnet = AutoModelForImageSegmentation.from_pretrained('ZhengPeng7/BiRefNet_HR', trust_remote_code=True)
使用GitHub的代码和HuggingFace的权重
仅使用HuggingFace上的权重 -- 优点:代码始终是最新的;缺点:需要从我的GitHub克隆BiRefNet仓库。
# 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_HR')
使用GitHub的代码和本地的权重
仅使用本地的权重和代码。
# 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)
使用加载好的BiRefNet进行推理
# 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 = (2048, 2048)
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. 本地使用推理端点
你可能需要自己点击 deploy 并设置端点,这可能会产生一些费用。
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
📚 详细文档
本BiRefNet用于标准二分图像分割(DIS),在 DIS-TR 上进行训练,并在 DIS-TEs 和 DIS-VD 上进行验证。本仓库包含了我们论文中提出的BiRefNet的权重。
在线演示
相关链接
📄 许可证
本项目采用MIT许可证。详情请见 LICENSE。
致谢
非常感谢 @freepik 为训练此模型提供的GPU资源支持!
引用
@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}
}
Clipseg Rd64 Refined
Apache-2.0
CLIPSeg是一种基于文本与图像提示的图像分割模型,支持零样本和单样本图像分割任务。
图像分割
Transformers

C
CIDAS
10.0M
122
RMBG 1.4
其他
BRIA RMBG v1.4 是一款先进的背景移除模型,专为高效分离各类图像的前景与背景而设计,适用于非商业用途。
图像分割
Transformers

R
briaai
874.12k
1,771
RMBG 2.0
其他
BRIA AI开发的最新背景移除模型,能有效分离各类图像的前景与背景,适合大规模商业内容创作场景。
图像分割
Transformers

R
briaai
703.33k
741
Segformer B2 Clothes
MIT
基于ATR数据集微调的SegFormer模型,用于服装和人体分割
图像分割
Transformers

S
mattmdjaga
666.39k
410
Sam Vit Base
Apache-2.0
SAM是一个能够通过输入提示(如点或框)生成高质量对象掩码的视觉模型,支持零样本分割任务
图像分割
Transformers 其他

S
facebook
635.09k
137
Birefnet
MIT
BiRefNet是一个用于高分辨率二分图像分割的深度学习模型,通过双边参考网络实现精确的图像分割。
图像分割
Transformers

B
ZhengPeng7
626.54k
365
Segformer B1 Finetuned Ade 512 512
其他
SegFormer是一种基于Transformer的语义分割模型,在ADE20K数据集上进行了微调,适用于图像分割任务。
图像分割
Transformers

S
nvidia
560.79k
6
Sam Vit Large
Apache-2.0
SAM是一个能够通过输入提示点或边界框生成高质量物体掩膜的视觉模型,具备零样本迁移能力。
图像分割
Transformers 其他

S
facebook
455.43k
28
Face Parsing
基于nvidia/mit-b5微调的语义分割模型,用于面部解析任务
图像分割
Transformers 英语

F
jonathandinu
398.59k
157
Sam Vit Huge
Apache-2.0
SAM是一个能够根据输入提示生成高质量对象掩码的视觉模型,支持零样本迁移到新任务
图像分割
Transformers 其他

S
facebook
324.78k
163
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98