facebook/mask2former-swin-large-ade-semanticarchitectures: ["Mask2FormerForUniversalSegmentation"], model_type: mask2formerclass_queries_logits [B,100,151] 与 masks_queries_logits [B,100,96,96],通过 post_process_semantic_segmentation 得到 [H,W] 语义图(0-149)npu-smi info:
NPU 7 Ascend910 | Health OK | Power 168.2W | Temp 45C | HBM 3112/65536 MB (chip 0, Phy-ID 14, Bus 0000:0A:00.0)
NPU 7 Ascend910 | Health OK | Power - | Temp 43C | HBM 2869/65536 MB (chip 1, Phy-ID 15, Bus 0000:0B:00.0)/tmp/mask2former-hf(通过 ModelScope 与 HuggingFace snapshot_download 获取)python scripts/check_npu.py --output env_check.json passed=truepip install -r requirements.txt
# requirements.txt:
# torch>=2.0
# torch_npu>=2.0
# transformers>=4.40
# Pillow
# numpy
# requests
# safetensors
# modelscope
# huggingface_hub
# matplotlib# 默认推理 (自动选择 npu:0, 若无 NPU 回退 CPU)
PYTHONPATH=/usr/local/python3.11.14/lib/python3.11/site-packages:$PYTHONPATH python inference.py
# 指定图像
PYTHONPATH=/usr/local/python3.11.14/lib/python3.11/site-packages:$PYTHONPATH python inference.py --image /path/to/image.jpg --output-vis vis.png
# CPU/NPU 一致性验证
PYTHONPATH=/usr/local/python3.11.14/lib/python3.11/site-packages:$PYTHONPATH python inference.py --mode validate
# 性能测试 (10次迭代, 含预热)
PYTHONPATH=/usr/local/python3.11.14/lib/python3.11/site-packages:$PYTHONPATH python inference.py --mode benchmark推理流程:
AutoImageProcessor.from_pretrained 加载 preprocessor_config(size 384,mean/std)Mask2FormerForUniversalSegmentation.from_pretrained 加载实际权重(215M params),并调用 .to(npu:0)pixel_values [1,3,384,384] + pixel_mask [1,384,384]torch.npu.synchronize() 前后进行计时,前向传播得到 class/mask logitspost_process_semantic_segmentation 生成语义分割图,并统计类别分布输入:COCO val2017 000000039769.jpg(640x480,cats),下载自 https://images.cocodataset.org/val2017/000000039769.jpg,通过 https(curl -L -k)
NPU 推理 (npu:0, float32):
[INFO] using model_dir /tmp/mask2former-hf
[INFO] selected device npu:0 torch_npu available True
[INFO] model_name: facebook/mask2former-swin-large-ade-semantic
[INFO] task_type: image-segmentation (semantic)
[INFO] architecture: Mask2FormerForUniversalSegmentation (Swin-Large)
[INFO] loading_backend: transformers
[INFO] device: npu:0
[INFO] requested_dtype: float32
[INFO] num_labels: 150 num_queries: 100
[INFO] loaded default image /tmp/cats3.jpg size (640, 480)
[INFO] input pixel_values shape: torch.Size([1, 3, 384, 384])
[INFO] pixel_mask shape: torch.Size([1, 384, 384])
[INFO] model params: 215.49M
[INFO] class_queries_logits shape: (1, 100, 151) mean -3.5041
[INFO] masks_queries_logits shape: (1, 100, 96, 96) mean -32.9512
[INFO] inference latency: 58.10 ms
[INFO] predicted semantic map shape: (480, 640) (H,W) target (480, 640)
[INFO] unique predicted classes: [0, 15, 39, 56, 98, 120, 147, 148]
[INFO] num unique classes: 8 / 150
class 0 wall pixels 36075 ratio 0.1174
class 15 table pixels 121 ratio 0.0004
class 39 cushion pixels 18 ratio 0.0001
class 56 pool table pixels 176389 ratio 0.5742
class 98 bottle pixels 82 ratio 0.0003
class 120 food pixels 90985 ratio 0.2962
class 147 glass pixels 26 ratio 0.0001
class 148 clock pixels 3504 ratio 0.0114
[RESULT] PASSnpu:0 上执行前向推理,并通过 torch.npu.synchronize() 进行计时,延迟为 58.10 ms (单次)命令: python inference.py --mode validate
结果:
[VALIDATE] CPU vs NPU consistency check
[VALIDATE] class logits max_diff 0.794116 mean_diff 0.011465
[VALIDATE] masks logits max_diff 18.635185
[VALIDATE] semantic map diff_ratio 0.000150 cpu unique [0, 15, 39, 56, 98, 120, 147, 148] npu unique [0, 15, 39, 56, 98, 120, 147, 148]
[VALIDATE] PASS/tmp/cats3.jpg)分别在 CPU 与 npu:0 上执行前向传播,并比较 class_queries_logits 与 masks_queries_logits命令:python inference.py --mode benchmark(batch 1,384x384,float32,npu:0,预热 3 次,正式 10 次,torch.npu.synchronize() 同步包裹)
结果(来自 10 次迭代):
[BENCHMARK] repeat=10 batch=1 size=384x384 dtype=float32 device=npu:0
iter 1: 56.29 ms
iter 2: 55.84 ms
iter 3: 55.90 ms
iter 4: 55.68 ms
iter 5: 56.02 ms
iter 6: 56.62 ms
iter 7: 56.56 ms
iter 8: 56.63 ms
iter 9: 56.56 ms
iter 10: 56.51 ms
[BENCHMARK] avg 56.26 ms min 55.68 max 56.63 p50 56.40 p90 56.62 p95 56.63
[BENCHMARK] throughput 17.77 images/s batch 1
[BENCHMARK] NPU memory free 59.73GB total 61.27GB used 1.54GB
[BENCHMARK] device Ascend910_9362assets/agent_workflow.png:完整适配流程日志(下载、侦察、NPU 推理、验证、性能)assets/npu_device_call.png:NPU 设备调用证据(npu-smi info、torch.npu.is_available()、设备名、模型参数 device)assets/model_result.png:python inference.py 真实输出(任务、形状、类别分布、延迟、PASS)/tmp/mask2former-hftransformers 慢速处理器(slow)与 float32,因 NPU 与 CPU 的浮点累积差异,存在 <0.01% 的像素不一致,属于可接受误差preprocessor_config.json 中 _max_size 等冗余字段警告已合并,不影响推理allow_internel_format=False 警告,属于 torch_npu 内部格式提示,不影响正确性Hardware: NPU, NPU, Ascend, Ascend910, Mask2Former, Swin, Semantic-Segmentation, ADE20K, Vision