冬
gcw_IDzXRVNw/RMBG-2.0-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

RMBG-2.0-ascend

RMBG-2.0背景去除模型适配昇腾NPU版本。

模型简介

  • 基础模型: briaai/RMBG-2.0
  • 任务: 背景去除 (Background Removal)
  • 架构: BiRefNet (Bilateral Reference for High-Resolution Dichotomous Image Segmentation)
  • 参数量: 220.70M
  • 输入尺寸: 1024×1024 RGB图像
  • 输出: 多尺度分割图 (1/32, 1/16, 1/8, 1/1)

性能表现

指标数值
推理时间~19s (batch_size=1)
NPU显存占用~3.2MB (峰值)
CPU回退deform_conv2d操作回退到CPU

环境依赖

torch>=2.0.0
torch_npu>=2.0.0
transformers>=4.30.0
safetensors>=0.4.0
kornia>=0.7.0
timm>=0.9.0
torchvision>=0.15.0
numpy>=1.21.0
pillow>=9.0.0

分步推理流程

1. 安装依赖

cd /workspace/agent/RMBG-2.0-ascend
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple

2. 运行推理

cd /workspace/agent/RMBG-2.0-ascend
python inference.py --device npu:0 --show_npu_memory

3. 参数说明

  • --model_path: 模型路径,默认 /workspace/agent/RMBG-2.0
  • --device: 运行设备,默认 npu:0
  • --show_npu_memory: 显示NPU显存使用情况

测试用例与输出

测试代码

import torch
from transformers import AutoModelForImageSegmentation

device = "npu:0"
model = AutoModelForImageSegmentation.from_pretrained(
    "/workspace/agent/RMBG-2.0",
    trust_remote_code=True,
    local_files_only=True
)
model = model.to(device)
model.eval()

images = torch.randn(1, 3, 1024, 1024).to(device)
with torch.no_grad():
    output = model(images)

# output[0] 是最终的mask
mask = output[0].sigmoid()

完整输出

[设备] npu:0
NPU设备数量: 1
  设备 0: Ascend910B2

[模型] RMBG-2.0 (BiRefNet)
[参数量] 220.70M

[模型加载]
[模型已迁移至] npu:0

构建测试输入: 随机图像 (1024x1024x3)
图像形状: torch.Size([1, 3, 1024, 1024])

运行推理...

==========================================================================
输出摘要
==========================================================================
  模型: RMBG-2.0 (BiRefNet)
  任务: 背景去除
  输入形状: torch.Size([1, 3, 1024, 1024])
  输出数量: 4
  输出[0]形状: torch.Size([1, 1, 32, 32])
  输出[1]形状: torch.Size([1, 1, 64, 64])
  输出[2]形状: torch.Size([1, 1, 128, 128])
  输出[3]形状: torch.Size([1, 1, 1024, 1024])
  总耗时: 19.03s
  输出 finite: True
  前景概率范围: [0.0000, 1.0000]
  平均前景概率: 0.5000

输出格式

模型输出4个多尺度分割结果:

  • output[0]: 1/32分辨率 (32×32)
  • output[1]: 1/16分辨率 (64×64)
  • output[2]: 1/8分辨率 (128×128)
  • output[3]: 1/1分辨率 (1024×1024) - 最终输出

每个输出形状为 (batch_size, 1, H, W),值域为 [0, 1],表示前景概率。

使用后处理

from PIL import Image
import torchvision.transforms as transforms

# 加载图像
image = Image.open("input.jpg")
transform = transforms.Compose([
    transforms.Resize((1024, 1024)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
input_tensor = transform(image).unsqueeze(0).to("npu:0")

# 推理
with torch.no_grad():
    output = model(input_tensor)[-1].sigmoid()

# 后处理
mask = transforms.Resize(image.size)(output.squeeze())
mask_pil = transforms.ToPILImage()(mask)
image.putalpha(mask_pil)
image.save("output.png")

注意事项

  1. 模型使用transformers库加载,需设置trust_remote_code=True
  2. 部分操作(如deform_conv2d)会回退到CPU运行
  3. 预处理使用ImageNet归一化 (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
  4. 输入图像建议Resize到1024×1024以获得最佳效果