gcw_6a3w2yXz/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
  • 模型地址: https://huggingface.co/briaai/RMBG-1.4
  • 任务: 图像分割 / 背景移除 (image-segmentation / remove background)
  • 架构: BriaRMBG (BiRefNet) — RSU7(64,32,64) → RSU6(64,32,128) → RSU5(128,64,256) → RSU4(256,128,512) → RSU4F(512,256,512) → RSU4F(512,256,512) 编码器 + 对称解码器 RSU4F/RSU4/RSU5/RSU6/RSU7 + 6 路 side conv (side1..side6) 输出 [sigmoid(d1)...sigmoid(d6)], 取 d1 为主输出。config.json: in_ch 3 out_ch 1 model_type SegformerForSemanticSegmentation architectures ["BriaRMBG"] auto_map MyConfig.RMBGConfig / briarmbg.BriaRMBG
  • 参数: 44,046,790 (~168.0 MB FP32, model.safetensors 168.2 MB, model.pth 168.5 MB, pytorch_model.bin 168.4 MB)
  • 输入: RGB 图像 np.uint8 [H,W,3] → preprocess_image: permute 2,0,1 → interpolate [1024,1024] → /255 → normalize(mean 0.5 std 1.0) → Tensor [1,3,1024,1024] float32
  • 输出: 6 个 sigmoid mask logits [1,1,1024,1024] + 6 层 hidden;取 result[0][0] 经 postprocess_image 双线性插值回原图尺寸、按 min-max 归一到 0-255 uint8
  • 许可: bria-rmbg-1.4 (source-available, non-commercial), 需表单申请;本仓使用已下载的公开权重做本地 NPU 验证
  • 官方示例: example_inference.py 使用 BriaRMBG.from_pretrained("briaai/RMBG-1.4") + preprocess_image + postprocess_image + putalpha

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910), 2 devices, HBM 64GB (free ~60.8GB), npu-smi 25.5.5, CANN 8.5.1
  • torch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04, transformers: 4.38.0, Python: 3.11.14, Pillow 11.3.0, numpy 1.26.4, safetensors 0.6.2
  • 验证命令: torch.npu.is_available()=True, torch.npu.get_device_name(0)=Ascend910_9362, torch.npu.set_device(0) → npu:0
  • 权重来源: 本地缓存 /opt/atomgit/.cache/huggingface/hub/models--briaai--RMBG-1.4/snapshots/2ceba5a5efaec153162aedea169f76caf9b46cf8 (config.json + model.safetensors 168.2 MB, 798 keys) — snapshot_download 已完整获取 20 文件
  • 测试图像:
    • 真实图: example_input.jpg (933×1405, 随权重一同发布) — 用于端到端 NPU 推理
    • 合成图: 固定 seed0 1024×1024 随机 uint8 — 用于 CPU/NPU 单样本 smoke consistency

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# torchvision==0.24.0
# transformers==4.38.0
# safetensors==0.6.2
# Pillow==11.3.0
# numpy==1.26.4
# scikit-image==0.25.2

4. NPU 推理

默认命令(真实 NPU, npu:0, FP32, 同步计时):

python inference.py
# 或显式
python inference.py --mode inference
python inference.py --mode validate   # 仅 CPU/NPU 一致性校验,exit 0/1
python inference.py --mode benchmark  # 仅性能压测
  • 脚本自动 torch.npu.set_device(0),从本地目录通过 safetensors.torch.load_file 加载权重并 .to("npu:0"),校验首参数 device npu:0 dtype torch.float32
  • 修复 briarmbg.py 中 from .MyConfig import RMBGConfig 相对导入为绝对导入(本地审查 MyConfig.py 无网络/写文件行为,仅定义 RMBGConfig)
  • 关键区间前后调用 torch.npu.synchronize() 计时,异常时返回非零退出码
  • 输入尺寸默认 1024,可通过 --input-size 调整;阈值 --threshold 127 用于二值化统计

5. 真实推理结果

以下为本次在 npu:0 上真实执行 python inference.py 的完整输出(关键行截取,详见 logs/inference.log 与 assets/model_result.png):

======================================================================
briaai/RMBG-1.4 Ascend NPU Inference
Task: image segmentation / background removal (BriaRMBG BiRefNet, 6 side outputs)
Architecture: RSU7/RSU6/RSU5/RSU4/RSU4F + 6 side conv, in_ch 3 out_ch 1
Backend: torch_npu, requested device npu:0, dtype FP32
Model dir: /opt/atomgit/.cache/huggingface/hub/models--briaai--RMBG-1.4/snapshots/2ceba5a5efaec153162aedea169f76caf9b46cf8
Mode: inference
======================================================================
[DEVICE] torch.npu.is_available()=True, device_count=2, device_name=Ascend910_9362
[DEVICE] target npu:0 set
[DEVICE] using device npu:0 (npu:0)
[MODEL] loading from .../snapshots/.../2ceba5a5efaec153162aedea169f76caf9b46cf8
[MODEL] loading safetensors .../model.safetensors (168.2 MB)
[MODEL] safetensors loaded, keys 798
[MODEL] first param device npu:0, dtype torch.float32
[MODEL] total params 44046790 ~ 168.0 MB FP32
[DEVICE] model param device npu:0, dtype torch.float32
[DATA] synthetic input orig (1024, 1024) -> model_input [1024, 1024], tensor torch.Size([1, 3, 1024, 1024]) dtype torch.float32 mean -0.0020
[DATA] loaded real image from .../example_input.jpg size 933x1405
[INFER] running NPU inference on real image...
[NPU] real result shape (1, 1, 1024, 1024) dtype torch.float32 device npu:0
[NPU] real latency 172.68 ms (single, with sync)
[NPU] real raw stats min 0.000000 max 0.999998 mean 0.084272 std 0.275416 sample [0,0,:5,0] [6.12e-05, 3.29e-05, 9.57e-06, 2.83e-06, 8.52e-07]
[NPU] postprocessed mask shape (1405, 933) dtype uint8 min 0 max 255 mean 21.41
[NPU] mask foreground >127: 110533/1310865 = 8.43%
[NPU] mask sample top-left 5x5 mean 0.0
[COMPARE] CPU vs NPU consistency on synthetic input (seed0 1024)
[COMPARE] cpu_model loaded
[CPU] synthetic result shape (1, 1, 1024, 1024) stats min 0.000003 max 0.997244 mean 0.302763
[NPU] synthetic result shape (1, 1, 1024, 1024) stats min 0.000003 max 0.997245 mean 0.302827
[COMPARE] raw sigmoid diff max_abs 0.002241 mean_abs 0.000131 all_finite True
[COMPARE] mask IoU @127 = 0.999273 cpu_fg 233696 npu_fg 233758 shape (1024, 1024)
[COMPARE] cpu mask mean 76.92 npu mask mean 76.93 diff mean_abs 0.0335
[CHECK] IoU >=0.99 ? True (actual 0.999273)
[CHECK] max_abs <0.01 ? True (actual 0.002241)
[OVERALL] CPU-NPU consistency PASS=True (smoke single sample, not full dataset)
[BENCH] warmup 3, iters 10, input [1024, 1024]
[BENCH] compile/first included in warmup, stable avg 21.76 ms min 21.69 max 21.82 p50 21.76 p90 21.81 p95 21.81 batch 1 dtype FP32 throughput 45.96 images/s
[MEM] NPU HBM used 1893 MB free 60847 MB total 62740 MB
[PASS] briaai/RMBG-1.4 NPU inference successful on npu:0 device_name Ascend910_9362
  • 真实图掩码: example_input.jpg 933×1405 → NPU mask 1405×933 uint8 均值 21.41,前景 8.43%,已验证可 putalpha 生成去背景图
  • 合成图一致性: 同输入同权重 CPU vs NPU raw sigmoid 误差极低,mask IoU 0.999,证明 NPU 计算正确

6. CPU-NPU 一致性验证

  • 输入: 固定合成图 seed0 1024×1024,经 preprocess_image 归一化后 Tensor [1,3,1024,1024] float32 mean -0.0020
  • 模型: 同权重 FP32,CPU (cpu) vs NPU (npu:0),权重均来自 model.safetensors 798 keys
  • 对比对象:
    • raw 预测 sigmoid 输出 [1,1,1024,1024] 逐元素 abs diff
    • 后处理后 uint8 mask [1024,1024] 二值化 @127 的 IoU
  • 指标:
    • raw max_abs 0.002241, mean_abs 0.000131, all finite True
    • mask IoU 0.999273 (cpu_fg 233696 vs npu_fg 233758),mask mean diff 0.0335 (76.92 vs 76.93)
    • 阈值:IoU ≥0.99 PASS, max_abs <0.01 PASS
  • 类型: 单样本 smoke consistency(非完整数据集 mIoU),证明 Ascend 算子融合与 CPU 逻辑一致;差值来源于浮点累加顺序差异,在允许范围内
  • 复现:
python inference.py --mode validate
# 或手动对比已保存的 npy(示例)
# python -c "import numpy as np; print(np.load('cpu_mask.npy').shape)"

7. 性能测试

  • 方法: torch.npu.synchronize() 前后计时,warmup 3 次,正式 10 次,batch 1, 1024×1024, FP32, npu:0
  • 结果 (本次真实执行,详见 model_result.png):
    • 首次/编译延迟: 172.68 ms(含真实图单次,含图编译与 HBM 分配)
    • 稳定推理: avg 21.76 ms min 21.69 max 21.82 p50 21.76 p90 21.81 p95 21.81
    • 吞吐: 45.96 images/s
    • 峰值显存: 1893 MB used (free 60847 MB / total 62740 MB)
    • 端到端含 preprocess + forward + postprocess 已计入 NPU real latency;纯 forward 稳定延迟如上
  • 说明: 未启用 torch.compile,未混合精度;FP32 已满足单卡 64GB 约束,切换 FP16 可进一步提速但本验证保持与官方 torch_dtype float32 一致

8. 自验证截图

  • assets/agent_workflow.png — 完整阶段日志(下载、侦察、加载、NPU 推理、验证、性能)
  • assets/npu_device_call.png — npu-smi info, torch.npu.is_available(), device_name, 模型参数 device npu:0 dtype float32
  • assets/model_result.png — python inference.py 真实任务输出、mask 统计、IoU 与性能、PASS/FAIL

9. 已知限制

  • 固定输入尺寸: 默认 1024×1024,与官方 model_input_size=[1024,1024] 一致;任意尺寸经双线性插值处理,但非 1024 可能影响边缘细节与性能
  • 单样本验证: 一致性为单合成图 smoke check,未做完整数据集(如 DUTS/BiRefNet 评测集)mIoU/MAE;mask 经 min-max 归一,非 raw logit 直接阈值
  • 精度: 保持 FP32,未测试 FP16/BF16;NPU 与 CPU 存在约 1e-3 级别误差,属 Ascend 融合算子正常范围
  • 依赖远程代码: 需本地审查 MyConfig.py/briarmbg.py 并修复相对导入后加载;未使用 trust_remote_code 隐式拉取
  • 去背景后处理: 本仓仅输出 mask,putalpha 合成需用户自行用 PIL 完成(同 example_inference.py)
  • 显存: 峰值 ~1.9GB,远低于 64GB,单卡可批量但未测试 batch>1

10. 标签

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