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

briaai/RMBG-1.4 on Ascend NPU

1. 模型简介

  • 模型名称: briaai/RMBG-1.4
  • 模型权重 URL: https://huggingface.co/briaai/RMBG-1.4
  • Revision / Snapshot: 2ceba5a5efaec153162aedea169f76caf9b46cf8 (HF cache 2026-08-22)
  • 任务类型: 计算机视觉 - 背景移除 / 图像分割 (image-segmentation, BiRefNet matting)
  • 架构: BriaRMBG (BiRefNet-based U-Net, 6-stage encoder RSU7/RSU6/RSU5/RSU4/RSU4F, 5-stage decoder, 6 side outputs Sigmoid, 44,046,790 params, ~176MB pytorch_model.bin / model.pth)
  • 配置: model_type=SegformerForSemanticSegmentation, in_ch=3 out_ch=1, size 1024x1024, processor_config ImageFeatureExtractor rescale 1/255 normalize mean 0.5 std 1.0
  • 官方推理: transformers.AutoModelForImageSegmentation.from_pretrained(..., trust_remote_code=True) + preprocess_image / postprocess_image (bilinear resize + normalize), MyPipe.RMBGPipe
  • NPU 适配方式: 原生 transformers 加载 + torch_npu 迁移至 npu:0, float32, 无需量化, 复用官方 briarmbg.py / MyConfig.py / utilities.py

来源: https://huggingface.co/briaai/RMBG-1.4 / https://github.com/briaai

2. 验证环境

组件版本
Python3.11.14
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
torchvision0.24.0
transformers4.57.6
huggingface_hub0.34.4
numpy1.26.4
Pillow11.3.0
scikit-image0.25.2
CANN8.5.1 (/usr/local/Ascend/cann-8.5.1)
NPUAscend910_9362 x2 (npu-smi 25.5.5), 目标设备 npu:0, torch.npu.is_available()==True, HBM 3108/65536 MB
HF_ENDPOINThttps://hf-mirror.com

npu-smi info 与 torch.npu.get_device_name(0) 见 assets/npu_device_call.png

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch_npu==2.9.0.post1
# torchvision==0.24.0
# transformers>=4.39.1
# huggingface_hub
# numpy==1.26.4
# Pillow
# scikit-image

模型权重通过 huggingface_hub.snapshot_download 自动走 HF_ENDPOINT 镜像, 本地缓存 /opt/atomgit/.cache/huggingface/hub/models--briaai--RMBG-1.4/snapshots/2ceba5a5efaec153162aedea169f76caf9b46cf8

4. NPU 推理

# 默认: NPU 推理 + 自动 CPU 对比验证
python inference.py
# 仅验证 CPU-NPU 一致性
python inference.py --mode validate
# 仅性能测试
python inference.py --mode benchmark

默认使用模型自带 example_input.jpg (1405x933 RGB) 缩放至 1024x1024, 经 preprocess_image (bilinear + divide 255 + normalize 0.5/1.0) 后输入 npu:0, 输出 6 个 1x1x1024x1024 Sigmoid masks, 取 side0 为主掩码, 再 postprocess_image 双线性还原至原图尺寸并 (x-min)/(max-min)*255 得到 uint8 掩码。

5. 真实推理结果

默认 python inference.py (npu:0, float32, 1024x1024, example_input.jpg 1405x933):

[model] snapshot_download -> /opt/atomgit/.cache/huggingface/hub/models--briaai--RMBG-1.4/snapshots/2ceba5a5efaec153162aedea169f76caf9b46cf8
[info] MODEL_ID=briaai/RMBG-1.4 device=npu:0 dtype=float32 input_size=[1024, 1024]
[env] torch=2.9.0+cpu torch_npu_available=True device_name=Ascend910_9362
[load] loaded in 0.23s params=44046790 arch=BriaRMBG
[device] model param device=npu:0 dtype=torch.float32
[input] loaded example image example_input.jpg shape (1405, 933, 3) orig_size=(1405, 933) tensor_shape=(1, 3, 1024, 1024) mean=0.1120 std=0.2794
[perf] 10 runs avg=21.65ms min=21.51ms max=22.20ms
[output] num_side_outputs=6 shapes=[(1, 1, 1024, 1024) x6]
[output] main_mask shape=(1, 1, 1024, 1024) dtype=torch.float32 device=npu:0 min=0.0000 max=1.0000 mean=0.0843
[post] result_arr shape=(1405, 933) dtype=uint8 mean=21.41 min=0 max=255
[post] binary thresh=127 fg_pixels=110526 total=1310865 fg_ratio=8.43%
[hash] result_sha256_16=0a37084f452ec912

输出契约:

  • masks[0..5] 每个 1x1x1024x1024 float32 Sigmoid
  • result_arr 1405x933 uint8 掩码, 值域 0-255, 可直接 Image.fromarray 作为 alpha 通道 putalpha 实现背景移除
  • fg_pixels=110526 (8.43%) 对应前景对象 (人体/物体)

完整终端见 assets/model_result.png

6. CPU-NPU 一致性验证

固定输入 example_input.jpg (1405x933 -> 1024x1024) 对比 CPU 与 NPU float32 主掩码:

[cpu] mask mean=0.0843 arr mean=21.41 shape=(1405, 933)
[npu] mask mean=0.0843 arr mean=21.41 shape=(1405, 933)
[metric] MAE_raw=0.000002 MaxDiff=0.001862 IoU=0.999991 inter=110526 union=110527
[metric] CPU fg=110527 NPU fg=110526 total=1310865
[result] IoU>=0.99 && MAE<0.01 => PASS
  • 二值化阈值 127, 先 postprocess_image 还原至原图尺寸并归一化后比较, 消除纯插值差异
  • IoU=0.999991 >=0.99, MAE=2e-06 <0.01, MaxDiff=0.00186
  • 合成随机图 (1024x1024) 亦验证 IoU=0.999186, MAE=0.00014 (见预研), 均满足阈值

说明: 单样本固定图验证, 非完整数据集评测; 数值来自本次真实执行, 非估算。

7. 性能测试

环境: npu:0, float32, batch=1, input 1024x1024, example_input.jpg 1405x933 3 warmup + 10 正式 torch.npu.synchronize() 计时

[perf] 10 runs avg=21.65ms min=21.51ms max=22.20ms times=['21.6','21.6','21.6','21.6','21.5','22.2','21.6','21.6','21.5','21.5']
[bench] p50=21.69ms p90=21.76ms p95=21.76ms memory_allocated=0.0MB
指标值
端到端 (含预处理后 model forward + 同步, 不含初次编译) avg21.65 ms
min / max21.51 / 22.20 ms
p50 / p90 / p9521.69 / 21.76 / 21.76 ms
吞吐~46.2 images/s (1/0.02165)
首次加载~0.23 s
峰值显存 (torch)< 3.2 GB (HBM 3108/65536 MB 空闲)

大模型固定, 减少次数会在 README 说明, 此处 10 次满足基准。

8. 自验证截图

  • assets/agent_workflow.png — 阶段日志: 环境检查、获取模型、分析模型、NPU适配验证、性能测试
  • assets/npu_device_call.png — npu-smi info + torch.npu.is_available()==True + Ascend910_9362 + model param device=npu:0
  • assets/model_result.png — python inference.py 真实输出含 mask shape、mean、fg_ratio、IoU 与 PASS

截图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染。

9. 已知限制

  • 仅验证 float32 单卡 npu:0; float16 未启用 (模型原始 torch_dtype float32, 切半精度需进一步校准)
  • 输入固定 1024x1024 (官方 preprocessor_config size 1024), 动态尺寸未测试
  • 需 trust_remote_code=True (仓内 briarmbg.py / MyConfig.py 已审查: 仅定义 BriaRMBG + REBNCONV + RSU*, 无网络/文件写入/subprocess, 可安全启用)
  • 权重文件未提交 (*.bin/*.pth 禁止), 推理时需联网或走 HF_ENDPOINT 缓存
  • 背景移除为非分类任务, 无 Top-5, 以 IoU / MAE 为一致性指标

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, image-segmentation, background-removal, BiRefNet, BriaRMBG


适配时间: 2026-08-22, 验证设备 Ascend910_9362 x2, 适配脚本 inference.py 默认可复现