Delicate02/vit_small_patch14_reg4_dinov2.lvd142m
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

vit_small_patch14_reg4_dinov2.lvd142m 模型卡片

带寄存器的视觉Transformer(ViT)图像特征模型。使用自监督DINOv2方法在LVD-142M上预训练。

模型详情

  • 模型类型: 图像分类 / 特征骨干网络
  • 模型统计信息:
    • 参数(百万):22.1
    • GMACs:29.6
    • 激活值(百万):57.5
    • 图像尺寸:518 x 518
  • 相关论文:
    • Vision Transformers Need Registers: https://arxiv.org/abs/2309.16588
    • DINOv2: Learning Robust Visual Features without Supervision: https://arxiv.org/abs/2304.07193
    • An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale: https://arxiv.org/abs/2010.11929v2
  • 原始项目: https://github.com/facebookresearch/dinov2
  • 预训练数据集: LVD-142M

昇腾 NPU 适配验证报告

本文档记录 vit_small_patch14_reg4_dinov2.lvd142m 在华为昇腾 NPU(Ascend910B4)上的适配与验证结果。

1. 验证环境

组件版本
CANN8.5.1
torch2.9.0+cpu
torch-npu2.9.0.post1+gitee7ba04
timm1.0.27
transformers4.57.6
safetensors>=0.4
  • NPU: Ascend910B4(1 逻辑卡)
  • 模型路径: ./model.safetensors
  • 输入尺寸: 518 x 518
  • 输出维度: 384

2. 快速开始

2.1 环境准备

pip install torch torch_npu timm pillow safetensors numpy

注意:torch_npu 需与 CANN 版本匹配,请参考昇腾官方文档安装。

2.2 下载模型

git clone https://gitcode.com/hf_mirrors/timm/vit_small_patch14_reg4_dinov2.lvd142m.git
cd vit_small_patch14_reg4_dinov2.lvd142m

若 model.safetensors 为 git-lfs 指针文件,可通过以下方式下载权重:

from huggingface_hub import hf_hub_download
hf_hub_download(
    repo_id="timm/vit_small_patch14_reg4_dinov2.lvd142m",
    filename="model.safetensors",
    local_dir=".",
    local_dir_use_symlinks=False,
)

2.3 单图推理

python3 inference.py --device npu --image your_image.jpg

或直接在 Python 中调用:

import torch
import timm
from safetensors.torch import load_file
from PIL import Image

# Load model
model = timm.create_model(
    "vit_small_patch14_reg4_dinov2.lvd142m",
    pretrained=False,
    num_classes=0,
)
state_dict = load_file("model.safetensors")
model.load_state_dict(state_dict, strict=False)
model = model.eval().to("npu")

# Prepare input
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)
img = Image.open("your_image.jpg").convert("RGB")
img_tensor = transforms(img).unsqueeze(0).to("npu")

# Inference
with torch.no_grad():
    features = model(img_tensor)
print(features.shape)  # torch.Size([1, 384])

3. 推理正常输出证据

以下为在 Ascend910B4 上运行推理的实际输出日志(inference.log):

Device: npu
NPU Name: Ascend910B4
Loading model...
Input size: (3, 518, 518)
Input tensor shape: torch.Size([1, 3, 518, 518])
Output shape: torch.Size([1, 384])
Output dtype: torch.float32
Output device: npu:0
Latency (ms): min=11.119, max=11.637, avg=11.307
Throughput: 88.44 images/sec

结论: 模型在 NPU 上推理正常,输出 shape 为 [1, 384],数据类型 float32,设备为 npu:0,单图推理平均延迟约 11.3ms。

3.1 运行推理

python3 inference.py --device npu

4. 精度评测(CPU vs NPU 精度误差)

4.1 评测方法

对比 CPU 与 NPU 在相同输入下的输出特征向量,采用多指标综合判定:

  • 截断均值相对误差(去掉 top 5% 异常值)< 1%
  • 余弦相似度 > 0.999
  • 平均绝对误差 < 0.01

4.2 运行评测

python3 accuracy_benchmark.py

4.3 精度误差数据

指标数值
max_absolute_error0.016564
mean_absolute_error0.004838
max_relative_error0.9488
mean_relative_error1.736%
median_relative_error0.449%
mean_relative_error_trimmed0.709%
cosine_similarity0.999990
rmse0.006102

CPU 输出示例:tensor([-1.9629, 0.0178, 1.6286, 2.0247, 1.0157])

NPU 输出示例:tensor([-1.9603, 0.0233, 1.6169, 2.0112, 1.0147], device='npu:0')

4.4 判定结果

检查项阈值实际值结果
截断均值相对误差< 1%0.7095%✅ 通过
余弦相似度> 0.9990.999990✅ 通过
平均绝对误差< 0.010.004838✅ 通过

综合判定:✅ 通过 — NPU 推理精度在可接受范围内,与 CPU 输出高度一致。

5. 性能基准

5.1 测试方法

使用 perf_benchmark.py 在 NPU 上测试不同 batch size 的推理延迟与吞吐量,预热(warmup)=10,重复次数(repeats)=50。

python3 perf_benchmark.py --batch-sizes 1 2 4 8

5.2 性能结果

批处理大小平均延迟(毫秒)P50 延迟(毫秒)P90 延迟(毫秒)吞吐量(张/秒)
111.11911.08211.44689.94
210.90810.89311.076183.35
416.07016.06616.112248.91
829.97129.95530.057266.92

6. 文件说明

文件说明
inference.py基础推理脚本,支持 CPU/NPU
accuracy_benchmark.py精度评测脚本(CPU 与 NPU 对比),自动判定通过/失败
perf_benchmark.py性能基准测试脚本(多批处理大小)
accuracy_benchmark.log精度评测运行日志
perf_benchmark.log性能评测运行日志
inference.log推理冒烟测试日志(推理正常输出证据)
accuracy_result.pt精度评测详细结果(PyTorch 格式)
perf_result.json性能评测详细结果(JSON 格式)
fusion_result.json算子融合日志
config.json模型配置

7. 注意事项

  1. 本地权重加载:由于网络环境限制,脚本使用 safetensors.torch.load_file 直接从本地 model.safetensors 加载权重,避免依赖 HuggingFace 在线下载。

  2. 双精度类型转换警告:NPU 当前不支持 double 精度,部分内部操作会自动转换为 float。该转换对最终输出精度影响可控(截断均值相对误差 < 1%)。

  3. 输入尺寸固定:该模型配置 fixed_input_size=true,输入必须严格为 518 x 518,预处理参数如下:

    • mean = [0.485, 0.456, 0.406]
    • std = [0.229, 0.224, 0.225]
    • interpolation = bicubic
    • crop_pct = 1.0
  4. 批处理大小选择:从性能数据看,批处理大小为 2 时单样本延迟最低(约 5.45 毫秒/图),批处理大小为 8 时总吞吐量最高(266.92 张/秒)。实际部署时可根据业务场景选择。

8. 优化建议

8.1 运行时优化(可选)

如希望进一步提升推理性能,可尝试启用 Ascend NPU 的任务队列并行下发:

export TASK_QUEUE_ENABLE=2
python3 inference.py --device npu

该优化适用于 host-bound 场景,可减少 CPU 调度开销。

8.2 内存优化

如遇到大 batch size 下的内存瓶颈,可尝试调整 NPU 内存分配器:

export PYTORCH_NPU_ALLOC_CONF=max_split_size_mb:512
python3 inference.py --device npu --batch-size 16

9. 验证结论

  • 功能:基于昇腾 NPU 跑通模型推理,输出 shape [1, 384],推理正常 ✅
  • 精度:与 CPU 截断均值相对误差 0.71%(< 1%),余弦相似度 0.999990 ✅
  • 性能:单卡 Batch=1 延迟约 11ms,Batch=8 吞吐量约 267 images/sec

模型用法(原始模型用法)

图像分类

from urllib.request import urlopen
from PIL import Image
import timm

img = Image.open(urlopen(
    'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
))

model = timm.create_model('vit_small_patch14_reg4_dinov2.lvd142m', pretrained=True)
model = model.eval()

# get model specific transforms (normalization, resize)
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)

output = model(transforms(img).unsqueeze(0))  # unsqueeze single image into batch of 1

top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)

图像嵌入

from urllib.request import urlopen
from PIL import Image
import timm

img = Image.open(urlopen(
    'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
))

model = timm.create_model(
    'vit_small_patch14_reg4_dinov2.lvd142m',
    pretrained=True,
    num_classes=0,  # remove classifier nn.Linear
)
model = model.eval()

# get model specific transforms (normalization, resize)
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)

output = model(transforms(img).unsqueeze(0))  # output is (batch_size, num_features) shaped tensor

# or equivalently (without needing to set num_classes=0)

output = model.forward_features(transforms(img).unsqueeze(0))
# output is unpooled, a (1, 1374, 384) shaped tensor

output = model.forward_head(output, pre_logits=True)
# output is a (1, num_features) shaped tensor

模型对比

在 timm 的 model results 中探索此模型的数据集和运行时指标。

引用

@article{darcet2023vision,
  title={Vision Transformers Need Registers},
  author={Darcet, Timoth{'e}e and Oquab, Maxime and Mairal, Julien and Bojanowski, Piotr},
  journal={arXiv preprint arXiv:2309.16588},
  year={2023}
}
@misc{oquab2023dinov2,
  title={DINOv2: Learning Robust Visual Features without Supervision},
  author={Oquab, Maxime and Darcet, Timothée and Moutakanni, Theo and Vo, Huy V. and Szafraniec, Marc and Khalidov, Vasil and Fernandez, Pierre and Haziza, Daniel and Massa, Francisco and El-Nouby, Alaaeldin and Howes, Russell and Huang, Po-Yao and Xu, Hu and Sharma, Vasu and Li, Shang-Wen and Galuba, Wojciech and Rabbat, Mike and Assran, Mido and Ballas, Nicolas and Synnaeve, Gabriel and Misra, Ishan and Jegou, Herve and Mairal, Julien and Labatut, Patrick and Joulin, Armand and Bojanowski, Piotr},
  journal={arXiv:2304.07193},
  year={2023}
}
@article{dosovitskiy2020vit,
  title={An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale},
  author={Dosovitskiy, Alexey and Beyer, Lucas and Kolesnikov, Alexander and Weissenborn, Dirk and Zhai, Xiaohua and Unterthiner, Thomas and  Dehghani, Mostafa and Minderer, Matthias and Heigold, Georg and Gelly, Sylvain and Uszkoreit, Jakob and Houlsby, Neil},
  journal={ICLR},
  year={2021}
}
@misc{rw2019timm,
  author = {Ross Wightman},
  title = {PyTorch Image Models},
  year = {2019},
  publisher = {GitHub},
  journal = {GitHub repository},
  doi = {10.5281/zenodo.4414861},
  howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
}