Y
gcw_hhrLdMXW/yolos-small-npu-deployment
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

yolos-small 昇腾 NPU 部署文档

1. 模型简介

模型名称: yolos-small(hustvl/yolos-small)

模型链接:

  • Hugging Face: https://huggingface.co/hustvl/yolos-small
  • AtomGit: https://ai.gitcode.com/hf_mirrors/hustvl/yolos-small

模型描述: yolos-small(hustvl/yolos-small)You Only Look at One Sequence 实时目标检测模型,ViT 骨干 + DETR 式检测头(NMS-free),COCO 91 类,在 NPU 上零改造直跑。

模型架构: YolosForObjectDetection(ViT-small 骨干,COCO 91 类,NMS-free 检测头)

参数规模: 约 30.7M 参数。

适配引擎: 昇腾 torch_npu(torch.npu 设备接口调用 NPU)。


2. 环境依赖

依赖项版本要求说明
Python>= 3.10(推荐 3.11)本机 3.11.14
CANN Toolkit8.0.RC2+本机 8.5.1,昇腾开发套件
torch2.1.0+本机 2.9.0+cpu
torch_npu2.9.0.post1随 CANN 提供,昇腾 NPU 适配层
transformers4.57.6模型加载/预处理
safetensors0.4.3+权重加载
Pillow10+图像读取

硬件: 昇腾 Ascend910_9362(64GB HBM ×2)。

安装命令:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple \
    transformers==4.57.6 safetensors Pillow numpy
# 昇腾 NPU 组件(torch_npu / CANN / 驱动)由昇腾工具链安装

3. 推理步骤

3.1 模型资源准备

mkdir -p model
for f in config.json model.safetensors preprocessor_config.json; do
  timeout 280 curl -sSL -o "model/$f" \
    "https://hf-mirror.com/hustvl/yolos-small/resolve/main/$f?download=true"
done

3.2 检查 NPU 设备

npu-smi info
python3 -c "import torch, torch_npu; print(torch.npu.device_count())"   # 输出 2

3.3 运行推理

python3 inference.py --device npu:0
# 带性能基准
python3 inference.py --device npu:0 --benchmark

4. 测试样例及输出结果

命令:

python3 inference.py --device npu:0 --benchmark

输出:

[INFO] ========== 昇腾推理引擎 ==========
[INFO] torch      : 2.9.0+cpu
[INFO] torch_npu  : 2.9.0.post1+gitee7ba04
[INFO] NPU 可用   : True
[INFO] NPU 数量   : 2
[INFO] NPU 型号   : Ascend910_9362
[INFO] ==================================
[模型加载] 完成, 参数量 30.7M, 设备 npu:0, 耗时 1.27s

[结果] yolos-small 实时目标检测
    [0] label=1 (person) score=1.000 box=(53.0, 395.3, 247.3, 904.1)
    [5] label=6 (bus)    score=0.996 box=(13.9, 228.5, 803.3, 737.2)
    [4] label=77 (cell phone) score=0.642 box=(240.7, 500.9, 263.6, 521.5)

[性能基准] npu:0 warmup=3 runs=20
  平均推理时延 = 17.38 ms/张 (57.5 FPS)

结果分析: 模型在昇腾 NPU 上前向成功,任务级输出正确,确定性验证通过。


5. 注意事项

  • 推理脚本通过 torch_npu(torch.npu.is_available() / torch.device("npu:0"))调用昇腾 NPU,未检测到 NPU 时自动回退 CPU。
  • 首次前向会触发算子编译/动态追踪,计时前需预热(脚本默认 warmup=3)。
  • 权重需从 hf-mirror.com 镜像拉取(?download=true 必须),本环境无法直连 huggingface.co。