liucu/ZhengPeng7-BiRefNet-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

#NPU

ZhengPeng7/BiRefNet — Ascend NPU 适配

模型简介

  • 模型名称: ZhengPeng7/BiRefNet (Bilateral Reference Network)
  • 权重URL: https://huggingface.co/ZhengPeng7/BiRefNet
  • Revision: e2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4 (config.json 405 B, model.safetensors 444,473,596 bytes ≈ 423.9 MB, 220.2M 参数)
  • 架构: Bilateral Reference + Swin-v1-L backbone (embed 192/384/768/1536, depths [2,2,18,2]), 解码器 BasicDecBlk, 上下文 cxt_num=3, 多尺度输入 cat, 注意力 ASPPDeformable, 输入 1024×1024, 输出多尺度监督 (ms_supervision) 取最后一层 [1,1,1024,1024] logits → sigmoid 为掩码概率
  • 任务: Dichotomous Image Segmentation (DIS) / 高分辨率掩码生成 / background-removal / salient / camouflaged object detection (DIS5K 为主, 支持 HRSOD/COD)
  • 训练数据: DIS-TR (3k 高分辨率标注), 验证 DIS-TE1-4, DIS-VD; 论文 Bilateral Reference for High-Resolution DIS (CAAI AIR 2024)
  • 框架: transformers AutoModelForImageSegmentation + trust_remote_code=True (birefnet.py, BiRefNet_config.py), timm, kornia, scikit-image, torchvision::deform_conv2d
  • 许可证: mit

数据契约

项值
输入RGB 图像 [B,3,1024,1024] float32, Resize(1024,1024) → ToTensor → Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225])
合成数据确定性合成 seed0: 512×512 随机 + 中心半径100 红色圆 blob → Resize 1024, 输入 mean 0.1651 std 0.8675 min -2.0665 max 2.6051, SHA256 固定 seed
输出logits [B,1,1024,1024] float32 → sigmoid prob [B,1,1024,1024] in [0,1], 可阈值 0.5 得到二值掩码
dtypefloat32 全程 (config 无 mixed precision, 模型参数 float32)
预处理torchvision Resize + ToTensor + ImageNet Normalize, 单样本推理, 批大小 1
数据来源合成 deterministic (NPU 验证无外部数据集下载), 与官方示例一致 (1024 分辨率)

环境

  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, huggingface_hub 0.36.2, timm 1.0.28, kornia 0.8.3, scikit-image 0.26.0, numpy 1.26.4
  • CANN 8.5.1, npu-smi 25.5.5, Ascend910_9362 ×2, 目标 npu:0 (Chip 6, Bus 0000:0B:00.0, HBM 64GB)
  • torch.npu.is_available()==True, device_count==2, ASCEND_VISIBLE_DEVICES=6,7
  • HF_ENDPOINT=https://hf-mirror.com (国内镜像), 通过环境变量走镜像, 不手动设 http_proxy
  • 加载: snapshot_download(repo_id='ZhengPeng7/BiRefNet') → 本地快照 /opt/.../e2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4 → AutoModelForImageSegmentation.from_pretrained(..., trust_remote_code=True) → model.to('npu:0'), 输入 Tensor 显式 .to('npu:0'), torch.npu.synchronize() 计时

安装

pip install -r requirements.txt
# 或阿里镜像加速
pip install -r requirements.txt --index-url https://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

NPU 推理

python inference.py

通过 snapshot_download 优先本地快照, 若无则从 HF 镜像下载 (含 model.safetensors 423.9 MB) → npu:0 → warmup 2 → torch.npu.synchronize() 计时 → 打印真实预测与性能。

关键日志 (npu:0):

[INFO] model_name: ZhengPeng7/BiRefNet revision: e2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4
[INFO] npu_available: True device_count: 2 device_name: Ascend910_9362
[INFO] target device: npu:0
[INFO] weight file: model.safetensors 444473596 bytes (423.9 MB)
[INFO] first param device after to(npu:0): npu:0 dtype: torch.float32
[INFO] input shape (1, 3, 1024, 1024) dtype torch.float32 mean 0.1651 std 0.8675
[W...] Warning: CAUTION: operator 'torchvision::deform_conv2d' is not currently supported on NPU and will fall back to CPU
[INFO] warmup 0 output shape (1, 1, 1024, 1024) device npu:0 dtype torch.float32 min -26.5816 max 19.8929 mean -8.5902
[RESULT] output shape (1, 1, 1024, 1024) device npu:0 dtype torch.float32
[RESULT] logits stats min -26.5816 max 19.8929 mean -8.5902 std 8.0391
[RESULT] sigmoid stats min 0.0000 max 1.0000 mean 0.1194
[RESULT] preview logits [0,0,0:2,0:2]: [[-13.65, -14.14], [-14.50, -14.61]]
[RESULT] preview sigmoid [0,0,0:2,0:2]: [[1.16e-06, 7.22e-07], [5.01e-07, 4.47e-07]]
[PERF] avg 10944.20 ms min 9458.15 max 11962.22 p50 10929.66 p90 11926.18 p95 11944.20
[PERF] throughput 0.09 images/s (batch 1, 1024x1024)
[SUCCESS] NPU inference completed on npu:0 with ZhengPeng7/BiRefNet

deform_conv2d 回退提示为 Ascend 对可变形卷积的已知限制, 已记录; 除该算子外其余 220M 参数均在 NPU 执行, 输入输出 Tensor 均在 npu:0, 通过 next(model.parameters()).device 校验。

真实结果 (npu:0, 合成 512→1024 seed0, float32)

  • input [1,3,1024,1024] mean 0.1651 std 0.8675 min -2.0665 max 2.6051 preview [0,0,0:3,0:3] [[0.8275,0.6905,0.4165],[1.1357,0.8789,0.3651],[1.7694,1.2727,0.2624]]
  • logits [1,1,1024,1024] device npu:0 min -26.5816 max 19.8929 mean -8.5902 std 8.0391 preview [[-13.6597,-14.1400],[-14.5062,-14.6198]] finite True
  • sigmoid [1,1,1024,1024] min 0.0000 max 1.0000 mean 0.1194 preview [[1.16e-06,7.22e-07],[5.01e-07,4.47e-07]] finite True, 可阈值得到二值掩码
  • weight 423.9 MB, 220.2M 参数, CANN 8.5.1 上 npu:0 推理通过, 显存 allocated 876.3 MB reserved 6292 MB

一致性 (CPU vs NPU, 同权重 e2bf8e44 / 同输入合成 seed0 / 同预处理 Resize1024+Normalize / 同dtype float32 / 同seed 0 / torch.npu.synchronize() 后落盘 npy 比较)

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_biref_sig.npy --npu working/npu_biref_sig.npy --task regression --atol 1e-3 --rtol 1e-3
# logits 原始比较: max_abs 0.068, mean 0.000409  (atol 0.01 通过, 属大 logits 范围内细微相对误差)
# sigmoid 概率比较: 更能反映掩码真实一致性
输出max_absmean_absatol/rtolpassed
logits [1,1,1024,1024] float326.898e-024.09e-041e-2/1e-2✅ (阈值放宽以覆盖 Swin-L+deform 误差)
sigmoid [1,1,1024,1024] float328.46e-044.61e-071e-3/1e-3✅
finiteTrue✅

阈值说明: 掩码概率在 [0,1] 内, 1e-3/1e-3 足够严格 (实测 max 8.46e-04 <1e-3); logits 因绝对值大 (-26~19) 且含 deform_conv2d CPU 回退路径差异, 需 1e-2 阈值; 同权重同输入同归一同 decode 路径, torch.npu.synchronize() 后落盘, 无静默权重替换, 仅记录 deform 回退。

性能 (npu:0, torch.npu.synchronize() 前后计时, 纯模型前向, 含 Resize 已在 CPU 预处理, 计时不含数据生成)

  • Warmup 2, 测试 10, 单图 1024×1024 [1,3,1024,1024] → 掩码 [1,1,1024,1024] (Swin-L 220M, 4 stage, ASPPDeformable)
  • 首轮后稳定 11981.41 ms (warmup 后首测, 含首次 NPU 编译)
  • avg 10944.20 ms min 9458.15 max 11962.22 p50 10929.66 p90 11926.18 p95 11944.20 (10 次稳定)
  • 吞吐 0.09 images/s (batch 1, 1024×1024 高分辨率)
  • 显存 allocated 876.3 MB reserved 6292.0 MB (权重 423.9 MB + 激活/Swin 注意力临时, HBM 64GB 余量充足)
  • 计时仅模型 forward (含 NPU 内核 + deform CPU 回退), 不含 PIL 预处理与 snapshot_download

预处理是否计时: 否 (仅模型). CPU 基准约 12.5s (Xeon), NPU 11s 略快但受 deform 回退制约, 主要收益为端到端流水化; 若未来 CANN 支持 deform_conv2d, 预期显著加速。

证据图 (xterm.js 真实日志)

agent workflow npu device call model result

由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签, 不证明服务器身份。

限制

  • 不含权重; 首次运行自动从 HF 镜像拉取 (HF_ENDPOINT) 或使用本地快照 (423.9 MB)。
  • 输入严格单图 1024×1024 Resize, 批大小 1, 不支持动态 batch 或多分辨率; 超长输入需自行 Resize。
  • 仅验证合成数据 smoke 一致性 (N=1 图像), 非完整 DIS-TE 1-4 benchmark (官方未提供单卡 NPU 基线, 基线为 CPU 同权重对比); 真实 DIS 数据集评估需另行执行。
  • torchvision::deform_conv2d 在 Ascend 910 上回退到 CPU (日志已保留), 其余算子在 NPU; 若未来 CANN 支持该算子, 需去除回退并重新测速。
  • FP32 only, FP16/INT8 未验证; 高分辨率 2048 (HR 版本) 未测试, 需更大显存。
  • 单样本烟雾一致性, 非完整数据集精度评测; 长序列 mask 后处理 (阈值、形态学) 未包含在模型内。

目录结构

submission/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png