weixin_72661020/vit_small_patch16_dinov3.lvd1689m
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

vit_small_patch16_dinov3.lvd1689m 模型卡片

DINOv3 ViT 模型图像特征编码器。基于 DINOv3 ViT-7B 模型在 LVD-1689M 数据集上蒸馏得到。

模型说明

  • 原始模型权重中所有 QKV 投影偏置均为零。在 timm 中,已为模型禁用 QKV 偏置(qkv_bias=False),且未加载这些零权重。对于部分模型尺寸,存在名称中包含 qkvb 的变体,这些变体启用了偏置(qkv_bias=True),但偏置值仍为零,以匹配 transformers 和原始模型的行为。
  • 原始模型将 RoPE 周期作为持久化的 bfloat16 缓冲区。timm 在初始化时生成 float32 周期。这会导致一些数值差异,但 timm 的方法在不支持 bfloat16 的设备上运行时问题更少,并且在微调时表现相同甚至略好。执行 model.rope.periods = model.rope.periods.to(torch.bfloat16).to(torch.float32) 会将周期截断为 bfloat16,从而产生匹配的输出。

模型详情

  • 模型类型: 图像特征编码器
  • 模型统计:
    • 参数(百万):21.6
    • GMACs:6.3
    • 激活值(百万):17.0
    • 图像尺寸:256 x 256
  • 原始链接: https://github.com/facebookresearch/dinov3
  • 许可证: DINOv3
  • 数据集: LVD-1689M
  • 相关论文:
    • DINOv3:https://arxiv.org/abs/2508.10104
    • An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale:https://arxiv.org/abs/2010.11929v2
    • PyTorch Image Models:https://github.com/huggingface/pytorch-image-models

模型使用

图像分类

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_patch16_dinov3.lvd1689m', 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_patch16_dinov3.lvd1689m',
    pretrained=True,
    features_only=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

for o in output:
    # print shape of each feature map in output
    # e.g.:
    #  torch.Size([1, 384, 16, 16])
    #  torch.Size([1, 384, 16, 16])
    #  torch.Size([1, 384, 16, 16])

    print(o.shape)

图像嵌入

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_patch16_dinov3.lvd1689m',
    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, 261, 384) shaped tensor

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

模型对比

有关评估协议的详细信息,请参见相关论文

在网络数据(LVD-1689M)上预训练(或蒸馏)的ViT骨干网络结果

模型IN-ReaLIN-RObj.NetOx.-HADE20kNYU↓DAVISNAVISPair
全局任务密集任务
DINOv3 ViT-S/1687.060.450.949.547.00.40372.756.350.4
DINOv3 ViT-S+/1688.068.854.650.048.80.39975.557.155.2
DINOv3 ViT-B/1689.376.764.158.551.80.37377.258.857.2
DINOv3 ViT-L/1690.288.174.863.154.90.35279.962.361.3
DINOv3 ViT-H+/1690.390.078.664.554.80.35279.363.356.3
DINOv3 ViT-7B/1690.491.191.172.855.90.30979.764.458.7

在网络数据(LVD-1689M)上蒸馏的ConvNeXt骨干网络结果

模型IN-ReaL @256pxIN-ReaL @512pxIN-R @256pxIN-R @512pxObj.Net @256pxObj.Net @512pxADE20kNYU↓
全局任务密集任务
DINOv3 ConvNeXt Tiny86.687.773.774.152.658.742.70.448
DINOv3 ConvNeXt Small87.988.773.774.152.658.744.80.432
DINOv3 ConvNeXt Base88.589.277.278.256.261.346.30.420
DINOv3 ConvNeXt Large88.989.481.382.459.365.247.80.403

在卫星数据(SAT-493M)上预训练(或蒸馏)的ViT骨干网络结果

(GEO-Bench)分类

模型m-BEnetm-brick-kilnm-eurosatm-forestnetm-pv4germ-so2sat均值
DINOv3 ViT-L/1673.096.594.160.696.057.479.6
DINOv3 ViT-7B/1674.097.294.862.396.162.181.1

(GEO-Bench)分割

模型m-cashewm-chesapeakem-NeonTreem-nz-cattlem-pv4ger-segm-SA-crop均值
DINOv3 ViT-L/1694.275.661.883.795.236.874.5
DINOv3 ViT-7B/1694.176.662.683.495.537.675.0

引用

@article{simeoni2025dinov3,
  title={DINOv3},
  author={Sim{'e}oni, Oriane and Vo, Huy V and Seitzer, Maximilian and Baldassarre, Federico and Oquab, Maxime and Jose, Cijo and Khalidov, Vasil and Szafraniec, Marc and Yi, Seungeun and Ramamonjisoa, Micha{"e}l and others},
  journal={arXiv preprint arXiv:2508.10104},
  year={2025}
}
}
@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}}
}

Ascend NPU 适配

本节记录 vit_small_patch16_dinov3.lvd1689m 在华为昇腾 Ascend910B4 NPU 上的适配与验证情况。

相关链接

  • 原始镜像仓库(GitCode):https://gitcode.com/hf_mirrors/timm/vit_small_patch16_dinov3.lvd1689m
  • 权重下载(hf-mirror):https://hf-mirror.com/timm/vit_small_patch16_dinov3.lvd1689m
  • 权重下载(HuggingFace):https://huggingface.co/timm/vit_small_patch16_dinov3.lvd1689m
  • timm 官方仓库:https://github.com/huggingface/pytorch-image-models
  • DINOv3 原始仓库:https://github.com/facebookresearch/dinov3

1. 验证环境

组件版本
torch2.9.0+cpu
torch-npu2.9.0
timm1.0.27
transformers4.57.6
CANN8.5.1
  • NPU:1 张逻辑卡(Ascend910B4)
  • 验证日期:2026-05-09

2. 模型加载与 NPU 适配

由于 timm 默认从 HuggingFace Hub 拉取权重,建议将 HF_ENDPOINT 设置为国内镜像站点:

export HF_ENDPOINT=https://hf-mirror.com

加载模型并将其移至NPU的示例代码:

import torch
import torch_npu
import timm

device = torch.device("npu:0")
torch.npu.set_device(device)

model = timm.create_model(
    "vit_small_patch16_dinov3.lvd1689m",
    pretrained=True
)
model = model.to(device).eval()

# Get model preprocessing config
data_config = timm.data.resolve_model_data_config(model)
input_size = data_config["input_size"]  # (3, 256, 256)

该模型可直接在NPU上运行,无需进行任何额外的算子替换或代码修改。timm的VisionTransformer实现与torch_npu完全兼容。

3. 冒烟验证

3.1 基本推理验证

import torch
import torch_npu
import timm

device = torch.device("npu:0")
model = timm.create_model("vit_small_patch16_dinov3.lvd1689m", pretrained=True)
model = model.to(device).eval()

dummy_input = torch.randn(1, 3, 256, 256).to(device)
with torch.no_grad():
    output = model(dummy_input)

print(f"Output shape: {output.shape}")      # torch.Size([1, 384])
print(f"Output range: [{output.min():.4f}, {output.max():.4f}]")

验证结果:

  • 输出维度:[1, 384](符合预期)
  • 输出值范围:[-1.1397, 2.0712]
  • 输出均值:-0.0053
  • 无错误,推理成功

3.2 精度对比(NPU 与 CPU)

使用相同的权重和输入,对比 NPU 与 CPU 的输出差异:

指标数值
最大绝对误差0.002358
平均绝对误差0.000584

结论:NPU 推理结果与 CPU 基准的浮点差异在 1e-2 数量级以内,满足视觉特征提取任务的精度要求。

4. 性能参考

在单张 Ascend910B4 卡上,使用 fp32 精度测试推理吞吐量和延迟(不包含预处理)。

批大小吞吐量(样本/秒)延迟(毫秒/批)
130.8932.38
4233.9717.10
8463.2117.27
16927.3517.25

测试说明:

  • 输入尺寸:3 x 256 x 256
  • 预热迭代次数:10
  • 正式测试迭代次数:100
  • 同步方式:每批后执行 torch.npu.synchronize()

从数据来看,批大小为 16 时实现最佳吞吐量(约 927 样本/秒),单张图像平均推理延迟约为 1.1 毫秒。

5. 验证脚本

本验证所用的完整脚本随仓库一同提供:

python validate_npu.py

该脚本包含以下内容:

  1. NPU设备检测与模型加载
  2. 单样本/批量推理输出验证(形状、数据类型、数值范围)
  3. NPU与CPU精度对比
  4. 多批量大小性能基准测试

6. 注意事项

  1. HF_ENDPOINT:在国内网络环境下,务必设置 export HF_ENDPOINT=https://hf-mirror.com。否则,timm 从 HuggingFace 官方网站下载权重时会超时。
  2. CANN 日志目录:首次运行 torch_npu 时,若提示 can not create directory: /home/atomgit/ascend/log,可忽略此提示,不影响推理。若要消除该提示,可手动创建该目录。
  3. 输入尺寸:该模型训练时采用 256 x 256 的输入,与标准 ViT 的 224 x 224 不同。使用时请注意 input_size 参数。
  4. 精度模式:当前验证基于 fp32。如需进一步加速,可尝试使用 torch.npu.set_amp_dtype(torch.float16) 进行混合精度推理,但需额外验证精度损失情况。

7. 文件列表

文件说明
validate_npu.pyNPU 适配验证脚本
README.md适配文档(本文档)
validation_output.txt验证脚本的原始输出日志