专业级图像分割工具完全开源免费,支持Windows和MacOS系统,毛发级精度的AI抠图解决方案
什么是BiRefNet?
BiRefNet是由南开大学计算机视觉团队开发的开源图像分割模型,采用创新的双边参考框架技术,在高分辨率图像分割领域实现了突破性进展。相比传统方案,BiRefNet在保持高精度的同时显著提升了处理速度,特别擅长处理复杂边缘如毛发、透明物体和精细纹理。
突破性性能
在DIS5K数据集测试中,S-Measure指标达到94.7%,比前代模型提升5.6%,4K图像处理仅需83ms
完全免费开源
基于MIT许可证开放全部源代码和预训练模型,商业项目可免费使用
多平台支持
原生支持Windows、MacOS和Linux系统,提供Python API、ComfyUI插件和在线演示
核心技术亮点
- 双边参考框架:结合定位模块(LM)和重建模块(RM),全局定位与局部重建完美结合
- 内部参考机制(InRef):直接调用原始高分辨率图像区块,避免下采样细节丢失
- 外部参考机制(OutRef):引入梯度图监督,显著强化边缘纹理识别能力
- 动态分辨率支持:单一模型支持256px到2304px任意尺寸输入
官方资源与快速访问
全平台安装指南
Windows系统安装步骤
- 安装Python环境:从Python官网下载3.8+版本,安装时勾选”Add to PATH”
- 安装PyTorch:根据显卡选择对应版本(推荐CUDA 11.7):
pip3 install torch torchvision torchaudio –index-url https://download.pytorch.org/whl/cu117
- 安装依赖库:
pip install opencv-python kornia huggingface_hub matplotlib tqdm
- 下载模型权重:从Hugging Face或GitHub下载Swin-L骨干网络和预训练模型
- 安装Visual Studio Build Tools:安装时勾选”C++桌面开发”组件
- 验证安装:运行测试脚本检查环境:
python -c “import torch; print(torch.__version__); print(torch.cuda.is_available())”
MacOS系统安装步骤
- 安装Homebrew:
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
- 安装Python环境:
brew install [email protected]
echo ‘export PATH=”/opt/homebrew/opt/[email protected]/bin:$PATH”‘ >> ~/.zshrc - 安装PyTorch(MPS加速):
pip3 install torch torchvision torchaudio
- 安装依赖库:
pip3 install opencv-python kornia huggingface_hub libpng
- 启用Metal加速:在Python脚本中添加:
# 设置使用Metal Performance Shaders
device = torch.device(“mps”)
model.to(device)
ComfyUI插件安装方法
- 在ComfyUI管理器中搜索”BiRefNet-Hugo”并安装
- 或手动安装:
# 进入ComfyUI自定义节点目录
cd ComfyUI/custom_nodes# 克隆插件仓库
git clone https://github.com/MoonHugo/ComfyUI-BiRefNet-Hugo.git# 安装依赖
pip install -r ComfyUI-BiRefNet-Hugo/requirements.txt - 下载预训练模型到指定目录:
ComfyUI/models/birefnet/
├── BiRefNet.pth
└── swin_large_patch4_window12_384_22k.pth - 重启ComfyUI,在节点菜单中找到BiRefNet Segmentor节点
使用教程与代码示例
Python基础使用
import torch
import cv2
from transformers import AutoModelForImageSegmentation
from torchvision import transforms# 加载模型(首次运行会自动下载)
model = AutoModelForImageSegmentation.from_pretrained(
“zhengpeng7/BiRefNet”,
trust_remote_code=True
).cuda() # Mac用户使用 .to(“mps”)# 图像预处理
def preprocess_image(image_path):
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
return transform(img).unsqueeze(0).cuda()
# 执行推理
input_tensor = preprocess_image(“input.jpg”)
with torch.no_grad():
output = model(input_tensor)
mask = (output.sigmoid() > 0.5).cpu().numpy().squeeze()
# 保存结果
cv2.imwrite(“output_mask.png”, mask * 255)
高级应用:透明背景生成
matting_model = AutoModelForImageSegmentation.from_pretrained(
“zhengpeng7/BiRefNet_HR-matting”,
trust_remote_code=True
).cuda()# 生成透明背景图像
def generate_transparent_bg(image_path, output_path):
img = cv2.imread(image_path)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
input_tensor = preprocess_image(image_path)with torch.no_grad():
alpha = matting_model(input_tensor).sigmoid().cpu().numpy().squeeze()
# 创建带透明通道的图像
rgba = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA)
rgba[:, :, 3] = (alpha * 255).astype(‘uint8’)
cv2.imwrite(output_path, rgba)
性能优化与最佳实践
不同硬件配置性能对比
硬件配置 | 分辨率 | 处理时间 | 显存占用 | 推荐设置 |
---|---|---|---|---|
NVIDIA RTX 4090 | 2048×2048 | 0.12秒 | 5.2GB | FP32模式 |
NVIDIA RTX 3080 | 1920×1080 | 0.25秒 | 3.8GB | FP32模式 |
Apple M2 Max | 1920×1080 | 0.68秒 | 统一内存 | MPS加速 |
CPU (i7-12700K) | 1280×720 | 3.2秒 | 系统内存 | 缩小分辨率 |
优化技巧
- 启用半精度模式:添加
model.half()
可将显存占用减少40%,速度提升25% - 批处理优化:同时处理多张图像可提升30%吞吐量(需统一尺寸)
- 动态分辨率模型:加载
BiRefNet_dynamic
处理非标准尺寸图像 - 梯度监督增强:设置
gradient_supervision=True
提升复杂边缘质量 - 视频处理优化:使用官方
video_inference.ipynb
笔记本进行批处理
提示:对于4K以上分辨率图像,建议先缩小到2048px宽度再处理,最后通过超分辨率放大,可在保持质量的同时减少75%处理时间。
没有回复内容