DINOv2-with-registers-giant-imagenet1k-1-layer 是 Meta(Facebook)基于 DINOv2 框架训练的自监督视觉模型的巨大版本,包含 registers 机制并仅使用 1 个 attention layer。模型在 ImageNet-1k 数据集上训练,参数量高达 1.14B(11.4 亿参数),是目前最大的 DINOv2 变体之一,适用于高精度图像分类和特征提取任务。registers 机制有助于改善 ViT 中 artifacts 问题,提升特征图质量。
image-classification(1000 类 ImageNet 图像分类)
ONNX
Dinov2WithRegistersForImageClassification(1.14B 参数)
该模型原始格式为 ONNX,在 CPU 上使用 ONNX Runtime 进行推理。在 NPU 适配中,采用 onnx2torch 将 ONNX 模型转换为 PyTorch 格式,然后在昇腾 NPU(Atlas 800 A2/A3)上使用 torch_npu 进行推理。由于模型参数量巨大(1.14B),CPU 推理极为缓慢(约 11.3 秒),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 | tiger cat | 282 | 11295 ms |
| NPU | tiger cat | 282 | 260 ms |
| 指标 | 值 |
|---|---|
| Logits MAE | 0.000084 |
| Logits 相对误差 | 0.019% |
| Prob MAE | 0.00000006 |
| 类别匹配率 | 100% |
| 分类结果一致性 | 完全一致(tiger cat, id:282) |
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 | 11295 ms | 1.0x(基线) |
| NPU | 260 ms | 43.42x |
注:该模型为巨大模型(1.14B 参数),CPU 推理极为缓慢(超过 11 秒/张)。NPU 利用昇腾硬件的强大并行计算能力,推理速度提升约 43.42 倍,从 11.3 秒降低至 0.26 秒,实现了实时推理。这是 NPU 适配中最具加速效果的模型之一,充分体现了昇腾 NPU 在大规模视觉模型推理中的优势。
| 文件 | 说明 |
|---|---|
| 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 #+大模型