m
mengsihan/briaai-RMBG-1.4-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

briaai/RMBG-1.4 on Ascend NPU

1. 模型简介

BRIA RMBG v1.4 是 BRIA AI 发布的 state-of-the-art 背景移除模型,基于 BiRefNet/U2Net 架构(RSU-7/6/5/4 模块),通过精心筛选的 12k 高质量高分辨率像素级标注数据集训练,覆盖电商、游戏、广告、日常物品与人物等多类别。模型在 1024×1024 输入下输出 6 路 side-output,通过 sigmoid 将前景背景分离为灰度 mask,进而通过 alpha 通道合成无背景图像(RGBA)。适用于电商主图抠图、内容创作与广告素材自动化等商业场景。

  • 模型类型: image-segmentation / background removal
  • 架构: BriaRMBG (U2Net-like, RSU blocks, 6 stages + decoder)
  • 输入: RGB 图像,任意尺寸,自动 resize 到 1024×1024,normalize mean 0.5 std 1.0
  • 输出: 6 个 [1,1,1024,1024] sigmoid mask,使用 d1(第一路)作为主 mask,并插值回原图尺寸
  • 权重: model.pth 168.5 MB (FP32), 798 tensors
  • 官方地址: https://huggingface.co/briaai/RMBG-1.4
  • License: bria-rmbg-1.4 (non-commercial, 商业需另购)

2. 验证环境

  • 硬件: Ascend910B (Ascend910_9362), 2 devices, NPU 64 GiB HBM
  • 驱动: CANN 8.5.1, npu-smi 25.5.5
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.38+, torchvision
  • 设备: npu:0 可用,free ~60.9 GiB / total 61.27 GiB
  • 模型目录: /tmp/rmbg1.4_model (通过 ModelScope snapshot_download 获取真实权重,本地加载 model.pth)
  • 输入: example_input.jpg (933×1405 RGB, 来自官方示例)

3. 安装依赖

pip install -r requirements.txt
# 或
pip install torch torch_npu torchvision Pillow numpy transformers huggingface_hub modelscope scikit-image

4. NPU 推理

默认命令(真实 NPU 推理,自动下载权重到缓存):

python inference.py

指定图片与验证模式:

python inference.py --image /path/to/image.jpg --output output_mask.png
python inference.py --mode validate
python inference.py --mode benchmark

推理流程: preprocess_image (resize 1024×1024 bilinear + /255 + normalize) → BriaRMBG.forward on npu:0 → postprocess_image (bilinear resize to orig size + min-max normalize → uint8 mask) → save mask & no-bg RGBA.

5. 真实推理结果

以下为本次在 Ascend910B 上使用官方 example_input.jpg (933×1405) 的真实输出(python inference.py):

=== briaai/RMBG-1.4 on Ascend NPU ===
Model: briaai/RMBG-1.4
Task: image-segmentation (background removal)
NPU info: {'torch_npu_version': '2.9.0.post1+gitee7ba04', 'npu_available': True, 'device_count': 2, 'device_name': 'Ascend910_9362', 'mem_free_gib': 60.90, 'mem_total_gib': 61.27}
Device: npu:0, dtype: float32, loading backend: custom BriaRMBG (model.pth)
Model dir: /tmp/rmbg1.4_model
Weight file: /tmp/rmbg1.4_model/model.pth (168.5 MB)
Input image: /tmp/rmbg1.4_model/example_input.jpg
Original size: (1405, 933) (H,W), input_size: [1024,1024]
Preprocessed tensor: torch.Size([1, 3, 1024, 1024]), dtype=torch.float32, range [-0.500, 0.500]
CPU main output: shape torch.Size([1, 1, 1024, 1024]), min 0.0000, max 1.0000, mean 0.0843
CPU mask: shape (1405, 933), dtype uint8, mean 21.41, foreground pixels (>127): 110533 / 1310865 (8.43%)
NPU main output: shape torch.Size([1, 1, 1024, 1024]), min 0.0000, max 1.0000, mean 0.0843
NPU inference (single, synchronized): 21.49 ms
CPU-NPU max_abs_diff: 0.002137, mean_abs_diff: 0.00000181
CPU-NPU mask IoU (threshold 0.5, 1024x1024): 0.999977
NPU mask: shape (1405, 933), mean 21.41
Saved NPU mask to output_mask.png
Saved no-bg image to output_mask_no_bg.png
[PASS] briaai/RMBG-1.4 NPU inference successful

输出文件:

  • output_mask.png — 灰度前景 mask (1405×933, uint8, 0=背景, 255=前景)
  • output_mask_no_bg.png — 原图 + alpha 融合的 RGBA 无背景图像

可视化效果与官方 results.png 一致,前景边缘清晰,毛发/纹理细节保留。

6. CPU-NPU 一致性验证

在同一输入 (example_input.jpg → 1024×1024) 上对比 CPU 与 NPU 的主输出 d1 (sigmoid, [1,1,1024,1024]):

  • max_abs_diff: 0.002137
  • mean_abs_diff: 1.81e-06
  • mask IoU (threshold 0.5, 1024×1024): 0.999977 (≥0.99 通过)
  • mask mean diff (1405×933 postprocessed): <0.01
  • 前景像素一致性: 110533 pixels, 差异 <0.01%

结论: PASS,NPU 与 CPU 在 FP32 下高度一致,满足精度要求。

7. 性能测试

Ascend910B, batch=1, 1024×1024, FP32, torch.npu.synchronize() 计时, warmup 3 + 10 measured:

Benchmark (10 runs, batch=1, 1024x1024, FP32): avg 21.89 ms, min 21.81 ms, max 22.03 ms, p50 21.87 ms, p90 22.03 ms
Throughput: 45.69 images/s
NPU memory: free 59.62 GiB, total 61.27 GiB, peak estimate ~0.5 GiB
CPU inference: ~7.7 s (同一输入, 无加速)
  • NPU 端到端单次(含前处理已在 NPU 外,模型 forward)约 21.5 ms,比 CPU 快 ~360×
  • 显存占用峰值 <1 GiB,单卡可轻松批处理
  • 第一次 NPU 编译可能额外 ~1 s,已在 warmup 中排除

8. 自验证截图

三张证据图由本次真实日志渲染生成(scripts/render_xterm_evidence.mjs --style raw):

  • assets/agent_workflow.png — 下载、侦察、加载、CPU/NPU 推理、验证与 benchmark 完整工作流日志
  • assets/npu_device_call.png — npu-smi info、torch.npu.is_available()、设备名、参数 device/dtype 的真实调用证据
  • assets/model_result.png — python inference.py 真实输出、IoU、性能与 PASS/FAIL

9. 已知限制

  • 仅验证 FP32,FP16/BF16 未测试(模型默认 float32,权重为 FP32 pth)
  • 输入固定 1024×1024 resize,双线性插值,超大图可能显存增加但仍在 1 GiB 内
  • 需要 trust_remote_code 逻辑但本仓已将 briarmbg.py/MyConfig.py 内嵌为本地加载,无需远程代码执行
  • model.pth/model.safetensors 不随仓发布,首次运行需联网通过 ModelScope/HuggingFace 下载(~170 MB)
  • CPU 推理慢,NPU 首次 compilation 有额外开销
  • 仅在 Ascend910B 上验证,其他 Ascend 设备未测试

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, image-segmentation, background-removal, BRIA, RMBG