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/插值逻辑,无网络请求或文件写入resize 1024x1024 + divide 255 + normalize mean 0.5 std 1.0, 输出 HxW 二值化掩码[sigmoid(d1)...sigmoid(d6)] + 6 层 hidden, 主输出 d1 为最终掩码torch.npu.is_available()=True, device_count=2/opt/atomgit/hf_cache/briaai/RMBG-1___4 (commit 30e7e83f526e109d2e849a678f2dcf90ec966af8)example_input.jpg (933x1405 RGB, 来自官方仓库), 缩放至 1024x1024pip 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# 默认执行 (自动完成 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:0F.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执行命令: 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.0843example_input.jpg (933x1405) -> 预处理 1x3x1024x1024mask_min=0, mask_max=255, mean=21.4, std~, 前景像素 110525 (8.43%), 与 CPU 差异 2 像素min ~0.0, max ~1.0, mean ~0.08, CPU-NPU 最大绝对差 0.00186cpu 与 npu:0 执行, 阈值 127 二值化后计算 IoU, 并计算 0-255 掩码 MAEnpu:0 (验证 next(model.parameters()).device == npu:0, conv_in.weight.device == npu:0)条件: 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)| 设备 | avg | min | max | p50 | p90 | p95 | throughput |
|---|---|---|---|---|---|---|---|
| NPU (Ascend910) | 24.32 ms | 22.1 ms | 26.3 ms | 24.44 ms | 25.47 ms | ~25.8 ms | 41.1 imgs/s |
| CPU | 7983 ms | - | - | - | - | - | 0.125 imgs/s |
npu-smi HBM 9457 MB (基线 3110 MB, 增量 ~6347 MB, 含权重 168 MB + 激活/临时), torch.npu.mem_get_info free 58742 MB / total 65787 MBassets/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/FAILexample_input.jpg) 一致性, 非大规模数据集 mIoU 评测model.safetensorsutilities.py 原文件缺失 (0 bytes), 本仓自实现预处理/后处理, 逻辑与官方 MyPipe.py 一致model.half() 但需重新验证 IoUtrust_remote_code=True, 已审查无风险但仍需信任Hardware: NPU, NPU, Ascend, Ascend910, Ascend910B, image-segmentation, background-removal, BriaRMBG, IS-Net