BiRefNet(Bilateral Reference Network)是面向高分辨率图像分割(图抠像 / 背景移除 / 显著目标分割)的双边参考网络。本仓将官方权重 ZhengPeng7/BiRefNet(约 2.2 亿参数,Swin-v1-L 骨干 + 含 ASPPDeformable 的解码器)真实适配到单卡昇腾 Ascend910B NPU 上完成推理。
ZhengPeng7/BiRefNet(model.safetensors,约 424 MiB,commit e2bf8e4)trust_remote_code=True(模型自带自定义网络代码)| 项目 | 值 |
|---|---|
| NPU | Ascend910(npu-smi 可见 2 卡,本任务使用 npu:0) |
| NPU 驱动/固件 | npu-smi 25.5.5 |
| CANN | /usr/local/Ascend/cann-8.5.1 |
| Python | 3.11.14 |
| torch | 2.9.0+cpu(+torch_npu 插件) |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| transformers | 5.7.0 |
| timm / kornia | 1.0.28 / 0.8.3 |
pip install torch==2.9.0 torchvision --index-url https://download.pytorch.org/whl/cpu
pip install torch-npu==2.9.0.post1 # 与 CANN 版本配套
pip install -r requirements.txt若
pip install torch时从默认源拉取到的 torch 带 CUDA 依赖,可使用--extra-index-url或换用 CPU 版 torch;torch_npu必须以与 CANN/torch 匹配的版本安装。
模型权重默认从 models/BiRefNet 本地目录加载;若不存在会自动从 hf-mirror.com 下载 ZhengPeng7/BiRefNet 快照(需外网可访问镜像)。
python inference.py # 默认执行一次 NPU 推理
python inference.py --mode validate # CPU-NPU mask 一致性验证
python inference.py --mode benchmark # NPU 延迟基准
python inference.py --input /path/to/img.jpg # 自定义输入inference.py 在加载模型前会为 torchvision.ops.deform_conv2d 打补丁,将其重定向到 CANN 原生 torch_npu.npu_deformable_conv2d(该算子在 Ascend 上默认回退 CPU,会造成数十倍性能损失)。补丁逻辑在脚本内的 npu_deform_conv2d() 中实现。
固定输入:sample_input.jpg(800×533 真实 RGB 照片),模型内部缩放到 1024×1024 后推理。
[env] device : npu:0 (Ascend910_9362)
[model] name : ZhengPeng7/BiRefNet
[model] task : image-segmentation / background-removal
[model] params : 220.2 M
[input] tensor : (1, 3, 1024, 1024) torch.float32
[result] mask shape : (1024, 1024)
[result] mask saved : output/mask.png
[result] fg ratio : 0.6088 (pixels > 0.5)
[result] sig range : [0.0000, 1.0000]
[result] latency : 0.315 s / image (single forward, warm)
[PASS] BiRefNet ran real segmentation on npu:0前景比例 60.88%:模型成功将图像中的显著前景从背景中分割出来。
同一固定输入、FP32,CPU 与 npu:0 分别推理,按 0.5 阈值二值化后计算 mask IoU:
[validate] mask IoU (thr=0.5) : 0.999991
[validate] sigmoid mean abs err: 0.000138
[validate] sigmoid max abs err : 0.035592
[validate] cpu fg ratio : 0.608760
[validate] npu fg ratio : 0.608754
[PASS] CPU-NPU consistency (IoU=0.999991)说明:本项为单固定样例的一致性验证(smoke consistency),不是完整数据集评测。mask IoU ≥ 0.99,满足一致性验收要求。
1024×1024 输入、FP32、npu:0,预热 3 次后正式测试 10 次:
[benchmark] warmup=3 iters=10 dtype=torch.float32 input=(1, 3, 1024, 1024)
[benchmark] avg: 0.314 s min: 0.314 s max: 0.314 s
[benchmark] p50: 0.314 s p90: 0.314 s p95: 0.314 s
[benchmark] throughput: 3.182 images/storch.npu.max_memory_allocated,单卡 HBM 61.3 GiB,占用约 5.4%)torchvision.ops.deform_conv2d 在 Ascend 上默认回退 CPU。本仓通过 CANN torch_npu.npu_deformable_conv2d 原生实现该算子,单次推理约 0.314s。assets/agent_workflow.png:适配执行流程(下载 → 侦察 → 加载 → NPU 推理 → 验证 → 性能 → 整理)assets/npu_device_call.png:npu-smi info、torch.npu.is_available()、设备名与模型参数设备/类型日志assets/model_result.png:python inference.py 真实输出(任务结果 + 性能 + PASS)trust_remote_code=True(自定义网络代码 birefnet.py、BiRefNet_config.py);已审查本地自定义代码,无网络外联/动态执行/写文件等高危行为,仅在加载时实例化模型网络结构。hf-mirror.com 下载官方 ZhengPeng7/BiRefNet 快照(首次运行需要外网)。NPU、Ascend、Ascend910、Hardware: NPU、image-segmentation、background-removal、computer-vision