YOLOS-small 是基于 Vision Transformer 的目标检测模型,由 hustvl 提出,论文 You Only Look at One Sequence。模型在 ImageNet-1k 预训练后在 COCO 2017 上微调,包含 12 层 Transformer、hidden_size 384、6 个注意力头、100 个 object queries,输入通过 YolosImageProcessor 预处理(shortest_edge 800, longest_edge 1333, 标准化 0.485/0.456/0.406)。输出为 100×92 logits 与 100×4 归一化 cxcywh 边框,经 post_process_object_detection 还原为 COCO 类别边框,COCO val AP 36.1。本仓在 Ascend910 上完成真实 NPU 推理、CPU-NPU 一致性与性能测试。
hustvl/yolos-small (ModelScope / HuggingFace)yolos, Architectures: YolosForObjectDetectiontransformers + torch_npu, dtype float32, trust_remote_code falsenpu-smi info -> Ascend910 OK, Health OK, Power ~168W
torch.npu.is_available(): True
torch.npu.get_device_name(0): Ascend910_9362pip install -r requirements.txt
# requirements.txt:
# torch>=2.0
# torch_npu>=2.0
# transformers>=4.36
# pillow
# requests
# numpy模型权重需通过 ModelScope 预下载:
from modelscope import snapshot_download
snapshot_download('hustvl/yolos-small') # -> /data/.cache/models/hustvl/yolos-small或首次运行 inference.py 会自动从 HuggingFace 拉取(已配置 hf-mirror)。
默认在 npu:0 上执行,使用本地权重与 COCO 样例图 (640×480, 2 cats + couch + remotes)。
python inference.py
python inference.py --mode validate # CPU-NPU 一致性
python inference.py --mode benchmark # 性能
python inference.py --image /path/to/image.jpg --threshold 0.5设备判断逻辑: torch.npu.is_available() 为真且 --device npu:0 时使用 NPU,否则回退 CPU,并打印 Model param device: npu:0.
使用 python inference.py (threshold=0.3, 输入 COCO 000000039769.jpg, 640×480):
Model: hustvl/yolos-small
Task: object-detection (YolosForObjectDetection, 100 queries, 91 classes)
Local dir: /data/.cache/models/hustvl/yolos-small exists=True
Device: npu:0 (Ascend910_9362)
torch_npu version: 2.9.0.post1+gitee7ba04
Num labels: 91, num_detection_tokens: 100
Model param device: npu:0, dtype: torch.float32
Input image: size=(640, 480), mode=RGB
pixel_values shape: (1, 3, 800, 1056), dtype: torch.float32
pixel_mask shape: (1, 800, 1056)
logits shape: (1, 100, 92)
pred_boxes shape: (1, 100, 4)
Inference latency: 17.2 ms (synchronized)
Detections with score >= 0.3: 10 (raw N/A queries, top 10)
Post-processed detections: 5 boxes (threshold 0.3 -> 5 at 0.5)
post [1] 75 remote score=0.9850 xyxy=[39.7,69.9,180.2,115.7]
post [2] 17 cat score=0.9967 xyxy=[343.8,20.7,638.6,371.4]
post [3] 63 couch score=0.9616 xyxy=[0.0,0.1,639.9,476.0]
post [4] 75 remote score=0.9983 xyxy=[331.7,78.8,369.1,188.5]
post [5] 17 cat score=0.9892 xyxy=[13.9,55.4,310.3,468.5]
PASSpython inference.py --mode validate 同时在 CPU 与 NPU 运行同一输入,计算差异:
logits_cpu shape (1, 100, 92), logits_npu shape (1, 100, 92)
logits max abs diff: 0.838453
logits mean abs diff: 0.026851
boxes max abs diff: 0.032904
boxes mean abs diff: 0.000540
label match rate (100 queries): 100.0%
max score max abs diff: 0.010641, mean 0.000173
Validation thresholds: logits_mean<0.05 (True), boxes_max<0.05 (True), score_max<0.02 (True), label_match>=99% (True)
PASS说明: 这是固定单样本一致性验证,非完整 COCO 数据集 AP 评测;完整 AP 需在 COCO val 5k 上评估,此处未执行以节省资源。
python inference.py --mode benchmark (warmup 3, iters 10, 同步计时, npu:0, FP32, pixel 1×3×800×1056):
Benchmark iters=10, warmup=3, dtype=torch.float32, device=npu:0
pixel_values (1, 3, 800, 1056)
peak NPU memory allocated: 346.3 MB
latency ms: avg=17.2 min=17.1 max=17.3 p50=17.2 p90=17.3 p95=17.3
throughput: 58.12 images/s
PASS三张证据图由 scripts/render_xterm_evidence.mjs --style raw 从真实日志渲染:
assets/agent_workflow.png - 下载、侦察、NPU 推理、验证、性能全流程assets/npu_device_call.png - npu-smi 与 torch.npu 设备调用assets/model_result.png - inference.py 真实输出与 PASS

torch_npu 与 CANN 8.5+,CPU 回退路径仅用于验证,未宣称 CPU 性能。Hardware: NPU, NPU, Ascend, Ascend910, YOLOS, object-detection, vision, transformer