hustvl/yolos-small 是一个基于 ViT 的端到端目标检测模型(DETR 风格,
YolosForObjectDetection),COCO 91 类(id2label 0-90,另有 1 个 "no-object" 背景类,
分类头输出 92 维 = num_labels + 1)。输入经 YolosImageProcessor 缩放到
shortest_edge 800 / longest_edge 1333,归一化用 ImageNet 均值方差。
权重分类头(class_labels_classifier.layers.2)shape 为 [92, 384],但 config.json 未声明
num_labels。本项目在内存中将 config.num_labels = len(config.id2label) = 91 后再加载权重,
使输出头为 91 + 1 = 92,与权重维度匹配。
YOLOS 前向在缩放位置编码时使用 nn.functional.interpolate(..., mode="bicubic")
(modeling_yolos.py L140/L168)。Ascend NPU 对 bicubic 插值支持可能缺失,
本项目在加载模型前对 torch.nn.functional.interpolate 做了 monkeypatch:
将 mode="bicubic" 统一降级为 mode="bilinear"(align_corners=False 语义一致),
以保证前向在 NPU 上可执行。若实测 NPU 原生支持 bicubic 则无需此 patch,此处保留为防御性适配。
2.9.0+cpu,torch_npu 2.9.0.post1,transformers 4.57.xnpu:0 / npu:1)inference.py)适配脚本仅使用 NPU 设备(npu:N),全代码域无任何 CUDA 调用;NPU 不可用时直接报错退出,
不做 CPU/CUDA 回退。权重首次运行从 GitCode HF 镜像自动下载并缓存到
model_weights/hustvl/yolos-small/,命中缓存时打印 [cache-hit]。
python inference.py --img test_input.png --device npu:0 [--output_json result.json]stdout 输出 JSON,含 model、requested_device、actual_device、npu_name、exit_status、
inference_ms 以及 detections(threshold=0.3 过滤,label/score/box 像素坐标)和
top_predictions(对每个 query 的 logits argmax 标签与 score,排除 no-object 背景类,
按 score 降序取前 5,保证结果非空)。
python inference.py --serve --port 8005
# 另一终端:
curl -s -F "file=@test_input.png" http://127.0.0.1:8005/predictPOST /predict 上传图片文件,返回与 CLI 相同的推理 JSON;GET / 返回服务与设备元信息。
python inference.py --img test_input.png --device npu:0 在 npu:0(Ascend910_9362)上 exit_status:0。8005 启动后 POST /predict 返回推理 JSON。assets/。hustvl/yolos-smallhttps://ai.gitcode.com/hf_mirrors/hustvl/yolos-small/resolve/main/<file>config.json、preprocessor_config.json、model.safetensors