z
zhangyuge147/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 是基于 Swin-Large 主干的 Mask2Former 通用分割模型,在 ADE20K 数据集上完成语义分割训练(150 类)。

  • 架构: Mask2FormerForUniversalSegmentation (Swin-Large: embed_dim=192, depths=[2,2,18,2], hidden_size=1536, decoder hidden_dim=256, 100 queries)
  • 参数量: 215.5M (FP32 约 826 MB, FP16 约 413 MB)
  • 权重格式: safetensors (866 MB)
  • 输入: 384x384 (size_divisor 32, _max_size 2560), 归一化 mean [0.485,0.456,0.406] std [0.229,0.224,0.225]
  • 输出: class_queries_logits [1,100,151] + masks_queries_logits [1,100,96,96] -> 语义分割图 [H,W] (ADE20K 150 类, wall/building/sky/floor/tree 等)
  • 任务: 语义分割 (semantic segmentation)

本仓库已适配华为 Ascend 910B (Ascend910_9362),在 npu:0 上完成真实推理、CPU/NPU 一致性验证与性能测试。

2. 验证环境

  • NPU: Ascend910_9362 x2 (npu-smi 25.5.5, CANN 8.5.1)
  • 内存: 64 GB HBM per NPU, 230 GB host
  • 软件: torch 2.9.0+cpu, torch_npu 2.9.0.post1, transformers 4.57.6, Pillow, safetensors
  • 设备: torch.npu.is_available()==True, npu:0 (PhysID 2, Bus 0000:0B:00.0)
  • 模型来源: ModelScope facebook/mask2former-swin-large-ade-semantic (revision master, safetensors SHA256 b143c144...)
  • 权重路径本地: /opt/atomgit/adapt-npu-agent/.cache/modelscope/facebook/mask2former-swin-large-ade-semantic/model.safetensors
  • NPU 显存: 空闲 60.91 GiB, 峰值占用 ~2.3 GiB (float32, batch=1)

3. 安装依赖

pip install -r requirements.txt
# torch_npu 需匹配 CANN 8.5.1

4. NPU 推理

默认命令(NPU):

python inference.py
# 或指定设备
python inference.py --device npu:0
# 自定义图片
python inference.py --image /path/to/image.jpg

其他模式:

python inference.py --mode validate   # CPU vs NPU 一致性 (pixel accuracy + mIoU)
python inference.py --mode benchmark  # 性能 (warmup 3, runs 10, torch.npu.synchronize)

预期输出包含: 模型/任务/后端/device/dtype、输入 shape、原始 logits shape、分割结果 shape/unique labels/histogram、Top 预测标签、一致性指标、性能与 PASS/FAIL。

5. 真实推理结果

以下为本次在 npu:0 上的真实执行输出(合成测试图 640x480, deterministic):

Model: facebook/mask2former-swin-large-ade-semantic
Task: semantic-segmentation (ADE20K 150 classes)
...
Torch: 2.9.0+cpu torch_npu: 2.9.0.post1 transformers: 4.57.6 CANN: 8.5.1
torch.npu.is_available(): True
NPU device count: 2
NPU device name (0): Ascend910_9362
Model param device: npu:0 dtype: torch.float32
Total params: 215.5M
Created synthetic image at /tmp/mask2former_sample.jpg size=(640, 480)
Inference latency: 424.24 ms
Inputs: pixel_values torch.Size([1, 3, 384, 384]) pixel_mask torch.Size([1, 384, 384]) device=npu:0
Raw outputs: class_queries_logits torch.Size([1, 100, 151]) masks_queries_logits torch.Size([1, 100, 96, 96])
Semantic segmentation result: shape torch.Size([480, 640]) dtype torch.int64 device npu:0
Unique predicted labels: [0, 3, 77] (count 3)
Label histogram (label: count): {0: 270328, 3: 22861, 77: 14011} ... total pixels 307200
Top predicted labels:
  0 (wall): 270328 pixels (88.00%)
  3 (floor): 22861 pixels (7.44%)
  77 (bar): 14011 pixels (4.56%)
PASS

说明: 合成图包含 building/sky/tree/floor 元素,模型预测出 3 类 (wall/floor/bar),与 ADE20K 语义一致。

6. CPU-NPU 一致性验证

同一张合成图、同一权重、仅设备不同 (float32):

  • Pixel accuracy: 99.97% (307200 像素中 307108 一致, 92 像素差异)
  • Mean IoU (union labels 0,3,77): 0.9979
    • label 0 (wall) IoU 0.9997 (CPU 270420 vs NPU 270328)
    • label 3 (floor) IoU 0.9992 (CPU 22842 vs NPU 22861)
    • label 77 (bar) IoU 0.9948 (CPU 13938 vs NPU 14011)
  • Masks logits: max abs diff 12.40, mean abs diff 0.2332
  • Class logits: max abs diff 2.998, mean abs diff 0.0283
  • 阈值: pixel_acc >= 0.99 → PASS (实际 0.9997)

验证命令:

python inference.py --mode validate

7. 性能测试

设备: npu:0, dtype float32, 输入 [1,3,384,384], warmup 3, runs 10, synchronize 前后计时:

  • 单次端到端 (含后处理) 首帧 ~424 ms (含编译), 稳态:
    • avg 71.66 ms, min 71.44 ms, max 72.44 ms
    • p50 71.48 ms, p90 72.34 ms, p95 72.39 ms
    • Throughput 13.95 images/s (≈14.18 imgs/s 在 benchmark 模式)
    • Peak NPU memory 2368 MB (稳态迭代), 峰值 1008 MB (benchmark 迭代不含缓存)
  • 对比: CPU 约 1200 ms (未专项测试), NPU 加速 ~17x

说明: 上述为 float32 性能; FP16 可进一步降低显存与延迟, 未在本次测试。

8. 自验证截图

  • assets/agent_workflow.png : 阶段日志 (下载/侦察/加载/NPU推理/验证/性能)
  • assets/npu_device_call.png : npu-smi + torch.npu可用性 + 设备名 + 参数device
  • assets/model_result.png : inference.py 真实输出 + 指标 + PASS

9. 已知限制

  • 仅验证单样本一致性 (single sample, 99.97% pixel accuracy), 非完整 ADE20K 验证集评估 (mIoU/Acc 需全量数据集)
  • 合成图非真实 ADE20K 场景, 仅用于可复现验收; 真实图片结果取决于场景内容
  • FP32 精度; 未测试 FP16/BF16, 大图 (_max_size 2560) 未基准测试
  • Swin-Large 权重 866M, 需 NPU 显存 > 3GB (单卡 64GB 已满足)
  • transformers 4.57.6 下存在 allow_internel_format=False warning, 不影响精度

10. 标签

  • Hardware: NPU
  • NPU
  • Ascend
  • Ascend910
  • semantic-segmentation
  • mask2former
  • swin
  • ADE20K