facebook/mask2former-swin-large-ade-semantichttps://huggingface.co/facebook/mask2former-swin-large-ade-semanticMask2FormerForUniversalSegmentation + 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)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 32model.safetensors 825.93 MB (866,052,064 bytes), config.json 82,540 Bpost_process_semantic_segmentation 输出 [H,W] 像素级标签npu:0, HBM 空闲 ~62.3GBtorch.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'}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本地权重通过 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.py640x640 (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])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 1class_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 1sky 类属预期batch seg [(512,512) unique [2], (384,384) unique [2]]logits [2,100,151] masks [2,100,96,96] -> batch seg [(512,512), (384,384)] 与CPU一致,形状符合 target_sizesPIL 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]float32,同一processor/seed,copy.deepcopy 后 .to("npu:0") 无重新初始化torch.npu.synchronize(),torch.inference_mode(),eval()mean 0.011423 max 0.932962 <1e-3 20.4% cosine 0.999929mean 0.125133 max 4.086583100.00% (409600/409600) unique_cpu [2] unique_npu [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,后续稳定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/savg 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/s68.03 ms (34.01 ms/image) 72.10 ms (36.05 ms/image) 形状 [2,100,151] 显示批处理近线性加速assets/agent_workflow.png - 真实阶段日志 (下载/侦察/CPU烟测/NPU推理/验证/性能/待发布)assets/npu_device_call.png - npu-smi 与 torch.npu 设备日志assets/model_result.png - python inference.py 完整输出与 PASS/FAILnpu:0 单卡、float32、384 输入;其他 dtype/尺寸需另测sky 类属固定种子结果,真实场景可测试 COCO/ADE 图验证多类分布Hardware: NPU, NPU, Ascend, Ascend910, Mask2Former, semantic-segmentation, ADE20k, Swin