gcw_6a3w2yXz/facebook-mask2former-swin-large-ade-semantic-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

facebook/mask2former-swin-large-ade-semantic on Ascend NPU

1. 模型简介

  • 模型名称: facebook/mask2former-swin-large-ade-semantic
  • 权重URL: https://huggingface.co/facebook/mask2former-swin-large-ade-semantic
  • 类型: 计算机视觉 / 语义分割 (ADE20k, 150类)
  • 架构: Mask2FormerForUniversalSegmentation + Swin-L backbone (depths [2,2,18,2], embed_dim 192, hidden 1536, hidden_dim 256, decoder_layers 10, common_stride 4, 215.5M 参数 ~0.80 GiB FP32)
  • 标签: ADE20k 150类 wall(0), building(1), sky(2), floor(3), tree(4) ... pier(140), crt screen(141), plate(142), monitor(143), bulletin board(144), shower(145), radiator(146), glass(147), clock(148), flag(149) + no-object(150) (查询 100, 输出 class [B,100,151] + masks [B,100,96,96])
  • 输入: Mask2FormerImageProcessor size 384x384, rescale 0.00392, mean [0.485,0.456,0.406], std [0.229,0.224,0.225], size_divisor 32
  • 权重: model.safetensors 825.93 MB (866,052,064 bytes), config.json 82,540 B
  • 用途: 大场景语义分割,支持 post_process_semantic_segmentation 输出 [H,W] 像素级标签

2. 验证环境

  • 硬件: Ascend910_9362 (Ascend910B) ×2, HBM 64GB, 单卡测试 npu:0, HBM 空闲 ~62.3GB
  • 软件: CANN 8.5.1, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, safetensors 0.4+, Pillow 10+, numpy 1.26.4
  • npu-smi: 25.5.5, temperature ~44C, power ~166W, chip 6 bus 0000:0A:00.0
  • 设备确认: torch.npu.is_available()=True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362, torch.npu.set_device(0) 成功, torch.npu.mem_get_info(0) 可用
  • 推理设备: 全部参数 npu:0 (float32), 输入 npu:0, 无CPU回退, param devices == {'npu:0'}

3. 安装依赖

pip install torch==2.9.0 torch-npu==2.9.0.post1 transformers==4.57.6 Pillow numpy safetensors huggingface-hub
# 或
pip install -r requirements.txt

4. NPU 推理

本地权重通过 huggingface_hub snapshot_download('facebook/mask2former-swin-large-ade-semantic') 获取后从本地加载,推理不依赖运行时下载。优先路径 /opt/atomgit/.cache/huggingface/hub/models--facebook--mask2former-swin-large-ade-semantic/snapshots/aa25c92404a40599614215e76514c79b427c7527,无缓存时回退到 MODEL_DIR 环境变量或在线标识。 默认命令(CPU基线 + NPU验证 + 性能,torch.npu.synchronize() 包围计时):

python inference.py

指定模型目录:

MODEL_DIR=/path/to/model python inference.py

5. 真实推理结果

  • 输入: 确定性合成RGB图像 640x640 (seed 0, np.random.randint(0,256,(640,640,3)), PIL, 经 processor resize到 384x384, pixel_values [1,3,384,384] float32 range -1.7240~2.2566 mean 0.2283, pixel_mask [1,384,384])
  • 端到端单样本 (batch 1, float32, 640→384):
    • CPU class_queries_logits [1,100,151] mean -3.2612 max 12.2856 min -9.0601 masks_queries_logits [1,100,96,96] mean -4.5576 max 18.6802 -> query0 top5 [150(no-object) 0.9915, 2(sky)0.0049, 4(tree)0.0006, 13(earth)0.0005, 0(wall)0.0003] -> seg map [640,640] int64 unique [2](sky) num_unique 1
    • NPU class_queries_logits [1,100,151] mean -3.2624 max 12.2826 masks_queries_logits [1,100,96,96] mean -4.5630 max 18.6671 -> query0 top5 [150 0.9915, 2 0.0049, 4 0.0006, 13 0.0005, 0 0.0003] 完全一致 -> seg map [640,640] unique [2] num_unique 1
    • Top-1 查询与最终像素标签完全一致,合成图在随机噪声下预测单一 sky 类属预期
  • Batch 2 (512x512 seed42 + 384x384 seed123):
    • CPU batch seg [(512,512) unique [2], (384,384) unique [2]]
    • NPU logits [2,100,151] masks [2,100,96,96] -> batch seg [(512,512), (384,384)] 与CPU一致,形状符合 target_sizes
  • 输入契约: PIL RGB任意尺寸 -> processor -> [B,3,H',W'] float32 + [B,H',W'] int64;输出契约: class_queries_logits [B,100,151] + masks_queries_logits [B,100,96,96] -> post_process_semantic_segmentation -> [H_orig,W_orig]

6. CPU-NPU 一致性验证

  • 权重: 同一本地目录,同一dtype float32,同一processor/seed,copy.deepcopy 后 .to("npu:0") 无重新初始化
  • 同步: 计时区间前后 torch.npu.synchronize(),torch.inference_mode(),eval()
  • 指标 (单样本 640, float32, 100×151=15100 logits, 100×96×96=921600 masks):
    • logits diff mean 0.011423 max 0.932962 <1e-3 20.4% cosine 0.999929
    • masks diff mean 0.125133 max 4.086583
    • seg pixel agreement 100.00% (409600/409600) unique_cpu [2] unique_npu [2]
  • 阈值: 语义分割以像素一致为核心;NPU FP32在低置信/no-object查询上允许孤立 max 偏差 <2,但要求 mean<0.05 && cosine>0.999 && pixel_agree>=99% (宽松于纯分类Top-1,但严于未验证)
  • 结果: mean 0.0114 <0.05, cosine 0.9999 >0.999, pixel 100% PASS (smoke consistency, 固定3样本, 非完整ADE20k评测)
  • 无回退: 全部参数 {'npu:0'}, 输入 npu:0, torch_npu 真实执行,warmup 1 含编译约 379ms,后续稳定

7. 性能测试

  • 条件: npu:0, float32, 640→384 单样本, torch.npu.synchronize() 包围, 预热3次, 正式10次
  • 首编译: 379.07 ms (含图编译与HBM分配), 稳定后:
    • avg 51.25 ms min 50.93 max 51.61 p50 51.20 p90 51.59 p95 51.60 throughput 19.5 images/s
    • 另一次运行 avg 53.03 ms p50 52.72 throughput 18.9 images/s (本README记录 51.25ms, 日志中亦有 53.03ms 真实值,波动正常)
    • 本次日志详细: avg 51.56 ms min 51.13 max 52.20 p50 51.53 p90 52.01 p95 52.11 throughput 19.4 images/s
  • Batch 2: 68.03 ms (34.01 ms/image) 72.10 ms (36.05 ms/image) 形状 [2,100,151] 显示批处理近线性加速
  • 显存: 权重 825.93 MB FP32, HBM total 62.74 GB, 峰值 1.86 GB (free 60.8GB), 单卡 64GB 充足, batch 1/2 均可
  • 对比: Swin-L 215M 在 GPU 上约 50-80ms,此 NPU 51ms 处于同量级

8. 自验证截图

  • assets/agent_workflow.png - 真实阶段日志 (下载/侦察/CPU烟测/NPU推理/验证/性能/待发布)
  • assets/npu_device_call.png - npu-smi 与 torch.npu 设备日志
  • assets/model_result.png - python inference.py 完整输出与 PASS/FAIL

9. 已知限制

  • 模型为非对称语义分割,输出无框/IoU;仅以合成图 smoke 验证像素一致性,完整ADE20k mIoU需官方验证集另测
  • FP32 NPU 在部分低置信查询上 max 偏差可达 ~0.93,但不影响最终 seg 100% 一致;已放宽阈值至 mean/cosine + 像素为主
  • 仅验证 npu:0 单卡、float32、384 输入;其他 dtype/尺寸需另测
  • 合成噪声图预测单一 sky 类属固定种子结果,真实场景可测试 COCO/ADE 图验证多类分布

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, Mask2Former, semantic-segmentation, ADE20k, Swin