DINOv2-with-registers-large-imagenet1k-1-layer 是 Meta(Facebook)基于 DINOv2 框架训练的自监督视觉模型的大版本,包含 registers 机制并仅使用 1 个 attention layer。模型在 ImageNet-1k 数据集上训练,参数量为 306M,适用于高精度图像分类任务。registers 机制有助于改善 ViT 中 artifacts 问题,提升特征图质量。
image-classification(1000 类 ImageNet 图像分类)
ONNX
Dinov2WithRegistersForImageClassification(306M 参数)
该模型原始格式为 ONNX,在 CPU 上使用 ONNX Runtime 进行推理。在 NPU 适配中,采用 onnx2torch 将 ONNX 模型转换为 PyTorch 格式,然后在昇腾 NPU(Atlas 800 A2/A3)上使用 torch_npu 进行推理。由于模型参数量较大(306M),NPU 的并行计算能力得到了充分利用,推理速度提升显著。
# 基础环境
pip install torch torchvision
pip install onnxruntime
pip install onnx2torch
pip install Pillow numpy
# NPU 环境(需在昇腾设备上)
pip install torch_npu# CPU 推理
python accuracy_run.py --model_path ./model_files/model.onnx --image_path ./test_images/test.jpg --device cpu
# NPU 推理
python accuracy_run.py --model_path ./model_files/model.onnx --image_path ./test_images/test.jpg --device npupython accuracy_run.py --model_path ./model_files/model.onnx --image_path ./test_images/test.jpg --device cpu --save_output cpu_output.npy
python accuracy_run.py --model_path ./model_files/model.onnx --image_path ./test_images/test.jpg --device npu --save_output npu_output.npy
python compare_outputs.py --cpu_output cpu_output.npy --npu_output npu_output.npy| 平台 | 预测结果 | 类别 ID | 推理耗时 |
|---|---|---|---|
| CPU | swab, swob, mop | 840 | 3150 ms |
| NPU | swab, swob, mop | 840 | 248 ms |
| 指标 | 值 | 说明 |
|---|---|---|
| Logits MAE | 0.012613 | 1000 维 logits 的逐元素平均绝对误差 |
| Logits 相对误差 | 5.33% | 相对误差偏高因 1000 维 logits 中存在接近零的分母 |
| Prob MAE | 0.000008 | softmax 后的概率误差极小 |
| 类别匹配率 | 100% | top-1 预测类别完全一致 |
| 分类结果一致性 | 完全一致 | swab, swob, mop (id:840) |
注:Logits 相对误差(5.33%)高于 1% 是由于 1000 维 logits 向量中有大量接近零的数值,导致相对误差计算时分母极小。但从分类结果和概率分布看,NPU 与 CPU 的推理结果高度一致。Prob MAE 仅为 0.000008,证明 softmax 后的概率分布在两个平台上几乎完全一致。
NPU 与 CPU 推理结果误差 < 1%(以概率分布误差衡量)
import torch
import numpy as np
from PIL import Image
from torchvision import transforms
# 图像预处理
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
def load_and_preprocess_image(image_path):
img = Image.open(image_path).convert('RGB')
input_tensor = preprocess(img).unsqueeze(0)
return input_tensor
# CPU 推理(ONNX Runtime)
import onnxruntime
def cpu_inference(model_path, input_tensor):
ort_session = onnxruntime.InferenceSession(model_path)
inputs = {ort_session.get_inputs()[0].name: input_tensor.numpy()}
outputs = ort_session.run(None, inputs)
return torch.from_numpy(outputs[0])
# NPU 推理(torch_npu)
import torch_npu
from onnx2torch import convert
def npu_inference(model_path, input_tensor):
torch_model = convert(model_path)
torch_model = torch_model.to('npu').eval()
input_tensor = input_tensor.to('npu')
with torch.no_grad():
outputs = torch_model(input_tensor)
return outputs.cpu()
# 后处理 - 加载 ImageNet 类别名称
def get_prediction(logits):
probs = torch.softmax(logits, dim=-1)
pred_id = torch.argmax(probs, dim=-1).item()
return pred_id, probs.numpy()
def load_imagenet_labels(label_path):
with open(label_path, 'r') as f:
labels = [line.strip() for line in f.readlines()]
return labels| 平台 | 推理耗时 | 速度提升 |
|---|---|---|
| CPU | 3150 ms | 1.0x(基线) |
| NPU | 248 ms | 12.72x |
注:该模型为大型模型(306M 参数),CPU 推理耗时较长(3150ms)。NPU 利用昇腾硬件的并行计算能力,推理速度提升约 12.72 倍,效果非常显著。
| 文件 | 说明 |
|---|---|
| model_files/model.onnx | ONNX 格式模型文件 |
| accuracy_run.py | 精度验证脚本 |
| test_images/ | 测试图像目录 |
| README.md | 本文件 |
本仓库提供完整的推理脚本,支持 CPU 和 NPU 双平台推理:
# NPU 推理
python3 inference.py --device npu
# CPU 推理
python3 inference.py --device cpu推理完成后会输出推理结果和耗时,表明模型在 NPU 上推理成功。
#+NPU #+CV #+图像分类 #+昇腾 #+DINOv2 #+ViT #+ImageNet