Y
YYPGD/mask2former-swin-large-ade-semantic
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

mask2former-swin-large-ade-semantic 昇腾NPU部署文档

1. 模型简介

模型名称: mask2former-swin-large-ade-semantic(facebook/mask2former-swin-large-ade-semantic) 模型链接: https://ai.gitcode.com/hf_mirrors/facebook/mask2former-swin-large-ade-semantic 模型描述: Mask2Former 是基于掩码注意力(masked attention)的通用分割框架,本模型为在 ADE20K(150 类)语义分割任务上微调的 Swin-Large 骨干版本。输入单张图像,直接输出逐像素语义类别标签图。 模型架构: Swin-Large 层级 Transformer 骨干(embed_dim=192,depths=[2,2,18,2])+ 像素解码器 + Transformer 解码器(9 个查询组),通过二分匹配以掩码形式预测语义类别。 参数规模: 约 220M(model.safetensors 约 850MB)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch / torch_npu2.1.0+NPU 上需安装 torch_npu
transformers>=4.51,<4.52Mask2FormerForUniversalSegmentation / Mask2FormerImageProcessor
einops最新版torch_npu 下 transformers 注意力工具依赖
pillow / numpy最新版图像构造与后处理
safetensors最新版权重加载
昇腾驱动CANN 8.0.RC2+推荐最新版

安装命令:

pip install --target /mnt/workspace/mask2former-swin-large-ade-semantic/libs -i https://pypi.tuna.tsinghua.edu.cn/simple "transformers>=4.51,<4.52" pillow numpy safetensors einops

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info
# 权重已就位
ls /home/developer/models/mask2former-swin-large-ade-semantic/   # config.json preprocessor_config.json model.safetensors pytorch_model.bin

3.2 运行推理

PYTHONPATH=/mnt/workspace/mask2former-swin-large-ade-semantic/libs python3 inference.py --model_path /home/developer/models/mask2former-swin-large-ade-semantic

3.3 推理参数说明

参数类型默认值说明
--model_pathstr/home/developer/models/mask2former-swin-large-ade-semantic本地权重目录
--devicestr自动(npu > cuda > cpu)推理设备
--outputstrassets/model_result.png结果图保存路径

4. 测试样例及输出结果

样例 1:内置合成场景样例(assets/sample_input.jpg,缺省时自动合成彩色场景图)

输入:

python3 inference.py --model_path /home/developer/models/mask2former-swin-large-ade-semantic

输出:

[INFO] device = npu
[INFO] 模型加载完成 load_time=15.20s
[INFO] 推理完成 耗时/elapsed=3.45s (input=384x384, device=npu)
[INFO] 分割图形状: (480, 640), 类别数上限: 150
[RESULT] status=ok device=npu 耗时/elapsed=3.45s

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果

6. 注意事项

  • 推理脚本通过 torch_npu 调用 NPU,设备自动选择 npu > cuda > cpu;
  • 权重仅从本地 --model_path 加载(from_pretrained(..., local_files_only=True)),推理全程离线,不联网下载任何文件;
  • 输入张量为 float32(NPU 不支持 float64 算子),由 Mask2FormerImageProcessor 完成 resize(384×384)/归一化;
  • 注意力采用标准 SDPA/eager 实现,未使用 flash_attention_2;
  • 输出为原图尺寸的逐像素类别标签图(ADE20K 150 类),可视化以半透明调色板叠加原图保存;
  • 如有精度差异请与官方 pipeline(facebook/mask2former HF 页面)输出对比确认。