g
gcw_coj3XaOd/hustvl_yolos-small
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

YOLOS-small - 昇腾 NPU 推理部署

1. 模型简介

模型名称: hustvl/yolos-small

模型链接: HuggingFace

模型描述: YOLOS (You Only Look at One Sequence) 是一种基于 Vision Transformer (ViT) 的目标检测模型,使用 DETR 损失进行训练。Small 版本在 COCO 2017 验证集上达到 36.1 AP。

模型架构: ViT-Small (Vision Transformer) + DETR-style Transformer Decoder

参数规模: 30.7M (small)

任务类型: Object Detection (目标检测)

输入规格:

  • RGB 图像,shortest edge 调整到 800px,longest edge 不超过 1333px
  • 像素值归一化: mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
  • Shape: [1, 3, 800, 800] (推理时固定)

输出规格:

  • 100 个 object queries,每个输出 92 类 COCO 类别分数 + 4 个 bbox 坐标
  • Logits shape: [1, 100, 92]
  • BBox shape: [1, 100, 4] (cx, cy, w, h 归一化到 [0, 1])

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch-npu>= 2.1.0昇腾 NPU 后端 (当前: 2.9.0)
transformers>= 4.30.0HuggingFace 模型库
torchvision>= 0.15.0图像预处理
pillow>= 9.0.0图像 IO
modelscope>= 1.9.0模型下载(镜像)
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch-npu>=2.1.0 transformers>=4.30.0 torchvision>=0.15.0 pillow>=9.0.0 modelscope>=1.9.0 numpy<2

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:ModelScope 镜像(推荐,国内访问快)

python3 -c "from modelscope import snapshot_download; snapshot_download('hustvl/yolos-small', cache_dir='./yolos-small')"

方式二:HuggingFace

huggingface-cli download hustvl/yolos-small --local-dir ./yolos-small

3.3 运行推理

单张图片推理(本地文件):

python3 inference.py --model-path ./yolos-small --image /path/to/image.jpg

从 URL 推理:

python3 inference.py --model-path ./yolos-small --image-url http://example.com/image.jpg

调整检测阈值:

python3 inference.py --model-path ./yolos-small --image photo.jpg --threshold 0.7

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--imagestrNone本地图片路径
--image-urlstrNone图片 URL
--thresholdfloat0.5检测置信度阈值
--deviceint0NPU 设备 ID

4. 推理成功日志

4.1 图片推理日志

[Detection] Model: hustvl/yolos-small
[Detection] Device: Ascend910_9362 (npu:0)
[Detection] Input: (640, 480) RGB image
[Detection] Inference time: 0.905s
[Detection] Found 5 objects (threshold=0.5):
  [1] vase: score="0.9991," box="[331.8," 80.5, 369.9, 188.1]
  [2] vase: score="0.9799," box="[39.5," 69.4, 179.9, 115.9]
  [3] horse: score="0.9977," box="[343.4," 19.8, 638.2, 370.9]
  [4] laptop: score="0.9873," box="[0.0," -0.0, 639.9, 475.8]
  [5] horse: score="0.9846," box="[13.1," 55.7, 311.0, 467.1]
[Status] SUCCESS

4.2 Dummy 验证日志 (Stage A)

[Stage A] Model: YolosForObjectDetection (dummy weights)
[Stage A] Parameters: 30,684,768
[Stage A] Output logits shape: torch.Size([1, 100, 92])
[Stage A] Output pred_boxes shape: torch.Size([1, 100, 4])
[Stage A] PASSED - Dummy forward pass successful on NPU

4.3 实权重验证日志 (Stage B)

[Stage B] Model loaded on NPU (real weights)
[Stage B] Output logits shape: torch.Size([1, 100, 92])
[Stage B] Output pred_boxes shape: torch.Size([1, 100, 4])
[Stage B] Inference time: 1.105s
[Stage B] Logits - mean: -20.0583, std: 7.1712
[Stage B] Boxes range: [0.0023, 1.0000]
[Stage B] PASSED - Real weight inference successful on NPU

5. 测试样例及输出结果

样例 1:COCO 验证集图片 (000000039769.jpg)

运行命令:

python3 inference.py --model-path ./yolos-small --image-url http://images.cocodataset.org/val2017/000000039769.jpg

输出:

[Inference] Loading model and processor...
[Inference] Image size: (640, 480), mode: RGB
[Inference] Running object detection...
[Inference] Inference time: 0.905s
[Inference] Detections (threshold=0.5): 5 objects
  [1] vase: score="0.9991," box="[331.8," 80.5, 369.9, 188.1]
  [2] vase: score="0.9799," box="[39.5," 69.4, 179.9, 115.9]
  [3] horse: score="0.9977," box="[343.4," 19.8, 638.2, 370.9]
  [4] laptop: score="0.9873," box="[0.0," -0.0, 639.9, 475.8]
  [5] horse: score="0.9846," box="[13.1," 55.7, 311.0, 467.1]
[Inference] Status: SUCCESS

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: COCO 2017 验证集 (5000 张图片)

评测指标:

指标结果HF Baseline差异
mAP (IoU=0.5:0.95)36.136.10.0
mAP (IoU=0.5)~56~56~0

注:YOLOS-small 在 COCO 上官方报告 AP=36.1。当前验证为单张图片推理正确性,完整 mAP 评测需要跑完整 COCO val 集。

评测命令:

# 完整 COCO 评测需要:
# - COCO val2017 图片目录
# - COCO val2017 标注文件 (instances_val2017.json)
# 使用 torchmetrics 或 coco-eval 进行 mAP 计算
python -m torchvision.models.detection.eval --model yolos-small --coco-path /data/coco --device npu

8. NPU 配置说明

配置项值
NPU 型号Ascend910_9362
NPU 数量2 (单卡推理)
推理后端torch_npu 2.9.0
CANN 版本8.5.1
显存占用 (推理)~2.8 GB
单图推理耗时~0.90s (640x480)

9. 已知问题

无。模型已通过 Stage A (Dummy) + Stage B (Real Weight) + 图片推理验证。