facebook/mask2former-swin-large-ade-semantichttps://huggingface.co/facebook/mask2former-swin-large-ade-semanticclass_queries_logits [1,100,151] + masks_queries_logits [1,100,96,96] -> post-process to semantic map [H,W] with 150 labels标签: #NPU, Ascend, Ascend910, Mask2Former, Swin, Segmentation, ADE20K, Semantic-Segmentation
pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.57.6
# Pillow>=10.0.0
# numpy>=1.24.0
# safetensors>=0.4.0
# huggingface-hub>=0.20.0# 默认推理 (自动下载权重到本地缓存并在 npu:0 推理)
python inference.py
# 一致性验证 (CPU vs NPU)
python inference.py --mode validate
# 性能测试 (10次)
python inference.py --mode benchmark模型加载: Mask2FormerForUniversalSegmentation.from_pretrained(MODEL_NAME).to("npu:0"), 显式移动输入到 npu:0, 调用 torch.npu.synchronize() 计时.
命令: python inference.py (合成随机图 384x384, seed 42)
[INFO] Model: facebook/mask2former-swin-large-ade-semantic
[INFO] Task: semantic segmentation (ADE20K 150 classes)
[INFO] Processor size: {'height': 384, 'width': 384}
torch.npu.is_available(): True
torch.npu.get_device_name(0): Ascend910_9362
torch.npu.device_count(): 2
Model class: Mask2FormerForUniversalSegmentation
First param device: npu:0
First param dtype: torch.float32
Device used: npu:0
pixel_values shape: torch.Size([1, 3, 384, 384]) dtype torch.float32
pixel_mask shape: torch.Size([1, 384, 384])
[RESULT] NPU inference timings (ms) over 10 runs:
avg 57.09 min 55.99 max 58.30 p50 57.33 p90 58.30 p95 58.30
throughput: 17.52 images/s
[RESULT] Raw outputs:
class_queries_logits: (1, 100, 151) dtype torch.float32 device npu:0
masks_queries_logits: (1, 100, 96, 96) dtype torch.float32 device npu:0
class_queries_logits mean -2.6955 std 2.4021 min -8.5578 max 10.6803
masks_queries_logits mean -9.8498 std 11.1421 min -70.1112 max 15.3067
[RESULT] Semantic segmentation map:
shape: (384, 384) dtype torch.int64 unique_classes 1
unique labels: [2]
class 2 (sky ) : 147456 pixels (100.00%)
[PASS] Mask2Former Swin-Large NPU inference completed successfully on npu:0随机合成图在 ADE20K 预训练模型上预测为单类是预期现象 (无真实场景结构). 使用更丰富的合成 pattern 图时同样验证了输出 shape 与流程正确性.
命令: python inference.py --mode validate (同一随机输入, 同一权重, FP32)
[VALIDATE] CPU vs NPU consistency check
class_queries_logits: max 0.1614 mean 0.002206 p99 0.0208
masks_queries_logits: max 0.5236 mean 0.014875 p99 0.1270
segmentation agreement (pixel accuracy): 1.0000
seg_cpu unique: [2]
seg_npu unique: [2]
[PASS] CPU-NPU segmentation consistency: PASS (agreement >= 0.99)class_queries_logits [1,100,151] 与 masks_queries_logits [1,100,96,96], FP32命令: python inference.py --mode benchmark (10次, 3次 warmup, torch.npu.synchronize())
[BENCHMARK] NPU performance test
avg 56.09 ms min 55.18 max 57.30 p50 56.01 p90 57.30
throughput 17.83 images/s
e2e avg 57.84 ms
[PASS] Benchmark completed更详细 benchmark (另一次运行, 512x512 输入会触发不同 overhead, 但此处报告 384x384):
assets/agent_workflow.png - 完整工作流 (inspect, env, npu_test, inference, validate, benchmark)assets/npu_device_call.png - npu-smi 与 torch.npu 设备调用assets/model_result.png - 默认推理真实输出与 PASSHardware: NPU, NPU, Ascend, Ascend910, Mask2Former, Swin, Segmentation