YOLOS-small (You Only Look at One Sequence) 是 HustVL 提出的纯 Transformer 目标检测模型,在 COCO 2017 (118k 标注图像) 上微调。本仓库为 昇腾 Ascend NPU (torch_npu) 适配版,全流程无 CUDA,使用 transformers 内置 YolosForObjectDetection + YolosImageProcessor 标准加载,检测框输出为 JSON 序列化格式。
torch_npu 2.9 + CANNtorch torch_npu transformers fastapi uvicorn python-multipart safetensors pillow numpy# 命令行单图目标检测
python3 inference.py --img sample.jpg
# 启动 FastAPI 服务 (根路径为上传表单, POST /predict 返回检测 JSON)
python3 inference.py --server| 参数 | 说明 |
|---|---|
--img | 输入图片路径 (必需) |
--server | 启动 FastAPI Web 服务 |
{
"model": "hustvl/yolos-small",
"device": "npu:0",
"image_size": [540, 360],
"num_detections": 14,
"threshold": 0.35,
"inference_time_s": 0.178,
"labels": [25, 24, 24],
"names": ["giraffe", "zebra", "zebra"],
"boxes": [[16.9, 154.7, 128.4, 283.7], ...],
"scores": [0.429, 0.4206, 0.9986],
"detections": [
{"label": 25, "name": "giraffe", "box": [16.9, 154.7, 128.4, 283.7], "score": 0.429},
...
]
}boxes 为 [x1, y1, x2, y2] 像素坐标 (原图尺寸)threshold = 0.35,bs = 1权重文件 (config.json + model.safetensors + preprocessor_config.json) 自动从 hf-mirror 镜像下载,缓存到 ./model_weights/hustvl/yolos-small/,优先复用本地缓存;支持 pytorch_model.bin 回退。
yolos-small/
├── inference.py # 主推理脚本 (CLI + FastAPI 双模式)
├── sample.jpg # 示例输入图像
├── README.md
├── .gitignore
├── model_weights/ # 权重缓存 (symlink 到共享缓存)
└── assets/ # 截图
├── agent_workflow.png
├── npu_device_call.png
└── model_result.png

