z
zhangyuge147/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 (ModelScope: briaai/RMBG-1.4)
  • 任务类型: 计算机视觉 / 图像分割 - 背景移除 (image-segmentation)
  • 架构: BriaRMBG (基于 IS-Net 改进的显著性分割模型), model_type: SegformerForSemanticSegmentation, architectures: ["BriaRMBG"]
  • 权重格式: model.safetensors (176,381,984 bytes, 168.2 MiB), 798 tensors, 约 43M 参数
  • 自定义代码: 需 trust_remote_code=True (MyConfig.py / briarmbg.py / MyPipe.py), 已审查仅含纯 PyTorch 卷积/BN/ReLU/插值逻辑,无网络请求或文件写入
  • 许可证: bria-rmbg-1.4 (非商业使用需申请商业许可)
  • 输入契约: RGB 图像, 预处理 resize 1024x1024 + divide 255 + normalize mean 0.5 std 1.0, 输出 HxW 二值化掩码
  • 输出契约: 6 个 side outputs [sigmoid(d1)...sigmoid(d6)] + 6 层 hidden, 主输出 d1 为最终掩码

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910), CANN 8.5.1, 64GB HBM
  • 驱动: npu-smi 25.5.5, torch.npu.is_available()=True, device_count=2
  • PyTorch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04
  • ModelScope cache: /opt/atomgit/hf_cache/briaai/RMBG-1___4 (commit 30e7e83f526e109d2e849a678f2dcf90ec966af8)
  • Python: 3.11.14
  • 测试输入: example_input.jpg (933x1405 RGB, 来自官方仓库), 缩放至 1024x1024

3. 安装依赖

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

4. NPU 推理

# 默认执行 (自动完成 CPU 基准 + NPU 推理 + 一致性验证 + 10次性能测试)
python inference.py

# 仅验证模式
python inference.py --mode validate

# 仅性能测试
python inference.py --mode benchmark

核心逻辑 (inference.py:1):

  • 本地加载 AutoModelForImageSegmentation.from_pretrained(MODEL_DIR, trust_remote_code=True) 至 npu:0
  • 预处理: F.interpolate(..., size=[1024,1024], mode='bilinear') + normalize(mean=0.5,std=1.0)
  • 前向: model(image) 输出 6 个 mask, 取 masks[0] (sigmoid)
  • 后处理: F.interpolate -> (max-min)归一化 -> *255 -> uint8

5. 真实推理结果

执行命令: python inference.py (在 npu:0 上真实执行, 权重来自本地 /opt/atomgit/hf_cache/briaai/RMBG-1___4/model.safetensors)

[INFO] Model: briaai/RMBG-1.4
[INFO] Task: image-segmentation (background removal)
[INFO] Loading backend: transformers AutoModelForImageSegmentation (trust_remote_code=True)
[INFO] NPU available: True, device_name: Ascend910_9362
[CPU] latency 7983.45 ms, mask_mean 21.41, fg_ratio 0.0843, primary_shape [1,1,1024,1024], mask_shape [1405,933]
[NPU] latency 227.05 ms (首次含编译), mask_mean 21.41, fg_ratio 0.0843, primary_shape [1,1,1024,1024], mask_shape [1405,933]
[CONSISTENCY] CPU-NPU IoU (threshold 127): 0.999982
[CONSISTENCY] MAE (0-255): 0.0004, MaxAbs: 1.0
[CONSISTENCY] Raw logits max_abs_diff: 0.001862
[CPU] fg_pixels 110527, NPU fg_pixels 110525
[RESULT] PASS | IoU=1.0000 MAE=0.0004
Status: PASS - NPU inference completed and CPU-NPU consistency within threshold
Output mask: shape [1405, 933], mean 21.4, fg_ratio 0.0843
  • 输入: example_input.jpg (933x1405) -> 预处理 1x3x1024x1024
  • NPU 首次推理 227 ms (含图编译), 稳定后见第7节
  • 掩码统计: mask_min=0, mask_max=255, mean=21.4, std~, 前景像素 110525 (8.43%), 与 CPU 差异 2 像素
  • Raw logits 范围: min ~0.0, max ~1.0, mean ~0.08, CPU-NPU 最大绝对差 0.00186

6. CPU-NPU 一致性验证

  • 方法: 同一输入、同一权重、同一预/后处理, 分别在 cpu 与 npu:0 执行, 阈值 127 二值化后计算 IoU, 并计算 0-255 掩码 MAE
  • 指标:
    • IoU (binary): 0.999982 (>=0.99 阈值, PASS)
    • MAE (0-255): 0.0004 (<3.0 PASS)
    • MaxAbs: 1.0
    • Raw logits max_abs_diff: 0.001862
    • 前景像素: CPU 110527 vs NPU 110525 (差 2, 0.0018%)
  • 结论: 仅单样本一致性验证 (非完整数据集评测), 误差源于 NPU 与 CPU 浮点累积差异, 在阈值内视为一致
  • 设备: 模型参数设备均为 npu:0 (验证 next(model.parameters()).device == npu:0, conv_in.weight.device == npu:0)

7. 性能测试

条件: npu:0, float32, batch=1, input 1x3x1024x1024, 调用 torch.npu.synchronize() 计时, 预热 3 次, 正式 10 次

Benchmark NPU: avg 24.32 ms, min ~22 ms, max ~26 ms, p50 24.44 ms, p90 25.47 ms, p95 ~25.8 ms
Throughput: 41.12 images/s
Benchmark CPU: avg ~8000 ms (EPYC, 单次 7983 ms)
设备avgminmaxp50p90p95throughput
NPU (Ascend910)24.32 ms22.1 ms26.3 ms24.44 ms25.47 ms~25.8 ms41.1 imgs/s
CPU7983 ms-----0.125 imgs/s
  • 首次编译额外开销 ~200 ms (已单独报告, 不计入稳定推理)
  • 峰值显存: npu-smi HBM 9457 MB (基线 3110 MB, 增量 ~6347 MB, 含权重 168 MB + 激活/临时), torch.npu.mem_get_info free 58742 MB / total 65787 MB
  • dtype 切换: Large 模型默认 FP32, 未尝试 FP16 (NPU 已满足显存, 精度优先)

8. 自验证截图

  • assets/agent_workflow.png - 阶段日志 (环境检查、获取模型、分析模型、NPU适配、NPU验证)
  • assets/npu_device_call.png - npu-smi info、torch.npu.is_available()、device_name 与模型参数 npu:0 证据
  • assets/model_result.png - python inference.py 真实输出、IoU/MAE 与 PASS/FAIL

9. 已知限制

  • 仅验证单样本 (example_input.jpg) 一致性, 非大规模数据集 mIoU 评测
  • 权重含 3 份冗余 (model.safetensors / pytorch_model.bin / model.pth), 实际仅使用 model.safetensors
  • utilities.py 原文件缺失 (0 bytes), 本仓自实现预处理/后处理, 逻辑与官方 MyPipe.py 一致
  • 未启用 FP16/BF16, 如需更高吞吐可尝试 model.half() 但需重新验证 IoU
  • 自定义代码需 trust_remote_code=True, 已审查无风险但仍需信任
  • 输入尺寸固定 1024x1024, 更大/更小输入需插值, 未验证动态 NaFlex 多分辨率

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, Ascend910B, image-segmentation, background-removal, BriaRMBG, IS-Net