briaai/RMBG-1.4BriaRMBG (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.BriaRMBGmodel.safetensors 168.2 MB, model.pth 168.5 MB, pytorch_model.bin 168.4 MB)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] float32sigmoid mask logits [1,1,1024,1024] + 6 层 hidden;取 result[0][0] 经 postprocess_image 双线性插值回原图尺寸、按 min-max 归一到 0-255 uint8bria-rmbg-1.4 (source-available, non-commercial), 需表单申请;本仓使用已下载的公开权重做本地 NPU 验证example_inference.py 使用 BriaRMBG.from_pretrained("briaai/RMBG-1.4") + preprocess_image + postprocess_image + putalphaAscend910_9362 (Ascend910), 2 devices, HBM 64GB (free ~60.8GB), npu-smi 25.5.5, CANN 8.5.12.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.2torch.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 consistencypip 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默认命令(真实 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.float32briarmbg.py 中 from .MyConfig import RMBGConfig 相对导入为绝对导入(本地审查 MyConfig.py 无网络/写文件行为,仅定义 RMBGConfig)torch.npu.synchronize() 计时,异常时返回非零退出码1024,可通过 --input-size 调整;阈值 --threshold 127 用于二值化统计以下为本次在 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_9362example_input.jpg 933×1405 → NPU mask 1405×933 uint8 均值 21.41,前景 8.43%,已验证可 putalpha 生成去背景图seed0 1024×1024,经 preprocess_image 归一化后 Tensor [1,3,1024,1024] float32 mean -0.0020cpu) vs NPU (npu:0),权重均来自 model.safetensors 798 keyssigmoid 输出 [1,1,1024,1024] 逐元素 abs diffuint8 mask [1024,1024] 二值化 @127 的 IoUpython inference.py --mode validate
# 或手动对比已保存的 npy(示例)
# python -c "import numpy as np; print(np.load('cpu_mask.npy').shape)"torch.npu.synchronize() 前后计时,warmup 3 次,正式 10 次,batch 1, 1024×1024, FP32, npu:0model_result.png):
preprocess + forward + postprocess 已计入 NPU real latency;纯 forward 稳定延迟如上torch.compile,未混合精度;FP32 已满足单卡 64GB 约束,切换 FP16 可进一步提速但本验证保持与官方 torch_dtype float32 一致assets/agent_workflow.png — 完整阶段日志(下载、侦察、加载、NPU 推理、验证、性能)assets/npu_device_call.png — npu-smi info, torch.npu.is_available(), device_name, 模型参数 device npu:0 dtype float32assets/model_result.png — python inference.py 真实任务输出、mask 统计、IoU 与性能、PASS/FAIL1024×1024,与官方 model_input_size=[1024,1024] 一致;任意尺寸经双线性插值处理,但非 1024 可能影响边缘细节与性能min-max 归一,非 raw logit 直接阈值MyConfig.py/briarmbg.py 并修复相对导入后加载;未使用 trust_remote_code 隐式拉取putalpha 合成需用户自行用 PIL 完成(同 example_inference.py)Hardware: NPU, NPU, Ascend, Ascend910, image-segmentation, background-removal, BiRefNet, bria, RMBG, segmentation