dinov2 icon indicating copy to clipboard operation
dinov2 copied to clipboard

Loading Pretrained Models Locally and Avoiding Redundant Downloads

Open aaiguy opened this issue 2 years ago • 7 comments

How can I load the pretrained Dinov2 model from a local source so that it loads the model even when there is no internet connection and does not attempt to download it again from the server? The normal way to load the model is as follows: diov2_vitl14 = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14')

I tried below approach passing folder path of torch hub facebookresearch/dinov2 along with source as local but still it tries to download model from server diov2_vitl14 = torch.hub.load(r'C:\Users\john1411/.cache\torch\hub\facebookresearch_dinov2_main', 'dinov2_vitl14',source='local') downloaded model is located in this location "C:\Users\john1411.cache\torch\hub\checkpoints\dinov2_vitl14_pretrain.pth"

aaiguy avatar May 08 '23 10:05 aaiguy

Hi Aaiguy,

You can, for example, initialize the model as follows

from dinov2.models.vision_transformer import vit_large 

model = vit_large(
    patch_size=14,
    img_size=526,
    init_values=1.0,
    block_chunks=0
 )

Then you can use the following line to load pretrained models (where it is in the same directory and called dinov2_vitl14_pretrain.pth:

model.load_state_dict(torch.load('dinov2_vitl14_pretrain.pth'))

Sntz91 avatar May 08 '23 12:05 Sntz91

Thanks this works :)

aaiguy avatar May 09 '23 07:05 aaiguy

Hi Aaiguy,

You can, for example, initialize the model as follows

from dinov2.models.vision_transformer import vit_large 

model = vit_large(
    patch_size=14,
    img_size=526,
    init_values=1.0,
    block_chunks=0
 )

Then you can use the following line to load pretrained models (where it is in the same directory and called dinov2_vitl14_pretrain.pth:

model.load_state_dict(torch.load('dinov2_vitl14_pretrain.pth'))

Hi friend: What is the tensor obtained after model processing? How can I use this tensor to see the result of my image after being processed by the model?

xcp2022beGood avatar May 11 '23 17:05 xcp2022beGood

it loads the model even when there is no internet connection and does not attempt to download it again from the server?

@aaiguy Copy the model weights to PyTorch Hub's cache under ~/.cache/torch/hub/checkpoints. Then torch.hub.load() will hit this cache and not try to download the weights again.

patricklabatut avatar May 11 '23 21:05 patricklabatut

The following worked:

wget -O dinov2_vits14.pth https://dl.fbaipublicfiles.com/dinov2/dinov2_vits14/dinov2_vits14_pretrain.pth
wget -O dinov2.zip https://github.com/facebookresearch/dinov2/archive/refs/heads/main.zip
unzip -q dinov2.zip && rm dinov2.zip && mv dinov2-main dinov2
model = torch.hub.load('dinov2', 'dinov2_vits14', source='local', pretrained=False)
model.load_state_dict(torch.load('dinov2_vits14.pth'))

IDDT avatar May 29 '23 10:05 IDDT

@aaiguy because pytorch hub has a default model & checkpoint root path, you can solve it through torch.hub.set_dir("your model root path")

sectionZ6 avatar Aug 16 '24 14:08 sectionZ6

Hi Aaiguy, You can, for example, initialize the model as follows from dinov2.models.vision_transformer import vit_large

model = vit_large( patch_size=14, img_size=526, init_values=1.0, block_chunks=0 )

Then you can use the following line to load pretrained models (where it is in the same directory and called dinov2_vitl14_pretrain.pth:

model.load_state_dict(torch.load('dinov2_vitl14_pretrain.pth'))

Hi friend: What is the tensor obtained after model processing? How can I use this tensor to see the result of my image after being processed by the model?

how to install dinov2 ? I cannot do that

wangfeng22 avatar Sep 16 '25 03:09 wangfeng22

Hi Aaiguy, You can, for example, initialize the model as follows from dinov2.models.vision_transformer import vit_large model = vit_large( patch_size=14, img_size=526, init_values=1.0, block_chunks=0 ) Then you can use the following line to load pretrained models (where it is in the same directory and called dinov2_vitl14_pretrain.pth:

model.load_state_dict(torch.load('dinov2_vitl14_pretrain.pth'))

Hi friend: What is the tensor obtained after model processing? How can I use this tensor to see the result of my image after being processed by the model?

how to install dinov2 ? I cannot do that

You do not need to install it , just clone dinov2 and pass the repo's path when loading the model.

AlexandreBrown avatar Nov 21 '25 20:11 AlexandreBrown