SAM(Segment Anything Model)ViT-Huge适配华为昇腾NPU推理。本模型卡片记录了在昇腾910硬件上的NPU适配、精度验证和性能基准测试情况。
| 组件 | 版本 |
|---|---|
| CANN | 8.5.1 |
| PyTorch | 2.9.0 |
| torch_npu | 25.5.2 |
| transformers | 4.57.6 |
| NPU | 昇腾910(2芯片) |
| 精度 | float16(NPU) |
pip install torch torch_npu transformers modelscope pillow requests numpy# Via ModelScope
pip install modelscope
python -c "from modelscope import snapshot_download; snapshot_download('KyanChen/sam-vit-huge')"模型将缓存至:~/.cache/modelscope/hub/models/KyanChen/sam-vit-huge/
import torch
import torch_npu
from transformers import SamModel, SamProcessor
from PIL import Image
# Load model in float16 for NPU
model_path = "KyanChen/sam-vit-huge" # or local path
model = SamModel.from_pretrained(model_path, torch_dtype=torch.float16).to("npu:0")
model.eval()
processor = SamProcessor.from_pretrained(model_path)
# Load image
image = Image.open("your_image.jpg").convert("RGB")
# Point prompt inference
inputs = processor(image, input_points=[[[320, 240]]], input_labels=[[1]],
return_tensors="pt")
inputs = {k: (v.to("npu:0").to(torch.float16) if v.dtype == torch.float32 else v.to("npu:0"))
for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
masks = processor.image_processor.post_process_masks(
outputs.pred_masks.cpu(),
inputs["original_sizes"].cpu(),
inputs["reshaped_input_sizes"].cpu()
)或者使用提供的推理脚本:
python inference.py在 COCO val2017 测试图像上进行 NPU(float16)与 CPU(float32)的精度对比。
| 指标 | 数值 | 阈值 | 状态 |
|---|---|---|---|
| Logit 相对误差 | 0.19% | <1% | 通过 |
| 余弦相似度 | 1.000005 | >0.999 | 通过 |
| 掩码交并比(Mask IoU) | 0.9859 | >0.98 | 通过 |
| 像素准确率 | 99.85% | >99% | 通过 |
| 归一化平均差异 | 4.0e-04 | <0.01 | 通过 |
结果:NPU float16 推理结果在容差范围内与 CPU float32 参考结果一致。所有精度指标均通过。
在 640x480 图像上进行单点提示推理,基于 Ascend 910 NPU,采用 float16 精度。
| 指标 | 数值 |
|---|---|
| 平均延迟 | 170.82 ms |
| 中位数延迟 | 170.68 ms |
| P95 延迟 | 172.11 ms |
| 标准差 | 0.93 ms |
| 最小延迟 | 169.65 ms |
| 最大延迟 | 172.54 ms |
预热后经过 20 次迭代,性能稳定,方差 <1%。
sam-npu-adapt/
├── inference.py # NPU inference & evaluation script
├── README.md # This file
└── output/
├── benchmark_npu.json # Performance benchmark results
├── accuracy_results.json # Accuracy comparison results
└── accuracy_logit_results.json # Detailed logit-level comparisonApache License 2.0(与原始 SAM 模型相同)
@article{kirillov2023segany,
title={Segment Anything},
author={Kirillov, Alexander and Mintun, Eric and Ravi, Nikhila and Mao, Hanzi and Rolland, Chloe and Gustafson, Laura and Xiao, Tete and Whitehead, Spencer and Berg, Alexander C. and Lo, Wan-Yen and Dollar, Piotr and Girshick, Ross},
journal={arXiv:2304.02643},
year={2023}
}