g
gcw_AVRCax4T/BEN2
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

BEN2 - Background Erase Network 2 (Ascend NPU Adaptation)

BEN2 是 PramaLLC 开发的高精度背景去除模型,基于 Confidence Guided Matting (CGM) 流水线。本仓库提供在 华为昇腾 Ascend NPU 上的完整适配推理方案。

模型简介

BEN2 (Background Erase Network 2) 引入新颖的 Confidence Guided Matting (CGM) 流水线:

  • Base 模型 (94M 参数): 执行初步前景分割
  • Refiner 网络: 专门处理 Base 模型低置信度像素,产生更精确的抠图结果(商业版)

架构

  • 骨干网络: SwinTransformer (embed_dim=128, depths=[2,2,18,2], num_heads=[4,8,16,32], window_size=12)
  • Head: Multi-field Cross-attention + Multi-scale Cross-attention Refinement Module
  • 输入: RGB 图像 (自动 resize 至 1024×1024)
  • 输出: Alpha 蒙版 (前景分割)

训练数据

  • DIS5k (Dichotomous Image Segmentation 5k) 数据集
  • 22K PramaLLC 专有分割数据集

基准性能 (DIS 5k Eval)

模型MAE ↓DICE ↑IOU ↑BER ↓ACC ↑
BEN_Base + Refiner (商业版)0.02830.89760.84300.05420.9725
BEN_Base (94M)0.03310.87430.83010.05600.9700
MVANet0.03530.86760.81040.06390.9660

Ascend NPU 适配结果

精度验证 (NPU vs CPU)

指标阈值实测值状态
MAE (平均绝对误差)-0.000529✅
Cosine Similarity> 0.991.000015✅
Relative Error< 1%0.738%✅

结论: NPU 推理精度与 CPU 参考结果高度一致,误差 < 1%。

性能基准 (Ascend910)

指标数值
平均推理延迟0.255s / image
吞吐量3.93 images/sec
模型显存占用~0.37 GB
峰值显存~3.15 GB

逐图精度详情

图像MAECosine SimRel Error
grid_example1.png0.0001051.0000210.906%
grid_example2.png0.0007701.0000140.993%
grid_example3.png0.0012730.9999851.167%
grid_example6.png0.0001481.0000490.210%
grid_example7.png0.0008421.0000010.585%
model_comparison.png0.0000391.0000230.566%

环境要求

组件版本
CANN8.5.1
torch2.6.0
torch_npu2.6.0
Python3.11
NPU 硬件Ascend910 / Ascend910B / Atlas 800T A2

依赖安装

pip install torch torch_npu torchvision
pip install timm einops safetensors Pillow numpy opencv-python

快速开始

1. 下载模型

pip install modelscope
modelscope download --model PramaLLC/BEN2 --local_dir ./BEN2

2. 运行推理

from inference_npu import BEN2_NPU
from PIL import Image

# 初始化模型
model = BEN2_NPU(npu_device_id=0)
model.load_npu_weights("./model.safetensors")

# 推理
image = Image.open("./input.png")
foreground = model.inference(image)
foreground.save("./output.png")

3. 批量处理

python inference_npu.py

输出图像保存在 ./output_npu/ 目录。

文件说明

文件说明
BEN2.py原始模型定义 (SwinTransformer + BEN_Base)
inference_npu.pyAscend NPU 适配推理脚本
benchmark_eval.py精度验证 + 性能基准测试
model.safetensors模型权重 (363MB)
evaluation_report.json评测报告 (精度/性能数据)

Ascend NPU 适配关键技术点

  1. 设备适配: 将 CUDA 设备替换为 torch_npu NPU 设备
  2. inplace 操作禁用: GELU/Dropout 的 inplace=True 在 NPU 上可能导致异常,已设为 False
  3. 自动混合精度: 移除 CUDA-specific torch.autocast 装饰器,使用 NPU 原生 float32 推理
  4. 随机种子: 使用 torch_npu.npu.manual_seed 替代 torch.cuda.manual_seed
  5. 权重加载: 使用 safetensors 格式直接加载,兼容性更好

引用

@software{BEN2_Background_Erase_Network_2,
  author = {PramaLLC},
  title = {BEN2: Background Erase Network 2},
  year = {2025},
  url = {https://huggingface.co/PramaLLC/BEN2}
}

License

本仓库代码和模型遵循 MIT License。原始模型版权归 PramaLLC 所有。