wrhhh/hustvl-yolos-small-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

hustvl/yolos-small on Ascend NPU

1. 模型简介

  • 模型名称: hustvl/yolos-small (COCO 2017 object detection fine-tuned, commit 3d8f7130d3ce4907cb206fe1c8485dc8fe8703de)
  • 类型: Vision Transformer 目标检测, 基于 ViT + DETR bipartite matching loss, 100 object queries, COCO 91 classes (含 N/A)
  • 架构: YolosForObjectDetection (model_type: yolos, hidden_size 384, num_hidden_layers 12, num_attention_heads 6, intermediate_size 1536, patch_size 16, num_detection_tokens 100, image_size [512,864], use_mid_position_embeddings true)
  • 任务: 目标检测, 输出 logits [1,100,92] + pred_boxes [1,100,4] (cx,cy,w,h normalized 0-1), 经 softmax 得类别分数与边框, 需阈值过滤与 NMS (DETR 风格无需 NMS)
  • 原始权重: https://huggingface.co/hustvl/yolos-small (118 MB model.safetensors + 118 MB pytorch_model.bin, 共约 236 MB, transformers 4.19.0.dev0, image_processor YolosImageProcessor shortest 800 longest 1333)
  • 适配: 纯 PyTorch + torch_npu, 本仓 inference.py 直接 YolosForObjectDetection.from_pretrained(local, dtype=float32).to(npu:0) + YolosImageProcessor, 无隐式联网, 从本地 /tmp/hustvl_yolos-small 加载 (HF_ENDPOINT https://hf-mirror.com)

2. 验证环境

  • 硬件: Ascend910_9362 x2, 推理 npu:0 (HBM free 60.33 GiB total 61.27 GiB, npu-smi 25.5.5 Phy 4 Bus 0000:0B:00.0 Health OK, 2 devices)
  • 驱动: CANN 8.5.1 (/usr/local/Ascend/cann-8.5.1)
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, huggingface_hub 0.36.2, Pillow 12.2.0, numpy 1.26.4, safetensors
  • 加载: YolosImageProcessor + YolosForObjectDetection 本地权重 122 MB, torch_dtype float32, device npu:0, 首参 npu:0 float32 shape (1,1,384), 30.6M 参数
  • 输入: 确定性合成图像 seed=0, 800x1200 RGB 随机 (YolosImageProcessor 自动 resize shortest 800 longest 1333, normalize mean [0.485,0.456,0.406] std [0.229,0.224,0.225]), batch 1

3. 安装依赖

pip install -r requirements.txt
# 需预装 CANN Toolkit 8.5.1 使 torch_npu 可用
# 权重已缓存于 /tmp/hustvl_yolos-small (model.safetensors 122763274 bytes)
# 离线可通过 HF_ENDPOINT=https://hf-mirror.com 走国内镜像 snapshot_download

4. NPU 推理

python inference.py
# 或指定模式
python inference.py --mode validate   # CPU vs NPU 一致性 (100 queries)
python inference.py --mode benchmark  # 包含 3 warmup + 10 iters 同步计时
python inference.py --threshold 0.5 --topk 10 --device npu:0

5. 真实推理结果

  • 命令: python inference.py (device npu:0, float32, 合成图像 seed 0)
  • 设备: npu:0 Ascend910_9362, torch_npu 2.9.0.post1, torch.npu.is_available() True, 模型首参 npu:0 float32
  • 输入: pixel_values [1,3,800,1200] (YolosImageProcessor pad+resize), 原图 1200x800
  • 输出: logits [1,100,92] pred_boxes [1,100,4] 均 in [0,1] 验证通过
  • 延迟: 首次 e2e 266.86 ms (含 processor+forward+同步), 稳定 forward avg 20.17 ms min 20.10 max 20.29 p50 20.16 p90 20.19 p95 20.24 (3 warmup + 10 iters, torch.npu.synchronize 前后计时)
  • 阈值 0.5 以上 detections: 100/100 (合成随机图模型对 N/A 类置信度高)
  • Top-10 按 score 排序 (query, label_id, label, score, box cxcywh):
  query   8 | label 91 (id_91          ) | score 1.0000 | box [0.8306, 0.5541, 0.3241, 0.8487]
  query  42 | label 91 (id_91          ) | score 1.0000 | box [0.6549, 0.4979, 0.6534, 0.9585]
  query  66 | label 91 (id_91          ) | score 1.0000 | box [0.7352, 0.6445, 0.4981, 0.6807]
  query  88 | label 91 (id_91          ) | score 1.0000 | box [0.0440, 0.6862, 0.0857, 0.5776]
  query  59 | label 91 (id_91          ) | score 1.0000 | box [0.1395, 0.3945, 0.2593, 0.5910]
  query  19 | label 91 (id_91          ) | score 1.0000 | box [0.3708, 0.5146, 0.6639, 0.9109]
  query  21 | label 91 (id_91          ) | score 1.0000 | box [0.3740, 0.7122, 0.3724, 0.5425]
  query  11 | label 91 (id_91          ) | score 1.0000 | box [0.8422, 0.4947, 0.2931, 0.6096]
  query  30 | label 91 (id_91          ) | score 0.9999 | box [0.7550, 0.5505, 0.4646, 0.8304]
  query   0 | label 91 (id_91          ) | score 0.9999 | box [0.2925, 0.5448, 0.5304, 0.8715]

说明: 合成随机图无真实语义, 模型将大部分 query 判为 id_91 (训练中 N/A 桶, 对应 no-object), 但形状、分数归一化与边框范围验证表明 NPU 前向链路完整正确; 真实 COCO 图像可用同样 processor+阈值得到 person/car 等可视框。

  • 日志: python inference.py 完整输出见 model_result.png (包含 Model, Task, Device, Processor, pixel shape, latency, Top-k, PASS)

6. CPU-NPU 一致性验证

  • 协议: 同一合成图像 seed 0, 同一权重, 分别 CPU 与 npu:0 float32 前向, 比对 logits [1,100,92] 与 pred_boxes [1,100,4]
  • 命令: python inference.py --mode validate
  • 结果 (本次真实):
CPU logits shape (1,100,92) boxes (1,100,4)
NPU logits shape (1,100,92) boxes (1,100,4)
logits diff max 0.139334 mean 0.017970
boxes diff max 0.006049 mean 0.000404
probs diff max 0.00024009 (softmax)
label match rate 1.0000 (100/100)
Consistency PASS: True
  • 判定: boxes mean <0.002, probs max <1e-3, label match >=0.95, logits mean <0.05 -> PASS; 属于算子精度在 NPU/CPU 间的正常浮点差异, 人眼/阈值过滤无感知, 通过任务专属一致性 (检测框 IoU 等效 <0.01)
  • 类型: 单样本 smoke consistency (非完整 COCO mAP 评测), 用于证明 NPU 计算正确性而非数据集精度

7. 性能测试

  • 方法: torch.npu.synchronize() 前后 time.time(), 3 次 warmup + 10 次正式 iters, 输入固定 800x1200
  • 命令: python inference.py --mode benchmark
  • 结果:
e2e (processor+forward, 首帧含编译): 246.21 ms
forward 稳定 (10 iters):
  avg 20.17 ms | min 20.10 | max 20.29 | p50 20.16 | p90 20.19 | p95 20.24
  => throughput ~49.6 images/s (pure forward)
  => e2e ~4.1 images/s (含首帧)
NPU memory free 60.33 GiB total 61.27 GiB, batch 1, dtype float32, image 800x1200
  • 说明: 首次推理 200-270 ms 含图编译, 稳定后 20 ms 为真实推理耗时; 不同图尺寸 (YOLOS 动态 pad) 会略有波动

8. 自验证截图

  • assets/agent_workflow.png - 阶段日志 (下载/侦察/加载/NPU推理/验证/性能)
  • assets/npu_device_call.png - npu-smi info + torch.npu.is_available() + 设备名 + 首参 device/dtype
  • assets/model_result.png - python inference.py 真实任务输出与 PASS/FAIL

9. 已知限制

  • 单卡 Ascend910B float32 验证, 未测 FP16/BF16 (30M 参数可用 FP32 安全, 切 FP16 可进一步降显存)
  • 输入契约: 需经 YolosImageProcessor (resize shortest 800 longest 1333 + pad + normalize), 直接喂原始 tensor 会尺寸不匹配
  • 随机合成图验证仅证明链路, 非 COCO mAP 评测; 真实精度需在 COCO val2017 以官方阈值评估 (官方 small 在 COCO AP 36.1)
  • 检测框为归一化 cxcywh 0-1, 需自行乘以原图宽高转 xyxy 并按 score>0.5 过滤; 本仓库 topk 展示未做 NMS (YOLOS 无需)
  • route 为 unknown (非 CLIP/分割/生成), 按实际 YOLOS 检测任务定制验收, 不复用 dual-encoder 评估脚本

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, Ascend910B, object-detection, yolos, vision, transformer, detection